1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Add iniSave() and iniMove() to Ini object.

iniSave() sorts alphabetically to maintain compatibility with the expect tests, but we plan to change this behavior when the migration is complete.
This commit is contained in:
David Steele 2019-04-23 13:03:22 -04:00
parent c650134a04
commit cddb0c05b4
5 changed files with 113 additions and 9 deletions

View File

@ -41,6 +41,10 @@
<p>Add <code>unsigned int</code> <code>Variant</code> type and update code to use it.</p>
</release-item>
<release-item>
<p>Add <code>iniSave()</code> and <code>iniMove()</code> to <code>Ini</code> object.</p>
</release-item>
<release-item>
<p><code>varNewKv()</code> accepts a <code>KeyValue</code> object rather than creating one.</p>
</release-item>

View File

@ -337,6 +337,75 @@ iniSet(Ini *this, const String *section, const String *key, const String *value)
FUNCTION_TEST_RETURN_VOID();
}
/***********************************************************************************************************************************
Save the ini file
***********************************************************************************************************************************/
void
iniSave(Ini *this, IoWrite *write)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(INI, this);
FUNCTION_TEST_PARAM(IO_WRITE, write);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(write != NULL);
MEM_CONTEXT_TEMP_BEGIN()
{
ioWriteOpen(write);
StringList *sectionList = strLstSort(iniSectionList(this), sortOrderAsc);
for (unsigned int sectionIdx = 0; sectionIdx < strLstSize(sectionList); sectionIdx++)
{
const String *section = strLstGet(sectionList, sectionIdx);
if (sectionIdx != 0)
ioWrite(write, LF_BUF);
ioWrite(write, BRACKETL_BUF);
ioWriteStr(write, section);
ioWriteLine(write, BRACKETR_BUF);
StringList *keyList = strLstSort(iniSectionKeyList(this, section), sortOrderAsc);
for (unsigned int keyIdx = 0; keyIdx < strLstSize(keyList); keyIdx++)
{
const String *key = strLstGet(keyList, keyIdx);
ioWriteStr(write, key);
ioWrite(write, EQ_BUF);
ioWriteStrLine(write, iniGet(this, section, key));
}
}
ioWriteClose(write);
}
MEM_CONTEXT_TEMP_END();
FUNCTION_TEST_RETURN_VOID();
}
/***********************************************************************************************************************************
Move to a new mem context
***********************************************************************************************************************************/
Ini *
iniMove(Ini *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(INI, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Free the ini
***********************************************************************************************************************************/

View File

@ -20,7 +20,9 @@ Ini *iniNew(void);
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
Ini *iniMove(Ini *this, MemContext *parentNew);
void iniParse(Ini *this, const String *content);
void iniSave(Ini *this, IoWrite *write);
void iniSet(Ini *this, const String *section, const String *key, const String *value);
/***********************************************************************************************************************************
@ -35,11 +37,6 @@ StringList *iniSectionList(const Ini *this);
String *iniFileName(const Ini *this);
bool iniFileExists(const Ini *this);
/***********************************************************************************************************************************
Constructor
***********************************************************************************************************************************/
void iniFree(Ini *this);
/***********************************************************************************************************************************
Destructor
***********************************************************************************************************************************/

View File

@ -284,7 +284,7 @@ unit:
# ----------------------------------------------------------------------------------------------------------------------------
- name: ini
total: 3
total: 4
coverage:
common/ini: full

View File

@ -1,6 +1,7 @@
/***********************************************************************************************************************************
Test Ini
***********************************************************************************************************************************/
#include "storage/driver/posix/storage.h"
/***********************************************************************************************************************************
Test Run
@ -10,6 +11,9 @@ testRun(void)
{
FUNCTION_HARNESS_VOID();
Storage *storageTest = storageDriverPosixInterface(
storageDriverPosixNew(strNew(testPath()), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL));
// *****************************************************************************************************************************
if (testBegin("iniNew() and iniFree()"))
{
@ -27,10 +31,18 @@ testRun(void)
{
Ini *ini = NULL;
TEST_ASSIGN(ini, iniNew(), "new ini");
MEM_CONTEXT_TEMP_BEGIN()
{
TEST_ASSIGN(ini, iniNew(), "new ini");
TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key1"), strNew("11")), "set section, key");
TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key2"), strNew("1.234")), "set section, key");
TEST_RESULT_VOID(iniMove(ini, MEM_CONTEXT_OLD()), "move ini");
TEST_RESULT_VOID(iniMove(NULL, MEM_CONTEXT_OLD()), "move null ini");
}
MEM_CONTEXT_TEMP_END();
TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key1"), strNew("11")), "set section, key");
TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key2"), strNew("1.234")), "set section, key");
TEST_RESULT_STR(strPtr(iniGet(ini, strNew("section1"), strNew("key1"))), "11", "get section, key");
TEST_RESULT_STR(strPtr(iniGet(ini, strNew("section1"), strNew("key2"))), "1.234", "get section, key");
@ -89,5 +101,27 @@ testRun(void)
TEST_RESULT_STR(strPtr(iniGet(ini, strNew("db"), strNew("pg1-path"))), "/path/to/pg", "get pg1-path");
}
// *****************************************************************************************************************************
if (testBegin("iniSave()"))
{
Ini *ini = iniNew();
iniSet(ini, strNew("section2"), strNew("key1"), strNew("value1"));
iniSet(ini, strNew("section1"), strNew("key2"), strNew("value2"));
iniSet(ini, strNew("section1"), strNew("key1"), strNew("value1"));
StorageFileWrite *write = storageNewWriteNP(storageTest, strNew("test.ini"));
TEST_RESULT_VOID(iniSave(ini, storageFileWriteIo(write)), "save ini");
TEST_RESULT_STR(
strPtr(strNewBuf(storageGetNP(storageNewReadNP(storageTest, strNew("test.ini"))))),
"[section1]\n"
"key1=value1\n"
"key2=value2\n"
"\n"
"[section2]\n"
"key1=value1\n",
"check ini");
}
FUNCTION_HARNESS_RESULT_VOID();
}