Getting an int from a file.
#1

pawn Код:
stock INI_GetInt(INI:filehandle, Key[], &dest) {

    if(!filehandle) return printf("Failed to open file!");
   
    new pos, line[MAX_FILE_STRING], temp[MAX_FILE_STRING];
   
    while( fread(filehandle, line) ) {
   
        if( (pos = strfind(line, Key)) != -1 ) {//it's found the key
       
            new idx = pos + strlen(Key)+1;
           
            strmid(temp, line[pos], 0, idx);
           
            if(!strcmp(temp[idx], " ", true)) {//if there is a space the strings won't match later on
                strmid(temp, line, 0, idx-1);
            }

            if(!strcmp(temp, Key, true)) {//If there is something like Score and PlayersScore strfind won't work
               
                if(line[idx] == '=') {//the value is a assignment
           
                    idx = pos + strlen(Key) + 2;//if the = sign is further across
               
                    return dest = (idx < sizeof(line)) ? strval(line[idx]) : 0;
               
                }
            }
        }
   
    }
    return 1;
}
It works.. but I'm testing to make sure if there is something like below; I want to get the value of score (5) but it returns scores
Код:
scores=5
score=5
Reply
#2

Nevermind, thought you were using Y_INI...
Reply
#3

you're going to have to find a way to do a strcmp check rather than a strfind
Reply
#4

If you look, I have 2 strcmp's to try and counter this problem.
Reply
#5

pawn Код:
stock INI_GetInt(INI:filehandle, Key[], &dest)
{

    if(!filehandle)
    {
        printf("Failed to open file!");
        return;
    }

    new
        line[128],
        len = strlen(Key);

    while( fread(filehandle, line) )
    {
        if(!strcmp(line, Key, true, len) && line[len] == '=')
        {
            strmid(line,line,len+1,strlen(line)-2);
            dest = strval(line);
            break;
        }
    }
}
Reply
#6

Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)