.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   a problem about difference between GNU/Linux and Windows (http://forums.bots-united.com/showthread.php?t=3352)

Whistler 07-01-2005 04:34

a problem about difference between GNU/Linux and Windows
 
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';
   }



koraX 07-01-2005 09:31

Re: a problem about difference between GNU/Linux and Windows
 
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


All times are GMT +2. The time now is 18:09.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.