Advertisement
Guest User

infinite.inc

a guest
Aug 16th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.91 KB | None | 0 0
  1. /* infinite.inc
  2. **  Erweitert SA:MP um unendlich viele unendlich große Arrays.
  3. **  Copyright (c) tionsys.de
  4. */
  5.  
  6. #include <a_samp>
  7.  
  8. static DB:inf_database;
  9. static database_count = 0; // AUTOINCREMENT scheint nicht zu funktionieren ._.
  10.  
  11. public OnGameModeInit() {
  12.     // Datenbank bei jedem Start löschen und neu initialisieren
  13.     if(fexist("infinite_arrays.db"))
  14.         fremove("infinite_arrays.db");
  15.     inf_database = db_open("infinite_arrays.db");
  16.     db_query(inf_database, "CREATE TABLE array_vars (id integer, array_name TEXT, array_value TEXT);");
  17.  
  18.     return CallLocalFunction("inf_OnGameModeInit", "");
  19. }
  20.  
  21. public OnFilterScriptInit() {
  22.     return CallLocalFunction("inf_OnFilterScriptInit", "");
  23. }
  24.  
  25. forward ArrayPushString(array[], value[]);
  26. public ArrayPushString(array[], value[]) {
  27.     if(strlen(array) == 0 || strlen(array) >= 32) {
  28.         printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
  29.         return false;
  30.     }
  31.     if(strlen(value) == 0 || strlen(value) >= 128) {
  32.         printf("<=[ Infinite Array ]=>\nFehler - der Array-Wert darf nicht länger als 128 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", value, strlen(value));
  33.         return false;
  34.     }
  35.     // v 80 = "Grundgerüst", 258 = 2*128 (worst case: value ist 32 mal "'", wird also zu 32 mal "\'"), 64 = 2*array
  36.     new query[80 + 256 + 64];
  37.     format(query, sizeof(query), "INSERT INTO array_vars (id, array_name, array_value) VALUES (%d, '%s', '%s');", database_count++, inf_arr_escape(array), inf_arr_escape(value));
  38.     db_query(inf_database, query);
  39.     return true;
  40. }
  41.  
  42. forward ArrayPushInteger(array[], value);
  43. public ArrayPushInteger(array[], value) {
  44.     new str[128];
  45.     format(str, 128, "%d", value);
  46.     ArrayPushString(array, str);
  47. }
  48.  
  49. forward ArrayLength(array[]);
  50. public ArrayLength(array[]) {
  51.     if(strlen(array) == 0 || strlen(array) >= 32) {
  52.         printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
  53.         return false;
  54.     }
  55.     // v 60 = "Grundgerüst", 64 = 2*array
  56.     new query[60 + 64];
  57.     format(query, sizeof(query), "SELECT COUNT(id) FROM array_vars WHERE array_name = '%s';", inf_arr_escape(array));
  58.     new DBResult:res = db_query(inf_database, query);
  59.     db_get_field(res, 0, query, sizeof(query));
  60.     db_free_result(res);
  61.     return strval(query);
  62. }
  63.  
  64. forward ArrayPullString(array[], index, value[]);
  65. public ArrayPullString(array[], index, value[]) {
  66.     if(strlen(array) == 0 || strlen(array) >= 32) {
  67.         printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
  68.         return false;
  69.     }
  70.     new query[98 + 256 + 64];
  71.     format(query, sizeof(query), "SELECT array_value FROM array_vars WHERE array_name = '%s' ORDER BY id ASC LIMIT %d,1;", inf_arr_escape(array), index);
  72.     new DBResult:res = db_query(inf_database, query);
  73.  
  74.     if(db_num_rows(res) >= 1) {
  75.         db_get_field(res, 0, value, 128);
  76.     }
  77.  
  78.     db_free_result(res);
  79.     return db_num_rows(res) >= 1;
  80. }
  81.  
  82. forward ArrayPullInteger(array[], index);
  83. public ArrayPullInteger(array[], index) {
  84.     new str[128];
  85.     ArrayPullString(array, index, str);
  86.     return strval(str);
  87. }
  88.  
  89. forward ArrayDeleteIndex(array[], index);
  90. public ArrayDeleteIndex(array[], index) {
  91.     if(strlen(array) == 0 || strlen(array) >= 32) {
  92.         printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
  93.         return false;
  94.     }
  95.     // v 80 = "Grundgerüst", 258 = 2*128 (worst case: value ist 32 mal "'", wird also zu 32 mal "\'"), 64 = 2*array
  96.     new query[113 + 64];
  97.     format(query, sizeof(query), "DELETE FROM array_vars WHERE id IN (SELECT id FROM array_vars WHERE array_name = '%s' ORDER BY id ASC LIMIT %d,1)", inf_arr_escape(array), index);
  98.     db_query(inf_database, query);
  99.     return true;
  100. }
  101.  
  102. stock inf_arr_escape(str[]) {
  103.     new copy[128];
  104.     format(copy, 128, str);
  105.     str_replace("\\", "\\\\", copy);
  106.     str_replace("\"", "\\\"", copy);
  107.     return copy;
  108. }
  109.  
  110. // Source: http://pastebin.com/bZsxHdK0 (Google result. No author found ._.)
  111. stock str_replace(const needle[], const replace[], haystack[], bool:ignorecase = true, needlen = sizeof(needle), haylen = sizeof(haystack)) {
  112.     new count, index = strfind(haystack, needle, ignorecase);
  113.     while(index != -1) {
  114.         strdel(haystack, index, index + (needlen - 1));
  115.         strins(haystack, replace, index, haylen);
  116.         index = strfind(haystack, needle, ignorecase);
  117.         count++;
  118.     }
  119.     return count;
  120. }
  121.  
  122. forward inf_OnGameModeInit();
  123. #if defined _ALS_OnGameModeInit
  124.     #undef OnGameModeInit
  125. #else
  126.     #define _ALS_OnGameModeInit
  127. #endif
  128. #define OnGameModeInit inf_OnGameModeInit
  129.  
  130. forward inf_OnFilterScriptInit();
  131. #if defined _ALS_OnFilterScriptInit
  132.     #undef OnFilterScriptInit
  133. #else
  134.     #define _ALS_OnFilterScriptInit
  135. #endif
  136. #define OnFilterscriptInit inf_OnFilterScriptInit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement