Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* infinite.inc
- ** Erweitert SA:MP um unendlich viele unendlich große Arrays.
- ** Copyright (c) tionsys.de
- */
- #include <a_samp>
- static DB:inf_database;
- static database_count = 0; // AUTOINCREMENT scheint nicht zu funktionieren ._.
- public OnGameModeInit() {
- // Datenbank bei jedem Start löschen und neu initialisieren
- if(fexist("infinite_arrays.db"))
- fremove("infinite_arrays.db");
- inf_database = db_open("infinite_arrays.db");
- db_query(inf_database, "CREATE TABLE array_vars (id integer, array_name TEXT, array_value TEXT);");
- return CallLocalFunction("inf_OnGameModeInit", "");
- }
- public OnFilterScriptInit() {
- return CallLocalFunction("inf_OnFilterScriptInit", "");
- }
- forward ArrayPushString(array[], value[]);
- public ArrayPushString(array[], value[]) {
- if(strlen(array) == 0 || strlen(array) >= 32) {
- printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
- return false;
- }
- if(strlen(value) == 0 || strlen(value) >= 128) {
- printf("<=[ Infinite Array ]=>\nFehler - der Array-Wert darf nicht länger als 128 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", value, strlen(value));
- return false;
- }
- // v 80 = "Grundgerüst", 258 = 2*128 (worst case: value ist 32 mal "'", wird also zu 32 mal "\'"), 64 = 2*array
- new query[80 + 256 + 64];
- 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));
- db_query(inf_database, query);
- return true;
- }
- forward ArrayPushInteger(array[], value);
- public ArrayPushInteger(array[], value) {
- new str[128];
- format(str, 128, "%d", value);
- ArrayPushString(array, str);
- }
- forward ArrayLength(array[]);
- public ArrayLength(array[]) {
- if(strlen(array) == 0 || strlen(array) >= 32) {
- printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
- return false;
- }
- // v 60 = "Grundgerüst", 64 = 2*array
- new query[60 + 64];
- format(query, sizeof(query), "SELECT COUNT(id) FROM array_vars WHERE array_name = '%s';", inf_arr_escape(array));
- new DBResult:res = db_query(inf_database, query);
- db_get_field(res, 0, query, sizeof(query));
- db_free_result(res);
- return strval(query);
- }
- forward ArrayPullString(array[], index, value[]);
- public ArrayPullString(array[], index, value[]) {
- if(strlen(array) == 0 || strlen(array) >= 32) {
- printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
- return false;
- }
- new query[98 + 256 + 64];
- 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);
- new DBResult:res = db_query(inf_database, query);
- if(db_num_rows(res) >= 1) {
- db_get_field(res, 0, value, 128);
- }
- db_free_result(res);
- return db_num_rows(res) >= 1;
- }
- forward ArrayPullInteger(array[], index);
- public ArrayPullInteger(array[], index) {
- new str[128];
- ArrayPullString(array, index, str);
- return strval(str);
- }
- forward ArrayDeleteIndex(array[], index);
- public ArrayDeleteIndex(array[], index) {
- if(strlen(array) == 0 || strlen(array) >= 32) {
- printf("<=[ Infinite Array ]=>\nFehler - der Array-Name darf nicht länger als 32 Zeichen sein.\nAktuell: \"%s\" (Zeichen: %d)", array, strlen(array));
- return false;
- }
- // v 80 = "Grundgerüst", 258 = 2*128 (worst case: value ist 32 mal "'", wird also zu 32 mal "\'"), 64 = 2*array
- new query[113 + 64];
- 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);
- db_query(inf_database, query);
- return true;
- }
- stock inf_arr_escape(str[]) {
- new copy[128];
- format(copy, 128, str);
- str_replace("\\", "\\\\", copy);
- str_replace("\"", "\\\"", copy);
- return copy;
- }
- // Source: http://pastebin.com/bZsxHdK0 (Google result. No author found ._.)
- stock str_replace(const needle[], const replace[], haystack[], bool:ignorecase = true, needlen = sizeof(needle), haylen = sizeof(haystack)) {
- new count, index = strfind(haystack, needle, ignorecase);
- while(index != -1) {
- strdel(haystack, index, index + (needlen - 1));
- strins(haystack, replace, index, haylen);
- index = strfind(haystack, needle, ignorecase);
- count++;
- }
- return count;
- }
- forward inf_OnGameModeInit();
- #if defined _ALS_OnGameModeInit
- #undef OnGameModeInit
- #else
- #define _ALS_OnGameModeInit
- #endif
- #define OnGameModeInit inf_OnGameModeInit
- forward inf_OnFilterScriptInit();
- #if defined _ALS_OnFilterScriptInit
- #undef OnFilterScriptInit
- #else
- #define _ALS_OnFilterScriptInit
- #endif
- #define OnFilterscriptInit inf_OnFilterScriptInit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement