Dini HELP!
#1

ok i got dini but i wana change the original file format it saves to which is currently saved as no extension, and i want to save them to a .ini
i would also like to save them to a different file if possible.
heres the original Dini code:
pawn Код:
/*
 *      Dini 1.6
 *   © Copyright 2006-2008 by DracoBlue
 *
 * @author  : DracoBlue (http://dracoblue.com)
 * @date   : 13th May 2006
 * @update  : 16th Sep 2008
 *
 * This file is provided as is (no warranties).
 *
 * It's released under the terms of MIT.
 *
 * Feel free to use it, a little message in
 * about box is honouring thing, isn't it?
 *
 */


#if defined _dini_included
 #endinput
#endif

#define _dini_included
#pragma library dini

#if defined MAX_STRING
#define DINI_MAX_STRING MAX_STRING
#else
#define DINI_MAX_STRING 255
#endif

stock dini_Exists(filename[]) {
    return fexist(filename);
}

stock dini_Remove(filename[]) {
    return fremove(filename);
}

stock dini_Create(filename[]) {
    if (fexist(filename)) return false;
    new File:fhnd;
    fhnd=fopen(filename,io_write);
    if (fhnd) {
        fclose(fhnd);
        return true;
    }
    return false;
}

stock dini_Set(filename[],key[],value[]) {
    // If we have no key, it can't be set
    // we also have no chance to set the value, if all together is bigger then the max string
    new key_length = strlen(key);
    new value_length = strlen(value);
    if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
   
    new File:fohnd, File:fwhnd;
    new tmpres[DINI_MAX_STRING];
    new bool:wasset=false;
   
    // Let's remove the old *.part file if there was one.
    format(tmpres,sizeof(tmpres),"%s.part",filename);
    fremove(tmpres);
   
    // We'll open the source file.
    fohnd=fopen(filename,io_read);
    if (!fohnd) return false;
   
    fwhnd=fopen(tmpres,io_write);
    if (!fwhnd) {
        // we can't open the second file for writing, so .. let's close the open one and exit.
        fclose(fohnd);
        return false;
    }
   
    while (fread(fohnd,tmpres)) {
        if (
            !wasset
            && tmpres[key_length]=='='
            && !strcmp(tmpres, key, true, key_length)  
        ) {
                // We've got what needs to be replaced!
                format(tmpres,sizeof(tmpres),"%s=%s",key,value);
                wasset=true;
        } else {
            DINI_StripNewLine(tmpres);
        }
        fwrite(fwhnd,tmpres);
        fwrite(fwhnd,"\r\n");
    }

    if (!wasset) {
        format(tmpres,sizeof(tmpres),"%s=%s",key,value);
        fwrite(fwhnd,tmpres);
        fwrite(fwhnd,"\r\n");
    }

    fclose(fohnd);
    fclose(fwhnd);

    format(tmpres,sizeof(tmpres),"%s.part",filename);
    if (DINI_fcopytextfile(tmpres,filename)) {
        return fremove(tmpres);
    }
    return false;
}


stock dini_IntSet(filename[],key[],value) {
 new valuestring[DINI_MAX_STRING];
 format(valuestring,DINI_MAX_STRING,"%d",value);
 return dini_Set(filename,key,valuestring);
}

stock dini_Int(filename[],key[]) {
 return strval(dini_Get(filename,key));
}

stock dini_FloatSet(filename[],key[],Float:value) {
 new valuestring[DINI_MAX_STRING];
 format(valuestring,DINI_MAX_STRING,"%f",value);
 return dini_Set(filename,key,valuestring);
}

stock Float:dini_Float(filename[],key[]) {
 return floatstr(dini_Get(filename,key));
}

stock dini_Bool(filename[],key[]) {
 return strval(dini_Get(filename,key));
}

stock dini_BoolSet(filename[],key[],value) {
    if (value) {
        return dini_Set(filename,key,"1");
    }
    return dini_Set(filename,key,"0");
}

