Problem with a string.

Post Reply
Kazo
Posts: 14
Joined: Mon Aug 29, 2011 8:06 am

Problem with a string.

Post by Kazo » Sun Sep 04, 2011 3:26 pm

For some reason, my string does not seem to get set correctly.

I am using fincs' SSEQ player and modding it a bit so that users can play the SSEQs that are on NDS files. I made a shortcut type file that has all the data necessary to quickly make the SSEQ list and have all the offsets for the SSEQs, BANKs, and WAVEARCs for the ROM it is for. It also contains a string to the ROM it uses. when I run a .sps file it first reads the nds path string and make sure it is still valid and the user has not moved/deleted the ROM. when certain games are first launched first, the string is corrupted.

Image

If I run a game that does not do this and then open this one it works fine, also if this happens and I go back to the sps list and then try to reopen it it works fine.

Image

The code for this would be in ReadSPS();

Code: Select all

#include <nds.h>
#include <stdio.h>
#include <sndcommon.h>
#include <filesystem.h>
#include <fat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void ReadDIR();
void ShowDIR();
void ReadSPS();
void ShowSSEQ();
void ReadSSEQ();

char* DIRList[5000];
char* CurrentNDS;
char* CurrentSPS;
char* CurrentFileSTR;

u32 FileCount;
u32 CurrentFile;
u32 i;

u32 SSEQCount;
u32 CharCount;
u32 CurrentSSEQ;
u32 SSEQNameOffset;
u32 SSEQDataOffset;
char* SSEQList[5000];

bool SSEQMode = false;
bool PlayMode = false;

u32 SSEQOffset;
u32 BANKOffset;
u32 WAVEARC1Offset;
u32 WAVEARC2Offset;
u32 WAVEARC3Offset;
u32 WAVEARC4Offset;
u32 SSEQSize;
u32 BANKSize;
u32 WAVEARC1Size;
u32 WAVEARC2Size;
u32 WAVEARC3Size;
u32 WAVEARC4Size;

