guhgames

bIni 2.0 By Bruno da Silva(Drakins)

Feb 25th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.55 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. stock dini_Get(filename[],key[]) {
  84.     new tmpres[255];
  85.  
  86.     new key_length = strlen(key);
  87.     if (key_length==0 || key_length+2>255) return tmpres;
  88.  
  89.     new File:fohnd;
  90.     fohnd=fopen(filename,io_read);
  91.     if (!fohnd) return tmpres;
  92.  
  93.     while (fread(fohnd,tmpres)) {
  94.         if (
  95.             tmpres[key_length]=='='
  96.             && !strcmp(tmpres, key, true, key_length)
  97.         ) {
  98.             /* We've got what we need */
  99.             DINI_StripNewLine(tmpres);
  100.             strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), 255);
  101.             fclose(fohnd);
  102.             return tmpres;
  103.         }
  104.     }
  105.     fclose(fohnd);
  106.     return tmpres;
  107. }
  108. stock DINI_StripNewLine(string[]) {
  109.     new len = strlen(string);
  110.     if (string[0]==0) return ;
  111.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  112.         string[len - 1] = 0;
  113.         if (string[0]==0) return ;
  114.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  115.     }
  116. }
  117.  
  118. CallBack INI_ReadString(keyGet[])
  119. {
  120.     new i = 0, valorGetNumerico = INI_HashString(keyGet);
  121.  
  122.  
  123.     for(i = 0;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  124.     {
  125.         if(strcmp(nomeKey[i], keyGet, false)) continue;
  126.  
  127.         return valorKey[i];
  128.     }
  129.     return stringTemporariaRetornar;
  130. }
  131.  
  132.  
  133.  
  134. ////////////////////////////////////////////////////////////////////////////////
  135.  
  136. CallBack INI_Open(arquivoLer[])
  137. {
  138.     if((strlen(nomeKey[0]) || strlen(valorKey[0]))) INI_Save(), INI_Close();
  139.     if(!fexist(arquivoLer)) INI_Create(arquivoLer);
  140.     new
  141.         bufferArquivo[MAX_CHARS],
  142.  
  143.         linhasArquivo = 0xFFFFFFFF,
  144.  
  145.         bufferIntArquivo = 0xFFFFFFFF,
  146.  
  147.         File:arquivoFopen = fopen(arquivoLer, io_read);
  148.  
  149.  
  150.     format(nomeArquivo, MAX_CHARS >>> 2, arquivoLer);
  151.  
  152.     while(fread(arquivoFopen, bufferArquivo))
  153.     {
  154.  
  155.         bufferIntArquivo = strfind(bufferArquivo, "=", true);
  156.  
  157.         if(bufferIntArquivo == -1 || bufferIntArquivo > MAX_CHARS) continue;
  158.  
  159.         bufferArquivo[strlen(bufferArquivo) - 2]  = '\0';
  160.  
  161.         format(valorKey[++linhasArquivo], MAX_CHARS >>> 1, bufferArquivo[bufferIntArquivo+1]);
  162.  
  163.         bufferArquivo[bufferIntArquivo] = EOS;
  164.  
  165.         format(nomeKey[linhasArquivo], MAX_CHARS >>> 1, bufferArquivo);
  166.  
  167.         valorNumerico[linhasArquivo] = INI_HashString(bufferArquivo);
  168.  
  169.  
  170.     }
  171.     totalLinhas = linhasArquivo+1;
  172.  
  173.     return fclose(arquivoFopen), true;
  174. }
  175.  
  176.  
  177. ////////////////////////////////////////////////////////////////////////////////
  178.  
  179. CallBack INI_HashString(buffer[])
  180. {
  181.     new totalContado = 0;
  182.     for(new i = strlen(buffer); i != -1; i--)
  183.     {
  184.         totalContado += buffer[i];
  185.     }
  186.     return  totalContado;
  187. }
  188.  
  189. ////////////////////////////////////////////////////////////////////////////////
  190.  
  191. CallBack INI_Save()
  192. {
  193.     new
  194.         i = 0,
  195.         line[MAX_CHARS]
  196.     ;
  197.  
  198.     new File:arquivoFopen = fopen(nomeArquivo, io_write);
  199.  
  200.     for( ; i != totalLinhas ; ++i)
  201.     {
  202.  
  203.         format(line, MAX_CHARS, "%s=%s\r\n", nomeKey[i], valorKey[i]);
  204.         fwrite(arquivoFopen, line);
  205.     }
  206.     return fclose(arquivoFopen);
  207. }
  208.  
  209. ////////////////////////////////////////////////////////////////////////////////
  210.  
  211. CallBack INI_Print()
  212. {
  213.     for(new i ; i != totalLinhas ; ++i)
  214.     {
  215.         printf("%s=%s",nomeKey[i],valorKey[i]);
  216.     }
  217.     return true;
  218. }
  219.  
  220. ////////////////////////////////////////////////////////////////////////////////
  221.  
  222. CallBack INI_Close()
  223. {
  224.     nomeArquivo[0] = EOS;
  225.  
  226.     for(new i; i != totalLinhas ; ++i)
  227.     {
  228.         nomeKey[i][0] = EOS;
  229.         valorKey[i][0] = EOS;
  230.         valorNumerico[i] = 0;
  231.     }
  232.     return totalLinhas = 0, true;
  233. }
  234.  
  235.  
  236.  
  237. ////////////////////////////////////////////////////////////////////////////////
  238.  
  239. CallBack INI_WriteString(keySet[], valueSet[])
  240. {
  241.  
  242.     new i, valorGetNumerico = INI_HashString(keySet);
  243.  
  244.  
  245.     for( ;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  246.     {
  247.         if(strcmp(nomeKey[i], keySet, false)) continue;
  248.  
  249.         return strcpy(valorKey[i], valueSet, MAX_CHARS);
  250.     }
  251.     valorNumerico[i]=valorGetNumerico ;
  252.  
  253.     return strcpy(nomeKey[i], keySet, MAX_CHARS), strcpy(valorKey[i], valueSet, MAX_CHARS), totalLinhas++, true;
  254. }
  255.  
  256.  
  257. ////////////////////////////////////////////////////////////////////////////////
  258.  
  259. CallBack INI_WriteInt(keySet[], valueSet)
  260. {
  261.     static destinoString[20];
  262.     format(destinoString, sizeof(destinoString), "%i", valueSet);
  263.     return INI_WriteString(keySet, destinoString);
  264. }
  265.  
  266. ////////////////////////////////////////////////////////////////////////////////
  267.  
  268. CallBack INI_WriteFloat(keySet[],Float:valueSet)
  269. {
  270.     static destinoString[20];
  271.     format(destinoString, sizeof(destinoString), "%f", valueSet);
  272.  
  273.     return INI_WriteString(keySet, destinoString);
  274. }
  275.  
  276.  
  277. /*
  278.  *            Dini 1.5.1
  279.  *       (c) Copyright 2006 by DracoBlue
  280.  *
  281.  * @author    : DracoBlue (http://dracoblue.com)
  282.  * @date      : 13th May 2006
  283.  * @update    : 3rd June 2007
  284.  * @require   : DUtils 1.8
  285.  *
  286.  * This file is provided as is (no warranties).
  287.  *
  288.  * Feel free to use it, a little message in
  289.  * about box is honouring thing, isn't it?
  290.  *
  291.  */
  292.  
  293. #define MAX_STRING 0xFF
  294.  
  295. stock  dini_Set(filename[],key[],value[])
  296. {
  297.     static
  298.         antigoFile[48]
  299.     ;
  300.  
  301.     if(!strcmp(filename, antigoFile, true)) INI_Save(), INI_Close();
  302.  
  303.     INI_Open(filename);
  304.  
  305.     format(antigoFile, 48, filename);
  306.  
  307.     return INI_WriteString(key, value);
  308. }
  309. stock  dini_IntSet(filename[],key[],value) {
  310.    new valuestring[MAX_STRING];
  311.    format(valuestring,sizeof(valuestring),"%d",value);
  312.    return dini_Set(filename,key,valuestring);
  313. }
  314.  
  315. stock  dini_Int(filename[],key[]) {
  316.    return strval(dini_Get(filename,key));
  317. }
  318.  
  319. stock  dini_FloatSet(filename[],key[],Float:value) {
  320.    new valuestring[MAX_STRING];
  321.    format(valuestring,sizeof(valuestring),"%f",value);
  322.    return dini_Set(filename,key,valuestring);
  323. }
  324.  
  325. stock  Float:dini_Float(filename[],key[]) {
  326.    return floatstr(dini_Get(filename,key));
  327. }
  328.  
  329. stock  dini_Bool(filename[],key[]) {
  330.    return strval(dini_Get(filename,key));
  331. }
  332.  
  333. stock  dini_BoolSet(filename[],key[],value) {
  334.    new valuestring[MAX_STRING];
  335.    format(valuestring,sizeof(valuestring),"%d",value);
  336.    return dini_Set(filename,key,valuestring);
  337. }
  338.  
  339. stock  dini_Unset(filename[],key[]) {
  340.     new File:fohnd, File:fwhnd;
  341.     new tmpres[MAX_STRING];
  342.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  343.     fohnd=fopen(filename,io_read);
  344.     if (!fohnd) return false;
  345.     fremove(tmpres);
  346.     fwhnd=fopen(tmpres,io_write);
  347.     //  if (!fwhnd) return false;
  348.     while (fread(fohnd,tmpres)) {
  349.         StripNewLine(tmpres);
  350.         if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  351.             /* We've got what needs to be removed! */
  352.         } else {
  353.             format(tmpres,sizeof(tmpres),"%s",tmpres);
  354.             fwrite(fwhnd,tmpres);
  355.             fwrite(fwhnd,"\r\n");
  356.         }
  357.     }
  358.  
  359.     fclose(fohnd);
  360.     fclose(fwhnd);
  361.  
  362.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  363.     if (fcopytextfile(tmpres,filename)) {
  364.         return fremove(tmpres);
  365.     }
  366.     return false;
  367. }
  368.  
  369.  
  370.  
  371. stock  dini_Isset(filename[],key[]) {
  372.     new File:fohnd;
  373.     new tmpres[MAX_STRING];
  374.     fohnd=fopen(filename,io_read);
  375.     if (!fohnd) return false;
  376.     while (fread(fohnd,tmpres)) {
  377.         StripNewLine(tmpres);
  378.         if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  379.             /* We've got what we need */
  380.             fclose(fohnd);
  381.             return true;
  382.         }
  383.     }
  384.     fclose(fohnd);
  385.     return false;
  386. }
Advertisement
Add Comment
Please, Sign In to add comment