stock dini_Unset(filename[],key[]) {
    // If we have no key, it can't be set
    // we also have no chance to unset the key, if all together is bigger then the max string
    new key_length = strlen(key);
    if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
   
    new File:fohnd, File:fwhnd;
    new tmpres[DINI_MAX_STRING];
   
    // Let's remove the old *.part file if there was one.
    format(tmpres,DINI_MAX_STRING,"%s.part",filename);
    fremove(tmpres);
   
    // We'll open the source file.
    fohnd=fopen(filename,io_read);
    if (!fohnd) return false;
   
    fwhnd=fopen(tmpres,io_write);
    if (!fwhnd) {
        // we can't open the second file for writing, so .. let's close the open one and exit.
        fclose(fohnd);
        return false;
    }
   
    while (fread(fohnd,tmpres)) {
        if (
            tmpres[key_length]=='='
            && !strcmp(tmpres, key, true, key_length)  
        ) {
                // We've got what needs to be removed!
        } else {
            DINI_StripNewLine(tmpres);
            fwrite(fwhnd,tmpres);
            fwrite(fwhnd,"\r\n");
        }
    }
   
    fclose(fohnd);
    fclose(fwhnd);

    format(tmpres,DINI_MAX_STRING,"%s.part",filename);
    if (DINI_fcopytextfile(tmpres,filename)) {
        return fremove(tmpres);
    }
    return false;
}

stock dini_Get(filename[],key[]) {
    new tmpres[DINI_MAX_STRING];
   
    new key_length = strlen(key);
    if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
   
    new File:fohnd;
    fohnd=fopen(filename,io_read);
    if (!fohnd) return tmpres;
   
    while (fread(fohnd,tmpres)) {
        if (
            tmpres[key_length]=='='
            && !strcmp(tmpres, key, true, key_length)  
        ) {
            /* We've got what we need */
            DINI_StripNewLine(tmpres);
            strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
            fclose(fohnd);
            return tmpres;
        }
    }
    fclose(fohnd);
    return tmpres;
}


stock dini_Isset(filename[],key[]) {
    new key_length = strlen(key);
    if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
   
    new File:fohnd;
    fohnd=fopen(filename,io_read);
    if (!fohnd) return false;
   
    new tmpres[DINI_MAX_STRING];
    while (fread(fohnd,tmpres)) {
        if (
                tmpres[key_length]=='='
            && !strcmp(tmpres, key, true, key_length)  
        ) {
            // We've got what we need
            fclose(fohnd);
            return true;
        }
    }
    fclose(fohnd);
    return false;
}



stock DINI_StripNewLine(string[]) {
    new len = strlen(string);
    if (string[0]==0) return ;
    if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
        string[len - 1] = 0;
        if (string[0]==0) return ;
        if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
    }
}

stock DINI_fcopytextfile(oldname[],newname[]) {
    new File:ohnd,File:nhnd;
    if (!fexist(oldname)) return false;
    ohnd=fopen(oldname,io_read);
    if (!ohnd) return false;
    nhnd=fopen(newname,io_write);
    if (!nhnd) {
        fclose(ohnd);
        return false;
    }
    new tmpres[DINI_MAX_STRING];
    while (fread(ohnd,tmpres)) {
        DINI_StripNewLine(tmpres);
        format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
        fwrite(nhnd,tmpres);
    }
    fclose(ohnd);
    fclose(nhnd);
    return true;
}
Reply
#2

You change it in Your script.

Instead of:
pawn Код:
dini_Create(pname);
You would do:
pawn Код:
new dinistr[30];
        format(dinistr, sizeof(dinistr), "%s.txt", pname);
        dini_Create(dinistr);
And instead of:
pawn Код:
dini_Int(pname, "level");
You would do:
pawn Код:
dini_Int(dinistr, "level");
Etc, etc, etc...
Reply
#3

ok and if im right, to put it in a folder i would do:
pawn Код:
new dinistr[30];
        format(dinistr, sizeof(dinistr), "/accounts/%s.txt", pname);
        dini_Create(dinistr);
Reply
#4

Yup. But make sure your string is big enough.

