mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2024-12-14 10:13:05 +02:00
Add iniSectionList() to Ini object and remove dead code.
Contributed by Cynthia Shang.
This commit is contained in:
parent
0e6b927a17
commit
4e38cbaea9
@ -32,6 +32,14 @@
|
||||
|
||||
<p>Add <code>uint64</code> variant type and supporting conversion functions.</p>
|
||||
</release-item>
|
||||
|
||||
<release-item>
|
||||
<release-item-contributor-list>
|
||||
<release-item-contributor id="shang.cynthia"/>
|
||||
</release-item-contributor-list>
|
||||
|
||||
<p>Add <code>iniSectionList()</code> to <code>Ini</code> object and remove dead code.</p>
|
||||
</release-item>
|
||||
</release-development-list>
|
||||
</release-core-list>
|
||||
|
||||
|
@ -18,11 +18,10 @@ struct Ini
|
||||
{
|
||||
MemContext *memContext; // Context that contains the ini
|
||||
KeyValue *store; // Key value store that contains the ini data
|
||||
String *fileName; // File name (if one has been set)
|
||||
};
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Create a new string from a zero-terminated string
|
||||
Create a new Ini object
|
||||
***********************************************************************************************************************************/
|
||||
Ini *
|
||||
iniNew()
|
||||
@ -99,8 +98,6 @@ iniGet(const Ini *this, const String *section, const String *key)
|
||||
if (result == NULL)
|
||||
THROW_FMT(FormatError, "section '%s', key '%s' does not exist", strPtr(section), strPtr(key));
|
||||
|
||||
return result;
|
||||
|
||||
FUNCTION_TEST_RESULT(CONST_VARIANT, result);
|
||||
}
|
||||
|
||||
@ -152,7 +149,7 @@ iniSectionKeyList(const Ini *this, const String *section)
|
||||
// Get the section
|
||||
KeyValue *sectionKv = varKv(kvGet(this->store, varNewStr(section)));
|
||||
|
||||
// Return key list of the section exists
|
||||
// Return key list if the section exists
|
||||
if (sectionKv != NULL)
|
||||
result = strLstNewVarLst(kvKeyList(sectionKv));
|
||||
// Otherwise return an empty list
|
||||
@ -166,6 +163,32 @@ iniSectionKeyList(const Ini *this, const String *section)
|
||||
FUNCTION_TEST_RESULT(STRING_LIST, result);
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Get a list of sections
|
||||
***********************************************************************************************************************************/
|
||||
StringList *
|
||||
iniSectionList(const Ini *this)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(INI, this);
|
||||
|
||||
FUNCTION_TEST_ASSERT(this != NULL);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
StringList *result = NULL;
|
||||
|
||||
MEM_CONTEXT_TEMP_BEGIN()
|
||||
{
|
||||
// Get the sections from the keyList
|
||||
result = strLstNewVarLst(kvKeyList(this->store));
|
||||
|
||||
strLstMove(result, MEM_CONTEXT_OLD());
|
||||
}
|
||||
MEM_CONTEXT_TEMP_END();
|
||||
|
||||
FUNCTION_TEST_RESULT(STRING_LIST, result);
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Parse ini from a string
|
||||
***********************************************************************************************************************************/
|
||||
@ -248,36 +271,6 @@ iniParse(Ini *this, const String *content)
|
||||
FUNCTION_TEST_RESULT_VOID();
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Load ini from a file
|
||||
***********************************************************************************************************************************/
|
||||
void
|
||||
iniLoad(Ini *this, const String *fileName)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(INI, this);
|
||||
FUNCTION_TEST_PARAM(STRING, fileName);
|
||||
|
||||
FUNCTION_TEST_ASSERT(this != NULL);
|
||||
FUNCTION_TEST_ASSERT(fileName != NULL);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
MEM_CONTEXT_BEGIN(this->memContext)
|
||||
{
|
||||
// Set the filename
|
||||
this->fileName = strDup(fileName);
|
||||
|
||||
MEM_CONTEXT_TEMP_BEGIN()
|
||||
{
|
||||
iniParse(this, strNewBuf(storageGetNP(storageNewReadNP(storageLocal(), this->fileName))));
|
||||
}
|
||||
MEM_CONTEXT_TEMP_END();
|
||||
}
|
||||
MEM_CONTEXT_END()
|
||||
|
||||
FUNCTION_TEST_RESULT_VOID();
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Set an ini value
|
||||
***********************************************************************************************************************************/
|
||||
|
@ -18,11 +18,12 @@ Ini *iniNew();
|
||||
const Variant *iniGet(const Ini *this, const String *section, const String *key);
|
||||
const Variant *iniGetDefault(const Ini *this, const String *section, const String *key, Variant *defaultValue);
|
||||
StringList *iniSectionKeyList(const Ini *this, const String *section);
|
||||
StringList *iniSectionList(const Ini *this);
|
||||
void iniParse(Ini *this, const String *content);
|
||||
void iniLoad(Ini *this, const String *fileName);
|
||||
void iniSet(Ini *this, const String *section, const String *key, const Variant *value);
|
||||
|
||||
void iniFree(Ini *this);
|
||||
String *iniFileName(const Ini *this);
|
||||
bool iniFileExists(const Ini *this);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Macros for function logging
|
||||
|
@ -23,7 +23,7 @@ testRun()
|
||||
}
|
||||
|
||||
// *****************************************************************************************************************************
|
||||
if (testBegin("iniSet(), iniGet(), iniGetDefault(), and iniSectionKeyList()"))
|
||||
if (testBegin("iniSet(), iniGet(), iniGetDefault(), iniSectionList(), and iniSectionKeyList()"))
|
||||
{
|
||||
Ini *ini = NULL;
|
||||
|
||||
@ -44,11 +44,15 @@ testRun()
|
||||
TEST_RESULT_INT(strLstSize(iniSectionKeyList(ini, strNew("bogus"))), 0, "get keys for missing section");
|
||||
TEST_RESULT_STR(strPtr(strLstJoin(iniSectionKeyList(ini, strNew("section1")), "|")), "key1|key2", "get keys for section");
|
||||
|
||||
TEST_RESULT_VOID(iniSet(ini, strNew("section2"), strNew("key2"), varNewInt(2)), "set section2, key, int");
|
||||
TEST_RESULT_INT(strLstSize(iniSectionList(ini)), 2, "number of sections");
|
||||
TEST_RESULT_STR(strPtr(strLstJoin(iniSectionList(ini), "|")), "section1|section2", "get sections");
|
||||
|
||||
TEST_RESULT_VOID(iniFree(ini), "free ini");
|
||||
}
|
||||
|
||||
// *****************************************************************************************************************************
|
||||
if (testBegin("iniParse() and iniLoad()"))
|
||||
if (testBegin("iniParse()"))
|
||||
{
|
||||
Ini *ini = NULL;
|
||||
String *content = NULL;
|
||||
@ -66,6 +70,7 @@ testRun()
|
||||
|
||||
content = strNew
|
||||
(
|
||||
"# Comment\n"
|
||||
"[global] \n"
|
||||
"compress=y \n"
|
||||
"\n"
|
||||
@ -77,27 +82,6 @@ testRun()
|
||||
|
||||
TEST_RESULT_STR(strPtr(varStr(iniGet(ini, strNew("global"), strNew("compress")))), "y", "get compress");
|
||||
TEST_RESULT_STR(strPtr(varStr(iniGet(ini, strNew("db"), strNew("pg1-path")))), "/path/to/pg", "get pg1-path");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TEST_ASSIGN(ini, iniNew(), "new ini");
|
||||
String *fileName = strNewFmt("%s/test.ini", testPath());
|
||||
|
||||
content = strNew
|
||||
(
|
||||
"# Comment\n"
|
||||
" [global]\n"
|
||||
" \n"
|
||||
" compress= y \n"
|
||||
"[db]\t\r\n"
|
||||
" pg1-path =/path/to/pg\n"
|
||||
"\n"
|
||||
);
|
||||
|
||||
TEST_RESULT_VOID(storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), bufNewStr(content)), "put ini to file");
|
||||
TEST_RESULT_VOID(iniLoad(ini, fileName), "load ini from file");
|
||||
|
||||
TEST_RESULT_STR(strPtr(varStr(iniGet(ini, strNew("global"), strNew("compress")))), "y", "get compress");
|
||||
TEST_RESULT_STR(strPtr(varStr(iniGet(ini, strNew("db"), strNew("pg1-path")))), "/path/to/pg", "get pg1-path");
|
||||
}
|
||||
|
||||
FUNCTION_HARNESS_RESULT_VOID();
|
||||
|
Loading…
Reference in New Issue
Block a user