Skip to content

Commit

Permalink
fixed flat hierachy
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-wa committed Oct 26, 2015
1 parent 17835a8 commit f908e67
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/plugins/ini/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,33 +303,21 @@ static short isSectionKey(Key *key)
*/
static char *getIniName(KeySet *keys, Key *parent, Key *key)

This comment has been minimized.

Copy link
@fberlakovich

fberlakovich Oct 28, 2015

As the parameter called parent is now actually the used section, I would rename it.

{
cursor_t currentCursor = ksGetCursor(keys);

Key *temp = keyDup(key);
Key *section;

do
if(!strcmp(keyName(parent), keyName(key)))
return strdup(keyBaseName(key));
char *buffer = malloc(strlen(keyName(key)) - strlen(keyName(parent)));
//strncpy(buffer, keyName(key)+strlen(keyName(parent))+1, strlen(keyName(key)) - strlen(keyName(parent)));
char *dest = buffer;
for(char *ptr = keyName(key)+strlen(keyName(parent))+1; *ptr; ++ptr)
{
keySetBaseName(temp, 0);

if (!keyCmp(temp, parent))
if(*ptr != '\\')
{
/* we reached the parent key, there won't be any more section keys */
section = parent;
break;
*dest = *ptr;
++dest;
}

section = ksLookup(keys, temp, KDB_O_NONE);
} while(!isSectionKey(section));

ksSetCursor(keys, currentCursor);
keyDel(temp);

char *buffer = elektraMalloc(keyGetNameSize(key));
elektraUnescapeKeyName(keyName(key) + keyGetNameSize(section), buffer);

}
*dest = 0;
return buffer;

}

static Key *generateSectionKey(Key *key, Key *parentKey)
Expand Down Expand Up @@ -363,6 +351,7 @@ int elektraIniSet(Plugin *handle, KeySet *returned, Key *parentKey)

ksRewind (returned);
Key *current;
Key *sectionKey = NULL;
while ((current = ksNext (returned)))
{
if (pluginConfig->autoSections && !keyIsDirectBelow(parentKey, current))
Expand All @@ -383,12 +372,24 @@ int elektraIniSet(Plugin *handle, KeySet *returned, Key *parentKey)
}

if (!strcmp (keyName(current), keyName(parentKey))) continue;

if(keyIsDirectBelow(parentKey, current))
{
if(*(keyString(current)) == NULL)
{
keyDel(sectionKey);
sectionKey = keyDup(current);
keySetBinary(sectionKey, 0, 0);
current = sectionKey;
}
}
writeComments (current, fh);

/* find the section the current key belongs to */
char *iniName = getIniName(returned, parentKey, current);

char *iniName;
if(sectionKey)
iniName = getIniName(returned, sectionKey, current);
else
iniName = getIniName(returned, parentKey, current);
/* keys with a NULL value are treated as sections */
if (isSectionKey(current))
{
Expand All @@ -399,9 +400,8 @@ int elektraIniSet(Plugin *handle, KeySet *returned, Key *parentKey)
/* if the key value is only single line, write a singleline INI key */
if(keyGetMeta(current, "ini/empty"))
{
printf("writing empty key\n");
fprintf(fh, "%s\n", iniName);

}
else if (strstr (keyString (current), "\n") == 0)
{
Expand All @@ -426,6 +426,8 @@ int elektraIniSet(Plugin *handle, KeySet *returned, Key *parentKey)
elektraFree(iniName);
if (ret < 0) break;
}
if(sectionKey)
keyDel(sectionKey);

fclose (fh);

Expand Down

1 comment on commit f908e67

@fberlakovich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a very nice and clean solution to the problem described in ElektraInitiative#138. I am not sure whether the algorithm works for all thinkable corner cases. However, it does for all we can think of up to now and for that reason it is definitively a better solution than the current one :).

Please sign in to comment.