Hi guys, I have "little" problem with SDL_RWops. If you don't know what's this, I tell you: it's part of SDL responsible for abstracting reading/writing operations on various streams (ie. file, memory). Wiki is
here and
here.
As to my problem - you see, I'm trying to load config file (which is normal text file) line by line (cause it's the way parser is working) from SDL_RWops source. That source is created using this code, borrowed from SDL_Image:
SDL_RWops *source = SDL_RWFromFile(filename.c_str(), "r");
Since lines can be of any sizes (could be 10, 1024 or 1, no limits), I need a way to determine where are '\n' located. That way, I can dynamically mallocate enough memory and fill it with all characters up to '\n' - which will be passed to parser, then we deallocate that memory, move to next line and so on.
Seeking for char inside SDL_RWops is performed by this "little" function. I know it's far from perfect, but it *should* be working - however, output I get from stderr shows that it isn't, and I suspect nasty bug in SDL_RWread() or SDL_RWtell().
#define CFG_DEBUG(x, y) fprintf(stderr, x, y);
// returns size of line to create, counting from current read-ptr position
// xxx -char we're looking for
int SDL_RWfind( SDL_RWops * source, char xxx, int * last_line_length)
{
const int buffer_size = 512;
// we're reading up to buffer_size characters to this buffer
char buffer [buffer_size];
// number of bytes read during last SDL_RWread()
int read;
// number of bytes that were read up to now
int read_all = 0;
char * buffer_ptr;
do
{
// -------------------------------- start of the most important part !
// get current offset (before reading)
CFG_DEBUG("\nSDL_RWfind: Current offset before RWread: %i.\n", SDL_RWtell(source));
// according to SDL Wiki, SDL_RWread: "It returns the number of memory blocks read"
// and here blocks have size of 1 - so it should return the number of characters read
read = SDL_RWread(source, buffer, 1, buffer_size );
CFG_DEBUG("SDL_RWfind: Read characters: %i.\n", read);
// get current offset (after reading)
CFG_DEBUG("\nSDL_RWfind: Current offset after RWread: %i.\n", SDL_RWtell(source));
// -------------------------------- end of the most important part, next lines of this function were added just for completness sake
if (read < 0) { CFG_DEBUG("Couldn't read even one block from stream while seeking: %c.\n", xxx); return -2; }
buffer_ptr = buffer;
read_all += read;
while ( (buffer_ptr - buffer) < read )
{
if ( (*buffer_ptr) == xxx) // we've found it
{
SDL_RWseek(source, -read_all, SEEK_CUR);
return ( buffer_ptr - buffer);
}
++buffer_ptr;
}
} while ( read == buffer_size );
*last_line_length = read_all;
SDL_RWseek(source, -read_all - 1 , SEEK_CUR);
return -1;
}
And now, most important part of this mail:
After using that function for this file:
bad_integer = 1234
good_integer = 1234
... I get in stderr this:
1) SDL_RWfind: Current offset before RWread: 0.
2) SDL_RWfind: Read characters: 41.
3) SDL_RWfind: Current offset after RWread: 45.
Do you see what's going on? We have read 41 characters (line 2) starting from offset 0 (line 1), so we should be at position 41, right? But line 3 says that we're at position 45!!!! And as you can see, there weren't ANY reading / writing / seeking etc. going on!
The only logic I could see after this behaviour: whole file has size of 45 bytes. But that shouldn't change anything...
If someone could tell me whether this is SDL_RWops bug, it's my code's failure, or has working function for finding char in SDL_RWops stream or sth similiar, I would be really gratefull for sharing it with me ( you know, ++rating etc. ;-) )