/ Published in: C

Skript vydaný pod licenciou GPL. VyužÃva viac RAM, aby obmedzil prÃstup k HDD. Podla testov je tento skript rýchlejÅ¡Ã, než známy y_ini.
Expand |
Embed | Plain Text
#include <file> #define _MAX_PARALEL 2 /* pocet suborov otvorenych naraz */ #define _MAX_UDAJOV 5 /* pocet riadkov v subore */ #define _MAX_ZNAKOV 52 /* pocet znakov na riadok */ #define _MAX_BAJTOV 208 /* pocet bajtov na riadok */ #define RIADOK_DLZKA _MAX_ZNAKOV #define ZAPISOVANIE 0 #define CITANIE 1 #define VytvoritSubor(%0) fclose(fopen(%0, io_write)) #define SuborExistuje(%0) fexist(%0) static _menosuboru[_MAX_PARALEL][32]; static _subory[_MAX_PARALEL][_MAX_UDAJOV][_MAX_ZNAKOV]; stock Subor:OtvoritSubor(menosuboru[], mod) { for (new i; i < _MAX_PARALEL; i++) { if (!_menosuboru[i][0]) { if (fexist(menosuboru)) { memcpy(_menosuboru[i], menosuboru, 0, 128, 32); if (mod) { /* ak citanie, naplnit cache */ new File:handle = fopen(menosuboru, io_read); for (new j; j < _MAX_UDAJOV; j++) if (fread(handle, _subory[i][j])) strdel(_subory[i][j], strlen(_subory[i][j]) - 2, strlen(_subory[i][j])); else break; fclose(handle); } return Subor:i; } } } return Subor:-1; } /* v tejto funkcii premenna _subory sa sprava ako cache - namiesto HDD sa cita z RAM */ stock PrecitatString(Subor:handle, key[], string[]) { for (new i; i < _MAX_UDAJOV; i++) /* prehladat cache */ if (!strfind(_subory[_:handle][i], key)) { /* if (strfind == 0) => ak nasiel */ memcpy(string, _subory[_:handle][i][strlen(key) + 1], 0, _MAX_BAJTOV, _MAX_ZNAKOV); return; } /* cache miss */ new najdene = -1, File:subor = fopen(_menosuboru[_:handle], io_read); naplnenie: /* naplnenie "cache" */ for (new i; i < _MAX_UDAJOV; i++) { if (fread(subor, _subory[_:handle][i])) { strdel(_subory[_:handle][i], strlen(_subory[_:handle][i]) - 2, strlen(_subory[_:handle][i])); if (!strfind(_subory[_:handle][i], key)) najdene = i; } else break; } if (najdene != -1) { fclose(subor); memcpy(string, _subory[_:handle][najdene][strlen(key) + 1], 0, _MAX_BAJTOV, _MAX_ZNAKOV); return; } goto naplnenie; /* uistite sa, ze key existuje, inak v tomto cykle zamrzne server */ } stock ZatvoritSubor(Subor:handle, mod) { if (!mod) _UlozitSubor(handle); else for (new i; i < _MAX_UDAJOV; i++) _subory[_:handle][i][0] = 0; _menosuboru[_:handle][0] = 0; } static stock _UlozitSubor(Subor:handle) { new str[_MAX_ZNAKOV], pom, str2[24]; /* (MAX_ZNAKOV / 2) ~ strlen(key) */ new File:ID, File:tmp; for (new i; (i < _MAX_UDAJOV) && _subory[_:handle][i][0]; i++) strins(_subory[_:handle][i], "\r\n", strlen(_subory[_:handle][i])); ID = fopen(_menosuboru[_:handle], io_readwrite); tmp = ftemp(); while (fread(ID, str)) { pom = 0; memcpy(str2, str, 0, 96, 24); str2[strfind(str2, "=")] = 0; /* do docasneho retazca som skopiroval key */ for (new i; i < _MAX_UDAJOV; i++) { /* ci ide iba o aktualizaciu udajov */ if (!strfind(_subory[_:handle][i], str2)) { fwrite(tmp, _subory[_:handle][i]); _subory[_:handle][i][0] = 0; pom = 1; break; } } if (!pom) fwrite(tmp, str); } for (new i; i < _MAX_UDAJOV; i++) /* zapisem data, ktore este predtym neboli v subore */ if (_subory[_:handle][i][0]) { fwrite(tmp, _subory[_:handle][i]); _subory[_:handle][i][0] = 0; } fseek(ID, 0, seek_start); fseek(tmp, 0, seek_start); while (fread(tmp, str)) /* z bufferu to zapisem do skutocneho suboru */ fwrite(ID, str); fclose(tmp); fclose(ID); } stock ZapisatString(Subor:handle, key[], string[]) { /* premenna _subory sa sprava ako buffer pre HDD */ for (new i; i < _MAX_UDAJOV; i++) { if (!strfind(_subory[_:handle][i], key)) /* ak uz raz key zapisoval, nezapisem ho znovu */ return; if (!_subory[_:handle][i][0]) { /* zapisem novy udaj na prazdne miesto v bufferi */ memcpy(_subory[_:handle][i], key, 0, _MAX_BAJTOV, _MAX_ZNAKOV); strcat(_subory[_:handle][i], "="); strcat(_subory[_:handle][i], string); /* format bol pomalsi a cisto memcpy to blblo */ return; } } _UlozitSubor(handle); /* ak sa nenaslo volne miesto, tak ulozit na disk a vyprazdnit buffer */ return; }
You need to login to post a comment.