Advertisement
Guest User

infinite.inc

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