24 (MAX_PLAYER_NAME) + 14 (amount of chars in format statement) = 38, so:
pawn Код:
new dinistr[40];
Reply
#5

THX!, one more thing, im trying to count the deaths but its not working, heres the script i made:
pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, sizeof(pname));
    new dinistr[40];
    format(dinistr, sizeof(dinistr), "/accounts/%s.ini", pname);

    new death;
    death = dini_Int(dinistr, "deaths");
    dini_IntSet(dinistr, "deaths", death +1);
}
what did i do wrong?
Reply
#6

I just tested it, and it works fine for me. Are you sure all the dini functions have been changed from "pname" to "dinistr"? And all extensions are ".ini"?
Reply
#7

i dont know, i checked but still... also this doesnt work... what did i do wrong?
pawn Код:
if(strcmp("/teleto", cmdtext, true, 7) == 0)
    {
        if(Level[playerid] == 0) return SendClientMessage(playerid, red, "Unknown Command: Type /cmds For The Commands.");
        if(strlen(cmdtext[7]) == 0) return SendClientMessage(playerid, red, "USAGE: /teleto [player id]");
        if(IsPlayerConnected(strval(cmdtext[7])) == 0) return SendClientMessage(playerid, red, "That Player Is Not Connected.");

        new cmdname[MAX_PLAYER_NAME], string[128];
        GetPlayerName(playerid, cmdname, sizeof(cmdname));
        GetPlayerName(strval(cmdtext[7]), cmdname, sizeof(cmdname));

        format(string, sizeof(string), "* You Have Teleported To %s (%d).", cmdname, playerid);
        SendClientMessage(playerid, pink, string);

        new Float:X,Float:Y,Float:Z;
        GetPlayerPos(cmdtext[7],X,Y,Z);
        SetPlayerPos(playerid,X +1,Y +1,Z +1);
        return 1;
    }
Reply
#8

2 things,

1
pawn Код:
/teleto 0
123456789
/teleto is indeed 7 characters long, what they type after that will be at the 8th char.

2
"strval" gets the value of a string. cmdtext is a string. Instead of:
pawn Код:
GetPlayerPos(cmdtext[8],X,Y,Z);
you need:
pawn Код:
GetPlayerPos(strval(cmdtext[8]),X,Y,Z);
So:
pawn Код:
if(strcmp("/teleto", cmdtext, true, 7) == 0)
    {
        if(Level[playerid] == 0) return SendClientMessage(playerid, red, "Unknown Command: Type /cmds For The Commands.");
        if(strlen(cmdtext[8]) == 0) return SendClientMessage(playerid, red, "USAGE: /teleto [player id]");
        if(IsPlayerConnected(strval(cmdtext[8])) == 0) return SendClientMessage(playerid, red, "That Player Is Not Connected.");

        new cmdname[MAX_PLAYER_NAME], string[128];
        GetPlayerName(playerid, cmdname, sizeof(cmdname));
        GetPlayerName(strval(cmdtext[8]), cmdname, sizeof(cmdname));

        format(string, sizeof(string), "* You Have Teleported To %s (%d).", cmdname, playerid);
        SendClientMessage(playerid, pink, string);

        new Float:X,Float:Y,Float:Z;
        GetPlayerPos(strval(cmdtext[8]),X,Y,Z);
        SetPlayerPos(playerid,X +1,Y +1,Z +1);
        return 1;
    }
EDIT:

Also, just for a heads up, doing:
pawn Код:
if(Level[playerid] == 0) return 0;
Will give the same effect as if they did an unknown command.
Reply
#9

Quote:
Originally Posted by Ethan1233
THX!, one more thing, im trying to count the deaths but its not working, heres the script i made:
pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, sizeof(pname));
    new dinistr[40];
    format(dinistr, sizeof(dinistr), "/accounts/%s.ini", pname);

    new death;
    death = dini_Int(dinistr, "deaths");
    dini_IntSet(dinistr, "deaths", death +1);
}
what did i do wrong?
try udb_encode(pname) while formatting
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)