[PROBLEM] Program Crash

Program crashes suddenly while processing a file...

Page 1 of 1

7 Replies - 1044 Views - Last Post: 02 March 2009 - 11:45 AM Rate Topic: -----

#1 Aureax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-September 08

[PROBLEM] Program Crash

Posted 01 March 2009 - 11:51 AM

Hey everyone,

I'm just a C++ newbie, but I hope you guys could help me with this one.

So I've build a program for converting a theme.ini file to a skin.xml file, but the program crashes while it's converting the values!

int x = 0; // tmp var 1
int y = 0; // tmp var 2
string vars[15]; // dataholder

void r4ini2cycloxml(char *source, char *target){

 //////////////////////////////////////////////////////
 // Read variables from original R4DS Theme.ini file //
 //////////////////////////////////////////////////////
 ifstream src;
 src.open(source, ios::in);
 while (!src.eof()) // variable read loop from original r4 theme.ini
 {
  if (x == 2) { x++; continue; } // skip vars[2]
  src >> vars[x]; // line of source r4ds theme.ini is read to vars
  if (vars[x][1] == ';') // if it's a comment
   { continue; } // return to the beginning to read the next line
  if (vars[x][1] == '[') // same for []
   { continue; } // return!
  if (vars[x][1] == 'D') // same for date indicator
   { continue; } // return!

  x++; // increase x, so next variable will be read into next vars
 }
 src.close();

 //////////////////////////////////////////
 // vars content table				   //
 ///////						   ////////
 // vars[0]	 "Title="theme title	  //
 // vars[1]	 "Author="theme author	//
 // vars[2]		undefined				//
 // vars[3-15]  "ColorXX="hex color	  //
 //////////////////////////////////////////

 //////////////////////////////////////////////////////////
 // Convert variables from R4DS format to CyclODS format //
 //////////////////////////////////////////////////////////

 // First: the theme title
 x = 5; // offset
 while(x <= (strlen(vars[0].c_str()) - 1))
 {
  vars[0][y] = vars[0][x];
  vars[0][x] = '\0';
  x++; y++;
 }

 // Theme author
 y = 0;
 x = 6; // offset
 while(x <= (strlen(vars[1].c_str()) - 1))
 {
  vars[1][y] = vars[1][x];
  vars[1][x] = '\0';
  x++; y++;
 }

 // Theme version
 vars[2] = "1.0";

 // Theme colors
 int z = 3;
 while(z <= 15){
  x = 7;
  y = 0;
  while(x <= (strlen(vars[z].c_str()) - 1))
  {
   vars[z][y] = vars[z][x];
   vars[z][x] = '\0';
   x++; y++;
  }
  z++;
 }


 //////////////////////////////////////////////////
 // Write variables to new CycloDS skin.xml file //
 //////////////////////////////////////////////////
 ofstream trgt;
 trgt.open(target, ios::trunc);
 trgt << "<?xml version=\"1.0\"?>\n";
 trgt << "<skin>\n";
 trgt << "<skin-name>" << vars[0] << "</skin-name>\n"; // theme title inserted
 trgt << "<skin-author>" << vars[1] << "</skin-author>\n"; // theme author inserted
 trgt << "<skin-version>" << vars[2] << "</skin-version>\n"; // theme version inserted
 trgt << "<top-bitmap file=\"top.bmp\" />\n";
 trgt << "<bottom-bitmap file=\"bottom.bmp\" />\n";
 trgt << "<menu-icon-bitmap file=\"menu-icons.bmp\" transparent-colour=\"" << vars[3] << "\" />\n";
 trgt << "<scrollbar-bitmap file=\"scrollbar.bmp\" transparent-colour=\"" << vars[4] << "\" />\n";
 trgt << "<cheat-icon-bitmap file=\"cheat-icons.bmp\" transparent-colour=\"" << vars[5] << "\" />\n";
 trgt << "<window-fill colour=\"" << vars[6] << "\" />\n";
 trgt << "<button-fill colour=\"" << vars[7] << "\" />\n";
 trgt << "<window-border colour=\"" << vars[8] << "\" />\n";
 trgt << "<text-list colour=\"" << vars[9] << "\" />\n";
 trgt << "<text-window colour=\"" << vars[10] << "\" />\n";
 trgt << "<text-status colour=\"" << vars[11] << "\" />\n";
 trgt << "<selection color=\"" << vars[12] << "\" transparency=\"5\" />\n";
 trgt.close();

 x = 0; y = 0;
}


Please help me! :D

This post has been edited by Aureax: 02 March 2009 - 08:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [PROBLEM] Program Crash

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: [PROBLEM] Program Crash

