Advertisement
Guest User

QWER ini Systém aka qini

a guest
Jul 21st, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.32 KB | None | 0 0
  1. #pragma tabsize 0
  2.  
  3.  
  4. #define MAX_KEY 50
  5. #define MAX_LINE 100
  6. #define MAX_VALUE 128
  7. #define MAX_FILE_NAME 55
  8.  
  9. new File:qFile,
  10.     qFileName[MAX_FILE_NAME],
  11.     qKey[MAX_LINE][MAX_KEY],
  12.     qValue[MAX_LINE][MAX_VALUE],
  13.     qLines;
  14.  
  15.  
  16. forward Float:qini_FloatGet(key[]);
  17.  
  18. stock qini_Create(file[])
  19. {
  20.     if(fexist(file)) return false;
  21.     new File:cFile = fopen(file,io_write);
  22.     return fclose(cFile);
  23. }
  24.  
  25. stock qini_Remove(file[]){
  26. if(!fexist(file)) return false;
  27. fremove(file);
  28. for(new i;i < qLines;i++){
  29. qKey[i][0] = 0;
  30. qValue[i][0] = 0;
  31. }
  32. qLines = 0;
  33. return true;
  34. }
  35.  
  36. stock qini_Int(key[],value){
  37. new str[120];
  38. format(str,120,"%d",value);
  39. qini_Set(key,str);
  40. return true;
  41. }
  42.  
  43. stock qini_IntGet(key[]) return strval(qini_Get(key));
  44.  
  45.  
  46. stock qini_Float(key[],Float:value){
  47. new str[120];
  48. format(str,120,"%f",value);
  49. qini_Set(key,str);
  50. return true;
  51. }
  52.  
  53. Float:qini_FloatGet(key[]){
  54. return floatstr(qini_Get(key));
  55. }
  56.  
  57. stock qini_Bool(key[],value){
  58. new str[120];
  59. format(str,120,"%d",value);
  60. qini_Set(key,str);
  61. }
  62.  
  63. stock bool:qini_BoolGet(key[]){
  64. return bool:strval(qini_Get(key));
  65. }
  66.  
  67. stock qini_Open(file[]){
  68. new qstr[180];
  69. qFile = fopen(file,io_read);
  70. format(qFileName,MAX_FILE_NAME,file);
  71. new qline = -1;
  72. new end;
  73.     while(fread(qFile,qstr)){
  74.         qline++;
  75.         DelChar(qstr);
  76.         end = chrfind('=',qstr);
  77.         strmid(qKey[qline],qstr,0,end);
  78.         strmid(qValue[qline],qstr,end+1,strlen(qstr));
  79.         qLines++;
  80.     }
  81. //  printf("Pocet riadkov %d",qLines);
  82.     return fclose(qFile);
  83. }
  84.  
  85. stock qini_Set(key[],value[]){
  86. new qline = -1;
  87. new bool:findet = false;
  88.     while((qline < qLines) && (qline < MAX_LINE)){
  89.     qline++;
  90.         if(!qKey[qline][0]) continue;
  91.         if(strcmp(key,qKey[qline],false) == 0){
  92.             format(qValue[qline],MAX_VALUE,value);
  93. //          printf("%s | %s | %s | %d | %d",qValue[qline],key,qKey[qline],qline,qLines);
  94.             findet = true;
  95.             break;
  96.         }
  97.     }
  98.     if(!findet){
  99.     qLines++;
  100.     format(qKey[qLines],MAX_KEY,key);
  101.     format(qValue[qLines],MAX_VALUE,value);
  102. //  printf("!!!!!!!!!!!!!!!!!!!!!!!!!%s=%s!!!!!!!!!!!!!!!!!!!!!!!!!!",qKey[qLines],qValue[qLines]);
  103.     }
  104. }
  105.  
  106. stock qini_Get(key[]){
  107.     new qline = -1;
  108.     while((qline < qLines) && (qline < MAX_LINE)){
  109.     qline++;
  110.         if(!strcmp(key,qKey[qline],false)){
  111.         return qValue[qline];
  112.         }
  113.    }
  114.    return qValue[0];
  115. }
  116.  
  117. stock qini_Close(){
  118.     new qline = -1;
  119.     new qstr[180];
  120.     qFile = fopen(qFileName,io_write);
  121.     while((qline < qLines) && (qline < MAX_LINE)){
  122.     qline++;
  123.     if(!strlen(qKey[qline]) || !strlen(qValue[qline])) continue;
  124.     format(qstr,180,"%s=%s\r\n",qKey[qline],qValue[qline]);
  125.     fwrite(qFile, qstr);
  126.     }
  127.     for(new i;i < qLines;i++){
  128.     qKey[i][0] = 0;
  129.     qValue[i][0] = 0;
  130.     }
  131.     qLines = 0;
  132. fclose(qFile);
  133. }
  134.  
  135. stock DelChar(tstring[])
  136. {
  137.     new ln = strlen(tstring);
  138.     if(tstring[ln-2] == '\r')tstring[ln-2] = '\0';
  139.     if(tstring[ln-1] == '\n')tstring[ln-1] = '\0';
  140. }
  141.  
  142. stock hash(str2[]) { // by DracoBlue
  143.    new tmpdasdsa[249];
  144.    tmpdasdsa[0]=0;
  145.    valstr(tmpdasdsa,num_hash(str2));
  146.    return tmpdasdsa;
  147. }
  148.  
  149. stock num_hash(buf[]) // by DracoBlue
  150.  {
  151.     new length=strlen(buf);
  152.     new s1 = 1;
  153.     new s2 = 0;
  154.     new n;
  155.     for (n=0; n<length; n++) {
  156.        s1 = (s1 + buf[n]) % 65521;
  157.        s2 = (s2 + s1)     % 65521;
  158.     }
  159.     return (s2 << 16) + s1;
  160.  }
  161.  
  162. chrfind(n,h[],s=0) {
  163. new l=strlen(h);
  164. while(s<l)
  165. {
  166. if(h[s]==n)
  167. return s;s++;
  168. }
  169. return -1;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement