Skip to content
Permalink
Browse files

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.
  • Loading branch information...
dwsteele committed Apr 23, 2019
1 parent c650134 commit cddb0c05b444aaed388e53e0cd0b02148307ca87
Showing with 113 additions and 9 deletions.
  1. +4 −0 doc/xml/release.xml
  2. +69 −0 src/common/ini.c
  3. +2 −5 src/common/ini.h
  4. +1 −1 test/define.yaml
  5. +37 −3 test/src/module/common/iniTest.c
@@ -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>
@@ -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
***********************************************************************************************************************************/
@@ -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
***********************************************************************************************************************************/
@@ -284,7 +284,7 @@ unit:

# ----------------------------------------------------------------------------------------------------------------------------
- name: ini
total: 3
total: 4

coverage:
common/ini: full
@@ -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();
}

0 comments on commit cddb0c0

Please sign in to comment.
You can’t perform that action at this time.