Posted 01 March 2009 - 11:56 AM

Common typo:
if (x = 2) { x++; continue; } // skip vars[2]

= for assignment, == for comparison.
Was This Post Helpful? 1
  • +
  • -

#3 Aureax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-September 08

Re: [PROBLEM] Program Crash

Posted 01 March 2009 - 11:59 AM

Alright and ofcourse mate, thanks very much. :)
I've recompiled it and now it works for that part...

Only now, the program crashes. :(

Full source code of the function:

void r4ini2cycloxml(char *source, char *target){

 //////////////////////////////////////////////////////
 // Read variables from original R4DS Theme.ini file //
 //////////////////////////////////////////////////////
 ifstream src;
 src.open(source, ios::in);
 while (!src.eof()) // variable read loop from original r4 theme.ini
 {
  if (x == 2) { x++; continue; } // skip vars[2]
  src >> vars[x]; // line of source r4ds theme.ini is read to vars
  if (vars[x][1] == ';') // if it's a comment
   { continue; } // return to the beginning to read the next line
  if (vars[x][1] == '[') // same for []
   { continue; } // return!
  if (vars[x][1] == 'D') // same for date indicator
   { continue; } // return!

  x++; // increase x, so next variable will be read into next vars
 }
 src.close();

 //////////////////////////////////////////
 // vars content table				   //
 ///////						   ////////
 // vars[0]	 "Title="theme title	  //
 // vars[1]	 "Author="theme author	//
 // vars[2]		undefined				//
 // vars[3-15]  "ColorXX="hex color	  //
 //////////////////////////////////////////

 //////////////////////////////////////////////////////////
 // Convert variables from R4DS format to CyclODS format //
 //////////////////////////////////////////////////////////

 // First: the theme title
 x = 5; // offset
 while(x <= (strlen(vars[0].c_str()) - 1))
 {
  vars[0][y] = vars[0][x];
  vars[0][x] = '\0';
  x++; y++;
 }

 // Theme author
 y = 0;
 x = 6; // offset
 while(x <= (strlen(vars[1].c_str()) - 1))
 {
  vars[1][y] = vars[1][x];
  vars[1][x] = '\0';
  x++; y++;
 }

 // Theme version
 vars[2] = "1.0";

 // Theme colors
 int z = 3;
 while(z <= 15){
  x = 7;
  y = 0;
  while(x <= (strlen(vars[z].c_str()) - 1))
  {
   vars[z][y] = vars[z][x];
   vars[z][x] = '\0';
   x++; y++;
  }
  z++;
 }


 //////////////////////////////////////////////////
 // Write variables to new CycloDS skin.xml file //
 //////////////////////////////////////////////////
 ofstream trgt;
 trgt.open(target, ios::trunc);
 trgt << "<?xml version=\"1.0\"?>\n";
 trgt << "<skin>\n";
 trgt << "<skin-name>" << vars[0] << "</skin-name>\n"; // theme title inserted
 trgt << "<skin-author>" << vars[1] << "</skin-author>\n"; // theme author inserted
 trgt << "<skin-version>" << vars[2] << "</skin-version>\n"; // theme version inserted
 trgt << "<top-bitmap file=\"top.bmp\" />\n";
 trgt << "<bottom-bitmap file=\"bottom.bmp\" />\n";
 trgt << "<menu-icon-bitmap file=\"menu-icons.bmp\" transparent-colour=\"" << vars[3] << "\" />\n";
 trgt << "<scrollbar-bitmap file=\"scrollbar.bmp\" transparent-colour=\"" << vars[4] << "\" />\n";
 trgt << "<cheat-icon-bitmap file=\"cheat-icons.bmp\" transparent-colour=\"" << vars[5] << "\" />\n";
 trgt << "<window-fill colour=\"" << vars[6] << "\" />\n";
 trgt << "<button-fill colour=\"" << vars[7] << "\" />\n";
 trgt << "<window-border colour=\"" << vars[8] << "\" />\n";
 trgt << "<text-list colour=\"" << vars[9] << "\" />\n";
 trgt << "<text-window colour=\"" << vars[10] << "\" />\n";
 trgt << "<text-status colour=\"" << vars[11] << "\" />\n";
 trgt << "<selection color=\"" << vars[12] << "\" transparency=\"5\" />\n";
 trgt.close();

 x = 0; y = 0;
}

This post has been edited by Aureax: 01 March 2009 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: [PROBLEM] Program Crash

Posted 01 March 2009 - 12:03 PM

where did u declare x and y?
Was This Post Helpful? 1
  • +
  • -

#5 Aureax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-September 08

Re: [PROBLEM] Program Crash

Posted 01 March 2009 - 12:06 PM

At the beginning of the program. ;)