int main()
{
	consoleDemoInit();
	InstallSoundSys();
	
	if(!fatInitDefault())
	{
		iprintf("Filesystem FAIL");
		for(;;) swiWaitForVBlank();
	}

	CurrentFile = 0;

	ReadDIR();
	ShowDIR();

	for(;;)
	{
		swiWaitForVBlank();

		scanKeys();
		if (keysDown() & KEY_A)
		{
			if(PlayMode)
			{

			}
			else
			{
				if(SSEQMode)
				{
					ReadSSEQ();
				}
				else
				{
					if (CurrentSPS != NULL)
					{
						free(CurrentSPS);
					}
					
					CurrentSPS = malloc(23 + strlen(DIRList[CurrentFile]));
					strcpy(CurrentSPS, "/data/NDS Music Player/");
					strcat(CurrentSPS, DIRList[CurrentFile]);
						
					ReadSPS();
				}
			}
		}

		if (keysDown() & KEY_B)
		{
			if(PlayMode)
			{
				PlayMode = false;
				SSEQMode = true;
				ShowSSEQ();
			}
			else
			{
				if(SSEQMode)
				{
					SSEQMode = false;
					ShowDIR();
				}
				else
				{
					ShowDIR();
				}
			}
		}

		if (keysDown() & KEY_UP)
		{
			if(PlayMode)
			{
			}
			else
			{
				if(SSEQMode)
				{
					if(CurrentSSEQ > 0)
					{
						CurrentSSEQ--;
					}
					else
					{
						CurrentSSEQ = SSEQCount - 1;
					}

					while(SSEQList[CurrentSSEQ][0] == '<')
					{
						CurrentSSEQ--;
						if(CurrentSSEQ > SSEQCount)
						{
							CurrentSSEQ = SSEQCount - 1;
						}
					}
					ShowSSEQ();
				}
				else
				{
					if(CurrentFile > 0)
					{
						CurrentFile--;
					}
					else
					{
						CurrentFile = FileCount;
					}
					ShowDIR();
				}
			}
		}

		if (keysDown() & KEY_DOWN)
		{
			if(PlayMode)
			{
			}
			else
			{
				if(SSEQMode)
				{
					if(CurrentSSEQ < SSEQCount - 1)
					{
						CurrentSSEQ++;
					}
					else
					{
						CurrentSSEQ = 0;
					}
					while(SSEQList[CurrentSSEQ][0] == '<')
					{
						CurrentSSEQ++;
						if(CurrentSSEQ > SSEQCount - 1)
						{
							CurrentSSEQ = 0;
						}
					}
					ShowSSEQ();
				}
				else
				{
					if(CurrentFile < FileCount)
					{
						CurrentFile++;
					}
					else
					{
						CurrentFile = 0;
					}
					ShowDIR();
				}
			}
		}

		if (keysDown() & KEY_LEFT)
		{
			if(PlayMode)
			{
			}
			else
			{
				if(SSEQMode)
				{
					if(CurrentSSEQ >= 23)
					{
						CurrentSSEQ -= 23;
					}
					else
					{
						CurrentSSEQ = 0;
						while(SSEQList[CurrentSSEQ][0] == '<')
						{
							CurrentSSEQ++;
							if(CurrentSSEQ < 0)
							{
								CurrentSSEQ = 0;
							}
						}
					}
					while(SSEQList[CurrentSSEQ][0] == '<')
					{
						CurrentSSEQ--;
						if(CurrentSSEQ < 0)
						{
							CurrentSSEQ = SSEQCount - 1;
						}
					}
					ShowSSEQ();
				}
				else
				{
					if(CurrentFile >= 23)
					{
						CurrentFile -= 23;
					}
					else
					{
						CurrentFile = 0;
					}
					ShowDIR();
				}
			}
		}

		if (keysDown() & KEY_RIGHT)
		{
			if(PlayMode)
			{
			}
			else
			{
				if(SSEQMode)
				{
					if(CurrentSSEQ < (SSEQCount - 24))
					{
						CurrentSSEQ += 23;
					}
					else
					{
						CurrentSSEQ = SSEQCount - 1;
						while(SSEQList[CurrentSSEQ][0] == '<')
						{
							CurrentSSEQ--;
							if(CurrentSSEQ > SSEQCount - 1)
							{
								CurrentSSEQ = SSEQCount - 1;;
							}
						}
					}
					while(SSEQList[CurrentSSEQ][0] == '<')
					{
						CurrentSSEQ++;
						if(CurrentSSEQ > SSEQCount - 1)
						{
							CurrentSSEQ = 0;
						}
					}
					ShowSSEQ();
				}
				else
				{
					if(CurrentFile < (FileCount - 24))
					{
						CurrentFile += 23;
					}
					else
					{
						CurrentFile = FileCount;
					}
					ShowDIR();
				}
			}
		}
	}
}

void ReadDIR()
{
	FileCount = 0;

	DIR *pdir;
	struct dirent *pent;
	struct stat statbuf;

	pdir = opendir("/data/NDS Music Player/");
	if (pdir)
	{
		while ((pent = readdir(pdir)) != NULL)
		{
			stat(pent -> d_name, &statbuf);
			if((pent -> d_name[strlen(pent -> d_name) - 4] == '.') && (pent -> d_name[strlen(pent -> d_name) - 3] == 's') && (pent -> d_name[strlen(pent -> d_name) - 2] == 'p') && (pent -> d_name[strlen(pent -> d_name) - 1] == 's'))
			{
				if (DIRList[FileCount] != NULL)
				{
					free(DIRList[FileCount]);
				}
				DIRList[FileCount] = malloc(strlen(pent -> d_name) + 1);
				strcpy(DIRList[FileCount], pent -> d_name);
				FileCount++;
			}
		}
		closedir(pdir);
		FileCount--;
	}
	else
	{
		iprintf("/data/NDS Music Player/ was not found.\nPlease run SPS Maker.");
		while(1)
		{
			swiWaitForVBlank();
		}
	}
}

