View Single Post
Re: a problem about difference between GNU/Linux and Windows
Old
  (#2)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: a problem about difference between GNU/Linux and Windows - 07-01-2005

I think I know where is your problem
imagine you have text file made under windows, so end line is CR+LF. And now you will load this .ini file into your parser in windows and linux :

WINDOWS :
if you call fgets, it will detect newline as CR+LF and end character in your string is \n
You will use [length - 2] and everything is allright

LINUX :
if you call fgets, it reads until LF and string will have \r\n in the end. So you must use [length -3] to skip to section name

How to fix it :
I use something like this in my parser to skip trailing whitespaces
Code:
#include <string>

int main()
{
	std::string line = "    test string   \r\t\n  \r  \t\n  ";
	std::string result;

	std::string::size_type j;
	std::string::size_type begin = 0;
	std::string::size_type end = line.size();

	// skip all leading whitespaces
	for (j = begin; j < line.size(); ++j) {
		if (line[j] > ' ') 
			break; 
		++begin;
	}
	// skip all trailing whitespaces
	for (j = end; j > 0; --j) {
		if (line[j - 1] > ' ') 
			break; 
		--end;
	}

	if (end > begin)
		result = line.substr (begin, end - begin);
	else
		result = " ";
}
However this code is for std::string class, not evil char array

You only check for \n in your trim function. You should also check for \r, or better, check for all whitespace characters by comparing them with space, like I did in previous example

more info : http://en.wikipedia.org/wiki/Newline


kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)

Last edited by koraX; 07-01-2005 at 09:46..
  
Reply With Quote