.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
a problem about difference between GNU/Linux and Windows
Old
  (#1)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default a problem about difference between GNU/Linux and Windows - 07-01-2005

This is from my .ini parser:
PHP Code:
      // check if this is a session line (e.g., [SECTION])
      
if (str[0] == '[' && str[length 1] == ']') {
         
strcpy(section, &str[1]);
         
section[length 2] = 0// remove the ] 
This works in Windows, but in GNU/Linux I have to use:
PHP Code:
      // check if this is a session line (e.g., [SECTION])
      
if (str[0] == '[' && str[length 2] == ']') {
         
strcpy(section, &str[1]);
         
section[length 3] = 0// remove the ] 
The problem is, why does the GNU/Linux has to use a different way ? Also I'm using gcc 3.4.2 for both of the OS's

I have also pre-processed the string read from the .ini file with this function:
PHP Code:
void trim(char *str)
{
   
int pos 0;
   
char *dest str;

   
// skip leading blanks
   
while (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n')
      
pos++;

   while (
str[pos]) {
      *
dest++ = str[pos];
      
pos++;
   }

   *
dest '\0'// store the null

   
int i strlen(str) - 1;
   while (
&& (str[i] == ' ' || str[i] == '\t' || str[i] == '\n')) { // remove trailing blanks
      
str[i--] = '\0';
   }

  
Reply With Quote
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 10:46..
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com