Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
toml: Fixed table writing
Browse files Browse the repository at this point in the history
  • Loading branch information
bauhaus93 committed Nov 26, 2019
1 parent af7c1be commit 6de556a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/plugins/toml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The plugin supports null keys. They are represented as special string of value '
## TODOs
- Document functions
- directory value
- proper sorting -> arrays
- base64 for binary k/v
- check why space restoration in front of comments not working
- Error checks for writing
Expand Down
48 changes: 36 additions & 12 deletions src/plugins/toml/utility.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
#include "utility.h"

#include <kdbhelper.h>
#include <kdbassert.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <string.h>

void dumpKS(KeySet * keys) {
FILE * f = fopen("keys.txt", "w");
void dumpKS (KeySet * keys)
{
FILE * f = fopen ("keys.txt", "w");
ksRewind (keys);
for (Key * key = ksNext (keys); key != NULL; key = ksNext (keys))
{
fprintf (f, "KEY = %s, VALUE = %s\n", keyName (key), keyString (key));
keyRewindMeta (key);
for (const Key * meta = keyNextMeta(key); meta != NULL; meta = keyNextMeta(key)) {
for (const Key * meta = keyNextMeta (key); meta != NULL; meta = keyNextMeta (key))
{
fprintf (f, "\tMETA KEY = %s, VALUE = %s\n", keyName (meta), keyString (meta));
}
}
ksRewind (keys);
}

void dumpMemKS (Key ** keys, size_t size)
{
FILE * f = fopen ("keys_mem.txt", "w");
for (size_t i = 0; i < size; i++)
{
{
Key * key = keys[i];
fprintf (f, "KEY = %s, VALUE = %s\n", keyName (key), keyString (key));
keyRewindMeta (key);
for (const Key * meta = keyNextMeta (key); meta != NULL; meta = keyNextMeta (key))
{
fprintf (f, "\tMETA KEY = %s, VALUE = %s\n", keyName (meta), keyString (meta));
}
}
}
}

Key * keyAppendIndex (size_t index, const Key * parent)
{
// Key * indexKey = keyDup (parent);
Expand Down Expand Up @@ -75,32 +94,37 @@ size_t arrayStringToIndex (const char * indexStr)
return val;
}

bool isArrayIndex(const char * basename) {
if (*basename++ != '#') {
bool isArrayIndex (const char * basename)
{
if (*basename++ != '#')
{
return false;
}
while (*basename == '_') {
while (*basename == '_')
{
basename++;
}
while (*basename >= '0' && *basename <= '9') {
while (*basename >= '0' && *basename <= '9')
{
basename++;
}
return *basename == 0;
}

bool isEmptyArray(Key * key) {
bool isEmptyArray (Key * key)
{
const Key * meta = findMetaKey (key, "array");
ELEKTRA_ASSERT (meta != NULL, "Supplied key must have array meta key, but hadn't");
const char * sizeStr = keyString(meta);
return elektraStrLen(sizeStr) == 1;
const char * sizeStr = keyString (meta);
return elektraStrLen (sizeStr) == 1;
}

size_t getArrayMax (Key * key)
{
const Key * meta = findMetaKey (key, "array");
ELEKTRA_ASSERT (meta != NULL, "Supplied key must have array meta key, but hadn't");

return arrayStringToIndex (keyString(meta));
return arrayStringToIndex (keyString (meta));
}

const Key * findMetaKey (Key * key, const char * metakeyName)
Expand Down
1 change: 1 addition & 0 deletions src/plugins/toml/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdbool.h>

void dumpKS(KeySet * keys);
void dumpMemKS(Key ** keys, size_t size);

Key * keyAppendIndex (size_t index, const Key * parent);
void keyUpdateArrayMetakey (Key * key, size_t newIndex);
Expand Down
89 changes: 70 additions & 19 deletions src/plugins/toml/write.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <kdb.h>
#include <kdbassert.h>
#include <kdbmeta.h>
#include <kdbproposal.h>
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdbmeta.h>
#include <kdbproposal.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -79,6 +79,8 @@ static ArrayInfo * updateArrayInfo (ArrayInfo * root, Key * name, size_t index);
static int keyCmpOrderWrapper (const void * a, const void * b);
static Key * getCurrentKey (Writer * writer);
static Key * getNextKey (Writer * writer);
static Key * getPreviousKey (Writer * writer);
static int keyCmpCustom (const Key * a, const Key * b);

int tomlWrite (KeySet * keys, Key * parent)
{
Expand Down Expand Up @@ -109,9 +111,9 @@ int tomlWrite (KeySet * keys, Key * parent)
}

int result = 0;
if (keyCmp (getCurrentKey(w), parent) == 0)
if (keyCmp (getCurrentKey (w), parent) == 0)
{
getNextKey(w);
getNextKey (w);
}
result |= writeKeys (parent, w);
CommentList * comments = collectComments (parent);
Expand Down Expand Up @@ -190,9 +192,17 @@ static Key * getNextKey (Writer * writer)
return getCurrentKey (writer);
}

static Key * getPreviousKey (Writer * writer)
{
if (writer->cursor > 0) {
writer->cursor--;
}
return getCurrentKey (writer);
}

static int writeKeys (Key * parent, Writer * writer)
{
Key * key = getCurrentKey(writer);
Key * key = getCurrentKey (writer);

int result = 0;

Expand All @@ -205,13 +215,20 @@ static int writeKeys (Key * parent, Writer * writer)
result |= writeNewline (writer);
break;
case KEY_TYPE_SIMPLE_TABLE:
if (keyCmp(parent, writer->rootKey) != 0) {
return result;
}
result |= writeSimpleTable (parent, key, writer);
break;
case KEY_TYPE_TABLE_ARRAY:
if (keyCmp(parent, writer->rootKey) != 0) {
return result;
}
result |= writeTableArray (parent, key, writer);
break;
}
key =getCurrentKey(writer);;
key = getCurrentKey (writer);
;
}
return result;
}
Expand All @@ -233,7 +250,7 @@ static int writeSimpleTable (Key * parent, Key * key, Writer * writer)
{
int result = 0;
result |= writeSimpleTableHeader (parent, key, writer);
getNextKey(writer);
getNextKey (writer);
result |= writeKeys (key, writer);

return result;
Expand All @@ -245,7 +262,7 @@ static int writeTableArray (Key * parent, Key * key, Writer * writer)
Key * arrayRoot = key;
size_t maxIndex = getArrayMax (arrayRoot);
size_t nextIndex = 0;
key = getNextKey(writer);
key = getNextKey (writer);

while (result == 0 && nextIndex <= maxIndex)
{
Expand Down Expand Up @@ -275,13 +292,13 @@ static int writeTableArray (Key * parent, Key * key, Writer * writer)

if (keyCmp (elementRoot, key) == 0) // holds for table array entries with comments
{
getNextKey(writer);
getNextKey (writer);
}
result |= writeKeys (elementRoot, writer);
nextIndex++;

keyDel (elementRoot);
key = getCurrentKey(writer);
key = getCurrentKey (writer);
}
else
{
Expand All @@ -308,7 +325,7 @@ static int writeArrayBody (Key * key, Writer * writer)
static int writeArrayElements (Key * parent, Writer * writer)
{
int result = 0;
Key * key = getNextKey(writer);
Key * key = getNextKey (writer);
while (keyIsDirectlyBelow (parent, key) == 1)
{
CommentList * comments = collectComments (key);
Expand Down Expand Up @@ -343,7 +360,7 @@ static int writeValue (Key * key, Writer * writer)
else
{
result |= writeScalar (key, writer);
getNextKey(writer);
getNextKey (writer);
}
return result;
}
Expand Down Expand Up @@ -392,17 +409,17 @@ static int writeScalar (Key * key, Writer * writer)
keyRewindMeta (key);
const Key * origValue = findMetaKey (key, "origvalue");
const Key * type = findMetaKey (key, "type");
const char * valueStr = keyString(key);
const char * valueStr = keyString (key);
if (origValue != NULL)
{
valueStr = keyString(origValue);
valueStr = keyString (origValue);
}

if (elektraStrLen (valueStr) == 1)
{
result |= fputs ("''", writer->f) == EOF;
}
else if (isBoolean (valueStr) && type != NULL && elektraStrCmp(keyString(type), "boolean") == 0)
else if (isBoolean (valueStr) && type != NULL && elektraStrCmp (keyString (type), "boolean") == 0)
{
if (isTrue (valueStr))
{
Expand Down Expand Up @@ -457,7 +474,7 @@ static int writeInlineTableBody (Key * key, Writer * writer)
static int writeInlineTableElements (Key * parent, Writer * writer)
{
int result = 0;
Key * key = getNextKey(writer);
Key * key = getNextKey (writer);
bool firstElement = true;
while (keyIsBelow (parent, key) == 1)
{
Expand All @@ -470,7 +487,7 @@ static int writeInlineTableElements (Key * parent, Writer * writer)
result |= fputs (", ", writer->f) == EOF;
}
result |= writeAssignment (parent, key, writer);
key = getCurrentKey(writer);
key = getCurrentKey (writer);
}
return result;
}
Expand Down Expand Up @@ -663,6 +680,7 @@ static void freeComments (CommentList * comments)
}
}


static void addMissingArrayKeys (KeySet * keys, Key * parent)
{
ArrayInfo * arrays = NULL;
Expand Down Expand Up @@ -698,6 +716,11 @@ static void addMissingArrayKeys (KeySet * keys, Key * parent)
}
else
{
const Key * meta = findMetaKey (arrayRoot, "array");
if (meta == NULL)
{
keyUpdateArrayMetakey (arrayRoot, arrays->maxIndex);
}
keyDel (arrays->name);
}
ArrayInfo * next = arrays->next;
Expand Down Expand Up @@ -769,7 +792,35 @@ static ArrayInfo * updateArrayInfo (ArrayInfo * root, Key * name, size_t index)
return element;
}

static int keyCmpOrderWrapper (const void * a, const void * b)
static int keyCmpOrderWrapper (const void * va, const void * vb)
{
return elektraKeyCmpOrder (*((const Key **) a), *((const Key **) b));
return elektraKeyCmpOrder (*((const Key **) va), *((const Key **) vb));
}

static int keyCmpCustom (const Key * a, const Key * b)
{
const char * partA = (const char *) keyUnescapedName (a);
const char * partB = (const char *) keyUnescapedName (b);
const char * stopA = ((const char *) partA) + keyGetUnescapedNameSize (a);
const char * stopB = ((const char *) partB) + keyGetUnescapedNameSize (b);
while (partA < stopA && partB < stopB)
{
bool aIndex = isArrayIndex (partA);
bool bIndex = isArrayIndex (partB);
if (aIndex && bIndex)
{
int result = elektraStrCmp (partA, partB);
if (result != 0)
{
return result;
}
}
else if (aIndex != bIndex)
{
return elektraKeyCmpOrder (a, b);
}
partA += elektraStrLen (partA);
partB += elektraStrLen (partB);
}
return elektraKeyCmpOrder (a, b);
}

0 comments on commit 6de556a

Please sign in to comment.