www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bytes into integer, need help

reply James <james gmail.com> writes:
im having problem in code below, while making my own file handler. the data
stored in buffer, so i can recall portion of data in diff format and length
like int,short,etc.  but im stuck with function getb() below. any suggestion?
thks.

struct T_FILE_RAM {
  ubyte[] buffer;
  int offset;

void getb(void* buf, int size) {
	buf=&b[offset..offset+size]; //<-- prob here
}

void read(out uint x) { getb(&x, x.sizeof); }
void read(out ushort x) { getb(&x, x.sizeof); }

}
Nov 05 2008
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"James" wrote
 im having problem in code below, while making my own file handler. the 
 data stored in buffer, so i can recall portion of data in diff format and 
 length like int,short,etc.  but im stuck with function getb() below. any 
 suggestion? thks.

 struct T_FILE_RAM {
  ubyte[] buffer;
  int offset;

 void getb(void* buf, int size) {
 buf=&b[offset..offset+size]; //<-- prob here
 }

 void read(out uint x) { getb(&x, x.sizeof); }
 void read(out ushort x) { getb(&x, x.sizeof); }

 }

I think you meant this? buf[0..size] = buffer[offset..offset+size]; And also, you need to increment offset, no? offset += size. I'd also recommend using a template: void getb(T)(ref T t) { auto size = t.sizeof; auto buf = (cast(ubyte *)&t)[0..size]; buf[] = buffer[offset..offset+size]; offset += size; } Now you don't need to implement each type, and you can call like this: int x; ushort y; getb(x); // reads 4 bytes getb(y); // reads 2 bytes -Steve
Nov 06 2008
next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 06 Nov 2008 18:22:36 +0300, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 "James" wrote
 im having problem in code below, while making my own file handler. the
 data stored in buffer, so i can recall portion of data in diff format  
 and
 length like int,short,etc.  but im stuck with function getb() below. any
 suggestion? thks.

 struct T_FILE_RAM {
  ubyte[] buffer;
  int offset;

 void getb(void* buf, int size) {
 buf=&b[offset..offset+size]; //<-- prob here
 }

 void read(out uint x) { getb(&x, x.sizeof); }
 void read(out ushort x) { getb(&x, x.sizeof); }

 }

I think you meant this? buf[0..size] = buffer[offset..offset+size]; And also, you need to increment offset, no? offset += size. I'd also recommend using a template: void getb(T)(ref T t) { auto size = t.sizeof; auto buf = (cast(ubyte *)&t)[0..size]; buf[] = buffer[offset..offset+size]; offset += size; } Now you don't need to implement each type, and you can call like this: int x; ushort y; getb(x); // reads 4 bytes getb(y); // reads 2 bytes -Steve

Good solution also cares about endianess. Also take a look at tango.io.protocol - it should cover all your needs.
Nov 06 2008
prev sibling parent James <James gmail.com> writes:
Steven Schveighoffer Wrote:

 "James" wrote
 im having problem in code below, while making my own file handler. the 
 data stored in buffer, so i can recall portion of data in diff format and 
 length like int,short,etc.  but im stuck with function getb() below. any 
 suggestion? thks.

 struct T_FILE_RAM {
  ubyte[] buffer;
  int offset;

 void getb(void* buf, int size) {
 buf=&b[offset..offset+size]; //<-- prob here
 }

 void read(out uint x) { getb(&x, x.sizeof); }
 void read(out ushort x) { getb(&x, x.sizeof); }

 }

I think you meant this? buf[0..size] = buffer[offset..offset+size]; And also, you need to increment offset, no? offset += size. I'd also recommend using a template: void getb(T)(ref T t) { auto size = t.sizeof; auto buf = (cast(ubyte *)&t)[0..size]; buf[] = buffer[offset..offset+size]; offset += size; } Now you don't need to implement each type, and you can call like this: int x; ushort y; getb(x); // reads 4 bytes getb(y); // reads 2 bytes -Steve

thanks, exactly what im looking for. endianness isnt a problem at this point as im not working on a portable project.
Nov 06 2008