Advertisement
Guest User

Arrays (c) by Kaliber

a guest
Oct 8th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. /* SA-MP Include Arrays
  2.  *
  3.  * (c) by Kaliber, 2014
  4.  *
  5.  */
  6. #if !defined _samp_included
  7.     #include <a_samp>
  8. #endif
  9. #if defined _samp_arrays
  10.     #endinput
  11. #endif
  12. #define _samp_arrays
  13. #define _m_Data 0xF
  14. #define _s_Data 0x40
  15. #define _r_Data 0x80
  16. #define _b_Data 0x100
  17. #define INVALID -1
  18. #define GetArray(%0)<%1> __@(%0,%1)
  19. #define GetArrayInt(%0)<%1> __@Int(%0,%1)
  20. #define GetArrayFloat(%0)<%1> __@Float(%0,%1)
  21. #define SaveArray(%0,%1) __@Save(%0,%1)
  22. #define SaveArrayEx(%0+%1+%2,%3) __@Save(__@com(%0,%1,%2),%3)
  23. #define LoadArray(%0) __@Load(%0)
  24. #define Push(%0,%1) %0[0]='\0',strcat(%0,%1,0x80)
  25. #define CountIndexArray(%0) __@count(%0)
  26. #define SortIndexArray(%0) __@Sort(%0)
  27. #define GetIndexArray(%0)<%1> __@index(%0,%1)
  28. #define return->Array(%0); {new __tmp[_b_Data]; return format(__tmp,_b_Data,%0),__tmp;}
  29. #define LoadArrayEx(%0+%1+%2) __@Load(__@com(%0,%1,%2))
  30. stock static const null[_s_Data] = "<null>",rNull[_r_Data] = "<null>",bNull[_b_Data] = "<null>";
  31. stock __@com(const s1[], const s2[], const s3[]) {
  32.     new tmp[_s_Data];
  33.     return strcat(tmp,s1,_s_Data),strcat(tmp,s2,_s_Data),strcat(tmp,s3,_s_Data),tmp;
  34. }
  35. stock __@count(const arr[]) {
  36.     if(!arr[0]) return 0;
  37.     if(arr[0] != 'i' || arr[1] != 'd' || arr[2] != 'x' || arr[3] != ':') return 0;
  38.     for(new i, pos; i<_s_Data; i++) {
  39.         pos = strfind(arr,",",false,pos+1);
  40.         if(pos == -1) return (i+1);
  41.     }
  42.     return 0;
  43. }
  44. stock __@index(const arr[], idx) {
  45.     if(!arr[0] || idx < 0) return INVALID;
  46.     if(arr[0] != 'i' || arr[1] != 'd' || arr[2] != 'x' || arr[3] != ':') return INVALID;
  47.     new _tmp[_m_Data];
  48.     for(new i, pos, oldpos = strfind(arr,":")+1; i<_s_Data; i++) {
  49.         pos = strfind(arr,",",false,oldpos);
  50.     if(i == idx) return strmid(_tmp,arr,oldpos,(pos!=-1)?pos:strlen(arr),_m_Data),strval(_tmp);
  51.     oldpos = pos+1;
  52.     if(pos == -1 && i < idx) return INVALID;
  53.     }
  54.     return INVALID;
  55. }
  56. stock __@Sort(const arr[]) {
  57.     if(!arr[0]) return rNull;
  58.     if(arr[0] != 'i' || arr[1] != 'd' || arr[2] != 'x' || arr[3] != ':') return rNull;
  59.     new array[_s_Data],l=CountIndexArray(arr);
  60.     for(new i; i<l; i++) array[i] = GetIndexArray(arr)<i>;
  61.     __quickSort(array, 0, l - 1);
  62.     new tmp[_r_Data];
  63.     format(tmp,_r_Data,"idx: %d",array[0]);
  64.     for(new x=1; x<l; x++) format(tmp,_r_Data,"%s, %d",tmp,array[x]);
  65.     return tmp;
  66. }
  67. stock __quickSort(array[], left, right) //(c) by Ryder
  68. {
  69.     new tempLeft = left,tempRight = right,pivot = array[(left + right) / 2],tempVar;
  70.     while(tempLeft <= tempRight) {
  71.         while(array[tempLeft] < pivot) tempLeft++;
  72.         while(array[tempRight] > pivot) tempRight--;
  73.         if(tempLeft <= tempRight) {
  74.             tempVar = array[tempLeft], array[tempLeft] = array[tempRight], array[tempRight] = tempVar;
  75.             tempLeft++, tempRight--;
  76.         }
  77.     }
  78.     if(left < tempRight) __quickSort(array, left, tempRight);
  79.     if(tempLeft < right) __quickSort(array, tempLeft, right);
  80. }
  81. stock __@Save(const path[], const data[]) {
  82.     new File:_f=fopen(path,io_write);
  83.     return fwrite(_f,data),fclose(_f);
  84. }
  85. stock __@Load(const path[]) {
  86.     if(!fexist(path)) return bNull;
  87.     new File:_f=fopen(path,io_read),_buf[_b_Data];
  88.     return fread(_f,_buf),fclose(_f),_buf;
  89. }
  90. stock __@(const string[], const sub[]) {
  91.     new tmp[_s_Data],l=strlen(sub)+1;
  92.     if(l > (_s_Data-1)) return null;
  93.     strcat(tmp,sub,sizeof tmp),strcat(tmp,":",sizeof tmp);
  94.     new pos = strfind(string,sub),pos1 = strfind(string,";",false,pos);
  95.     if(pos == -1 || pos1 == -1) return null;
  96.     return strmid(tmp,string,pos+(l+1),pos1,sizeof tmp),tmp;
  97. }
  98. stock Float:__@Float(const string[], const sub[]) {
  99.     new tmp[_s_Data],l=strlen(sub)+1;
  100.     if(l > (_s_Data-1)) return 0.0;
  101.     strcat(tmp,sub,sizeof tmp),strcat(tmp,":",sizeof tmp);
  102.     new pos = strfind(string,sub),pos1 = strfind(string,";",false,pos);
  103.     if(pos == -1 || pos1 == -1) return 0.0;
  104.     return strmid(tmp,string,pos+(l+1),pos1,sizeof tmp),floatstr(tmp);
  105. }
  106. stock __@Int(const string[], const sub[]) {
  107.     new tmp[_s_Data],l=strlen(sub)+1;
  108.     if(l > (_s_Data-1)) return 0;
  109.     strcat(tmp,sub,sizeof tmp),strcat(tmp,":",sizeof tmp);
  110.     new pos = strfind(string,sub),pos1 = strfind(string,";",false,pos);
  111.     if(pos == -1 || pos1 == -1) return 0;
  112.     return strmid(tmp,string,pos+(l+1),pos1,sizeof tmp),strval(tmp);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement