diff --git a/winpr/include/winpr/ini.h b/winpr/include/winpr/ini.h index 963fdcc4f..a32fc41d5 100644 --- a/winpr/include/winpr/ini.h +++ b/winpr/include/winpr/ini.h @@ -30,27 +30,123 @@ extern "C" { #endif + /** @brief read an ini file from a buffer + * + * @param ini The instance to use, must not be \b NULL + * @param buffer The buffer to read from, must be a '\0' terminated string. + * + * @return > 0 for success, < 0 for failure + */ WINPR_API int IniFile_ReadBuffer(wIniFile* ini, const char* buffer); + + /** @brief read an ini file from a file + * + * @param ini The instance to use, must not be \b NULL + * @param filename The name of the file to read from, must be a '\0' terminated string. + * + * @return > 0 for success, < 0 for failure + */ WINPR_API int IniFile_ReadFile(wIniFile* ini, const char* filename); + /** @brief write an ini instance to a buffer + * + * @param ini The instance to use, must not be \b NULL + * + * @return A newly allocated string, use \b free after use. \b NULL in case of failure + */ WINPR_API char* IniFile_WriteBuffer(wIniFile* ini); + + /** @brief write an ini instance to a file + * + * @param ini The instance to use, must not be \b NULL + * @param filename The name of the file as '\0' terminated string. + * + * @return > 0 for success, < 0 for failure + */ WINPR_API int IniFile_WriteFile(wIniFile* ini, const char* filename); - WINPR_API char** IniFile_GetSectionNames(wIniFile* ini, int* count); - WINPR_API char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count); + /** @brief Get the number and names of sections in the ini instance + * + * @param ini The instance to use, must not be \b NULL + * @param count A buffer that will contain the number of sections + * + * @return A newly allocated array of strings (size \b count). Use \b free after use + */ + WINPR_API char** IniFile_GetSectionNames(wIniFile* ini, size_t* count); + /** @brief Get the number and names of keys of a section in the ini instance + * + * @param ini The instance to use, must not be \b NULL + * @param section The name of the section as '\0' terminated string. + * @param count A buffer that will contain the number of sections + * + * @return A newly allocated array of strings (size \b count). Use \b free after use + */ + WINPR_API char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, size_t* count); + + /** @brief Get an ini [section/key] value of type string + * + * @param ini The instance to use, must not be \b NULL + * @param section The name of the section as '\0' terminated string. + * @param key The name of the key as '\0' terminated string. + * + * @return The value of the [section/key] as '\0' terminated string or \b NULL + */ WINPR_API const char* IniFile_GetKeyValueString(wIniFile* ini, const char* section, const char* key); + + /** @brief Get an ini [section/key] value of type int + * + * @param ini The instance to use, must not be \b NULL + * @param section The name of the section as '\0' terminated string. + * @param key The name of the key as '\0' terminated string. + * + * @return The value of the [section/key] + */ WINPR_API int IniFile_GetKeyValueInt(wIniFile* ini, const char* section, const char* key); + /** @brief Set an ini [section/key] value of type string + * + * @param ini The instance to use, must not be \b NULL + * @param section The name of the section as '\0' terminated string. + * @param key The name of the key as '\0' terminated string. + * @param value The value of the [section/key] as '\0' terminated string. + * + * @return > 0 for success, < 0 for failure + */ WINPR_API int IniFile_SetKeyValueString(wIniFile* ini, const char* section, const char* key, const char* value); + + /** @brief Set an ini [section/key] value of type int + * + * @param ini The instance to use, must not be \b NULL + * @param section The name of the section as '\0' terminated string. + * @param key The name of the key as '\0' terminated string. + * @param value The value of the [section/key] + * + * @return > 0 for success, < 0 for failure + */ WINPR_API int IniFile_SetKeyValueInt(wIniFile* ini, const char* section, const char* key, int value); + /** @brief Create a new ini instance + * + * @return The newly allocated instance or \b NULL if failed. + */ WINPR_API wIniFile* IniFile_New(void); + + /** @brief Free a ini instance + * + * @param ini The instance to free, may be \b NULL + */ WINPR_API void IniFile_Free(wIniFile* ini); + /** @brief Clone a ini instance + * + * @param ini The instance to free, may be \b NULL + * + * @return the cloned instance or \b NULL in case of \b ini was \b NULL or failure + */ WINPR_API wIniFile* IniFile_Clone(const wIniFile* ini); #ifdef __cplusplus diff --git a/winpr/libwinpr/utils/ini.c b/winpr/libwinpr/utils/ini.c index 0c0078a19..57c3e2770 100644 --- a/winpr/libwinpr/utils/ini.c +++ b/winpr/libwinpr/utils/ini.c @@ -535,15 +535,8 @@ int IniFile_ReadFile(wIniFile* ini, const char* filename) return IniFile_Load(ini); } -char** IniFile_GetSectionNames(wIniFile* ini, int* count) +char** IniFile_GetSectionNames(wIniFile* ini, size_t* count) { - char* p; - size_t index; - size_t length; - size_t nameLength; - char** sectionNames; - wIniFileSection* section = NULL; - WINPR_ASSERT(ini); if (!count) @@ -552,52 +545,44 @@ char** IniFile_GetSectionNames(wIniFile* ini, int* count) if (ini->nSections > INT_MAX) return NULL; - length = (sizeof(char*) * ini->nSections) + sizeof(char); + size_t length = (sizeof(char*) * ini->nSections) + sizeof(char); - for (index = 0; index < ini->nSections; index++) + for (size_t index = 0; index < ini->nSections; index++) { - section = ini->sections[index]; - nameLength = strlen(section->name); + wIniFileSection* section = ini->sections[index]; + const size_t nameLength = strlen(section->name); length += (nameLength + 1); } - sectionNames = (char**)calloc(length, sizeof(char*)); + char** sectionNames = (char**)calloc(length, sizeof(char*)); if (!sectionNames) return NULL; - p = (char*)&((BYTE*)sectionNames)[sizeof(char*) * ini->nSections]; + char* p = (char*)&((BYTE*)sectionNames)[sizeof(char*) * ini->nSections]; - for (index = 0; index < ini->nSections; index++) + for (size_t index = 0; index < ini->nSections; index++) { sectionNames[index] = p; - section = ini->sections[index]; - nameLength = strlen(section->name); + wIniFileSection* section = ini->sections[index]; + const size_t nameLength = strlen(section->name); CopyMemory(p, section->name, nameLength + 1); p += (nameLength + 1); } *p = '\0'; - *count = (int)ini->nSections; + *count = ini->nSections; return sectionNames; } -char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count) +char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, size_t* count) { - char* p; - size_t index; - size_t length; - size_t nameLength; - char** keyNames; - wIniFileKey* pKey = NULL; - wIniFileSection* pSection = NULL; - WINPR_ASSERT(ini); if (!section || !count) return NULL; - pSection = IniFile_GetSection(ini, section); + wIniFileSection* pSection = IniFile_GetSection(ini, section); if (!pSection) return NULL; @@ -605,33 +590,33 @@ char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count if (pSection->nKeys > INT_MAX) return NULL; - length = (sizeof(char*) * pSection->nKeys) + sizeof(char); + size_t length = (sizeof(char*) * pSection->nKeys) + sizeof(char); - for (index = 0; index < pSection->nKeys; index++) + for (size_t index = 0; index < pSection->nKeys; index++) { - pKey = pSection->keys[index]; - nameLength = strlen(pKey->name); + wIniFileKey* pKey = pSection->keys[index]; + const size_t nameLength = strlen(pKey->name); length += (nameLength + 1); } - keyNames = (char**)calloc(length, sizeof(char*)); + char** keyNames = (char**)calloc(length, sizeof(char*)); if (!keyNames) return NULL; - p = (char*)&((BYTE*)keyNames)[sizeof(char*) * pSection->nKeys]; + char* p = (char*)&((BYTE*)keyNames)[sizeof(char*) * pSection->nKeys]; - for (index = 0; index < pSection->nKeys; index++) + for (size_t index = 0; index < pSection->nKeys; index++) { keyNames[index] = p; - pKey = pSection->keys[index]; - nameLength = strlen(pKey->name); + wIniFileKey* pKey = pSection->keys[index]; + const size_t nameLength = strlen(pKey->name); CopyMemory(p, pKey->name, nameLength + 1); p += (nameLength + 1); } *p = '\0'; - *count = (int)pSection->nKeys; + *count = pSection->nKeys; return keyNames; } diff --git a/winpr/libwinpr/utils/test/TestIni.c b/winpr/libwinpr/utils/test/TestIni.c index 8d68647ee..ed750a421 100644 --- a/winpr/libwinpr/utils/test/TestIni.c +++ b/winpr/libwinpr/utils/test/TestIni.c @@ -39,9 +39,8 @@ static const char TEST_INI_03[] = "[FreeRDS]\n" int TestIni(int argc, char* argv[]) { int rc = -1; - int i, j; - int nKeys; - int nSections; + size_t nKeys; + size_t nSections; UINT32 iValue; wIniFile* ini = NULL; const char* sValue; @@ -64,14 +63,14 @@ int TestIni(int argc, char* argv[]) if (!sectionNames && (nSections > 0)) goto fail; - for (i = 0; i < nSections; i++) + for (size_t i = 0; i < nSections; i++) { free(keyNames); keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys); printf("[%s]\n", sectionNames[i]); if (!keyNames && (nKeys > 0)) goto fail; - for (j = 0; j < nKeys; j++) + for (size_t j = 0; j < nKeys; j++) { sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]); printf("%s = %s\n", keyNames[j], sValue); @@ -130,7 +129,7 @@ int TestIni(int argc, char* argv[]) if (!sectionNames && (nSections > 0)) goto fail; - for (i = 0; i < nSections; i++) + for (size_t i = 0; i < nSections; i++) { free(keyNames); keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys); @@ -138,7 +137,7 @@ int TestIni(int argc, char* argv[]) if (!keyNames && (nKeys > 0)) goto fail; - for (j = 0; j < nKeys; j++) + for (size_t j = 0; j < nKeys; j++) { sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]); printf("%s = %s\n", keyNames[j], sValue);