Advertisement
delaypawn

dini

Aug 11th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.14 KB | None | 0 0
  1. /*
  2.  *            Dini 1.6
  3.  *       (c) Copyright 2006-2008 by DracoBlue
  4.  *
  5.  * @author    : DracoBlue (http://dracoblue.com)
  6.  * @date      : 13th May 2006
  7.  * @update    : 16th Sep 2008
  8.  *
  9.  * This file is provided as is (no warranties).
  10.  *
  11.  * It's released under the terms of MIT.
  12.  *
  13.  * Feel free to use it, a little message in
  14.  * about box is honouring thing, isn't it?
  15.  *
  16.  */
  17.  
  18. #if defined _dini_included
  19.   #endinput
  20. #endif
  21.  
  22. #define _dini_included
  23. #pragma library dini
  24.  
  25. #if defined MAX_STRING
  26. #define DINI_MAX_STRING MAX_STRING
  27. #else
  28. #define DINI_MAX_STRING 255
  29. #endif
  30.  
  31. stock dini_Exists(filename[]) {
  32.     return fexist(filename);
  33. }
  34.  
  35. stock dini_Remove(filename[]) {
  36.     return fremove(filename);
  37. }
  38.  
  39. stock dini_Create(filename[]) {
  40.     if (fexist(filename)) return false;
  41.     new File:fhnd;
  42.     fhnd=fopen(filename,io_write);
  43.     if (fhnd) {
  44.         fclose(fhnd);
  45.         return true;
  46.     }
  47.     return false;
  48. }
  49.  
  50. stock dini_Set(filename[],key[],value[]) {
  51.     // If we have no key, it can't be set
  52.     // we also have no chance to set the value, if all together is bigger then the max string
  53.     new key_length = strlen(key);
  54.     new value_length = strlen(value);
  55.     if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
  56.    
  57.     new File:fohnd, File:fwhnd;
  58.     new tmpres[DINI_MAX_STRING];
  59.     new bool:wasset=false;
  60.    
  61.     // Let's remove the old *.part file if there was one.
  62.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  63.     fremove(tmpres);
  64.    
  65.     // We'll open the source file.
  66.     fohnd=fopen(filename,io_read);
  67.     if (!fohnd) return false;
  68.    
  69.     fwhnd=fopen(tmpres,io_write);
  70.     if (!fwhnd) {
  71.         // we can't open the second file for writing, so .. let's close the open one and exit.
  72.         fclose(fohnd);
  73.         return false;
  74.     }
  75.    
  76.     while (fread(fohnd,tmpres)) {
  77.         if (
  78.             !wasset
  79.             && tmpres[key_length]=='='
  80.             && !strcmp(tmpres, key, true, key_length)  
  81.         ) {
  82.                 // We've got what needs to be replaced!
  83.                 format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  84.                 wasset=true;
  85.         } else {
  86.             DINI_StripNewLine(tmpres);
  87.         }
  88.         fwrite(fwhnd,tmpres);
  89.         fwrite(fwhnd,"\r\n");
  90.     }
  91.  
  92.     if (!wasset) {
  93.         format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  94.         fwrite(fwhnd,tmpres);
  95.         fwrite(fwhnd,"\r\n");
  96.     }
  97.  
  98.     fclose(fohnd);
  99.     fclose(fwhnd);
  100.  
  101.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  102.     if (DINI_fcopytextfile(tmpres,filename)) {
  103.         return fremove(tmpres);
  104.     }
  105.     return false;
  106. }
  107.  
  108.  
  109. stock dini_IntSet(filename[],key[],value) {
  110.    new valuestring[DINI_MAX_STRING];
  111.    format(valuestring,DINI_MAX_STRING,"%d",value);
  112.    return dini_Set(filename,key,valuestring);
  113. }
  114.  
  115. stock dini_Int(filename[],key[]) {
  116.    return strval(dini_Get(filename,key));
  117. }
  118.  
  119. stock dini_FloatSet(filename[],key[],Float:value) {
  120.    new valuestring[DINI_MAX_STRING];
  121.    format(valuestring,DINI_MAX_STRING,"%f",value);
  122.    return dini_Set(filename,key,valuestring);
  123. }
  124.  
  125. stock Float:dini_Float(filename[],key[]) {
  126.    return floatstr(dini_Get(filename,key));
  127. }
  128.  
  129. stock dini_Bool(filename[],key[]) {
  130.    return strval(dini_Get(filename,key));
  131. }
  132.  
  133. stock dini_BoolSet(filename[],key[],value) {
  134.     if (value) {
  135.         return dini_Set(filename,key,"1");
  136.     }
  137.     return dini_Set(filename,key,"0");
  138. }
  139.  
  140. stock dini_Unset(filename[],key[]) {
  141.     // If we have no key, it can't be set
  142.     // we also have no chance to unset the key, if all together is bigger then the max string
  143.     new key_length = strlen(key);
  144.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  145.    
  146.     new File:fohnd, File:fwhnd;
  147.     new tmpres[DINI_MAX_STRING];
  148.    
  149.     // Let's remove the old *.part file if there was one.
  150.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  151.     fremove(tmpres);
  152.    
  153.     // We'll open the source file.
  154.     fohnd=fopen(filename,io_read);
  155.     if (!fohnd) return false;
  156.    
  157.     fwhnd=fopen(tmpres,io_write);
  158.     if (!fwhnd) {
  159.         // we can't open the second file for writing, so .. let's close the open one and exit.
  160.         fclose(fohnd);
  161.         return false;
  162.     }
  163.    
  164.     while (fread(fohnd,tmpres)) {
  165.         if (
  166.             tmpres[key_length]=='='
  167.             && !strcmp(tmpres, key, true, key_length)  
  168.         ) {
  169.                 // We've got what needs to be removed!
  170.         } else {
  171.             DINI_StripNewLine(tmpres);
  172.             fwrite(fwhnd,tmpres);
  173.             fwrite(fwhnd,"\r\n");
  174.         }
  175.     }
  176.    
  177.     fclose(fohnd);
  178.     fclose(fwhnd);
  179.  
  180.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  181.     if (DINI_fcopytextfile(tmpres,filename)) {
  182.         return fremove(tmpres);
  183.     }
  184.     return false;
  185. }
  186.  
  187. stock dini_Get(filename[],key[]) {
  188.     new tmpres[DINI_MAX_STRING];
  189.    
  190.     new key_length = strlen(key);
  191.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
  192.    
  193.     new File:fohnd;
  194.     fohnd=fopen(filename,io_read);
  195.     if (!fohnd) return tmpres;
  196.    
  197.     while (fread(fohnd,tmpres)) {
  198.         if (
  199.             tmpres[key_length]=='='
  200.             && !strcmp(tmpres, key, true, key_length)  
  201.         ) {
  202.             /* We've got what we need */
  203.             DINI_StripNewLine(tmpres);
  204.             strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
  205.             fclose(fohnd);
  206.             return tmpres;
  207.         }
  208.     }
  209.     fclose(fohnd);
  210.     return tmpres;
  211. }
  212.  
  213.  
  214. stock dini_Isset(filename[],key[]) {
  215.     new key_length = strlen(key);
  216.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  217.    
  218.     new File:fohnd;
  219.     fohnd=fopen(filename,io_read);
  220.     if (!fohnd) return false;
  221.    
  222.     new tmpres[DINI_MAX_STRING];
  223.     while (fread(fohnd,tmpres)) {
  224.         if (
  225.                 tmpres[key_length]=='='
  226.             &&  !strcmp(tmpres, key, true, key_length) 
  227.         ) {
  228.             // We've got what we need
  229.             fclose(fohnd);
  230.             return true;
  231.         }
  232.     }
  233.     fclose(fohnd);
  234.     return false;
  235. }
  236.  
  237.  
  238.  
  239. stock DINI_StripNewLine(string[]) {
  240.     new len = strlen(string);
  241.     if (string[0]==0) return ;
  242.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  243.         string[len - 1] = 0;
  244.         if (string[0]==0) return ;
  245.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  246.     }
  247. }
  248.  
  249. stock DINI_fcopytextfile(oldname[],newname[]) {
  250.     new File:ohnd,File:nhnd;
  251.     if (!fexist(oldname)) return false;
  252.     ohnd=fopen(oldname,io_read);
  253.     if (!ohnd) return false;
  254.     nhnd=fopen(newname,io_write);
  255.     if (!nhnd) {
  256.         fclose(ohnd);
  257.         return false;
  258.     }
  259.     new tmpres[DINI_MAX_STRING];
  260.     while (fread(ohnd,tmpres)) {
  261.         DINI_StripNewLine(tmpres);
  262.         format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  263.         fwrite(nhnd,tmpres);
  264.     }
  265.     fclose(ohnd);
  266.     fclose(nhnd);
  267.     return true;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement