forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment_list.c
168 lines (153 loc) · 3.53 KB
/
comment_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @file comment_list.c
*
* @brief Functions for handling the CommentList struct.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "comment_list.h"
#include <kdb.h>
#include <kdbassert.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "utility.h"
static int keyAddComment (Key * key, const char * commentStr, const char * origStr, size_t index);
CommentList * commentListNew (const char * comment, const char * orig)
{
CommentList * newComment = elektraCalloc (sizeof (CommentList));
if (newComment == NULL)
{
return NULL;
}
if (comment != NULL)
{
newComment->str = strdup (comment);
}
if (orig != NULL)
{
newComment->orig = strdup (orig);
}
return newComment;
}
void commentListFree (CommentList * root)
{
while (root != NULL)
{
CommentList * nextComment = root->next;
if (root->str != NULL)
{
elektraFree (root->str);
}
if (root->orig != NULL)
{
elektraFree (root->orig);
}
elektraFree (root);
root = nextComment;
}
}
CommentList * commentListAdd (CommentList * back, const char * comment, const char * orig)
{
ELEKTRA_ASSERT (back != NULL, "Back expected to be non-NULL, but was NULL");
ELEKTRA_ASSERT (back->next == NULL, "Back->next expected to be NULL, but was not NULL");
back->next = commentListNew (comment, orig);
return back->next;
}
CommentList * commentListAddNewlines (CommentList * back, size_t newlineCount)
{
ELEKTRA_ASSERT (back != NULL, "Back expected to be non-NULL, but was NULL");
CommentList * newBack = back;
while (newlineCount > 0)
{
newBack = commentListAdd (newBack, NULL, 0);
if (newBack == NULL)
{
return NULL;
}
newlineCount--;
}
return newBack;
}
int keyAddCommentList (Key * key, CommentList * root)
{
size_t index = 1;
int err = 0;
while (root != NULL && err == 0)
{
err = keyAddComment (key, root->str, root->orig, index++);
root = root->next;
}
return err;
}
int keyAddInlineComment (Key * key, CommentList * root)
{
if (root->next != NULL)
{
return ERROR_INTERNAL;
}
return keyAddComment (key, root->str, root->orig, 0);
}
static int keyAddComment (Key * key, const char * commentStr, const char * origStr, size_t index)
{
// add comment str
char * indexStr = indexToArrayString (index);
if (indexStr == NULL)
{
return ERROR_MEMORY;
}
size_t metaLen = elektraStrLen (indexStr) + 8;
char * metaName = (char *) elektraCalloc (sizeof (char) * metaLen);
if (metaName == NULL)
{
elektraFree (indexStr);
return ERROR_MEMORY;
}
snprintf (metaName, metaLen, "comment/%s", indexStr);
elektraFree (indexStr);
if (commentStr != NULL)
{
keySetMeta (key, metaName, commentStr);
}
// add start symbol
size_t metaInfoLen = metaLen + 6;
char * metaInfoName = (char *) elektraCalloc (sizeof (char) * metaInfoLen);
if (metaInfoName == NULL)
{
elektraFree (metaName);
return ERROR_MEMORY;
}
snprintf (metaInfoName, metaInfoLen, "%s/start", metaName);
if (commentStr != NULL)
{
keySetMeta (key, metaInfoName, "#");
}
else
{
keySetMeta (key, metaInfoName, "");
}
// add preceding whitespace
snprintf (metaInfoName, metaInfoLen, "%s/space", metaName);
if (commentStr != NULL)
{
size_t len = strspn (origStr, " \t");
char * startSeq = elektraMemDup (origStr, len + 1);
startSeq[len] = '\0';
if (startSeq == NULL)
{
return ERROR_MEMORY;
}
keySetMeta (key, metaInfoName, startSeq);
elektraFree (startSeq);
}
else
{
keySetMeta (key, metaInfoName, "");
}
elektraFree (metaInfoName);
elektraFree (metaName);
return 0;
}