Advertisement
Guest User

a_ini

a guest
Aug 11th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.03 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. //
  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. #define MAX_LINES       30
  48. #define MAX_CHARS               50
  49.  
  50. #define CallBack%0(%1)  stock %0(%1)
  51. #define Variable%0<%1>          static stock %0[%1]
  52.  
  53. #define INI_Isset(%0)           (INI_ReadInt(tagExists) == 0xFF)
  54. #define strcpy(%0,%1,%2)        strcat((%0[0] = EOS, %0), %1, %2 + 1)
  55.  
  56. #define INI_ReadInt(%0)     strval(INI_ReadString(%0))
  57. #define INI_ReadFloat(%0)   floatstr(INI_ReadString(%0))
  58.  
  59.  
  60. Variable nomeKey        <MAX_LINES>[MAX_CHARS >>> 1];
  61. Variable valorKey       <MAX_LINES>[MAX_CHARS >>> 1];
  62. Variable valorNumerico  <MAX_LINES>;
  63. Variable nomeArquivo    <MAX_CHARS / 2>;
  64. new totalLinhas = 0;
  65.  
  66.  
  67. //////////////////////////////////////////////////////////////////////////////
  68.  
  69. CallBack INI_Create(checarArquivo[])
  70. {
  71.     if(fexist(checarArquivo)) return false;
  72.  
  73.     new File:arquivoFopen = fopen(checarArquivo, io_append);
  74.  
  75.     return fclose(arquivoFopen), true;
  76. }
  77.  
  78. ////////////////////////////////////////////////////////////////////////////////
  79.  
  80. static stringTemporariaRetornar[MAX_CHARS / 2] = "255";
  81.  
  82. CallBack INI_ReadString(keyGet[])
  83. {
  84.     new i = 0, valorGetNumerico = INI_HashString(keyGet);
  85.  
  86.  
  87.     for(i = 0;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  88.     {
  89.         if(strcmp(nomeKey[i], keyGet, false)) continue;
  90.  
  91.         return valorKey[i];
  92.     }
  93.     return stringTemporariaRetornar;
  94. }
  95.  
  96.  
  97. ////////////////////////////////////////////////////////////////////////////////
  98.  
  99. CallBack INI_Open(arquivoLer[])
  100. {
  101.     if((strlen(nomeKey[0]) || strlen(valorKey[0]))) INI_Save(), INI_Close();
  102.  
  103.     new
  104.         bufferArquivo[MAX_CHARS],
  105.  
  106.         linhasArquivo = 0xFFFFFFFF,
  107.  
  108.         bufferIntArquivo = 0xFFFFFFFF,
  109.  
  110.         File:arquivoFopen = fopen(arquivoLer, io_read);
  111.  
  112.  
  113.     format(nomeArquivo, MAX_CHARS >>> 2, arquivoLer);
  114.  
  115.     while(fread(arquivoFopen, bufferArquivo))
  116.     {
  117.  
  118.         bufferIntArquivo = strfind(bufferArquivo, "=", true);
  119.  
  120.         if(bufferIntArquivo == -1 || bufferIntArquivo > MAX_CHARS) continue;
  121.  
  122.         bufferArquivo[strlen(bufferArquivo) - 2]  = '\0';
  123.  
  124.         format(valorKey[++linhasArquivo], MAX_CHARS >>> 1, bufferArquivo[bufferIntArquivo+1]);
  125.  
  126.         bufferArquivo[bufferIntArquivo] = EOS;
  127.  
  128.         format(nomeKey[linhasArquivo], MAX_CHARS >>> 1, bufferArquivo);
  129.  
  130.         valorNumerico[linhasArquivo] = INI_HashString(bufferArquivo);
  131.  
  132.  
  133.     }
  134.     totalLinhas = linhasArquivo+1;
  135.  
  136.     return fclose(arquivoFopen), true;
  137. }
  138.  
  139.  
  140. ////////////////////////////////////////////////////////////////////////////////
  141.  
  142. CallBack INI_HashString(buffer[])
  143. {
  144.     new totalContado = 0;
  145.     for(new i = strlen(buffer); i != -1; i--)
  146.     {
  147.         totalContado += buffer[i];
  148.     }
  149.     return  totalContado;
  150. }
  151.  
  152. ////////////////////////////////////////////////////////////////////////////////
  153.  
  154. CallBack INI_Save()
  155. {
  156.     new
  157.         i = 0,
  158.         line[MAX_CHARS]
  159.     ;
  160.  
  161.     new File:arquivoFopen = fopen(nomeArquivo, io_write);
  162.  
  163.     for( ; i != totalLinhas ; ++i)
  164.     {
  165.  
  166.         format(line, MAX_CHARS, "%s=%s\r\n", nomeKey[i], valorKey[i]);
  167.         fwrite(arquivoFopen, line);
  168.     }
  169.     return fclose(arquivoFopen);
  170. }
  171.  
  172. ////////////////////////////////////////////////////////////////////////////////
  173.  
  174. CallBack INI_Print()
  175. {
  176.     for(new i ; i != totalLinhas ; ++i)
  177.     {
  178.         printf("%s=%s",nomeKey[i],valorKey[i]);
  179.     }
  180.     return true;
  181. }
  182.  
  183. ////////////////////////////////////////////////////////////////////////////////
  184.  
  185. CallBack INI_Close()
  186. {
  187.     nomeArquivo[0] = EOS;
  188.  
  189.     for(new i; i != totalLinhas ; ++i)
  190.     {
  191.         nomeKey[i][0] = EOS;
  192.         valorKey[i][0] = EOS;
  193.         valorNumerico[i] = 0;
  194.     }
  195.     return totalLinhas = 0, true;
  196. }
  197.  
  198.  
  199.  
  200. ////////////////////////////////////////////////////////////////////////////////
  201.  
  202. CallBack INI_WriteString(keySet[], valueSet[])
  203. {
  204.  
  205.     new i, valorGetNumerico = INI_HashString(keySet);
  206.  
  207.  
  208.     for( ;  i != totalLinhas ; ++i) if(valorGetNumerico == valorNumerico[i])
  209.     {
  210.         if(strcmp(nomeKey[i], keySet, false)) continue;
  211.  
  212.         return strcpy(valorKey[i], valueSet, MAX_CHARS);
  213.     }
  214.     valorNumerico[i]=valorGetNumerico ;
  215.  
  216.     return strcpy(nomeKey[i], keySet, MAX_CHARS), strcpy(valorKey[i], valueSet, MAX_CHARS), totalLinhas++, true;
  217. }
  218.  
  219.  
  220. ////////////////////////////////////////////////////////////////////////////////
  221.  
  222. CallBack INI_WriteInt(keySet[], valueSet)
  223. {
  224.     static destinoString[20];
  225.     format(destinoString, sizeof(destinoString), "%i", valueSet);
  226.     return INI_WriteString(keySet, destinoString);
  227. }
  228.  
  229. ////////////////////////////////////////////////////////////////////////////////
  230.  
  231. CallBack INI_WriteFloat(keySet[],Float:valueSet)
  232. {
  233.     static destinoString[20];
  234.     format(destinoString, sizeof(destinoString), "%f", valueSet);
  235.  
  236.     return INI_WriteString(keySet, destinoString);
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement