Advertisement
ipsBruno

(Pawn) Bini INI Files 4.1

Jun 24th, 2012
2,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.80 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //        #### ########   ######     ######## ########    ###    ##     ##
  5. //         ##  ##     ## ##    ##       ##    ##         ## ##   ###   ###
  6. //         ##  ##     ## ##             ##    ##        ##   ##  #### ####
  7. //         ##  ########   ######        ##    ######   ##     ## ## ### ##
  8. //         ##  ##              ##       ##    ##       ######### ##     ##
  9. //         ##  ##        ##    ##       ##    ##       ##     ## ##     ##
  10. //        #### ##         ######        ##    ######## ##     ## ##     ##
  11. //
  12. //
  13. //                    Criado por Bruno da Silva (iPs DraKiNs)
  14. //
  15. //          Acesse meu blog sobre programação www.brunodasilva.com
  16. //
  17. //              Seja membro da melhor equipe de programação
  18. //         http://ips-team.forumeiros.com/t2-informacao-inscricao-na-ips
  19.  
  20. //                    [iPs]TeaM soluções de programação em geral
  21. //
  22. //
  23. //              [Bruno]Ini Files (Bini) Versão 4.0 (ultra rápido)
  24. //
  25. //
  26. /////////////////////////////////////////////////////////////////////////////////
  27.  
  28. // Funções Atuais:
  29.  
  30. //   CallBack INI_WriteString("arquivo", "key", "valor")
  31. //   CallBack INI_WriteFloat("arquivo"",key", valor)
  32. //   CallBack INI_WriteInt("arquivo","key", valor)
  33. //   CallBack INI_WriteBool("arquivo","key", bool:valor)
  34.  
  35. //   CallBack INI_ReadString("arquivo", "key")
  36. //   CallBack INI_ReadInt("arquivo", "key")
  37. //   CallBack INI_ReadFloat("arquivo","key")
  38. //   CallBack INI_ReadBool("arquivo","key")
  39.  
  40. //   CallBack INI_Create("arquivo")
  41. //   CallBack INI_Delete("arquivo")
  42. //   CallBack INI_Open("arquivo")
  43.  
  44. //   CallBack INI_Isset("arquivo","tag")
  45. //   CallBack INI_Print()
  46.  
  47.  
  48. // [ Configurações Gerais ]
  49.  
  50. #define max_lines_file          (999)
  51. #define max_chars_file          (256)
  52. #define max_dep_memory          (125)
  53.  
  54. // [ Declarações ]
  55.  
  56. stock bufferStrings[max_lines_file][max_dep_memory];
  57. stock nomeKey[max_lines_file][max_dep_memory];
  58. stock nomeArquivo[max_dep_memory];
  59. stock sizeMemory   = 0;
  60. stock destinoString[20];
  61. stock bool:vaWrite;
  62. stock totalLinhas = 0;
  63.  
  64.  
  65. #define stringGet(%0)               bufferStrings[getproperty(0,%0)]
  66. #define CallBack%0(%1)              stock %0(%1)
  67. #define INI_Isset(%2,%0)            !!(INI_Open(%2), existproperty(0, %0))
  68. #define INI_Create(%1)              fclose(fopen(%1, io_write))
  69. #define INI_WriteBool(%2,%0,%1)     INI_WriteString(%0, %1 ? ("true") : ("false"))
  70. #define INI_ReadInt(%2,%0)          strval(INI_ReadString(%2,%0))
  71. #define INI_ReadFloat(%2,%0)        floatstr(INI_ReadString(%2,%0))
  72. #define INI_Delete(%0)              fremove(%0)
  73. #define INI_ReadBool(%2,%0)         bool:strcmp((format(destinoString, sizeof(destinoString), "%i", %0),destinoString), false)
  74. #define INI_WriteInt(%2,%0,%1)      INI_WriteString(%2,%0,(format(destinoString, sizeof(destinoString), "%i", %1),destinoString))
  75. #define INI_WriteFloat(%2,%0,%1)    INI_WriteString(%2,%0,(format(destinoString, sizeof(destinoString), "%f", %1),destinoString))
  76. #define INI_ReadString(%0,%1)       (INI_Open(%0), stringGet(%1))
  77. #define prev_strcmp(%0,%1)          INI_HashString(%0) == INI_HashString(%1)
  78.  
  79. CallBack INI_Exit()
  80. {
  81.     if(strlen(nomeArquivo)) {
  82.         if(vaWrite) INI_Save();
  83.         INI_Close();
  84.     }
  85.     return true;
  86. }
  87.  
  88. CallBack INI_Unset(nome[]) {
  89.     for(new i ; i < totalLinhas ; ++i) {
  90.         if(prev_strcmp(nome, nomeKey[i])) {
  91.             if(!strcmp(nomeKey[i], nome, false)) {
  92.                 nomeKey[i][0] = EOS;
  93.             }
  94.         }
  95.     }
  96.     return true;
  97. }
  98.  
  99.  
  100. CallBack INI_Open(arquivoLer[])
  101. {
  102.     if(prev_strcmp(arquivoLer, nomeArquivo)) {
  103.         if(!strcmp(arquivoLer, nomeArquivo, false)) {
  104.             return true;
  105.         }
  106.     }
  107.     else {
  108.         INI_Exit();
  109.     }
  110.  
  111.     new
  112.         bufferArquivo[max_chars_file],
  113.         linhasArquivo = 0xFFFFFFFF,
  114.         bufferIntArquivo = 0xFFFFFFFF,
  115.         File:arquivoFopen,
  116.         bufferStringValor[max_dep_memory];
  117.  
  118.     strcat((nomeArquivo[0] = '\0', nomeArquivo),arquivoLer,sizeof nomeArquivo);
  119.  
  120.     if((arquivoFopen = fopen(arquivoLer, io_read))) {
  121.  
  122.         while(fread(arquivoFopen, bufferArquivo)) {
  123.             if(strlen(bufferArquivo) > 1) {
  124.                 if((bufferIntArquivo = strfind(bufferArquivo, "=")) == -1 || bufferIntArquivo > max_chars_file) continue;
  125.  
  126.                 bufferArquivo[strlen(bufferArquivo) - 2]  = EOS;
  127.                 strcat((bufferStringValor[0] = '\0', bufferStringValor), bufferArquivo[bufferIntArquivo+1], max_dep_memory);
  128.  
  129.                 ++linhasArquivo;
  130.                 bufferArquivo[bufferIntArquivo] = EOS;
  131.  
  132.                 strcat((nomeKey[linhasArquivo][0] = '\0', nomeKey[linhasArquivo]), bufferArquivo, max_dep_memory);
  133.                 stringSet(bufferArquivo, bufferStringValor);
  134.             }
  135.         }
  136.         totalLinhas = linhasArquivo+1;
  137.         fclose(arquivoFopen);
  138.     }
  139.     return true;
  140. }
  141.  
  142.  
  143. new
  144.     idxMatriz
  145. ;
  146.  
  147. CallBack stringSet(string[], set[])
  148. {
  149.     if(existproperty(0, string)) {
  150.         idxMatriz = getproperty(0,string);
  151.         setproperty(0, .name=string, .value=idxMatriz);
  152.         strcat((bufferStrings[idxMatriz][0] = '\0', bufferStrings[idxMatriz]), set, max_dep_memory);
  153.     }
  154.     else {
  155.         sizeMemory++;
  156.         setproperty(0, .name=string, .value=sizeMemory);
  157.         strcat((bufferStrings[sizeMemory][0] = '\0', bufferStrings[sizeMemory]), set, max_dep_memory);
  158.     }
  159.     return true;
  160. }
  161.  
  162.  
  163. new
  164.     totalContadoINIHASH = 0
  165. ;
  166.  
  167. CallBack INI_HashString(buffer[])
  168. {
  169.     totalContadoINIHASH = 0;
  170.     for(new i = strlen(buffer); i != -1; i--) {
  171.         totalContadoINIHASH += buffer[i];
  172.     }
  173.     return totalContadoINIHASH;
  174. }
  175.  
  176.  
  177. new lineSaveINI[max_chars_file];
  178. new File:arquivoIniFopen;
  179.  
  180.  
  181. CallBack INI_Save()
  182. {
  183.     arquivoIniFopen = fopen(nomeArquivo, io_write);
  184.  
  185.     for(new i = 0; i < totalLinhas; ++i) {
  186.         if(nomeKey[i][0]) {
  187.                 format(lineSaveINI, max_chars_file, "%s=%s\r\n", nomeKey[i], stringGet(nomeKey[i]));
  188.                 fwrite(arquivoIniFopen, lineSaveINI);
  189.         }
  190.     }
  191.     return fclose(arquivoIniFopen);
  192. }
  193.  
  194.  
  195. CallBack INI_Print()
  196. {
  197.     for(new i ; i < totalLinhas ; ++i) {
  198.         printf("%s=%s" ,nomeKey[i], stringGet(nomeKey[i]));
  199.     }
  200.     return true;
  201. }
  202.  
  203.  
  204. CallBack INI_Close()
  205. {
  206.     vaWrite = false;
  207.     for(new i = 0; i <= totalLinhas; i++) {
  208.         nomeKey[i][0] = EOS;
  209.         bufferStrings[i][0] = EOS;
  210.         deleteproperty(0, "", i);
  211.     }
  212.     return sizeMemory = 0, totalLinhas = 0, true;
  213. }
  214.  
  215.  
  216. CallBack INI_WriteString(file[], keySet[], valueSet[])
  217. {
  218.     INI_Open(file);
  219.  
  220.     if(!existproperty(0, keySet)) {
  221.         strcat(nomeKey[totalLinhas], keySet);
  222.         totalLinhas++;
  223.     }
  224.  
  225.     vaWrite = true;
  226.     return stringSet(keySet, valueSet);
  227. }
  228.  
  229.  
  230. ////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement