1

I have this bit of code:

                        second = strtok (NULL,"\n");
                        logprintf(second);
                        if(_stricmp(second,"WINDOWS") == 0)

The logprintf prints data into a logfile, and it prints "WINDOWS" (without quotes).

but _stricmp returns 13 somehow.. So the if check never gets passed. I've tried doing sscanf/sprintf/other string ways but none work. I'm out of ideas.

The full code:

#ifdef WIN32


char buf[65535];
bool found = false;
bool install = false;
bool installing = false;
unsigned int installed = 0;


WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(L"./*.AIFPAK", &fd);

char * NAME = NULL;
char * ID = NULL;
char * AUTHOR = NULL;
int VERSION;

HZIP hz = OpenZip(fd.cFileName,0);

ZIPENTRY ze; 
GetZipItem(hz,-1,&ze); 
int numitems=ze.index;
for (int i=0; i<numitems; ++i)
{ 
    GetZipItem(hz,i,&ze);

    if(found == false)
    {
        if(_wcsicmp(ze.name,L"INSTALL.AIFLIST") == 0)
        {
            found = true;
            UnzipItem(hz,i,buf,65535);
            i = 0;
            stringstream strx;
            strx << buf;
            string line;     
            while (getline(strx, line)) { 
                char * first = NULL;
                char * second = NULL;
                char * third = NULL;
                first = strtok ((char *)line.c_str()," ");
                if(install == false)
                {
                    if(_stricmp(first,"NAME") == 0)
                    {
                        NAME = strtok (NULL,"\n");
                    }
                    if(_stricmp(first,"ID") == 0)
                    {
                        ID = strtok (NULL,"\n");
                    }
                    if(_stricmp(first,"AUTHOR") == 0)
                    {
                        AUTHOR = strtok (NULL,"\n");
                    }
                    if(_stricmp(first,"VERSION") == 0)
                    {
                        VERSION = atoi(strtok (NULL,"\n"));
                    }
                    if(_stricmp(first,"START_INSTALL") == 0)
                    {
                        second = strtok (NULL,"\n");
                        logprintf(second);
                        if(_stricmp(second,"WINDOWS") == 0)
                        {
                            cout << "INSTALLAH\n";
                            install = true;
                            cout << NAME << "|" << ID << "|" << AUTHOR << "|" << VERSION << "|\n";
                        }
                    }
                }
                else
                {
                    cout << "ELSE FIRST: " << first << "\n";
                    if(_stricmp(first,"UNPACK") == 0)
                    {
                        second = strtok (NULL,">");
                        third = strtok (NULL,"\n");
                        cout << first << "|" << second << "|" << third << "|\n";
                        ToDoVec.push_back(ToDoInfo(0,second,third));
                    }
                    if(_stricmp(first,"PRINT") == 0)
                    {
                        second = strtok (NULL,"\n");
                        cout << first << "|" << second << "|" << third << "|\n";
                        ToDoVec.push_back(ToDoInfo(3,second,""));
                    }
                    if(_stricmp(first,"ADD_PLUGIN") == 0)
                    {
                        second = strtok (NULL,"\n");
                        cout << first << "|" << second << "|" << third << "|\n";
                        ToDoVec.push_back(ToDoInfo(1,second,""));
                    }
                    if(_stricmp(first,"ADD_FILTERSCRIPT") == 0)
                    {
                        second = strtok (NULL,"\n");
                        cout << first << "|" << second << "|" << third << "|\n";
                        ToDoVec.push_back(ToDoInfo(2,second,""));
                    }
                    if(_stricmp(first,"END_INSTALL") == 0)
                    {
                        break;
                    }
                }
            } 
        }
    }
    else
    {
        if(installing == false)
        {
            i = 0;
            installing = true;
            for(unsigned int ix = 0; ix < ToDoVec.size(); ++ix)
            {
                cout << "|" << ToDoVec.at(ix).action << "|" << ToDoVec.at(ix).string1 << "|" << ToDoVec.at(ix).string2 << "|\n";
            }
        }
        else
        {

        }
    }
}
found = false;
install = false;
installing = false
CloseZip(hz);

while (FindNextFile(h, &fd))
{
//fnVec.push_back(fd.cFileName);
}
#else//assuming linux

#endif

The "INSTALL.AIFLIST" file looks like this:

NAME Route Connector Plugin
ID GAMER_GPS
VERSION 1733
AUTHOR Gamer_Z

START_INSTALL WINDOWS

