11 #include <drift/dslcore.h>
12 #include <drift/ini-reader.h>
13 #include <drift/Mutex.h>
14 #include <drift/GenLib.h>
16 DSL_Mutex * iniMutex()
18 static DSL_Mutex actualMutex;
22 char * DSL_CC Get_INI_String(
const char * pszFilename,
const char *pszSection,
const char *pszItem,
char *pszValue,
size_t lValueBufferSize,
const char * pszDefault) {
23 AutoMutexPtr(iniMutex());
28 FILE * hFile = fopen(pszFilename,
"rb");
30 fseek(hFile, 0, SEEK_END);
31 long lFileSize = ftell(hFile);
32 char * cINIFile = (
char *)dsl_malloc(lFileSize + 1);
33 cINIFile[lFileSize] = 0;
35 fseek(hFile, 0, SEEK_SET);
36 if (fread(cINIFile, lFileSize, 1, hFile) == 1) {
37 ret = Get_INI_String_Memory(cINIFile, pszSection, pszItem, pszValue, lValueBufferSize, pszDefault);
44 if (ret == NULL && pszDefault) {
45 strlcpy(pszValue, pszDefault, lValueBufferSize);
52 char * DSL_CC Get_INI_String_Memory(
const char * data,
const char *pszSection,
const char *pszItem,
char *pszValue,
size_t lValueBufferSize,
const char * pszDefault) {
53 AutoMutexPtr(iniMutex());
55 memset(pszValue, 0, lValueBufferSize);
57 char * cfile = dsl_strdup(data);
58 size_t len = strlen(cfile);
61 while (strstr(cfile,
" ")) {
68 char * line = strtok_r(cfile,
"\n", &line2);
71 if (*line ==
'[' && strchr(line,
']')) {
75 char * p = strchr(line,
'=');
78 char * item = dsl_strdup(line);
79 strchr(item,
'=')[0] = 0;
80 while (item[0] ==
' ') {
81 memmove(item, item+1, strlen(item));
83 while (item[strlen(item)-1] ==
' ') {
84 item[strlen(item)-1] = 0;
86 char * value = dsl_strdup(p+1);
87 while (value[0] ==
' ') {
88 memmove(value, value+1, strlen(value));
90 while (value[strlen(value)-1] ==
' ') {
91 value[strlen(value)-1] = 0;
95 if (!stricmp(item, pszItem)) {
96 strlcpy(pszValue, value, lValueBufferSize);
107 if (*line ==
'[' && !strnicmp(line+1, pszSection, strlen(pszSection)) && line[strlen(pszSection)+1] ==
']') {
111 line = strtok_r(NULL,
"\n", &line2);
115 strlcpy(pszValue, pszDefault, lValueBufferSize);
125 int64 DSL_CC Get_INI_Int(
const char *path,
const char *section,
const char *item, int64 iDefault) {
126 AutoMutexPtr(iniMutex());
129 char * ret = Get_INI_String(path, section, item, ivalue,
sizeof(ivalue), NULL);
136 DSL_API int64 DSL_CC Get_INI_Int_Memory(
const char * cINIFile,
const char *section,
const char *item, int64 iDefault) {
137 AutoMutexPtr(iniMutex());
140 char * ret = Get_INI_String_Memory(cINIFile, section, item, ivalue,
sizeof(ivalue), NULL);
147 double DSL_CC Get_INI_Float(
const char *path,
const char *section,
const char *item,
double iDefault) {
148 AutoMutexPtr(iniMutex());
151 char * ret = Get_INI_String(path, section, item, ivalue,
sizeof(ivalue), NULL);
158 DSL_API
double DSL_CC Get_INI_Float_Memory(
const char * cINIFile,
const char *section,
const char *item,
double iDefault) {
159 AutoMutexPtr(iniMutex());
162 char * ret = Get_INI_String_Memory(cINIFile, section, item, ivalue,
sizeof(ivalue), NULL);
169 int DSL_CC Write_INI_String(
const char *pszPath,
const char *pszSection,
const char *pszItem,
const char *pszValue) {
170 FILE * hFile = fopen(pszPath,
"rb");
173 hFile = fopen(pszPath,
"wb");
178 fprintf(hFile,
"[%s]\r\n%s=%s\r\n", pszSection, pszItem, pszValue);
182 fseek(hFile, 0, SEEK_END);
183 size_t len = (size_t)ftell(hFile);
184 fseek(hFile, 0, SEEK_SET);
185 char * cfile = (
char *)dsl_malloc(len+1);
187 if (fread(cfile, len, 1, hFile) != 1) {
193 hFile = fopen(pszPath,
"wb");
200 while (strstr(cfile,
" ")) {
205 bool written =
false;
208 char * line = strtok_r(cfile,
"\n", &line2);
211 if (*line ==
'[' && strchr(line,
']')) {
215 fprintf(hFile,
"%s=%s\r\n\r\n", pszItem, pszValue);
220 char * p = strchr(line,
'=');
223 char * item = dsl_strdup(line);
224 strchr(item,
'=')[0] = 0;
225 while (item[0] ==
' ') {
226 memmove(item, item+1, strlen(item));
228 while (item[strlen(item)-1] ==
' ') {
229 item[strlen(item)-1] = 0;
231 char * value = dsl_strdup(p+1);
232 while (value[0] ==
' ') {
233 memmove(value, value+1, strlen(value));
235 while (value[strlen(value)-1] ==
' ') {
236 value[strlen(value)-1] = 0;
240 if (!stricmp(item, pszItem)) {
241 fprintf(hFile,
"%s=%s\r\n", pszItem, pszValue);
245 line = strtok_r(NULL,
"\n", &line2);
253 if (*line ==
'[' && !strnicmp(line+1, pszSection, strlen(pszSection)) && line[strlen(pszSection)+1] ==
']') {
259 fprintf(hFile,
"%s\r\n", line);
260 line = strtok_r(NULL,
"\n", &line2);
263 if (written ==
false) {
266 fprintf(hFile,
"%s=%s\r\n", pszItem, pszValue);
269 fprintf(hFile,
"[%s]\r\n%s=%s\r\n", pszSection, pszItem, pszValue);
278 int DSL_CC Write_INI_Int(
const char *pszPath,
const char *pszSection,
const char *pszItem, int64 iValue) {
280 sprintf(value, I64FMT, iValue);
281 return Write_INI_String(pszPath, pszSection, pszItem, value);
DSL_API void DSL_CC dsl_free(void *ptr)
DSL_API size_t DSL_CC strlcpy(char *dst, const char *src, size_t siz)
DSL_API_CLASS int DSL_CC str_replaceA(char *Str, size_t BufSize, const char *FindStr, const char *ReplStr)
Simple string replacement.