| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265 |
- /**
- * WinPR: Windows Portable Runtime
- * .ini config file
- *
- * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef _WIN32
- #include <unistd.h>
- #endif
- #include <errno.h>
- #include <winpr/wtypes.h>
- #include <winpr/crt.h>
- #include <winpr/ini.h>
- #include "../log.h"
- #define TAG WINPR_TAG("ini")
- struct _wIniFileKey
- {
- char* name;
- char* value;
- BOOL invalid; /*extend */
- };
- typedef struct _wIniFileKey wIniFileKey;
- struct _wIniFileSection
- {
- char* name;
- size_t nKeys;
- size_t cKeys;
- wIniFileKey** keys;
- BOOL invalid; /*extend */
- };
- typedef struct _wIniFileSection wIniFileSection;
- struct _wIniFile
- {
- FILE* fp;
- char* line;
- char* nextLine;
- size_t lineLength;
- char* tokctx;
- char* buffer;
- char* filename;
- BOOL readOnly;
- size_t nSections;
- size_t cSections;
- wIniFileSection** sections;
- };
- static BOOL IniFile_Load_NextLine(wIniFile* ini, char* str)
- {
- size_t length = 0;
- still: // fix \r\n gifur
- ini->nextLine = strtok_s(str, "\n", &ini->tokctx);
- if (ini->nextLine)
- length = strlen(ini->nextLine);
- if (length > 0)
- {
- if (ini->nextLine[length - 1] == '\r')
- {
- ini->nextLine[length - 1] = '\0';
- length--;
- }
- if (length < 1) { // must be zero !!
- if(strlen(ini->tokctx) > 0) {
- goto still;
- }else {
- ini->nextLine = NULL;
- }
- }
- }
- return (ini->nextLine) ? TRUE : FALSE;
- }
- static BOOL IniFile_Load_String(wIniFile* ini, const char* iniString)
- {
- size_t fileSize;
- if (!ini || !iniString)
- return FALSE;
- ini->line = NULL;
- ini->nextLine = NULL;
- ini->buffer = NULL;
- fileSize = strlen(iniString);
- if (fileSize < 1)
- return FALSE;
- ini->buffer = (char*)malloc(fileSize + 2);
- if (!ini->buffer)
- return FALSE;
- CopyMemory(ini->buffer, iniString, fileSize);
- ini->buffer[fileSize] = '\n';
- ini->buffer[fileSize + 1] = '\0';
- IniFile_Load_NextLine(ini, ini->buffer);
- return TRUE;
- }
- /*fopen*/
- static BOOL IniFile_Open_File(wIniFile* ini, const char* filename)
- {
- if (!ini || !filename)
- return FALSE;
- if (ini->readOnly)
- ini->fp = fopen(filename, "rb");
- else
- ini->fp = fopen(filename, "w+b");
- if (!ini->fp)
- return FALSE;
- return TRUE;
- }
- /**/
- static BOOL IniFile_Load_File(wIniFile* ini, const char* filename)
- {
- INT64 fileSize;
- if (!IniFile_Open_File(ini, filename))
- return FALSE;
- if (_fseeki64(ini->fp, 0, SEEK_END) < 0)
- goto out_file;
- fileSize = _ftelli64(ini->fp);
- if (fileSize < 0)
- goto out_file;
- if (_fseeki64(ini->fp, 0, SEEK_SET) < 0)
- goto out_file;
- ini->line = NULL;
- ini->nextLine = NULL;
- ini->buffer = NULL;
- if (fileSize < 1)
- goto out_file;
- ini->buffer = (char*)malloc((size_t)fileSize + 2);
- if (!ini->buffer)
- goto out_file;
- if (fread(ini->buffer, (size_t)fileSize, 1, ini->fp) != 1)
- goto out_buffer;
- fclose(ini->fp);
- ini->fp = NULL;
- ini->buffer[fileSize] = '\n';
- ini->buffer[fileSize + 1] = '\0';
- IniFile_Load_NextLine(ini, ini->buffer);
- return TRUE;
- out_buffer:
- free(ini->buffer);
- ini->buffer = NULL;
- out_file:
- fclose(ini->fp);
- ini->fp = NULL;
- return FALSE;
- }
- static void IniFile_Load_Finish(wIniFile* ini)
- {
- if (!ini)
- return;
- if (ini->buffer)
- {
- free(ini->buffer);
- ini->buffer = NULL;
- }
- }
- static BOOL IniFile_Load_HasNextLine(wIniFile* ini)
- {
- if (!ini)
- return FALSE;
- return (ini->nextLine) ? TRUE : FALSE;
- }
- static char* IniFile_Load_GetNextLine(wIniFile* ini)
- {
- if (!ini)
- return NULL;
- ini->line = ini->nextLine;
- ini->lineLength = strlen(ini->line);
- IniFile_Load_NextLine(ini, NULL);
- return ini->line;
- }
- static wIniFileKey* IniFile_Key_New(const char* name, const char* value)
- {
- wIniFileKey* key;
- if (!name || !value)
- return NULL;
- key = malloc(sizeof(wIniFileKey));
- if (key)
- {
- key->name = _strdup(name);
- key->value = _strdup(value);
- key->invalid = FALSE;
- if (!key->name || !key->value)
- {
- free(key->name);
- free(key->value);
- free(key);
- return NULL;
- }
- }
- return key;
- }
- static void IniFile_Key_Free(wIniFileKey* key)
- {
- if (!key)
- return;
- free(key->name);
- free(key->value);
- free(key);
- }
- static wIniFileSection* IniFile_Section_New(const char* name)
- {
- wIniFileSection* section;
- if (!name)
- return NULL;
- section = malloc(sizeof(wIniFileSection));
- if (!section)
- return NULL;
- section->name = _strdup(name);
- if (!section->name)
- {
- free(section);
- return NULL;
- }
- section->invalid = FALSE;
- section->nKeys = 0;
- section->cKeys = 64;
- section->keys = (wIniFileKey**)calloc(section->cKeys, sizeof(wIniFileKey*));
- if (!section->keys)
- {
- free(section->name);
- free(section);
- return NULL;
- }
- return section;
- }
- static void IniFile_Section_Free(wIniFileSection* section)
- {
- size_t index;
- if (!section)
- return;
- free(section->name);
- for (index = 0; index < section->nKeys; index++)
- {
- IniFile_Key_Free(section->keys[index]);
- }
- free(section->keys);
- free(section);
- }
- static wIniFileSection* IniFile_GetSection(wIniFile* ini, const char* name)
- {
- size_t index;
- wIniFileSection* section = NULL;
- if (!ini || !name)
- return NULL;
- for (index = 0; index < ini->nSections; index++)
- {
- if (_stricmp(name, ini->sections[index]->name) == 0
- && !ini->sections[index]->invalid)
- {
- section = ini->sections[index];
- break;
- }
- }
- return section;
- }
- static wIniFileSection* IniFile_AddSection(wIniFile* ini, const char* name)
- {
- wIniFileSection* section;
- if (!ini || !name)
- return NULL;
- section = IniFile_GetSection(ini, name);
- if (!section)
- {
- if ((ini->nSections + 1) >= (ini->cSections))
- {
- size_t new_size;
- wIniFileSection** new_sect;
- new_size = ini->cSections * 2;
- new_sect = (wIniFileSection**)realloc(ini->sections, sizeof(wIniFileSection*) * new_size);
- if (!new_sect)
- return NULL;
- ini->cSections = new_size;
- ini->sections = new_sect;
- }
- section = IniFile_Section_New(name);
- ini->sections[ini->nSections] = section;
- ini->nSections++;
- }
- return section;
- }
- static BOOL IniFile_RemoveSection(wIniFile* ini, const char* name, BOOL* changed)
- {
- size_t index;
- wIniFileSection* section = NULL;
- if (!ini || !name)
- return FALSE;
- for (index = 0; index < ini->nSections; index++) {
- if (_stricmp(name, ini->sections[index]->name) == 0) {
- section = ini->sections[index];
- break;
- }
- }
- if(section)
- {
- ini->nSections--;
- ini->sections[index] = ini->sections[ini->nSections];
- ini->sections[ini->nSections] = NULL;
- IniFile_Section_Free(section);
- if(changed)
- {
- *changed = TRUE;
- }
- }
- return TRUE;
- }
- static wIniFileKey* IniFile_GetKey(wIniFile* ini, wIniFileSection* section, const char* name)
- {
- size_t index;
- wIniFileKey* key = NULL;
- if (!ini || !section || !name)
- return NULL;
- for (index = 0; index < section->nKeys; index++)
- {
- if (_stricmp(name, section->keys[index]->name) == 0)
- {
- key = section->keys[index];
- break;
- }
- }
- return key;
- }
- static wIniFileKey* IniFile_AddKey(wIniFile* ini, wIniFileSection* section, const char* name,
- const char* value, BOOL* changed)
- {
- wIniFileKey* key;
- if (!section || !name || !value)
- return NULL;
- key = IniFile_GetKey(ini, section, name);
- if (!key)
- {
- if ((section->nKeys + 1) >= (section->cKeys))
- {
- size_t new_size;
- wIniFileKey** new_key;
- new_size = section->cKeys * 2;
- new_key = (wIniFileKey**)realloc(section->keys, sizeof(wIniFileKey*) * new_size);
- if (!new_key)
- return NULL;
- section->cKeys = new_size;
- section->keys = new_key;
- }
- key = IniFile_Key_New(name, value);
- if (!key)
- return NULL;
- section->keys[section->nKeys] = key;
- section->nKeys++;
- if (changed) { *changed = TRUE; }
- }
- else if(_stricmp(key->value, value) != 0)
- {
- free(key->value);
- key->value = _strdup(value);
- if (!key->value)
- return NULL;
- if (changed) { *changed = TRUE; }
- }
- return key;
- }
- static BOOL IniFile_RemoveKey(wIniFile* ini, const char* sectionName, const char* keyName, BOOL* changed)
- {
- size_t index;
- wIniFileKey* key = NULL;
- wIniFileSection* section = NULL;
- if (!sectionName || !keyName)
- return FALSE;
- section = IniFile_GetSection(ini, sectionName);
- if(section)
- {
- for (index = 0; index < section->nKeys; index++) {
- if (_stricmp(keyName, section->keys[index]->name) == 0) {
- key = section->keys[index];
- break;
- }
- }
- if(key)
- {
- section->nKeys--;
- section->keys[index] = section->keys[section->nKeys];
- section->keys[section->nKeys] = NULL;
- IniFile_Key_Free(key);
- if(changed)
- {
- *changed = TRUE;
- }
- }
- }
- return TRUE;
- }
- static int IniFile_Load(wIniFile* ini)
- {
- char* line;
- char* name;
- char* value;
- char* separator;
- char *beg, *end;
- wIniFileSection* section = NULL;
- if (!ini)
- return -1;
- while (IniFile_Load_HasNextLine(ini))
- {
- line = IniFile_Load_GetNextLine(ini);
- if (line[0] == ';' || line[0] == '#')
- continue;
- if (line[0] == '[')
- {
- beg = &line[1];
- end = strchr(line, ']');
- if (!end) {
- fprintf(stderr, "parse line \"%s\" with [ failed.\n", line);
- return -1;
- }
- *end = '\0';
- IniFile_AddSection(ini, beg);
- section = ini->sections[ini->nSections - 1];
- }
- else
- {
- separator = strchr(line, '=');
- if (separator == NULL) {
- fprintf(stderr, "parse line \"%s\" with = failed.\n", line);
- return -1;
- }
- end = separator;
- while ((&end[-1] > line) && ((end[-1] == ' ') || (end[-1] == '\t')))
- end--;
- *end = '\0';
- name = line;
- beg = separator + 1;
- while (*beg && ((*beg == ' ') || (*beg == '\t')))
- beg++;
- if (*beg == '"')
- beg++;
- end = &line[ini->lineLength];
- while ((end > beg) && ((end[-1] == ' ') || (end[-1] == '\t')))
- end--;
- if (end[-1] == '"')
- end[-1] = '\0';
- value = beg;
- if (!IniFile_AddKey(ini, section, name, value, NULL)) {
- fprintf(stderr, "IniFile_AddKey \"%s\" with %s=%s failed.\n", section->name, name, value);
- return -1;
- }
- }
- }
- IniFile_Load_Finish(ini);
- return 1;
- }
- int IniFile_ReadBuffer(wIniFile* ini, const char* buffer)
- {
- BOOL status;
- if (!ini || !buffer)
- return -1;
- ini->readOnly = TRUE;
- ini->filename = NULL;
- status = IniFile_Load_String(ini, buffer);
- if (!status)
- return -1;
- return IniFile_Load(ini);
- }
- int IniFile_ReadFile(wIniFile* ini, const char* filename)
- {
- ini->readOnly = TRUE;
- free(ini->filename);
- ini->filename = _strdup(filename);
- if (!ini->filename)
- return -1;
- if (!IniFile_Load_File(ini, filename))
- return -1;
- return IniFile_Load(ini);
- }
- char** IniFile_GetSectionNames(wIniFile* ini, int* count)
- {
- char* p;
- size_t index;
- size_t length;
- size_t nameLength;
- char** sectionNames;
- wIniFileSection* section = NULL;
- if (!ini || !count)
- return NULL;
- if (ini->nSections > INT_MAX)
- return NULL;
- length = (sizeof(char*) * ini->nSections) + sizeof(char);
- for (index = 0; index < ini->nSections; index++)
- {
- section = ini->sections[index];
- nameLength = strlen(section->name);
- length += (nameLength + 1);
- }
- sectionNames = (char**)malloc(length);
- if (!sectionNames)
- return NULL;
- p = (char*)&((BYTE*)sectionNames)[sizeof(char*) * ini->nSections];
- for (index = 0; index < ini->nSections; index++)
- {
- sectionNames[index] = p;
- section = ini->sections[index];
- nameLength = strlen(section->name);
- CopyMemory(p, section->name, nameLength + 1);
- p += (nameLength + 1);
- }
- *p = '\0';
- *count = (int)ini->nSections;
- return sectionNames;
- }
- char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count)
- {
- char* p;
- size_t index;
- size_t length;
- size_t nameLength;
- char** keyNames;
- wIniFileKey* pKey = NULL;
- wIniFileSection* pSection = NULL;
- if (!ini || !section || !count)
- return NULL;
- pSection = IniFile_GetSection(ini, section);
- if (!pSection)
- return NULL;
- if (pSection->nKeys > INT_MAX)
- return NULL;
- length = (sizeof(char*) * pSection->nKeys) + sizeof(char);
- for (index = 0; index < pSection->nKeys; index++)
- {
- pKey = pSection->keys[index];
- nameLength = strlen(pKey->name);
- length += (nameLength + 1);
- }
- keyNames = (char**)malloc(length);
- if (!keyNames)
- return NULL;
- p = (char*)&((BYTE*)keyNames)[sizeof(char*) * pSection->nKeys];
- for (index = 0; index < pSection->nKeys; index++)
- {
- keyNames[index] = p;
- pKey = pSection->keys[index];
- nameLength = strlen(pKey->name);
- CopyMemory(p, pKey->name, nameLength + 1);
- p += (nameLength + 1);
- }
- *p = '\0';
- *count = (int)pSection->nKeys;
- return keyNames;
- }
- const char* IniFile_GetKeyValueString(wIniFile* ini, const char* section, const char* key)
- {
- const char* value = NULL;
- wIniFileKey* pKey = NULL;
- wIniFileSection* pSection = NULL;
- pSection = IniFile_GetSection(ini, section);
- if (!pSection)
- return NULL;
- pKey = IniFile_GetKey(ini, pSection, key);
- if (!pKey)
- return NULL;
- value = (const char*)pKey->value;
- return value;
- }
- int IniFile_GetKeyValueInt(wIniFile* ini, const char* section, const char* key, const int defValue)
- {
- int err;
- long value = 0;
- wIniFileKey* pKey = NULL;
- wIniFileSection* pSection = NULL;
- pSection = IniFile_GetSection(ini, section);
- if (!pSection)
- return defValue;
- pKey = IniFile_GetKey(ini, pSection, key);
- if (!pKey)
- return defValue;
- err = errno;
- errno = 0;
- value = strtol(pKey->value, NULL, 0);
- if ((value < INT_MIN) || (value > INT_MAX) || (errno != 0))
- {
- errno = err;
- return defValue;
- }
- return (int)value;
- }
- int IniFile_SetKeyValueString(wIniFile* ini, const char* section, const char* key,
- const char* value)
- {
- wIniFileKey* pKey = NULL;
- wIniFileSection* pSection = NULL;
- pSection = IniFile_GetSection(ini, section);
- if (!pSection)
- pSection = IniFile_AddSection(ini, section);
- if (!pSection)
- return -1;
- pKey = IniFile_AddKey(ini, pSection, key, value, NULL);
- if (!pKey)
- return -1;
- return 1;
- }
- int IniFile_SetKeyValueInt(wIniFile* ini, const char* section, const char* key, int value)
- {
- char strVal[128];
- wIniFileKey* pKey = NULL;
- wIniFileSection* pSection = NULL;
- sprintf_s(strVal, sizeof(strVal), "%d", value);
- pSection = IniFile_GetSection(ini, section);
- if (!pSection)
- pSection = IniFile_AddSection(ini, section);
- if (!pSection)
- return -1;
- pKey = IniFile_AddKey(ini, pSection, key, strVal, NULL);
- if (!pKey)
- return -1;
- return 1;
- }
- char* IniFile_WriteBuffer(wIniFile* ini)
- {
- size_t i, j;
- size_t offset;
- size_t size;
- char* buffer;
- wIniFileKey* key;
- wIniFileSection* section;
- size = 0;
- if (!ini)
- return NULL;
- /*calculate the rquired buffer size for storing*/
- for (i = 0; i < ini->nSections; i++)
- {
- section = ini->sections[i];
- size += (strlen(section->name) + 3);
- for (j = 0; j < section->nKeys; j++)
- {
- key = section->keys[j];
- size += (strlen(key->name) + strlen(key->value) + 2);
- }
- size += 1;
- }
- /*allocate buffer*/
- size += 1;
- buffer = malloc(size + 1);
- if (!buffer)
- return NULL;
- offset = 0;
- /*write to the buffer*/
- for (i = 0; i < ini->nSections; i++)
- {
- section = ini->sections[i];
- sprintf_s(&buffer[offset], size - offset, "[%s]\n", section->name);
- offset += (strlen(section->name) + 3);
- for (j = 0; j < section->nKeys; j++)
- {
- key = section->keys[j];
- sprintf_s(&buffer[offset], size - offset, "%s=%s\n", key->name, key->value);
- offset += (strlen(key->name) + strlen(key->value) + 2);
- }
- sprintf_s(&buffer[offset], size - offset, "\n");
- offset += 1;
- }
- buffer[offset] = '\0';
- return buffer;
- }
- int IniFile_WriteFile(wIniFile* ini, const char* filename)
- {
- size_t length;
- char* buffer;
- int ret = 1;
- buffer = IniFile_WriteBuffer(ini);
- if (!buffer)
- return -1;
- length = strlen(buffer);
- /*set write flag*/
- ini->readOnly = FALSE;
- if (!filename)
- filename = ini->filename;
- if (!IniFile_Open_File(ini, filename))
- {
- free(buffer);
- return -1;
- }
- if (fwrite((void*)buffer, length, 1, ini->fp) != 1)
- ret = -1;
- fclose(ini->fp);
- free(buffer);
- return ret;
- }
- wIniFile* IniFile_New(void)
- {
- wIniFile* ini = (wIniFile*)calloc(1, sizeof(wIniFile));
- if (ini)
- {
- ini->nSections = 0;
- ini->cSections = 64;
- ini->sections = (wIniFileSection**)calloc(ini->cSections, sizeof(wIniFileSection*));
- if (!ini->sections)
- {
- free(ini);
- return NULL;
- }
- }
- return ini;
- }
- void IniFile_Free(wIniFile* ini)
- {
- size_t index;
- if (!ini)
- return;
- free(ini->filename);
- for (index = 0; index < ini->nSections; index++)
- IniFile_Section_Free(ini->sections[index]);
- free(ini->sections);
- free(ini);
- }
- #ifndef _WIN32
- DWORD GetPrivateProfileStringA(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpDefault,
- LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName)
- {
- wIniFile* ini;
- DWORD nRetSize = 0;
- DWORD nRealSize = 0;
- const char* value;
- wIniFileSection* pSection = NULL;
- int index;
- wIniFileKey* key = NULL;
- char *t;
- size_t totalLen = 0;
- if(lpAppName == NULL)
- {
- return GetPrivateProfileSectionNamesA(lpReturnedString, nSize, lpFileName);
- }
- ini = IniFile_New();
- if(IniFile_ReadFile(ini, lpFileName) < 0) {
- IniFile_Free(ini);
- WLog_ERR(TAG, "failed to parse %s", lpFileName);
- return 0;
- }
- if(lpKeyName != NULL) {
- value = IniFile_GetKeyValueString(ini, lpAppName, lpKeyName);
- if(value) {
- nRealSize = strlen(value);
- if(nRealSize +1 > nSize)
- {
- nRetSize = nSize - 1;
- }
- else
- {
- if (nSize > 0 && lpReturnedString) {
- strncpy(lpReturnedString, value, strlen(value) + 1);
- }
- nRetSize = nRealSize;
- }
- }
- } else { // key == null
- pSection = IniFile_GetSection(ini, lpAppName);
- if (pSection) {
- totalLen = 0;
- for (index = 0; index < pSection->nKeys; index++) {
- key = pSection->keys[index];
- if(strlen(key->name) > 0)
- totalLen += strlen(key->name) + 1;
- }
- totalLen++;
- // if(totalLen > 0) {
- // totalLen--;
- // }
- nRealSize = nRetSize = totalLen;
- if(totalLen > 1 && nSize >= 2 && lpReturnedString) {
- if(totalLen > nSize) {
- // for compatible the upper invoke
- nRetSize = nSize - 2;
- } else {
- char* temp = malloc(sizeof(char)*totalLen);
- if(temp) {
- memset(temp, 0, sizeof(char)*totalLen);
- t = temp;
- for (index = 0; index < pSection->nKeys; index++) {
- key = pSection->keys[index];
- if(strcmp(key->name, "InitiativeTransfer") == 0) {
- ;
- }
- if(strlen(key->name) > 0) {
- strcpy(t, key->name);
- t += strlen(key->name);
- *t = '\0';
- t++;
- }
- }
- *t = '\0';
- totalLen = nSize > totalLen ? totalLen : nSize;
- memcpy(lpReturnedString, temp, sizeof(char)*totalLen);
- free(temp);
- }
- }
- }
- }
- }
- if(nRealSize == 0 && nSize > 0 && lpReturnedString) {
- lpReturnedString[0] = '\0';
- if(lpDefault) {
- nRealSize = nSize > strlen(lpDefault)+1 ? strlen(lpDefault)+1 : nSize;
- strncpy(lpReturnedString, lpDefault, nRealSize);
- nRetSize = strlen(lpReturnedString);
- }
- }
- IniFile_Free(ini);
- return nRetSize;
- }
- DWORD GetPrivateProfileStringW(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
- LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName)
- {
- WLog_ERR(TAG, "%s operation not implemented", __FUNCTION__);
- return FALSE;
- }
- UINT GetPrivateProfileIntA(LPCSTR lpAppName, LPCSTR lpKeyName, INT nDefault, LPCSTR lpFileName)
- {
- UINT ret = nDefault;
- wIniFile* ini;
- ini = IniFile_New();
- if(!ini) {
- return ret;
- }
- if(IniFile_ReadFile(ini, lpFileName) < 0) {
- IniFile_Free(ini);
- WLog_ERR(TAG, "failed to parse %s", lpFileName);
- return ret;
- }
- ret = IniFile_GetKeyValueInt(ini, lpAppName, lpKeyName, nDefault);
- IniFile_Free(ini);
- return ret;
- }
- UINT GetPrivateProfileIntW(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault, LPCWSTR lpFileName)
- {
- WLog_ERR(TAG, "%s operation not implemented", __FUNCTION__);
- return FALSE;
- }
- DWORD GetPrivateProfileSectionNamesA(LPSTR lpszReturnBuffer, DWORD nSize, LPCSTR lpFileName)
- {
- DWORD ret = 0;
- wIniFile* ini;
- int index;
- int length;
- int validCount = 0;
- int nameLength;
- char* sectionNames;
- wIniFileSection* section = NULL;
- char *p;
- ini = IniFile_New();
- if(!ini) {
- return ret;
- }
- if(IniFile_ReadFile(ini, lpFileName) < 0) {
- IniFile_Free(ini);
- WLog_ERR(TAG, "failed to parse %s", lpFileName);
- return ret;
- }
- length = 0;
- for (index = 0; index < ini->nSections; index++) {
- section = ini->sections[index];
- if(section->invalid) {
- continue;
- }
- validCount++;
- nameLength = (int) strlen(section->name);
- length += (nameLength + 1);
- }
- length++;
- sectionNames = (char*)malloc(length);
- if (!sectionNames)
- return ret;
- p = sectionNames;
- for (index = 0; index < ini->nSections; index++)
- {
- section = ini->sections[index];
- if(section->invalid) {
- continue;
- }
- nameLength = (int) strlen(section->name);
- CopyMemory(p, section->name, nameLength + 1);
- p += (nameLength + 1);
- }
- *p = '\0';
- ret = length;
- if(lpszReturnBuffer != NULL && nSize > 0) {
- if(nSize < length) {
- ret = nSize - 2;
- } else {
- CopyMemory(lpszReturnBuffer, sectionNames, length);
- }
- }
- if(sectionNames != NULL) {
- free(sectionNames);
- }
- IniFile_Free(ini);
- return ret;
- }
- DWORD GetPrivateProfileSectionNamesW(LPWSTR lpszReturnBuffer, DWORD nSize, LPCWSTR lpFileName)
- {
- WLog_ERR(TAG, "%s operation not implemented", __FUNCTION__);
- return FALSE;
- }
- BOOL WritePrivateProfileStringA(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpString, LPCSTR lpFileName)
- {
- wIniFile* ini;
- BOOL ret = TRUE;
- BOOL changed = FALSE;
- ini = IniFile_New();
- if(!ini) {
- return FALSE;
- }
- if(IniFile_ReadFile(ini, lpFileName) < 0 && access(lpFileName, F_OK) == 0) {
- printf("open ini file failed.\n");
- ret = FALSE;
- } else {
- if(lpAppName == NULL)
- {
- //TODO:
- }
- if(lpKeyName == NULL)
- {
- //remove all entries inner section and the section also.
- ret = IniFile_RemoveSection(ini, lpAppName, &changed);
- }
- else if(lpString == NULL)
- {
- //remote the specified entry inner section
- ret = IniFile_RemoveKey(ini, lpAppName, lpKeyName, &changed);
- }
- else
- {
- if (IniFile_SetKeyValueString(ini, lpAppName, lpKeyName, lpString) < 0) {
- ret = FALSE;
- } else
- {
- changed = TRUE;
- }
- }
- }
- finished:
- if(ret && changed)
- {
- if (IniFile_WriteFile(ini, lpFileName) < 0) {
- ret = FALSE;
- }
- }
- IniFile_Free(ini);
- return ret;
- }
- BOOL WritePrivateProfileStringW(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpString, LPCWSTR lpFileName)
- {
- WLog_ERR(TAG, "%s operation not implemented", __FUNCTION__);
- return FALSE;
- }
- BOOL WritePrivateProfileSectionA(LPCSTR lpAppName, LPCSTR lpString, LPCSTR lpFileName)
- {
- wIniFile* ini;
- BOOL ret = TRUE;
- BOOL changed = FALSE;
- wIniFileSection* pSection = NULL;
- wIniFileKey* pKey = NULL;
- char* curToken = NULL;
- char* nextToken = NULL;
- char* newKey = NULL;
- char* newValue = NULL;
- char* newString = NULL;
- char* keyValue = NULL;
- ini = IniFile_New();
- if(!ini) {
- return FALSE;
- }
- if(IniFile_ReadFile(ini, lpFileName) < 0) {
- ret = FALSE;
- } else {
- if(lpAppName != NULL)
- {
- if (lpString == NULL)
- {
- printf("to delete the section...\n");
- ret = IniFile_RemoveSection(ini, lpAppName, &changed);
- }
- else
- {
- pSection = IniFile_GetSection(ini, lpAppName);
- if(!pSection)
- {
- pSection = IniFile_AddSection(ini, lpAppName);
- if(!pSection)
- {
- ret = FALSE;
- goto end;
- }
- changed = TRUE;
- }
- curToken = (char*)&lpString[0];
- while (curToken != NULL && curToken[0] != '\0')
- {
- keyValue = _strdup(curToken);
- if (!keyValue) {
- ret = FALSE;
- goto end;
- }
- newKey = strtok_s(keyValue, "=", &newValue);
- newValue = strtok_s(NULL, "=", &newValue);
- pKey = IniFile_GetKey(ini, pSection, newKey);
- if (pKey == NULL) {
- pKey = IniFile_AddKey(ini, pSection, newKey, newValue, NULL);
- changed = TRUE;
- } else if(_stricmp(pKey->value, newValue) != 0)
- {
- free(pKey->value);
- pKey->value = _strdup(newValue);
- if (!pKey->value)
- {
- free(keyValue);
- ret = FALSE;
- goto end;
- }
- changed = TRUE;
- }
- free(keyValue);
- //curToken = strtok_s(NULL, "\0", &nextToken);
- curToken = curToken + strlen(curToken) + 1;
- }
- }
- }
- }
- if(ret == TRUE && changed) {
- if(IniFile_WriteFile(ini, lpFileName) < 0)
- {
- ret = FALSE;
- }
- }
- end:
- if(newString != NULL)
- {
- free(newString);
- }
- IniFile_Free(ini);
- return ret;
- }
- WINPR_API BOOL WritePrivateProfileSectionW(LPCWSTR lpAppName, LPCWSTR lpString,LPCWSTR lpFileName)
- {
- WLog_ERR(TAG, "%s operation not implemented", __FUNCTION__);
- return FALSE;
- }
- #endif //_WIN32
|