#define MAP_H
// Databases (hash tables)
-const float DB_BUCKETS = 8192;
-void db_save(float db, string pFilename)
+const int DB_BUCKETS = 8192;
+void db_save(int db, string filename)
{
- int fh = fopen(pFilename, FILE_WRITE);
+ int fh = fopen(filename, FILE_WRITE);
if (fh < 0)
{
- LOG_INFO(strcat("^1Can't write DB to ", pFilename));
+ LOG_WARNINGF("^1Can't write DB to %s\n", filename);
return;
}
- int n = buf_getsize(db);
fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
- for (int i = 0; i < n; ++i)
+ for (int i = 0, n = buf_getsize(db); i < n; ++i)
fputs(fh, strcat(bufstr_get(db, i), "\n"));
fclose(fh);
}
return buf_create();
}
-void db_put(float db, string pKey, string pValue);
+void db_put(int db, string key, string value);
-int db_load(string pFilename)
+int db_load(string filename)
{
int db = buf_create();
if (db < 0) return -1;
- int fh = fopen(pFilename, FILE_READ);
+ int fh = fopen(filename, FILE_READ);
if (fh < 0) return db;
string l = fgets(fh);
- if (stof(l) == DB_BUCKETS)
+ if (stoi(l) == DB_BUCKETS)
{
for (int i = 0; (l = fgets(fh)); ++i)
{
return db;
}
-void db_dump(float db, string pFilename)
+void db_dump(int db, string filename)
{
- int fh = fopen(pFilename, FILE_WRITE);
- if (fh < 0) error(strcat("Can't dump DB to ", pFilename));
- int n = buf_getsize(db);
+ int fh = fopen(filename, FILE_WRITE);
+ if (fh < 0) LOG_FATALF("Can't dump DB to %s\n");
fputs(fh, "0\n");
- for (int i = 0; i < n; ++i)
+ for (int i = 0, n = buf_getsize(db); i < n; ++i)
{
int m = tokenizebyseparator(bufstr_get(db, i), "\\");
for (int j = 2; j < m; j += 2)
fclose(fh);
}
-void db_close(float db)
+void db_close(int db)
{
buf_del(db);
}
-string db_get(float db, string pKey)
+string db_get(int db, string key)
{
- int h = crc16(false, pKey) % DB_BUCKETS;
- return uri_unescape(infoget(bufstr_get(db, h), pKey));
+ int h = crc16(false, key) % DB_BUCKETS;
+ return uri_unescape(infoget(bufstr_get(db, h), key));
}
-void db_put(float db, string pKey, string pValue)
+#define db_remove(db, key) db_put(db, key, "")
+
+void db_put(int db, string key, string value)
{
- int h = crc16(false, pKey) % DB_BUCKETS;
- bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue)));
+ int h = crc16(false, key) % DB_BUCKETS;
+ bufstr_set(db, h, infoadd(bufstr_get(db, h), key, uri_escape(value)));
}
void db_test()