summaryrefslogtreecommitdiff
path: root/src/sitespec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sitespec.c')
-rw-r--r--src/sitespec.c86
1 files changed, 75 insertions, 11 deletions
diff --git a/src/sitespec.c b/src/sitespec.c
index 4df62a9f..0332af2d 100644
--- a/src/sitespec.c
+++ b/src/sitespec.c
@@ -25,6 +25,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
25#include <the_Foundation/file.h> 25#include <the_Foundation/file.h>
26#include <the_Foundation/path.h> 26#include <the_Foundation/path.h>
27#include <the_Foundation/stringhash.h> 27#include <the_Foundation/stringhash.h>
28#include <the_Foundation/toml.h>
28 29
29iDeclareClass(SiteParams) 30iDeclareClass(SiteParams)
30iDeclareObjectConstruction(SiteParams) 31iDeclareObjectConstruction(SiteParams)
@@ -32,11 +33,13 @@ iDeclareObjectConstruction(SiteParams)
32struct Impl_SiteParams { 33struct Impl_SiteParams {
33 iObject object; 34 iObject object;
34 uint16_t titanPort; 35 uint16_t titanPort;
36 int dismissWarnings;
35 /* TODO: theme seed, style settings */ 37 /* TODO: theme seed, style settings */
36}; 38};
37 39
38void init_SiteParams(iSiteParams *d) { 40void init_SiteParams(iSiteParams *d) {
39 d->titanPort = 0; /* undefined */ 41 d->titanPort = 0; /* undefined */
42 d->dismissWarnings = 0;
40} 43}
41 44
42void deinit_SiteParams(iSiteParams *d) { 45void deinit_SiteParams(iSiteParams *d) {
@@ -49,14 +52,17 @@ iDefineObjectConstruction(SiteParams)
49/*----------------------------------------------------------------------------------------------*/ 52/*----------------------------------------------------------------------------------------------*/
50 53
51struct Impl_SiteSpec { 54struct Impl_SiteSpec {
52 iString savePath; 55 iString saveDir;
53 iStringHash sites; 56 iStringHash sites;
57 iSiteParams *loadParams;
54}; 58};
55 59
56static iSiteSpec siteSpec_; 60static iSiteSpec siteSpec_;
61static const char *fileName_SiteSpec_ = "sitespec.ini";
57 62
58static void load_SiteSpec_(iSiteSpec *d) { 63static void loadOldFormat_SiteSpec_(iSiteSpec *d) {
59 iFile *f = iClob(new_File(&d->savePath)); 64 clear_StringHash(&d->sites);
65 iFile *f = iClob(new_File(collect_String(concatCStr_Path(&d->saveDir, "sitespec.txt"))));
60 if (open_File(f, readOnly_FileMode | text_FileMode)) { 66 if (open_File(f, readOnly_FileMode | text_FileMode)) {
61 iString *src = collect_String(readString_File(f)); 67 iString *src = collect_String(readString_File(f));
62 iRangecc split = iNullRange; 68 iRangecc split = iNullRange;
@@ -93,15 +99,63 @@ static void load_SiteSpec_(iSiteSpec *d) {
93 } 99 }
94} 100}
95 101
102static void handleIniTable_SiteSpec_(void *context, const iString *table, iBool isStart) {
103 iSiteSpec *d = context;
104 if (isStart) {
105 iAssert(d->loadParams == NULL);
106 d->loadParams = new_SiteParams();
107 }
108 else {
109 iAssert(d->loadParams != NULL);
110 insert_StringHash(&d->sites, table, d->loadParams);
111 iReleasePtr(&d->loadParams);
112 }
113}
114
115static void handleIniKeyValue_SiteSpec_(void *context, const iString *table, const iString *key,
116 const iTomlValue *value) {
117 iSiteSpec *d = context;
118 iUnused(table);
119 if (!d->loadParams) {
120 return;
121 }
122 if (!cmp_String(key, "titanPort")) {
123 d->loadParams->titanPort = number_TomlValue(value);
124 }
125 else if (!cmp_String(key, "dismissWarnings") && value->type == int64_TomlType) {
126 d->loadParams->dismissWarnings = value->value.int64;
127 }
128}
129
130static iBool load_SiteSpec_(iSiteSpec *d) {
131 iBool ok = iFalse;
132 iFile *f = new_File(collect_String(concatCStr_Path(&d->saveDir, fileName_SiteSpec_)));
133 if (open_File(f, readOnly_FileMode | text_FileMode)) {
134 iTomlParser *toml = new_TomlParser();
135 setHandlers_TomlParser(toml, handleIniTable_SiteSpec_, handleIniKeyValue_SiteSpec_, d);
136 ok = parse_TomlParser(toml, collect_String(readString_File(f)));
137 delete_TomlParser(toml);
138 }
139 iRelease(f);
140 iAssert(d->loadParams == NULL);
141 return ok;
142}
143
96static void save_SiteSpec_(iSiteSpec *d) { 144static void save_SiteSpec_(iSiteSpec *d) {
97 iFile *f = new_File(&d->savePath); 145 iFile *f = new_File(collect_String(concatCStr_Path(&d->saveDir, fileName_SiteSpec_)));
98 if (open_File(f, writeOnly_FileMode | text_FileMode)) { 146 if (open_File(f, writeOnly_FileMode | text_FileMode)) {
99 iString *buf = new_String(); 147 iString *buf = new_String();
100 iConstForEach(StringHash, i, &d->sites) { 148 iConstForEach(StringHash, i, &d->sites) {
101 const iBlock * key = &i.value->keyBlock; 149 const iBlock * key = &i.value->keyBlock;
102 const iSiteParams *params = i.value->object; 150 const iSiteParams *params = i.value->object;
103 format_String(buf, "# %s\n", cstr_Block(key)); 151 format_String(buf, "[%s]\n", cstr_Block(key));
104 appendFormat_String(buf, "titanPort: %u\n", params->titanPort); 152 if (params->titanPort) {
153 appendFormat_String(buf, "titanPort = %u\n", params->titanPort);
154 }
155 if (params->dismissWarnings) {
156 appendFormat_String(buf, "dismissWarnings = 0x%x\n", params->dismissWarnings);
157 }
158 appendCStr_String(buf, "\n");
105 write_File(f, utf8_String(buf)); 159 write_File(f, utf8_String(buf));
106 } 160 }
107 delete_String(buf); 161 delete_String(buf);
@@ -111,15 +165,18 @@ static void save_SiteSpec_(iSiteSpec *d) {
111 165
112void init_SiteSpec(const char *saveDir) { 166void init_SiteSpec(const char *saveDir) {
113 iSiteSpec *d = &siteSpec_; 167 iSiteSpec *d = &siteSpec_;
114 initCStr_String(&d->savePath, concatPath_CStr(saveDir, "sitespec.txt")); 168 d->loadParams = NULL;
115 init_StringHash(&d->sites); 169 init_StringHash(&d->sites);
116 load_SiteSpec_(d); 170 initCStr_String(&d->saveDir, saveDir);
171 if (!load_SiteSpec_(d)) {
172 loadOldFormat_SiteSpec_(d);
173 }
117} 174}
118 175
119void deinit_SiteSpec(void) { 176void deinit_SiteSpec(void) {
120 iSiteSpec *d = &siteSpec_; 177 iSiteSpec *d = &siteSpec_;
121 deinit_StringHash(&d->sites); 178 deinit_StringHash(&d->sites);
122 deinit_String(&d->savePath); 179 deinit_String(&d->saveDir);
123} 180}
124 181
125void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) { 182void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) {
@@ -135,6 +192,11 @@ void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) {
135 case titanPort_SiteSpecKey: 192 case titanPort_SiteSpecKey:
136 params->titanPort = iClamp(value, 0, 0xffff); 193 params->titanPort = iClamp(value, 0, 0xffff);
137 needSave = iTrue; 194 needSave = iTrue;
195 break;
196 case dismissWarnings_SiteSpecKey:
197 params->dismissWarnings = value;
198 needSave = iTrue;
199 break;
138 default: 200 default:
139 break; 201 break;
140 } 202 }
@@ -152,6 +214,8 @@ int value_SiteSpec(const iString *site, enum iSiteSpecKey key) {
152 switch (key) { 214 switch (key) {
153 case titanPort_SiteSpecKey: 215 case titanPort_SiteSpecKey:
154 return params->titanPort; 216 return params->titanPort;
217 case dismissWarnings_SiteSpecKey:
218 return params->dismissWarnings;
155 default: 219 default:
156 return 0; 220 return 0;
157 } 221 }