Original Post
I've been creating an image loading lib, to increase my knowledge of file loading and data parsing and so on. I decided I'd like to just load all my data into kind of a smart memory block. Where you can move forward and backward and grab as many bytes as you need from wherever and it'd cleanup after itself. Probably not a good idea, but for the sake of a learning experience I still want to do it. I'm having trouble though. I've overloaded the [] operator for random access, easy. Now I'm having trouble grabbing as many bytes as I need though and shoving them into a type. The pos member indicates the position within the buffer. byte is typedef unsigned char. So say I have loaded a bitmap file into this DataBlock class, if I read the first 2 bytes I'd have 'BM', but if I were to say, move 4 bytes and read another 4 I should have 0 for the first reserved bit in the file header. I'm not getting this though, however if I use the random access operator and move to that point I get the correct value? This is what I was doing: I should have got 0 but I get some massive number instead. I know this isn't exactly a useful idea, in some circumstances you'd just read only a bit of the file stream in the first place, especially if it's like a bitmap and you just need the texture data you can skip half the junk you don't need... But not understanding why this didn't work in the first place is driving me insane.
byte* DataBlock::readBytes(int n){
if(this->pos+n <= this->size){
byte* buffer = new byte[n];
memcpy(buffer, &this->data[this->pos], n);
this->pos+= n;
return buffer;
}
return 0;
}
cout << data.readBytes(2) << endl;
data.move(4);
cout << (int)data.readBytes(4) << endl;