-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappconfig.c
543 lines (497 loc) · 16 KB
/
appconfig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "appconfig.h"
#include "inifile.h"
#include "datatypes.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define PATH_SEPARATOR '\\'
#define ENV_VARNAME_1 "APPDATA"
#elif __linux__
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PATH_SEPARATOR '/'
#define ENV_VARNAME_1 "XDG_CONFIG_HOME"
#define ENV_VARNAME_2 "HOME"
#define LINUX_CFG_DIR ".config"
#endif
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* >= C99 */
#define INLINE_FCT inline
#define INLINE_PROT static inline
#else /* No inline available from C Standard */
#define INLINE_FCT static
#define INLINE_PROT static
#endif /* __STDC_VERSION__ >= C99 */
#define DEFAULT_FILENAME "config.ini"
#ifdef APPCONFIG_PRINT_ERRORS
#define ERR_PRINT(str) fputs("AppConfig Error: " str "\n",stderr)
#define ERR_PRINT_VARGS(str,...) fprintf(stderr,"AppConfig Error: " str "\n",__VA_ARGS__)
#else
#define ERR_PRINT(str)
#define ERR_PRINT_VARGS(str,...)
#endif /* APPCONFIG_PRINT_ERRORS */
#define INI_PRINT_ERROR(strfunc,errcode) fprintf(stderr,strfunc "() failed (%d): %s\n",errcode,IniFile_GetErrorText(errcode))
#define APPCFG_PRINT_ERROR(strfunc,errcode) fprintf(stderr,strfunc "() failed (%d): %s\n",errcode,appConfig_GetErrorString(errcode))
enum
{
MAX_PATHLEN = 260,
};
struct TagAppConfig_T
{
char caPath[MAX_PATHLEN+1];
unsigned int uiPathLen;
AppConfigEntry *pEntries;
char *pcEntryData;
size_t szEntriesCount;
union StoreMethod_T /* Here add probably more methods for storage, e.g. registry for windows... */
{
Inifile iniFile;
}store;
};
/**
* Functions related to storage via Inifile (inifile.c/.h)
*/
INLINE_PROT int iAppConfig_Ini_Create_m(AppConfig pCfg);
INLINE_PROT int iAppConfig_Ini_DataLoad_m(AppConfig pCfg);
INLINE_PROT int iAppConfig_Ini_DataSave_m(AppConfig pCfg);
INLINE_PROT void vAppConfig_Ini_Close_m(AppConfig pCfg);
INLINE_PROT int iAppConfig_AssembleDestFolderPath_m(char *pcPathOut,
unsigned int *puiBufSize,
const char *pcAppName,
const char *pcFileName,
const char *pcLocation);
INLINE_PROT int iAppConfig_GetDefaultConfigPath_m(char *pcPath,
unsigned int *uiBufsize);
INLINE_PROT int iAppConfig_FileExists_m(const char *pcPath);
INLINE_PROT int iAppConfig_CreateDirIfNotExist_m(const char *pcDir);
int appConfig_New(AppConfig *config,
AppConfigEntry *entries,
size_t entriesCount,
const char *appName,
const char *fileName,
const char *location)
{
unsigned int uiTmp;
int iRc;
if(!((*config)=malloc(sizeof(struct TagAppConfig_T)+entriesCount)))
{
ERR_PRINT("malloc() failed");
return(APPCFG_ERR_INTERNAL);
}
if(!fileName)
fileName=DEFAULT_FILENAME;
uiTmp=MAX_PATHLEN;
if((iRc=iAppConfig_AssembleDestFolderPath_m((*config)->caPath,
&uiTmp,
appName,
fileName,
location)) != APPCFG_ERR_NONE)
{
APPCFG_PRINT_ERROR("iAppConfig_AssembleDestFolderPath_m",iRc);
free(*config);
return(iRc);
}
if((iRc=iAppConfig_Ini_Create_m(*config)) != APPCFG_ERR_NONE)
{
APPCFG_PRINT_ERROR("iAppConfig_Ini_Create_m",iRc);
free(*config);
return(iRc);
}
(*config)->pEntries=entries;
(*config)->szEntriesCount=entriesCount;
(*config)->pcEntryData=((char*)(*config))+sizeof(struct TagAppConfig_T);
/* bufferlength is checked in AssembleDestFolderPath, so strcpy is fine */
strcpy(&(*config)->caPath[uiTmp-1],fileName);
return(APPCFG_ERR_NONE);
}
int appConfig_DataLoad(AppConfig config)
{
if(!config)
return(APPCFG_ERR_PARAM);
return(iAppConfig_Ini_DataLoad_m(config));
}
int appConfig_DataSave(AppConfig config)
{
return(iAppConfig_Ini_DataSave_m(config));
}
int appConfig_DataDelete(const AppConfig config)
{
errno=0;
if(!config)
return(APPCFG_ERR_PARAM);
if(remove(config->caPath))
{
ERR_PRINT_VARGS("Failed to delete File \"%s\": %s",config->caPath,strerror(errno));
return(APPCFG_ERR_IO);
}
return(APPCFG_ERR_NONE);
}
const char *appConfig_GetErrorString(int error)
{
switch(error)
{
case APPCFG_ERR_NONE:
return("No Error");
case APPCFG_LOAD_EXISTING:
return("Loaded existing Config");
case APPCFG_LOAD_NEW:
return("Created new config");
case APPCFG_ERR_PARAM:
return("Invalid Parameter");
case APPCFG_ERR_DATA_MALFORMED:
return("Data can't be read, is malformed");
case APPCFG_ERR_IO:
return("I/O Error occured while accessing the data storage");
case APPCFG_ERR_INTERNAL:
return("Internal Error occured");
default:
return("Unknown error code");
}
}
const char *appConfig_GetPath(const AppConfig config)
{
return(config->caPath);
}
void appConfig_Close(AppConfig config)
{
vAppConfig_Ini_Close_m(config);
free(config);
}
void appConfig_DumpContents(const AppConfig config,
FILE *fp)
{
unsigned int uiIndex;
unsigned int uiIndexBin;
for(uiIndex=0;uiIndex < config->szEntriesCount;++uiIndex)
{
switch(config->pEntries[uiIndex].tagData.eType & 0xFF)
{
case eDataType_Int:
fprintf(fp,
"Group: \"%s\", (Int)key: \"%s\"=%d\n",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName,
config->pEntries[uiIndex].tagData.data.iVal);
break;
case eDataType_Uint:
fprintf(fp,
"Group: \"%s\", (Uint)key: \"%s\"=%u\n",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName,
config->pEntries[uiIndex].tagData.data.uiVal);
break;
case eDataType_Double:
fprintf(fp,
"Group: \"%s\", (Double)key: \"%s\"=%f\n",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName,
config->pEntries[uiIndex].tagData.data.dVal);
break;
case eDataType_Boolean:
fprintf(fp,
"Group: \"%s\", (Boolean)key: \"%s\"=%s\n",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName,
(config->pEntries[uiIndex].tagData.data.bVal)?"true":"false");
break;
case eDataType_String:
fprintf(fp,
"Group: \"%s\", (String)key: \"%s\"=\"%s\"\n",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName,
config->pEntries[uiIndex].tagData.data.pcVal);
break;
case eDataType_Binary:
fprintf(fp,
"Group: \"%s\", (Binary)key: \"%s\"=",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName);
for(uiIndexBin=0;uiIndexBin < config->pEntries[uiIndex].tagData.uiDataSizeUsed;++uiIndexBin)
fprintf(fp,"0x%.2X ",config->pEntries[uiIndex].tagData.data.pucVal[uiIndexBin]);
putc('\n',fp);
break;
default:
printf("Group: \"%s\", (Unknown type)key: \"%s\"=???",
config->pEntries[uiIndex].groupName,
config->pEntries[uiIndex].keyName);
break;
}
}
}
INLINE_FCT int iAppConfig_Ini_Create_m(AppConfig pCfg)
{
int iRc;
if(iAppConfig_CreateDirIfNotExist_m(pCfg->caPath) < 0)
{
ERR_PRINT("iAppConfig_CreateDirIfNotExist_m() failed");
return(APPCFG_ERR_IO);
}
if((iRc=IniFile_New(&pCfg->store.iniFile,INI_OPT_CASE_SENSITIVE)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_New",iRc);
return(APPCFG_ERR_INTERNAL);
}
return(APPCFG_ERR_NONE);
}
INLINE_FCT int iAppConfig_Ini_DataLoad_m(AppConfig pCfg)
{
const char *pcSection;
const char *pcKey;
unsigned int uiIndex;
unsigned int uiEntriesReadCount;
int iRc;
if(!iAppConfig_FileExists_m(pCfg->caPath))
return(APPCFG_LOAD_NEW);
memset(pCfg->pcEntryData,0,pCfg->szEntriesCount);
if((iRc=IniFile_Read(pCfg->store.iniFile,
pCfg->caPath)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_Read",iRc);
switch(iRc)
{
case INI_ERR_NONE: /* No error */
iRc=APPCFG_LOAD_EXISTING;
break;
case INI_ERR_MALFORMED:
iRc=APPCFG_ERR_DATA_MALFORMED;
break;
case INI_ERR_IO:
iRc=APPCFG_ERR_IO;
break;
case INI_ERR_INTERNAL:
default:
iRc=APPCFG_ERR_INTERNAL;
break;
}
return(iRc);
}
uiEntriesReadCount=0;
for(pcSection=IniFile_Iterator_SetSectionIndex(pCfg->store.iniFile,ITERATOR_FIRST);
pcSection;
pcSection=IniFile_Iterator_NextSection(pCfg->store.iniFile))
{
for(uiIndex=0;uiIndex < pCfg->szEntriesCount;++uiIndex)
{
if(pCfg->pcEntryData[uiIndex]) /* Skip already read entries */
continue;
if((pCfg->pEntries[uiIndex].groupName) &&
(IniFile_StringCompare(pCfg->store.iniFile,pcSection,pCfg->pEntries[uiIndex].groupName) != 0))
continue;
++uiEntriesReadCount;
pCfg->pcEntryData[uiIndex]=1;
for(pcKey=IniFile_Iterator_SetKeyIndex(pCfg->store.iniFile,ITERATOR_FIRST);
pcKey;
pcKey=IniFile_Iterator_NextKey(pCfg->store.iniFile))
{
if(IniFile_StringCompare(pCfg->store.iniFile,pcKey,pCfg->pEntries[uiIndex].keyName) == 0)
{
if(((iRc=IniFile_Iterator_KeyGetValue(pCfg->store.iniFile,&pCfg->pEntries[uiIndex].tagData)) != INI_ERR_NONE) &&
(iRc != INI_ERR_DATA_EMPTY))
{
INI_PRINT_ERROR("IniFile_Iterator_KeyGetValue",iRc);
switch(iRc)
{
case INI_ERR_PARAM:
return(APPCFG_ERR_PARAM);
default:
return(APPCFG_ERR_INTERNAL);
}
}
break;
}
}
}
if(uiEntriesReadCount == pCfg->szEntriesCount) /* Check if all entries have been read already */
break;
}
/* Got all needed entries, clear inifile data */
IniFile_Clean(pCfg->store.iniFile);
return(APPCFG_LOAD_EXISTING);
}
INLINE_FCT int iAppConfig_Ini_DataSave_m(AppConfig pCfg)
{
unsigned int uiIndex;
int iRc;
for(uiIndex=0;uiIndex < pCfg->szEntriesCount;++uiIndex)
{
if(pCfg->pEntries[uiIndex].groupName)
{
if((iRc=IniFile_Iterator_CreateSection(pCfg->store.iniFile,pCfg->pEntries[uiIndex].groupName)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_Iterator_CreateSection",iRc);
switch(iRc)
{
case INI_ERR_PARAM:
return(APPCFG_ERR_PARAM);
case INI_ERR_INTERNAL:
default:
return(APPCFG_ERR_INTERNAL);
}
}
}
else
{
IniFile_Iterator_SetSectionIndex(pCfg->store.iniFile,ITERATOR_FIRST);
}
if((iRc=IniFile_Iterator_CreateKey(pCfg->store.iniFile,pCfg->pEntries[uiIndex].keyName)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_Iterator_CreateKey",iRc);
switch(iRc)
{
case INI_ERR_PARAM:
return(APPCFG_ERR_PARAM);
case INI_ERR_INTERNAL:
default:
return(APPCFG_ERR_INTERNAL);
}
}
if((iRc=IniFile_Iterator_KeySetValue(pCfg->store.iniFile,&pCfg->pEntries[uiIndex].tagData)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_Iterator_KeySetValue",iRc);
switch(iRc)
{
case INI_ERR_PARAM:
return(APPCFG_ERR_PARAM);
case INI_ERR_INTERNAL:
default:
return(APPCFG_ERR_INTERNAL);
}
}
}
if((iRc=IniFile_Write(pCfg->store.iniFile,pCfg->caPath)) != INI_ERR_NONE)
{
INI_PRINT_ERROR("IniFile_Write",iRc);
switch(iRc)
{
case INI_ERR_PARAM:
return(APPCFG_ERR_PARAM);
case INI_ERR_IO:
return(APPCFG_ERR_IO);
case INI_ERR_INTERNAL:
default:
return(APPCFG_ERR_INTERNAL);
}
}
return(APPCFG_ERR_NONE);
}
INLINE_FCT void vAppConfig_Ini_Close_m(AppConfig pCfg)
{
IniFile_Dispose(pCfg->store.iniFile);
}
INLINE_FCT int iAppConfig_AssembleDestFolderPath_m(char *pcOutPath,
unsigned int *puiBufSize,
const char *pcAppName,
const char *pcFileName,
const char *pcLocation)
{
unsigned int uiTmp;
if(!pcAppName) /* Appname must be specified */
return(APPCFG_ERR_PARAM);
if(!pcLocation)
{
uiTmp=*puiBufSize;
if(iAppConfig_GetDefaultConfigPath_m(pcOutPath,
&uiTmp))
{
ERR_PRINT("iAppConfig_GetDefaultPath_m() failed");
return(APPCFG_ERR_INTERNAL);
}
}
else
{
if((uiTmp=strlen(pcLocation)+1) > *puiBufSize-2)
return(APPCFG_ERR_PARAM);
memcpy(pcOutPath,pcLocation,uiTmp);
}
/* Check if buffer is big enough to hold the whole path */
if(uiTmp+strlen(pcAppName)+strlen(pcFileName) > (*puiBufSize)-2)
{
ERR_PRINT("Buffer for path too small, would exceed");
return(APPCFG_ERR_PARAM);
}
if(pcOutPath[uiTmp-2] != PATH_SEPARATOR) /* Add path separator if needed */
pcOutPath[uiTmp-1]=PATH_SEPARATOR;
strcpy(&pcOutPath[uiTmp],pcAppName);
uiTmp=strlen(pcOutPath);
if(pcOutPath[uiTmp-1] != PATH_SEPARATOR) /* Add path separator if needed */
{
pcOutPath[uiTmp++]=PATH_SEPARATOR;
pcOutPath[uiTmp]='\0';
}
*puiBufSize=uiTmp+1;
return(APPCFG_ERR_NONE);
}
INLINE_FCT int iAppConfig_GetDefaultConfigPath_m(char *pcPath,
unsigned int *puiBufSize)
{
char *pcTmp;
unsigned int uiTmp=0;
#ifdef _WIN32
if((!(pcTmp=getenv(ENV_VARNAME_1))) || (pcTmp[0] == '\0'))
return(-1);
uiTmp=strlen(pcTmp)+1;
if(uiTmp+2 > *puiBufSize)
return(-1);
memcpy(pcPath,pcTmp,uiTmp);
*puiBufSize=uiTmp;
#elif __linux__
#ifdef _GNU_SOURCE
#define GETENV(env) secure_getenv(env)
#else /* Fallback to default getenv function... */
#define GETENV(env) getenv(env)
#endif
/* Look for XDG_CONFIG_HOME Environment var first */
if((pcTmp=GETENV(ENV_VARNAME_1)) && (pcTmp[0] != '\0'))
{
uiTmp=strlen(pcTmp)+1;
if(uiTmp+2 > *puiBufSize)
return(-1);
memcpy(pcPath,pcTmp,uiTmp);
*puiBufSize=uiTmp;
} /* As 2nd option, look for home, and add .config folder manually */
else if((pcTmp=GETENV(ENV_VARNAME_2)) && (pcTmp[0] != '\0'))
{
uiTmp=strlen(pcTmp)+1;
if(uiTmp+sizeof(LINUX_CFG_DIR)+2 > *puiBufSize)
return(-1);
memcpy(pcPath,pcTmp,uiTmp);
pcPath[uiTmp-1]=PATH_SEPARATOR;
memcpy(&pcPath[uiTmp],LINUX_CFG_DIR,sizeof(LINUX_CFG_DIR));
*puiBufSize=uiTmp+sizeof(LINUX_CFG_DIR);
}
else
return(-1); /* Env not found or empty */
#else
#error "Unknown platform, can't determine config path"
#endif
return(0);
}
INLINE_FCT int iAppConfig_FileExists_m(const char *pcPath)
{
FILE *fp;
if(!(fp=fopen(pcPath,"r")))
return(0);
fclose(fp);
return(1);
}
INLINE_FCT int iAppConfig_CreateDirIfNotExist_m(const char *pcDir)
{
#ifdef _WIN32
if(CreateDirectory(pcDir,NULL))
return(1);
if(GetLastError() == ERROR_ALREADY_EXISTS)
return(0);
return(-1);
#elif __linux__
errno=0;
if(mkdir(pcDir,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0)
return(1);
if(errno == EEXIST)
return(0);
return(-1);
#endif
}