int x = 0; // tmp var 1
int y = 0; // tmp var 2

Was This Post Helpful? 0
  • +
  • -

#6 Aureax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-September 08

Re: [PROBLEM] Program Crash

Posted 02 March 2009 - 08:50 AM

Nobody? :(
Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: [PROBLEM] Program Crash

Posted 02 March 2009 - 09:32 AM

Are you sure the error is occuring when the program is reading data from the input stream?
Was This Post Helpful? 0
  • +
  • -

#8 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: [PROBLEM] Program Crash

Posted 02 March 2009 - 11:45 AM

Does my r4DsToCycloDs function work for you?

char *trim(char *);

int r4DsToCycloDs(const char *r4DsIniFile, const char *cycloDsXmlFile)
{
	ifstream r4DsStream;
	ofstream cycloDsStream;
	char skinName[40] = "";
	char skinAuthor[40] = "";
	char skinColors[13][40] = {""};

	r4DsStream.open(r4DsIniFile);

	if(!r4DsStream.is_open())
		return 0;

	while(!r4DsStream.eof()) {
		char line[80];

		r4DsStream.getline(line, 80);

		/* Remove leading and trailing whitespace */
		strcpy(line, trim(line));

		if(*line == ';' || *line == '[') /* Ignore comment and description lines */
			continue;

		if(strchr(line, '=')) {
			char *varName;
			char *varValue;

			/* Split the line into variable name and value */
			varName  = strtok(line, "=");
			varValue = strtok(NULL, "=");

			/* Remove leading and trailing whitespace */
			varName  = trim(varName);
			varValue = trim(varValue);

			/* Save the values of the variables */
			if(!strcmp(varName, "Title"))
				strcpy(skinName, varValue);
			else if(!strcmp(varName, "Author"))
				strcpy(skinAuthor, varValue);
			else if(!strncmp(varName, "Color", 5)) {
				int i;

				i = atoi(varName + 5); /*  Which color value are we setting? */

				if(i >= 0 && i <= 12)
					strcpy(skinColors[i], varValue);
			}
		}
	}

	r4DsStream.close();

	cycloDsStream.open(cycloDsXmlFile);

	if(!cycloDsStream.is_open())
		return 0;

	/* Write the variable values from the R4DS theme INI file
	   to the CycloDS theme XML file */
	cycloDsStream << "<?xml version=\"1.0\"?>" << endl << endl;
	cycloDsStream << "<skin>";
	cycloDsStream << "<skin-name>" << skinName << "</skin-name>";
	cycloDsStream << "<skin-author>" << skinAuthor << "</skin-author>";
	/* Assume version 1.0 */
	cycloDsStream << "<skin-version>1.0</skin-version>" << endl;
	cycloDsStream << "<top-bitmap file=\"top.bmp\" />";
	cycloDsStream << "<bottom-bitmap file=\"bottom.bmp\" />";
	cycloDsStream << "<menu-icon-bitmap file=\"menu-icons.bmp\" transparent-colour=\"" << skinColors[0] << "\" />";
	cycloDsStream << "<scrollbar-bitmap file=\"scrollbar.bmp\" transparent-colour=\"" << skinColors[1] << "\" />";
	cycloDsStream << "<cheat-icon-bitmap file=\"cheat-icons.bmp\" transparent-colour=\"" << skinColors[2] << "\" />" << endl;
	cycloDsStream << "<window-fill colour=\"" << skinColors[3] << "\" />";
	cycloDsStream << "<button-fill colour=\"" << skinColors[4] << "\" />";
	cycloDsStream << "<window-border colour=\"" << skinColors[5] << "\" />";
	cycloDsStream << "<text-list colour=\"" << skinColors[6] << "\" />";
	cycloDsStream << "<text-window colour=\"" << skinColors[7] << "\" />";
	cycloDsStream << "<text-status colour=\"" << skinColors[8] << "\" />";
	/* Selection bar transparency defaults to 7 */
	cycloDsStream << "<selection colour=\"" << skinColors[9] << "\" transparency=\"7\" />" << endl;
	cycloDsStream << "</skin>";

	cycloDsStream.close();

	return 1;
}

char *trim(char *string)
{
	int i;

	if(!string)
		return NULL;

	while(isspace(*string))
		string ++;

	i = strlen(string) - 1;

	while(isspace(string[i]))
		string[i --] = '\0';

	return string;
}

This post has been edited by bsaunders: 02 March 2009 - 11:57 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1