Guest User

SA-MP Include Easy Saving (c) by Kaliber

a guest
Aug 14th, 2014
326
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* SA-MP Include Easy Saving
  2.  *
  3.  * (c) by Kaliber, 2014
  4.  *
  5.  */
  6. #if !defined _samp_included
  7.     #include <a_samp>
  8. #endif
  9. #if defined _EasySystem
  10.     #endinput
  11. #endif
  12. #define _EasySystem
  13. #define INVALID -1
  14. #if !defined PATH
  15.     #define PATH "%s.txt"
  16. #endif
  17. static const e_r[16] = "-1";
  18. stock Save(playerid,size,{Float,_}:...) {
  19.     if(numargs() != 3) return INVALID;
  20.     static tmp[64],File:f,data;
  21.     GetPlayerName(playerid,tmp,MAX_PLAYER_NAME),format(tmp,sizeof tmp,PATH,tmp);
  22.     f=fopen(tmp,io_write);
  23.     for(new i; i<size+1; i++) {
  24.         data = getarg(2,i);
  25.         format(tmp,sizeof tmp,((GetVarType(data) != 1) ? ("%d|") : ("%.04f|")),data),fwrite(f,tmp);
  26.     }
  27.     return fclose(f);
  28. }
  29. stock Edit(const path[],idx,value) {
  30.     if(!fexist(path) || idx <= 0) return INVALID;
  31.     new File:f = fopen(path,io_read),tmp[16],buffer[512],pos,pos2;
  32.     fread(f,buffer),fclose(f);
  33.     for(new i; i<idx; i++) {
  34.         if(!i) strmid(tmp,buffer,0,strfind(buffer,"|",false,pos),32),pos=strfind(buffer,"|",false,pos)+1;
  35.         else pos2=pos,strmid(tmp,buffer,pos,strfind(buffer,"|",false,pos+1),32),pos=strfind(buffer,"|",false,pos)+1;
  36.     }
  37.     if(!tmp[0]) return INVALID;
  38.     valstr(tmp,value);
  39.     if(idx==1) strdel(buffer,-1,pos);
  40.     else strdel(buffer,strfind(buffer,"|",false,pos2)-1,pos-1);
  41.     strins(buffer,tmp,pos2);
  42.     return f=fopen(path,io_write),fwrite(f,buffer),fclose(f);
  43. }
  44. stock Get(const path[],idx) {
  45.     if(!fexist(path) || idx <= 0) return e_r;
  46.     new File:f = fopen(path,io_read),tmp[16],buffer[512],pos;
  47.     fread(f,buffer),fclose(f);
  48.     for(new i; i<idx; i++) {
  49.         if(!i) strmid(tmp,buffer,0,strfind(buffer,"|",false,pos),32),pos=strfind(buffer,"|",false,pos)+1;
  50.         else strmid(tmp,buffer,pos,strfind(buffer,"|",false,pos+1),32),pos=strfind(buffer,"|",false,pos)+1;
  51.     }
  52.     return (!tmp[0]) ? e_r : tmp;
  53. }
  54. stock Load(playerid,size,{Float,_}:...) {
  55.     if(numargs() != 3) return INVALID;
  56.     static tmp[64],File:f,buffer[512],pos,tmp2[16];
  57.     GetPlayerName(playerid,tmp,MAX_PLAYER_NAME),format(tmp,sizeof tmp,PATH,tmp);
  58.     f=fopen(tmp,io_read);
  59.     if(!f) return INVALID;
  60.     fread(f,buffer);
  61.     for(new i; i<size+1; i++) {
  62.         if(!i) strmid(tmp,buffer,0,strfind(buffer,"|",false,pos),32),pos=strfind(buffer,"|",false,pos)+1;
  63.         else strmid(tmp,buffer,pos,strfind(buffer,"|",false,pos+1),32),pos=strfind(buffer,"|",false,pos)+1;
  64.         if(!tmp[0]) break;
  65.         format(tmp2,sizeof tmp2,"%.02f",floatstr(tmp));
  66.         setarg(2,i,(strfind(tmp2,".00") != -1) ? (strval(tmp)) : (_:floatstr(tmp)));
  67.     }
  68.     return fclose(f);
  69. }
  70. stock GetVarType({Float,_}:...) {
  71.     if(numargs() != 1) return INVALID;
  72.     static buffer[16],tmp[16];
  73.     format(tmp,sizeof tmp,"%.02f",getarg(0,0));
  74.     if(strcmp(tmp,"0.00") != 0 && strfind(tmp,".") != -1) return 1;
  75.     for(new i; i<sizeof buffer; i++) buffer[i] = getarg(0,i);
  76.     return (strlen(buffer) >= 2) ? 0 : 2;
  77. }
  78. #undef INVALID
RAW Paste Data