void ShowDIR()
{
	consoleClear();
	
	if(DIRList[0] == NULL)
	{
		return true;
	}

	for(i = CurrentFile; i < CurrentFile + 23; i++)
	{
		if (i > FileCount)
		{
			break;
		}
		if(i == CurrentFile)
		{
			CurrentFileSTR = "*";
		}
		else
		{
			CurrentFileSTR = " ";
		}
		iprintf("%s%s\n", CurrentFileSTR, DIRList[i]);
	}
}

void ReadSPS()
{
	consoleClear();

	FILE* f = fopen(CurrentSPS, "rb");

	fseek(f, 0x0C, SEEK_SET);
	iprintf("Reading NDS path length.\n");
	swiWaitForVBlank();
	CharCount = fgetc(f);

	if (CurrentNDS != NULL)
	{
		free(CurrentNDS);
	}

	CurrentNDS = malloc(CharCount + 1);
	iprintf("Reading NDS path.\n");
	swiWaitForVBlank();
	fread(CurrentNDS, 1, CharCount, f);
	CurrentNDS[CharCount] = 0;
	fclose(f);
	
	f = fopen(CurrentNDS, "rb");

	iprintf("Checking NDS path.\n");
	swiWaitForVBlank();
	if(!f)
	{
		iprintf("%s\n", CurrentNDS);
		iprintf("ROM not in specified location.\nPlease run SPS Maker.");
		swiWaitForVBlank();
		while(!(keysDown() & KEY_B))
		{
			scanKeys();
			swiWaitForVBlank();
		}
		return;
	}
	else
	{
		iprintf("NDS path valid.\n");
		swiWaitForVBlank();
	}
	fclose(f);

	f = fopen(CurrentSPS, "rb");
	
	//Reads SSEQCount
	fseek(f, 0, SEEK_SET);
	iprintf("Reading SSEQ count.\n");
	swiWaitForVBlank();
	fread(&SSEQCount, 1, 4, f);

	//Reads SSEQNameOffset
	fseek(f, 0x04, SEEK_SET);
	iprintf("Reading SSEQ name offset.\n");
	swiWaitForVBlank();
	fread(&SSEQNameOffset, 1, 4, f);

	fseek(f, SSEQNameOffset, SEEK_SET);

	//Reads all SSEQ names into the char**
	for (i = 0; i < SSEQCount; i++)
	{
		if (SSEQList[i] != NULL)
		{
			free(SSEQList[i]);
		}

		CharCount = fgetc(f);
		SSEQList[i] = malloc(CharCount + 1);
		/*iprintf("Reading SSEQ_%d name.\n", i);
		swiWaitForVBlank();*/
		fread(SSEQList[i], 1, CharCount, f);
		SSEQList[i][CharCount] = 0;
	}

	CurrentSSEQ = 0;
	SSEQMode = true;
	fclose(f);
	ShowSSEQ();
}

//displays the list of SSEQs
void ShowSSEQ()
{
	//Clears screen
	consoleClear();	

	//displays the current SSEQ and up to 22 more files after it
	for(i = CurrentSSEQ; i < CurrentSSEQ + 23; i++)
	{
		//stops list short if CurrentSSEQ + 23 is over SSEQCount
		if (i >= SSEQCount)
		{
			break;
		}

		//adds a * infront of the selected file
		if(i == CurrentSSEQ)
		{
			CurrentFileSTR = "*";
		}
		else
		{
			CurrentFileSTR = " ";
		}

		//Prints text on screen
		iprintf("%s%s\n", CurrentFileSTR, SSEQList[i]);
	}
}

void ReadSSEQ()
{
	//Clears screen
	consoleClear();

	//Opens file
	FILE* f = fopen(CurrentSPS, "rb");

	//Reads SSEQDataOffset
	fseek(f, 0x08, SEEK_SET);
	iprintf("Reading SSEQ data offset.\n");
	swiWaitForVBlank();
	fread(&SSEQDataOffset, 1, 4, f);

	//Reads SSEQ offset
	fseek(f, SSEQDataOffset + (CurrentSSEQ * 48), SEEK_SET);
	iprintf("Reading SSEQ offset.\n");
	swiWaitForVBlank();
	fread(&SSEQOffset, 1, 4, f);

	//Reads SSEQ size
	iprintf("Reading SSEQ size.\n");
	swiWaitForVBlank();
	fread(&SSEQSize, 1, 4, f);

	//Reads BANK offset
	iprintf("Reading BANK offset.\n");
	swiWaitForVBlank();
	fread(&BANKOffset, 1, 4, f);

	//Reads BANK size
	iprintf("Reading BANK size.\n");
	swiWaitForVBlank();
	fread(&BANKSize, 1, 4, f);

	//Reads WAVEARC1 offset
	iprintf("Reading WAVEARC1 offset.\n");
	swiWaitForVBlank();
	fread(&WAVEARC1Offset, 1, 4, f);

	//Reads WAVEARC1 size
	iprintf("Reading WAVEARC1 size.\n");
	swiWaitForVBlank();
	fread(&WAVEARC1Size, 1, 4, f);

	//Reads WAVEARC2 offset
	iprintf("Reading WAVEARC2 offset.\n");
	swiWaitForVBlank();
	fread(&WAVEARC2Offset, 1, 4, f);

	//Reads WAVEARC2 size
	iprintf("Reading WAVEARC2 size.\n");
	swiWaitForVBlank();
	fread(&WAVEARC2Size, 1, 4, f);

	//Reads WAVEARC3 offset
	iprintf("Reading WAVEARC3 offset.\n");
	swiWaitForVBlank();
	fread(&WAVEARC3Offset, 1, 4, f);

	//Reads WAVEARC3 size
	iprintf("Reading WAVEARC3 size.\n");
	swiWaitForVBlank();
	fread(&WAVEARC3Size, 1, 4, f);

	//Reads WAVEARC4 offset
	iprintf("Reading WAVEARC4 offset.\n");
	swiWaitForVBlank();
	fread(&WAVEARC4Offset, 1, 4, f);

	//Reads WAVEARC4 size
	iprintf("Reading WAVEARC4 size.\n");
	swiWaitForVBlank();
	fread(&WAVEARC4Size, 1, 4, f);

	//Closes file
	fclose(f);

	//Plays SSEQ
	if(SSEQOffset != 0)
	{
		PlaySeqNDS(CurrentNDS, SSEQOffset, SSEQSize, BANKOffset, BANKSize, WAVEARC1Offset, WAVEARC1Size, WAVEARC2Offset, WAVEARC2Size, WAVEARC3Offset, WAVEARC3Size, WAVEARC4Offset, WAVEARC4Size);
		iprintf("Playing %s.\n", SSEQList[CurrentSSEQ]);
		swiWaitForVBlank();
		PlayMode = true;
		SSEQMode = false;
	}
	else
	{
		iprintf("%s seems to be a dummy file.\n", SSEQList[CurrentSSEQ]);
		while(!(keysDown() & KEY_B))
		{
			scanKeys();
			swiWaitForVBlank();
		}
		ShowSSEQ();
	}
}

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: Problem with a string.

Post by Discostew » Sun Sep 04, 2011 5:04 pm

Pointer variables, such as 'CurrentNDS' might contain a garbage value when first constructed, leaving them wide open to problems when examined if they contain anything when they aren't equal to NULL. For these cases, initialize them to NULL first. Also, it is a good idea that after you 'free' an array to set it's pointer value to NULL as well to avoid false positives later on if and when they get examined again.

Not completely sure if this is the cause of the problem, but it's a possibility.

zeromus
Posts: 212
Joined: Wed Mar 31, 2010 6:05 pm

Re: Problem with a string.

Post by zeromus » Sun Sep 04, 2011 8:05 pm

23 isnt enough extra bytes for CurrentSPS (no room for null terminator)

print the name of the SPS file before trying to open it.
Check the return value from fopen(CurrentSPS, "rb") to make sure it isn't null.

SSEQList needs to be null initialized, in addition to what discostew suggested.

etc, etc

Kazo
Posts: 14
Joined: Mon Aug 29, 2011 8:06 am

Re: Problem with a string.

Post by Kazo » Tue Sep 06, 2011 3:35 pm

ok thanks

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 75 guests