Advertisement
DraKiNs

[INC] bIni Files

Sep 3rd, 2011
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.78 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //        #### ########   ######     ######## ########    ###    ##     ##
  5. //         ##  ##     ## ##    ##       ##    ##         ## ##   ###   ###
  6. //         ##  ##     ## ##             ##    ##        ##   ##  #### ####
  7. //         ##  ########   ######        ##    ######   ##     ## ## ### ##
  8. //         ##  ##              ##       ##    ##       ######### ##     ##
  9. //         ##  ##        ##    ##       ##    ##       ##     ## ##     ##
  10. //        #### ##         ######        ##    ######## ##     ## ##     ##
  11. //
  12. //
  13. //            Criado por Bruno da Silva (antigo nick iPs DraKiNs)
  14. //
  15. //          Acesse meu blog sobre programação www.ips-team.blogspot.com
  16. //
  17. //          [iPs]TeaM soluções de programação em geral (agora com cursos)
  18. //
  19. //                  www.ips-team.6te.net/smforum (contate-nos)
  20. //
  21. //              bIni Files (pós fini 5.0) Versão 1.0 (ultra rápido)
  22. //
  23. // [18:21:47] (Slick) executado em 385 ms
  24. // [18:21:47] (Bruno) executado em 117 ms (min. 3x fast)
  25. // [18:21:48] (Y_less) executado em 632 ms
  26. // [18:21:53] (Dini) executado em 6632 ms
  27. // [18:21:55] (Cini) executado em 2632 ms
  28. // [18:21:55] (Double) executado em 368 ms
  29. // [18:21:56] (dFile) executado em 398 ms
  30. // [18:21:56] (Fini) executado em 364 ms
  31. //      Executes by 100000 reads/writes
  32. //
  33. /////////////////////////////////////////////////////////////////////////////////
  34.  
  35.  
  36. // Funções Atuais:
  37.  
  38. //   CallBack INI_WriteString("key", "valor")
  39. //   CallBack INI_WriteFloat("key", valor)
  40. //   CallBack INI_WriteInt("key", valor)
  41.  
  42. //   CallBack INI_ReadString("key")
  43. //   CallBack INI_ReadInt("key")
  44. //   CallBack INI_ReadFloat("key")
  45.  
  46. //   CallBack INI_Create()
  47. //   CallBack INI_Open("arquivo")
  48.  
  49. //   CallBack INI_Isset("tag")
  50. //   CallBack INI_Print()
  51.  
  52. //   CallBack INI_Close()
  53. //   CallBack INI_Save()
  54.  
  55. #pragma dynamic 45000
  56. #define MAX_LINES       999
  57. #define MAX_CHARS   256
  58.  
  59. #define CallBack%0(%1)  stock %0(%1)
  60. #define Variable%0<%1>          static stock %0[%1]
  61.  
  62. #define INI_Isset(%0)           (INI_ReadInt(tagExists) == 0xFF)
  63. #define strcpy(%0,%1,%2)        strcat((%0[0] = EOS, %0), %1, %2 + 1)
  64.  
  65. #define INI_ReadInt(%0)     strval(INI_ReadString(%0))
  66. #define INI_ReadFloat(%0)   floatstr((INI_ReadString(%0))
  67.  
  68.  
  69. Variable nomeKey        <MAX_LINES>[MAX_CHARS >>> 1];
  70. Variable valorKey       <MAX_LINES>[MAX_CHARS >>> 1];
  71. Variable nomeArquivo    <MAX_CHARS / 2>;
  72. new totalLinhas = 0;
  73.  
  74.  
  75. //////////////////////////////////////////////////////////////////////////////
  76.  
  77. CallBack INI_Create(checarArquivo[])
  78. {
  79.     if(fexist(checarArquivo)) return false;
  80.  
  81.     new File:arquivoFopen = fopen(checarArquivo, io_write);
  82.  
  83.     return fclose(arquivoFopen), true;
  84. }
  85.  
  86. ////////////////////////////////////////////////////////////////////////////////
  87.  
  88. static stringTemporariaRetornar[MAX_CHARS / 2] = "255";
  89.  
  90. CallBack INI_ReadString(keyGet[])
  91. {
  92.     new i = -1;
  93.  
  94.     while((valorKey[++i][0]) && (i != MAX_LINES)) if(!strcmp(nomeKey[i], keyGet, false))
  95.     {      
  96.         return valorKey[i];
  97.     }  
  98.     return stringTemporariaRetornar;
  99. }
  100.  
  101.  
  102. ////////////////////////////////////////////////////////////////////////////////
  103.  
  104. CallBack INI_Open(arquivoLer[])
  105. {
  106.     if((strlen(nomeKey[0]) || strlen(valorKey[0]))) INI_Save(), INI_Close();
  107.  
  108.     new
  109.         bufferArquivo[MAX_CHARS],
  110.  
  111.         linhasArquivo = 0xFFFFFFFF,
  112.  
  113.         bufferIntArquivo = 0xFFFFFFFF,
  114.  
  115.         File:arquivoFopen = fopen(arquivoLer, io_read);
  116.  
  117.  
  118.     format(nomeArquivo, MAX_CHARS >>> 2, arquivoLer);
  119.  
  120.     while(fread(arquivoFopen, bufferArquivo))
  121.     {
  122.  
  123.         bufferIntArquivo = strfind(bufferArquivo, "=", true);
  124.  
  125.         if(bufferIntArquivo == -1 || bufferIntArquivo > MAX_CHARS) continue;
  126.        
  127.         bufferArquivo[strlen(bufferArquivo) - 2]  = '\0';
  128.  
  129.         format(valorKey[++linhasArquivo], MAX_CHARS >>> 1, bufferArquivo[bufferIntArquivo+1]);
  130.  
  131.         bufferArquivo[bufferIntArquivo] = EOS;
  132.  
  133.         format(nomeKey[linhasArquivo], MAX_CHARS >>> 1, bufferArquivo);
  134.  
  135.     }
  136.     totalLinhas = linhasArquivo+1;
  137.  
  138.     return fclose(arquivoFopen), true;
  139. }
  140.  
  141.  
  142. ////////////////////////////////////////////////////////////////////////////////
  143.  
  144.  
  145. CallBack INI_Save()
  146. {
  147.     new
  148.         i = 0,
  149.         line[MAX_CHARS]
  150.     ;
  151.  
  152.     new File:arquivoFopen = fopen(nomeArquivo, io_write);
  153.  
  154.     for( ; i != totalLinhas ; ++i)
  155.     {  
  156.        
  157.         format(line, MAX_CHARS, "%s=%s\r\n", nomeKey[i], valorKey[i]);
  158.         fwrite(arquivoFopen, line);
  159.     }
  160.     return fclose(arquivoFopen);
  161. }
  162.  
  163. ////////////////////////////////////////////////////////////////////////////////
  164.  
  165. CallBack INI_Print()
  166. {
  167.     for(new i ; i != totalLinhas ; ++i)
  168.     {
  169.         printf("%s=%s",nomeKey[i],valorKey[i]);
  170.     }
  171.     return true;
  172. }
  173.  
  174. ////////////////////////////////////////////////////////////////////////////////
  175.  
  176. CallBack INI_Close()
  177. {
  178.     nomeArquivo[0] = EOS;
  179.    
  180.     for(new i; i != totalLinhas ; ++i)
  181.     {
  182.         nomeKey[i][0] = EOS;
  183.         valorKey[i][0] = EOS;
  184.     }
  185.    
  186.     return totalLinhas = 0, true;
  187. }
  188.  
  189.  
  190.  
  191. ////////////////////////////////////////////////////////////////////////////////
  192.  
  193. CallBack INI_WriteString(keySet[], valueSet[])
  194. {
  195.     new i;
  196.     for(i = 0;  i != totalLinhas ; ++i) if(!strcmp(nomeKey[i], keySet, false))
  197.     {
  198.         return strcpy(valorKey[i], valueSet, MAX_CHARS);       
  199.     }
  200.     return strcpy(nomeKey[i], keySet, MAX_CHARS), strcpy(valorKey[i], valueSet, MAX_CHARS), totalLinhas++, true;
  201. }
  202.  
  203. ////////////////////////////////////////////////////////////////////////////////
  204.  
  205. CallBack INI_WriteInt(keySet[], valueSet)
  206. {
  207.     static destinoString[20];
  208.     format(destinoString, sizeof(destinoString), "%i", valueSet);
  209.     return INI_WriteString(keySet, destinoString);
  210. }
  211.  
  212. ////////////////////////////////////////////////////////////////////////////////
  213.  
  214. CallBack INI_WriteFloat(keySet[], Float:valueSet)
  215. {
  216.     static destinoString[20];
  217.     format(destinoString, sizeof(destinoString), "%f", valueSet);
  218.    
  219.     return INI_WriteString(keySet, destinoString);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement