Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
map.qh
Go to the documentation of this file.
1#pragma once
2
3#include "int.qh"
4
5// Databases (hash tables)
6const int DB_BUCKETS = 8192;
8void db_save(int db, string filename)
9{
10 int fh = fopen(filename, FILE_WRITE);
11 if (fh < 0)
12 {
13 LOG_WARNF("^1Can't write DB to %s", filename);
14 return;
15 }
16 fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
17 for (int i = 0, n = buf_getsize(db); i < n; ++i)
18 fputs(fh, strcat(bufstr_get(db, i), "\n"));
19 fclose(fh);
20}
21
23
26{
27 return buf_create();
28}
29#define HM_NEW(this) (this = db_create())
30
32void db_put(int db, string key, string value);
33
35int db_load(string filename)
36{
37 int db = buf_create();
38 if (db < 0)
39 return -1;
40 int fh = fopen(filename, FILE_READ);
41 if (fh < 0)
42 return db;
43 string l = fgets(fh);
44 if (stoi(l) == DB_BUCKETS)
45 {
46 for (int i = 0; (l = fgets(fh)); ++i)
47 if (l != "")
48 bufstr_set(db, i, l);
49 }
50 else
51 {
52 // different count of buckets, or a dump?
53 // need to reorganize the database then (SLOW)
54 //
55 // note: we also parse the first line (l) in case the DB file is
56 // missing the bucket count
57 do
58 {
59 int n = tokenizebyseparator(l, "\\");
60 for (int j = 2; j < n; j += 2)
61 db_put(db, argv(j - 1), uri_unescape(argv(j)));
62 }
63 while ((l = fgets(fh)));
64 }
65 fclose(fh);
66 return db;
67}
68
70void db_dump(int db, string filename)
71{
72 int fh = fopen(filename, FILE_WRITE);
73 if (fh < 0)
74 LOG_FATALF("Can't dump DB to %s", filename);
75 fputs(fh, "0\n");
76 for (int m, j, i = 0, n = buf_getsize(db); i < n; ++i)
77 {
78 m = tokenizebyseparator(bufstr_get(db, i), "\\");
79 for (j = 2; j < m; j += 2)
80 fputs(fh, strcat("\\", argv(j - 1), "\\", argv(j), "\n"));
81 }
82 fclose(fh);
83}
84
86void db_close(int db)
87{
88 buf_del(db);
89}
90#define HM_DELETE(this) db_close(this)
91
93string db_get(int db, string key)
94{
95 int h = crc16(false, key) % DB_BUCKETS;
96 return uri_unescape(infoget(bufstr_get(db, h), key));
97}
98#define HM_gets(this, k) db_get(this, k)
99
100#define db_remove(db, key) db_put(db, key, "")
101
103void db_put(int db, string key, string value)
104{
105 int h = crc16(false, key) % DB_BUCKETS;
106 bufstr_set(db, h, infoadd(bufstr_get(db, h), key, uri_escape(value)));
107}
108#define HM_sets(this, key, val) db_put(this, key, val)
109
110/*
111void db_test()
112{
113 LOG_INFO("LOAD...\n");
114 int db = db_load("foo.db");
115 LOG_INFO("LOADED. FILL...\n");
116 for (int i = 0; i < DB_BUCKETS; ++i)
117 db_put(db, ftos(random()), "X");
118 LOG_INFO("FILLED. SAVE...\n");
119 db_save(db, "foo.db");
120 LOG_INFO("SAVED. CLOSE...\n");
121 db_close(db);
122 LOG_INFO("CLOSED.\n");
123}
124*/
const float FILE_READ
const float FILE_WRITE
#define tokenizebyseparator
#define buf_create
#define stoi(s)
Definition int.qh:4
#define ERASEABLE
Definition _all.inc:37
#define USING(name, T)
Definition _all.inc:72
#define LOG_WARNF(...)
Definition log.qh:59
#define LOG_FATALF(...)
Definition log.qh:51
ERASEABLE void db_close(int db)
Definition map.qh:86
ERASEABLE int db_load(string filename)
Definition map.qh:35
const int DB_BUCKETS
Definition map.qh:6
ERASEABLE int db_create()
Definition map.qh:25
ERASEABLE void db_save(int db, string filename)
Definition map.qh:8
ERASEABLE string db_get(int db, string key)
Definition map.qh:93
ERASEABLE void db_put(int db, string key, string value)
Definition map.qh:103
int HashMap
Definition map.qh:22
ERASEABLE void db_dump(int db, string filename)
Definition map.qh:70
string fgets(float fhandle)
void fclose(float fhandle)
void fputs(float fhandle, string s)
float fopen(string filename, float mode)
string ftos(float f)
string argv(float n)
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))