UNPACK RouteConnector/plugins/RouteConnectorPlugin.dll>./plugins/RouteConnectorPlugin.dll
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.amx>./filterscripts/Node_GPS.amx
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.pwn>./filterscripts/Node_GPS.pwn
UNPACK RouteConnector/scriptfiles/GPS.dat>./scriptfiles/GPS.dat
UNPACK RouteConnector/sampGDK/EXTRACTED/libsampgdk-2.2.1-win32/bin/sampgdk2.dll>./sampgdk2.dll

ADD_PLUGIN RouteConnectorPlugin
ADD_FILTERSCRIPT Node_GPS

END_INSTALL

START_INSTALL LINUX

UNPACK RouteConnector/plugins/RouteConnectorPlugin.so>./plugins/RouteConnectorPlugin.so
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.amx>./filterscripts/Node_GPS.amx
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.pwn>./filterscripts/Node_GPS.pwn
UNPACK RouteConnector/scriptfiles/GPS.dat>./scriptfiles/GPS.dat
PRINT To make this plugin work you must install the sampgdk library from www.github.com/Zeex/ (if you don't have it installed)

ADD_PLUGIN RouteConnectorPlugin.so
ADD_FILTERSCRIPT Node_GPS

END_INSTALL

All the data is read correctly into 'buf'.

Can anybody suggest how to fix that problem?

  • 1
    Is there a space before or after WINDOWS in the variable? Try doing a strtok with "\n " as the tokens (or when you are printing it, print quotes around it to be sure. – vmpstr Feb 21 '12 at 21:49
  • 2
    You found a use for `std::string` and `getline()`. Now why oh why are you still messing around with `strtok` and `_stricmp`? – jrok Feb 21 '12 at 21:50
  • 1
    You're calling `strtok` on the const string returned by `c_str`. Don't do that. It's undefined behavior, which explains anything else strange that occurs in your program. Find a different way that doesn't involve modifying a string that way. – Rob Kennedy Feb 21 '12 at 21:51
  • 1
    @RobKennedy Modifying the characters returned by `c_str()` is undefined behaviour because the standard says "The program shall not alter any of the values stored in the character array." It is not undefined behaviour because it is a `const` string: it probably isn't `const` at all. –  Feb 21 '12 at 21:53
  • Fixed. Thanks, @Hvd. When I said "const string," though, I meant a const C-style string; a `char const*`. – Rob Kennedy Feb 21 '12 at 21:56
  • @jrok : In OP's defence, there are no equivalents to `_stricmp` and `strtok` in the C++ standard library that are as easy to use (C++03, that is, don't know about C++11). To do it the easy "C++ way", one could use Boost.Tokenizer or Boost.StringAlgorithm, but perhaps the OP doesn't want those dependencies. – Emile Cormier Feb 21 '12 at 21:56
  • I would like to but how? :P @Emile you are true, I don't like additional depencies. –  Feb 21 '12 at 21:58
  • @RobKennedy I commented because that's normally perfectly valid, assuming the string wasn't *defined* as `const`. The behaviour of `char s[] = "abc"; const char *p = s; *(char *)p = 'd';` is defined. –  Feb 21 '12 at 22:02
  • See this answer on how to use `std::getline` to extract tokens from a string: http://stackoverflow.com/a/325042/245265. See this question on how to do case-insensitive string comparisons: http://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c – Emile Cormier Feb 21 '12 at 22:08
  • @Rafal [Here's](http://stackoverflow.com/a/53921/947836) a pretty elegant way to tokenize a string. – jrok Feb 21 '12 at 22:17
  • As for fitting my needs I think I'll have to stick to my current code. However many thank. If problems arise I'll just switch. –  Feb 21 '12 at 22:45

3 Answers3

3

It looks as if you're not accounting for CRLF line endings. Opening a file in text mode will translate "\r\n" to "\n", but there's nothing in your code that does so. If "WINDOWS" is followed by "\r\n", you're treating that as "WINDOWS\r" followed by a "\n", because "\n" is all you're passing to strtok. There are several possible solutions, but one is passing "\r\n" to strtok instead.

2

13 is the carriage return character '\r', so your input has a trailing '\r' which doesn't show when printing. To remove it, pass "\r\n" as separators to strtok.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

The 13 indicates that your second string contains "WINDOWS\r". The \r is a carriage-return character - on Windows, lines in text files are terminated with a \r\n sequence, so it seems that your getline() function is terminating at a \n and returning the \r as part of the string.

caf
  • 233,326
  • 40
  • 323
  • 462