[2.0.3]-SDL_RWread() - Read Only One Line?

[2.0.3]-SDL_RWread() - Read Only One Line?

Hi,

Sorry, I need some help…
How can I use SDL_RWread() function to read only one line at a time?
(each line is formatted with correct end of line character(s) based on platform)

Here is my code:

Code:
void Data::LoadHighScoresAndOptions(void)
{
char filename[1024];
char textBuffer[50];
char *base_path = SDL_GetPrefPath(“16BitSoftInc”, “LettersFall-Retail”);
char *pref_path = NULL;

if (base_path)
{
    pref_path = SDL_strdup(base_path);
    SDL_free(base_path);
}
else  return;

strcpy(filename, pref_path);
strcat(filename, "LettersFall-5-Data");

SDL_RWops *fileToRead = SDL_RWFromFile(filename, "r");
if (fileToRead != NULL)
{
    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    audio->MusicVolume = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    audio->SoundVolume = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    visuals->FullScreenMode = atoi(textBuffer) > 0;

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->GameMode = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->SelectedBackground = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->SelectedMusicTrack = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->MusicJukebox = atoi(textBuffer) > 0;

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyButtonOne = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyButtonTwo = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyUP = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyRIGHT = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyDOWN = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyLEFT = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyPause = (int)atoi(textBuffer);

    for (int index = 0; index < 3; index++)
    {
        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyUP[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyDOWN[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyLEFT[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyRIGHT[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyButton1[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyButton2[index] = (int)atoi(textBuffer);
    }

    for (int gameMode = 0; gameMode < 5; gameMode++)
    {
        for (Uint8 rank = 0; rank < 10; rank++)
        {
            for (int index = 0; index < 18; index++)
            {
                HighScoresName[gameMode][rank][index] = char(NULL);
            }

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);

            for (Uint8 index = 0; index < 18; index++)
            {
                HighScoresName[gameMode][rank][index] = textBuffer[index];
            }

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
            HighScoresLevel[gameMode][rank] = atoi(textBuffer);

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
            HighScoresScore[gameMode][rank] = atoi(textBuffer);
        }
    }

    SDL_RWclose(fileToRead);
}

}

Any help would be appreciated…
Thanks!------------------------
JeZxLee
JessePalser <AT> GMail <DOT> com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com

Sorry, I forgot the file:
(you can’t see it, but each line is terminated by correct end of line char(s) based on OS platform)

Code:
0
1
0
1
0
0
1
-1
-1
-1
-1
-1
-1
-1
1
1
0
0
8
9
1
1
0
0
8
9
1
1
0
0
8
9
JeZxLee
10
10000
Daotheman
9
9000
mattmatteh
8
8000
You!
7
7000
You!
6
6000
You!
5
5000
You!
4
4000
You!
3
3000
You!
2
2000
You!
1
1000
JeZxLee
10
10000
Daotheman
9
9000
mattmatteh
8
8000
You!
7
7000
You!
6
6000
You!
5
5000
You!
4
4000
You!
3
3000
You!
2
2000
You!
1
1000
JeZxLee
10
10000
Daotheman
9
9000
mattmatteh
8
8000
You!
7
7000
You!
6
6000
You!
5
5000
You!
4
4000
You!
3
3000
You!
2
2000
You!
1
1000
JeZxLee
10
10000
Daotheman
9
9000
mattmatteh
8
8000
You!
7
7000
You!
6
6000
You!
5
5000
You!
4
4000
You!
3
3000
You!
2
2000
You!
1
1000
JeZxLee
10
10000
Daotheman
9
9000
mattmatteh
8
8000
You!
7
7000
You!
6
6000
You!
5
5000
You!
4
4000
You!
3
3000
You!
2
2000
You!
1
1000------------------------
JeZxLee
JessePalser <AT> GMail <DOT> com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com

If you aren’t comfortable writing and debugging the code to construct and
parse your own format, then you should definitely use an existing one with
a higher-level interface. When I need a simple text formatted file, I use
tinyxml2 personally, but there are lots of good options out there.

Jonny DOn Thu, May 7, 2015 at 10:44 AM, JeZ-l-Lee < JessePalserMailingLists at gmail.com> wrote:

[2.0.3]-SDL_RWread() - Read Only One Line?

Hi,

Sorry, I need some help…
How can I use SDL_RWread() function to read only one line at a time?
(each line is formatted with correct end of line character(s) based on
platform)

Here is my code:

Code:

void Data::LoadHighScoresAndOptions(void)
{
char filename[1024];
char textBuffer[50];
char *base_path = SDL_GetPrefPath(“16BitSoftInc”, “LettersFall-Retail”);
char *pref_path = NULL;

if (base_path)
{
    pref_path = SDL_strdup(base_path);
    SDL_free(base_path);
}
else  return;

strcpy(filename, pref_path);
strcat(filename, “LettersFall-5-Data”);

SDL_RWops *fileToRead = SDL_RWFromFile(filename, "r");
if (fileToRead != NULL)
{
    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    audio->MusicVolume = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    audio->SoundVolume = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    visuals->FullScreenMode = atoi(textBuffer) > 0;

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->GameMode = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->SelectedBackground = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->SelectedMusicTrack = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    logic->MusicJukebox = atoi(textBuffer) > 0;

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyButtonOne = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyButtonTwo = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyUP = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyRIGHT = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyDOWN = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyLEFT = (int)atoi(textBuffer);

    SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
    input->UserDefinedKeyPause = (int)atoi(textBuffer);

    for (int index = 0; index < 3; index++)
    {
        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyUP[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyDOWN[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyLEFT[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyRIGHT[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyButton1[index] = (int)atoi(textBuffer);

        SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
        input->JoyButton2[index] = (int)atoi(textBuffer);
    }

    for (int gameMode = 0; gameMode < 5; gameMode++)
    {
        for (Uint8 rank = 0; rank < 10; rank++)
        {
            for (int index = 0; index < 18; index++)
            {
                HighScoresName[gameMode][rank][index] = char(NULL);
            }

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);

            for (Uint8 index = 0; index < 18; index++)
            {
                HighScoresName[gameMode][rank][index] =

textBuffer[index];
}

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
            HighScoresLevel[gameMode][rank] = atoi(textBuffer);

            SDL_RWread(fileToRead, textBuffer, sizeof(textBuffer), 1);
            HighScoresScore[gameMode][rank] = atoi(textBuffer);
        }
    }

    SDL_RWclose(fileToRead);
}

}

Any help would be appreciated…
Thanks!


JeZxLee
JessePalser GMail com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonny D wrote:

If you aren’t comfortable writing and debugging the code to construct and parse your own format, then you should definitely use an existing one with a higher-level interface.?? When I need a simple text formatted file, I use tinyxml2 personally, but there are lots of good options out there.
Hi,

Thanks for the info…
I looked at tinyxml2, is it cross-platform (Windows/Linux/Mac OS X) ?
I was hoping to get it working with just SDL2 though, which I think would be best.
I got the saving data working perfectly now, just need to fix loading data…------------------------
JeZxLee
JessePalser <AT> GMail <DOT> com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com

Yes, tinyxml2 is cross-platform. It also has functions for working
in-memory, so you can still load using SDL_RWops, but parse it as XML
(important for Android assets).

While it is nice to have only a dependency on SDL2, you’re going to come
across problems that are more efficiently solved by using the work of
others. It’s totally up to you how you solve these problems, but I
recommend keeping your eyes open to options that will help get your
projects to completion with less time, work, and bugs.

Jonny DOn Thu, May 7, 2015 at 12:11 PM, JeZ-l-Lee < JessePalserMailingLists at gmail.com> wrote:

Jonny D wrote:

If you aren’t comfortable writing and debugging the code to construct
and parse your own format, then you should definitely use an existing one
with a higher-level interface.? When I need a simple text formatted file,
I use tinyxml2 personally, but there are lots of good options out there.

Hi,

Thanks for the info…
I looked at tinyxml2, is it cross-platform (Windows/Linux/Mac OS X) ?
I was hoping to get it working with just SDL2 though, which I think would
be best.
I got the saving data working perfectly now, just need to fix loading
data…


JeZxLee
JessePalser GMail com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

MrTAToad wrote:

My Triority system has a function that allows reading of one line of text, dealing with the three line terminators properly (hopefully).

The system mentioned uses std::fstream, but I have recently converted it to RWops, so I’ll post the code for that later
Hi,

I appreciate that, I do…
I almost got this working though.
I found this: http://wiki.libsdl.org/SDL_RWseek
and I believe it can solve my issue.

Problem is how do I use above function to move to the next line?
Thanks!------------------------
JeZxLee
JessePalser <AT> GMail <DOT> com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com

My Triority system has a function that allows reading of one line of text, dealing with the three line terminators properly (hopefully).

The system mentioned uses std::fstream, but I have recently converted it to RWops, so I’ll post the code for that later

Unless each line is a specific length and ends in a specific way, you wouldn’t be able to use binary mode without using something like my routine to get to the end of a line.

You could use RWops in text mode, and create a text array of a specific size that you (hope) the line wont exceed, and then use SDL_RWread to read up to that size.

However, the documentation doesn’t state what would happen if you try to read this amount but the line is shorter - I dont know whether it will x amount of characters or whether it will read up to the line terminator.

Here it is :

Code:
void GLBASIC::READLINE(DGNat index, DGStr& v)
{
DGNat one,two;

v.clear();
if (index<0 || index>=BOUNDS(files,0))
{
    SETERROR(CMP_INDEX_EXCEEDED);
    return;
}

if (files(index).handle)
{
	while (ENDOFFILE(index)==false)
	{
		READBYTE(index,one);
		switch (one) {
			case '\n':
						return;

			case '\r':
						READBYTE(index, two);
						if (two!= '\n')
						{
							FILESEEK(index, -1, SEEK_CUR); // Move back one byte
						}

						return;
						break;

			case EOF:
						// Also handle the case when the last line has no line ending
						// Move the file pointer to the end of the file
						FILESEEK(index, 0, RW_SEEK_END);
						break;

			default:
						v += one;
						break;
		};
	}
}
else
{
	SETERROR(CMP_FILE_ERROR);
}

}

Hi,

It took some deep thought, but we got everything working now.
Thanks to all that helped!------------------------
JeZxLee
JessePalser <AT> GMail <DOT> com
16BitSoft Inc.
Video Game Design Studio
www.16BitSoft.com