Advertisement
DraKiNs

[INC] bIni 2.0

Sep 4th, 2011
2,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.90 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //        #### ########   ######     ######## ########    ###    ##     ##
  5. //         ##  ##     ## ##    ##       ##    ##         ## ##   ###   ###
  6. //         ##  ##     ## ##             ##    ##        ##   ##  #### ####
  7. //         ##  ########   ######        ##    ######   ##     ## ## ### ##
  8. //         ##  ##              ##       ##    ##       ######### ##     ##
  9. //         ##  ##        ##    ##       ##    ##       ##     ## ##     ##
  10. //        #### ##         ######        ##    ######## ##     ## ##     ##
  11. //
  12. //
  13. //            Criado por Bruno da Silva (iPs DraKiNs) and [S]trong
  14. //                           Special thanks to Double-O-Seven
  15. //                            NOW COMPACTIBLE DINI!!!
  16. //          Acesse meu blog sobre programação www.ips-team.blogspot.com
  17. //
  18. //          [iPs]TeaM soluções de programação em geral (agora com cursos)
  19. //
  20. //                  www.ips-team.6te.net/smforum (contate-nos)
  21. //
  22. //              [iPs]Ini Files (pós fini 5.0) Versão 2.0 (ultra rápido)
  23. //
  24. //
  25. /////////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. // Funções Atuais:
  29.  
  30. //   CallBack INI_WriteString("key", "valor")
  31. //   CallBack INI_WriteFloat("key", valor)
  32. //   CallBack INI_WriteInt("key", valor)
  33.  
  34. //   CallBack INI_ReadString("key")
  35. //   CallBack INI_ReadInt("key")
  36. //   CallBack INI_ReadFloat("key")
  37.  
  38. //   CallBack INI_Create()
  39. //   CallBack INI_Open("arquivo")
  40.  
  41. //   CallBack INI_Isset("tag")
  42. //   CallBack INI_Print()
  43.  
  44. //   CallBack INI_Close()
  45. //   CallBack INI_Save()
  46.  
  47. #pragma dynamic 45000
  48. #define MAX_LINES       999
  49. #define MAX_CHARS   256
  50.  
  51. #define CallBack%0(%1)  stock %0(%1)
  52. #define Variable%0<%1>          static stock %0[%1]
  53.  
  54. #define INI_Isset(%0)           (INI_ReadInt(tagExists) == 0xFF)
  55. #define strcpy(%0,%1,%2)        strcat((%0[0] = EOS, %0), %1, %2 + 1)
  56.  
  57. #define INI_ReadInt(%0)     strval(INI_ReadString(%0))
  58. #define INI_ReadFloat(%0)   floatstr(INI_ReadString(%0))
  59.  
  60.  
  61. Variable nomeKey        <MAX_LINES>[MAX_CHARS >>> 1];
  62. Variable valorKey       <MAX_LINES>[MAX_CHARS >>> 1];
  63. Variable valorNumerico  <MAX_LINES>;
  64. Variable nomeArquivo    <MAX_CHARS / 2>;
  65. new totalLinhas = 0;
  66.  
  67.  
  68. //////////////////////////////////////////////////////////////////////////////
  69.  
  70. CallBack INI_Create(checarArquivo[])
  71. {
  72.     if(fexist(checarArquivo)) return false;
  73.  
  74.     new File:arquivoFopen = fopen(checarArquivo, io_append);
  75.  
  76.     return fclose(arquivoFopen), true;
  77. }
  78.  
  79. ////////////////////////////////////////////////////////////////////////////////
  80.  
  81. static stringTemporariaRetornar[MAX_CHARS / 2] = "255";
  82.  
  83. CallBack INI_ReadString(keyGet[])
  84. {
  85.     new i = 0, valorGetNumerico = INI_HashString(keyGet);
  86.  
  87.  
  88.     for(i = 0;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  89.     {
  90.         if(strcmp(nomeKey[i], keyGet, false)) continue;
  91.  
  92.         return valorKey[i];
  93.     }  
  94.     return stringTemporariaRetornar;
  95. }
  96.  
  97.  
  98. ////////////////////////////////////////////////////////////////////////////////
  99.  
  100. CallBack INI_Open(arquivoLer[])
  101. {
  102.     if((strlen(nomeKey[0]) || strlen(valorKey[0]))) INI_Save(), INI_Close();
  103.  
  104.     new
  105.         bufferArquivo[MAX_CHARS],
  106.  
  107.         linhasArquivo = 0xFFFFFFFF,
  108.  
  109.         bufferIntArquivo = 0xFFFFFFFF,
  110.  
  111.         File:arquivoFopen = fopen(arquivoLer, io_read);
  112.  
  113.  
  114.     format(nomeArquivo, MAX_CHARS >>> 2, arquivoLer);
  115.  
  116.     while(fread(arquivoFopen, bufferArquivo))
  117.     {
  118.  
  119.         bufferIntArquivo = strfind(bufferArquivo, "=", true);
  120.  
  121.         if(bufferIntArquivo == -1 || bufferIntArquivo > MAX_CHARS) continue;
  122.        
  123.         bufferArquivo[strlen(bufferArquivo) - 2]  = '\0';
  124.  
  125.         format(valorKey[++linhasArquivo], MAX_CHARS >>> 1, bufferArquivo[bufferIntArquivo+1]);
  126.  
  127.         bufferArquivo[bufferIntArquivo] = EOS;
  128.  
  129.         format(nomeKey[linhasArquivo], MAX_CHARS >>> 1, bufferArquivo);
  130.  
  131.         valorNumerico[linhasArquivo] = INI_HashString(bufferArquivo);
  132.  
  133.  
  134.     }
  135.     totalLinhas = linhasArquivo+1;
  136.  
  137.     return fclose(arquivoFopen), true;
  138. }
  139.  
  140.  
  141. ////////////////////////////////////////////////////////////////////////////////
  142.  
  143. CallBack INI_HashString(buffer[])
  144. {
  145.     new totalContado = 0;
  146.     for(new i = strlen(buffer); i != -1; i--)
  147.     {
  148.         totalContado += buffer[i];
  149.     }
  150.     return  totalContado;
  151. }
  152.  
  153. ////////////////////////////////////////////////////////////////////////////////
  154.  
  155. CallBack INI_Save()
  156. {
  157.     new
  158.         i = 0,
  159.         line[MAX_CHARS]
  160.     ;
  161.  
  162.     new File:arquivoFopen = fopen(nomeArquivo, io_write);
  163.  
  164.     for( ; i != totalLinhas ; ++i)
  165.     {  
  166.        
  167.         format(line, MAX_CHARS, "%s=%s\r\n", nomeKey[i], valorKey[i]);
  168.         fwrite(arquivoFopen, line);
  169.     }
  170.     return fclose(arquivoFopen);
  171. }
  172.  
  173. ////////////////////////////////////////////////////////////////////////////////
  174.  
  175. CallBack INI_Print()
  176. {
  177.     for(new i ; i != totalLinhas ; ++i)
  178.     {
  179.         printf("%s=%s",nomeKey[i],valorKey[i]);
  180.     }
  181.     return true;
  182. }
  183.  
  184. ////////////////////////////////////////////////////////////////////////////////
  185.  
  186. CallBack INI_Close()
  187. {
  188.     nomeArquivo[0] = EOS;
  189.    
  190.     for(new i; i != totalLinhas ; ++i)
  191.     {
  192.         nomeKey[i][0] = EOS;
  193.         valorKey[i][0] = EOS;
  194.         valorNumerico[i] = 0;
  195.     }  
  196.     return totalLinhas = 0, true;
  197. }
  198.  
  199.  
  200.  
  201. ////////////////////////////////////////////////////////////////////////////////
  202.  
  203. CallBack INI_WriteString(keySet[], valueSet[])
  204. {
  205.    
  206.     new i, valorGetNumerico = INI_HashString(keySet);
  207.  
  208.  
  209.     for( ;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  210.     {
  211.         if(strcmp(nomeKey[i], keySet, false)) continue;
  212.  
  213.         return strcpy(valorKey[i], valueSet, MAX_CHARS);       
  214.     }
  215.     valorNumerico[i]=valorGetNumerico ;
  216.  
  217.     return strcpy(nomeKey[i], keySet, MAX_CHARS), strcpy(valorKey[i], valueSet, MAX_CHARS), totalLinhas++, true;
  218. }
  219.  
  220.  
  221. ////////////////////////////////////////////////////////////////////////////////
  222.  
  223. CallBack INI_WriteInt(keySet[], valueSet)
  224. {
  225.     static destinoString[20];
  226.     format(destinoString, sizeof(destinoString), "%i", valueSet);
  227.     return INI_WriteString(keySet, destinoString);
  228. }
  229.  
  230. ////////////////////////////////////////////////////////////////////////////////
  231.  
  232. CallBack INI_WriteFloat(keySet[],Float:valueSet)
  233. {
  234.     static destinoString[20];
  235.     format(destinoString, sizeof(destinoString), "%f", valueSet);
  236.    
  237.     return INI_WriteString(keySet, destinoString);
  238. }
  239.  
  240.  
  241. /*
  242.  *            Dini 1.5.1
  243.  *       (c) Copyright 2006 by DracoBlue
  244.  *
  245.  * @author    : DracoBlue (http://dracoblue.com)
  246.  * @date      : 13th May 2006
  247.  * @update    : 3rd June 2007
  248.  * @require   : DUtils 1.8
  249.  *
  250.  * This file is provided as is (no warranties).
  251.  *
  252.  * Feel free to use it, a little message in
  253.  * about box is honouring thing, isn't it?
  254.  *
  255.  */
  256.  
  257. #define MAX_STRING 0xFF
  258.  
  259. stock  dini_Set(filename[],key[],value[])
  260. {
  261.     static
  262.         antigoFile[48]
  263.     ;
  264.  
  265.     if(!strcmp(filename, antigoFile, true)) INI_Save(), INI_Close();
  266.  
  267.     INI_Open(filename);
  268.  
  269.     format(antigoFile, 48, filename);
  270.  
  271.     return INI_WriteString(key, value);
  272. }
  273.  
  274.  
  275. stock  dini_Get(filename[],key[])
  276. {
  277.     static
  278.         antigoFile[48]
  279.     ;
  280.  
  281.     if(!strcmp(filename, antigoFile, true)) INI_Save(), INI_Close();
  282.  
  283.     INI_Open(filename);
  284.  
  285.     format(antigoFile, 48, filename);
  286.  
  287.     return INI_ReadString(key);
  288. }
  289.  
  290.  
  291. stock  dini_IntSet(filename[],key[],value) {
  292.    new valuestring[MAX_STRING];
  293.    format(valuestring,sizeof(valuestring),"%d",value);
  294.    return dini_Set(filename,key,valuestring);
  295. }
  296.  
  297. stock  dini_Int(filename[],key[]) {
  298.    return strval(dini_Get(filename,key));
  299. }
  300.  
  301. stock  dini_FloatSet(filename[],key[],Float:value) {
  302.    new valuestring[MAX_STRING];
  303.    format(valuestring,sizeof(valuestring),"%f",value);
  304.    return dini_Set(filename,key,valuestring);
  305. }
  306.  
  307. stock  Float:dini_Float(filename[],key[]) {
  308.    return floatstr(dini_Get(filename,key));
  309. }
  310.  
  311. stock  dini_Bool(filename[],key[]) {
  312.    return strval(dini_Get(filename,key));
  313. }
  314.  
  315. stock  dini_BoolSet(filename[],key[],value) {
  316.    new valuestring[MAX_STRING];
  317.    format(valuestring,sizeof(valuestring),"%d",value);
  318.    return dini_Set(filename,key,valuestring);
  319. }
  320.  
  321. stock  dini_Unset(filename[],key[]) {
  322.     new File:fohnd, File:fwhnd;
  323.     new tmpres[MAX_STRING];
  324.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  325.     fohnd=fopen(filename,io_read);
  326.     if (!fohnd) return false;
  327.     fremove(tmpres);
  328.     fwhnd=fopen(tmpres,io_write);
  329.     //  if (!fwhnd) return false;
  330.     while (fread(fohnd,tmpres)) {
  331.         StripNewLine(tmpres);
  332.         if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  333.             /* We've got what needs to be removed! */
  334.         } else {
  335.             format(tmpres,sizeof(tmpres),"%s",tmpres);
  336.             fwrite(fwhnd,tmpres);
  337.             fwrite(fwhnd,"\r\n");
  338.         }
  339.     }
  340.  
  341.     fclose(fohnd);
  342.     fclose(fwhnd);
  343.  
  344.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  345.     if (fcopytextfile(tmpres,filename)) {
  346.         return fremove(tmpres);
  347.     }
  348.     return false;
  349. }
  350.  
  351.  
  352.  
  353. stock  dini_Isset(filename[],key[]) {
  354.     new File:fohnd;
  355.     new tmpres[MAX_STRING];
  356.     fohnd=fopen(filename,io_read);
  357.     if (!fohnd) return false;
  358.     while (fread(fohnd,tmpres)) {
  359.         StripNewLine(tmpres);
  360.         if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  361.             /* We've got what we need */
  362.             fclose(fohnd);
  363.             return true;
  364.         }
  365.     }
  366.     fclose(fohnd);
  367.     return false;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement