Browse Source

Fix config and file modules, add debug output

Ilja Kartašov 8 months ago
parent
commit
2f5c10cf5d
5 changed files with 80 additions and 50 deletions
  1. 64
    39
      config.c
  2. 11
    8
      config.h
  3. 2
    2
      file.c
  4. 1
    0
      file.h
  5. 2
    1
      retcode.h

+ 64
- 39
config.c View File

@@ -12,15 +12,20 @@
12 12
  *
13 13
  * @see https://lowenware.com/
14 14
  */
15
+#include <stdio.h>
16
+#include <stdlib.h>
17
+#include <string.h>
18
+#include <errno.h>
19
+#include <ctype.h>
15 20
 #include <stdbool.h>
16 21
 #include <cStuff/file.h>
17 22
 #include <cStuff/config.h>
18 23
 
19 24
 static const char
20 25
 	m_mixed_line_indent[] = "Line indent contains tabs and spaces"
21
-	, m_mixed_block_indent[] = "Block was initiated with different indent"
22 26
 	, m_incomplete_key[] = "Incomplete key"
23 27
 	, m_parser_error[] = "Parser error"
28
+	, m_indent_error[] = "Indent error"
24 29
 	, m_out_of_memory[] = "Out of memory"
25 30
 ;
26 31
 
@@ -29,7 +34,7 @@ struct config_path {
29 34
 	int size;
30 35
 	int indent;
31 36
 	int level;
32
-	bool is_block;
37
+	bool is_node;
33 38
 };
34 39
 
35 40
 struct config_ctx {
@@ -50,13 +55,12 @@ config_get_indent(char *line)
50 55
 		if (c == ' ' || c == '\t') {
51 56
 			if (!chr) {
52 57
 				chr = c;
58
+			}
59
+			if (chr == c) {
60
+				result++;
53 61
 			} else {
54
-				if (chr == c) {
55
-					result++;
56
-				} else {
57
-					/* mixed indent */
58
-					return -1;
59
-				}
62
+				/* mixed indent */
63
+				return -1;
60 64
 			}
61 65
 		} else {
62 66
 			break;
@@ -73,30 +77,35 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
73 77
 {
74 78
 	const char *err = NULL;
75 79
 	CStuffConfigEvent evt = CSTUFF_CONFIG_NONE;
76
-	ssize_t i;
77
-	int k_len = 0, eq = -1, v_len = 0, last_ch = -1, tab = 0;
78
-	char c, *key, *val = NULL; 
80
+	ssize_t i = 0;
81
+	int k_len = 0, v_len = 0, indent = 0;
82
+	char c, *key = NULL, *val = NULL; 
79 83
 
80 84
 	cfg->line_num++;
85
+	 fprintf(stderr, "cfg:%d ", cfg->line_num);
81 86
 
82 87
 	if ((indent = config_get_indent(line)) != -1) {
88
+	  fprintf(stderr, "indent=%d ", indent);
83 89
 		key = &line[indent];
84 90
 
85 91
 		for (i = indent; i < length; i++) {
86 92
 			c = line[i];
87
-			if (islanum(c) || c == '_' || c == '-') {
93
+			if (isalnum(c) || c == '_' || c == '-') {
94
+				k_len++;
88 95
 				continue;
89 96
 			}
90
-			k_len++;
97
+			break;
91 98
 		}
92 99
 
93 100
 		while (i < length) {
94 101
 			switch ((c = line[i++])) {
95 102
 			case ':':
96 103
 				evt = CSTUFF_CONFIG_NODE;
104
+	  		fprintf(stderr, "node! ");
97 105
 				continue;
98 106
 			case '=':
99 107
 				evt = CSTUFF_CONFIG_PAIR;
108
+	  		fprintf(stderr, "pair! ");
100 109
 				continue;
101 110
 			case ' ':
102 111
 			case '\t':
@@ -114,8 +123,7 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
114 123
 				err = m_incomplete_key;
115 124
 			}
116 125
 		} else if (evt == CSTUFF_CONFIG_PAIR) {
117
-			int l = i;
118
-			val = &line[i];
126
+			val = &line[i - 1];
119 127
 			while (i < length) {
120 128
 				switch (line[i++]) {
121 129
 				case ';':
@@ -125,7 +133,7 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
125 133
 				case '\t':
126 134
 					continue;
127 135
 				default:
128
-					v_len = &line[i - 1];
136
+					v_len = (int) (&line[i] - val) - 1;
129 137
 					continue;
130 138
 				}
131 139
 				break;
@@ -135,30 +143,32 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
135 143
 			err = m_parser_error;
136 144
 		}
137 145
 	} else {
138
-		evt = cSTUFF_CONFIG_ERROR;
146
+		evt = CSTUFF_CONFIG_ERROR;
139 147
 		err = m_mixed_line_indent;
140 148
 	}
141 149
 
142 150
 	/* Key ! */
143
-	key[k_len] = 0;
151
+	if (key)
152
+		key[k_len] = 0;
144 153
 
154
+	fprintf(stderr, "\n");
145 155
 	switch ((cfg->evt = evt)) {
146 156
 	case CSTUFF_CONFIG_PAIR:
147 157
 		cfg->data.pair.key = key;
148
-		cfg->data.pair.value = val;
149
-		cfg->data.pair.value_len = v_len;
158
+		cfg->data.pair.val = val;
159
+		cfg->data.pair.val_len = v_len;
150 160
 		cfg->data.pair.key_len = k_len;
151 161
 		val[v_len] = 0;
152 162
 		return indent;
153 163
 	case CSTUFF_CONFIG_NODE:
154
-		cfg->data.pair.node = key;
155
-		cfg->data.pair.node_len = k_len;
164
+		cfg->data.node.name = key;
165
+		cfg->data.node.name_len = k_len;
156 166
 		return indent;
157 167
 	case CSTUFF_CONFIG_ERROR:
158 168
 	default:
159 169
 		cfg->data.error.char_num = i;
160
-		cfg->data.error.line_text = line;
161
-		cfg->data.error.err_text = err;
170
+		cfg->data.error.line_txt = line;
171
+		cfg->data.error.err_txt = err;
162 172
 		return -1;
163 173
 	}
164 174
 }
@@ -168,7 +178,8 @@ static void
168 178
 config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
169 179
 {
170 180
 	struct config_path *path = &ctx->path;
171
-	const char *key = ctx->cfg.data.pair.key, ptr;
181
+	const char *key = ctx->cfg.data.pair.key;
182
+	char *ptr;
172 183
 	int i, len, level, k_len = ctx->cfg.data.pair.key_len;
173 184
 
174 185
 	if (line_indent) {
@@ -183,11 +194,14 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
183 194
 		level = 0;
184 195
 	}
185 196
 
186
-	if (level > path->level && !path->is_node ||
187
-			level - path->level != path->indent) {
197
+	if (level > path->level && (!path->is_node ||
198
+			level - path->level != path->indent)) {
199
+		fprintf(stderr, "cfg:%d ((%d > %d && !%d) || %d - %d != %d)\n",
200
+				ctx->cfg.line_num, level,
201
+				path->level, path->is_node & 0xFF, level, path->level, path->indent);
188 202
 		ctx->cfg.evt = CSTUFF_CONFIG_ERROR;
189
-		ctx->cfg.data.error.line_text = NULL;
190
-		ctx->cfg.data.error.err_text = m_indent_error;
203
+		ctx->cfg.data.error.line_txt = NULL;
204
+		ctx->cfg.data.error.err_txt = m_indent_error;
191 205
 		ctx->cfg.data.error.char_num = 0;
192 206
 		return;
193 207
 	}
@@ -195,7 +209,8 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
195 209
 	ptr = ctx->path.key;
196 210
 	for (i = 0; i < level; i++) {
197 211
 		int l = strcspn(ptr, ".");
198
-		ptr += (l + 1); 
212
+		ptr += l; 
213
+		fprintf(stderr, "l = %d\n", l);
199 214
 	}
200 215
 
201 216
 	len = (ptr - ctx->path.key) + 1 + k_len + 1;
@@ -203,8 +218,8 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
203 218
 		char *new_key;
204 219
 		if (!(new_key = realloc(ctx->path.key, len))) {
205 220
 			ctx->cfg.evt = CSTUFF_CONFIG_ERROR;
206
-			ctx->cfg.data.error.line_text = NULL;
207
-			ctx->cfg.data.error.err_text = m_out_of_memory;
221
+			ctx->cfg.data.error.line_txt = NULL;
222
+			ctx->cfg.data.error.err_txt = m_out_of_memory;
208 223
 			ctx->cfg.data.error.char_num = 0;
209 224
 			return;
210 225
 		}
@@ -212,9 +227,14 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
212 227
 		ctx->path.size = len;
213 228
 	}
214 229
 
215
-	*(ptr++) = '.';
230
+	if (level) {
231
+		*(ptr++) = '.';
232
+	} else {
233
+		*ptr = 0;
234
+	}
216 235
 	strcpy(ptr, key);
217 236
 	path->level = level;
237
+	path->is_node = is_node;
218 238
 
219 239
 	ctx->cfg.data.pair.key = ctx->path.key;
220 240
 }
@@ -224,16 +244,19 @@ static int
224 244
 config_get_line(char *line, ssize_t length, void *p_ctx)
225 245
 {
226 246
 	int result;
227
-	struct config_ctx *ctx = (struct config_ctx *)ctx;
247
+	struct config_ctx *ctx = (struct config_ctx *)p_ctx;
228 248
 
229 249
 	memset(&ctx->cfg.data, 0, sizeof (ctx->cfg.data));
230 250
 	if ((result = config_read_line(line, length, &ctx->cfg)) != -1) {
231 251
 		bool is_node = (ctx->cfg.evt == CSTUFF_CONFIG_NODE);
232 252
 		/* result == indent */
233
-		config_set_path_key(ctx, indent, is_node);
234
-
235
-		result = ctx->callback(&ctx->cfg, ctx->u_ptr);
253
+		config_set_path_key(ctx, result, is_node);
254
+	} else {
255
+		fprintf(stderr, "cfg: read line failed %d\n", result);
236 256
 	}
257
+
258
+	result = ctx->callback(&ctx->cfg, ctx->u_ptr);
259
+
237 260
 	return result;
238 261
 }
239 262
 
@@ -250,14 +273,16 @@ cstuff_config_parse(const char *file, CStuffConfigCallback callback, void *u_ptr
250 273
 		return CSTUFF_MALLOC_ERROR;
251 274
 	} else {
252 275
 		ctx.path.size = CSTUFF_CONFIG_PATH_SIZE;
253
-		ctx.path.indent = 0;
254
-		ctx.path.in_chr = 0;
255 276
 	}
256 277
 	ctx.u_ptr = u_ptr;
257 278
 	ctx.callback = callback;
258 279
 
259 280
 	rc = cstuff_file_get_lines(file, config_get_line, (void *)&ctx);
260 281
 
282
+	if (rc) {
283
+		fprintf(stderr, "cfg: get lines failed %d (%s)\n", rc, strerror(errno));
284
+	}
285
+
261 286
 	if (ctx.path.key)
262 287
 		free(ctx.path.key);
263 288
 	return rc;

+ 11
- 8
config.h View File

@@ -13,6 +13,9 @@
13 13
  * @see https://lowenware.com/
14 14
  */
15 15
 
16
+#include "retcode.h"
17
+
18
+
16 19
 #ifndef CSTUFF_CONFIG_H_6207A85E_26F1_4B64_818F_92C6286FC5F0
17 20
 #define CSTUFF_CONFIG_H_6207A85E_26F1_4B64_818F_92C6286FC5F0
18 21
 
@@ -28,30 +31,30 @@ typedef enum {
28 31
 } CStuffConfigEvent;
29 32
 
30 33
 struct cstuff_config_node {
31
-	const char *node;
32
-	int node_len;
34
+	const char *name;
35
+	int name_len;
33 36
 };
34 37
 
35 38
 struct cstuff_config_pair {
36 39
 	const char *key;
37
-	const char *value;
40
+	const char *val;
38 41
 	int key_len;
39
-	int value_len;
42
+	int val_len;
40 43
 };
41 44
 
42 45
 struct cstuff_config_error {
43
-	const char *line_text;
44
-	const char *err_text;
46
+	const char *line_txt;
47
+	const char *err_txt;
45 48
 	int char_num;
46 49
 };
47 50
 
48 51
 struct cstuff_config {
49 52
 	CStuffConfigEvent evt;
50
-	int line_number;
53
+	int line_num;
51 54
 	union {
52 55
 		struct cstuff_config_node node;
53 56
 		struct cstuff_config_pair pair;
54
-		struct cstuff_config_ierror error;
57
+		struct cstuff_config_error error;
55 58
 	} data;
56 59
 };
57 60
 

+ 2
- 2
file.c View File

@@ -86,7 +86,7 @@ cstuff_file_move(const char *src, const char *dest)
86 86
 
87 87
 
88 88
 CStuffRetcode
89
-cstuff_file_get_lines(const char *src, CStuffFileGetLine get_line)
89
+cstuff_file_get_lines(const char *src, CStuffFileGetLine get_line, void *ctx)
90 90
 {
91 91
 	FILE *f = NULL;
92 92
 	size_t sz = 256;
@@ -101,7 +101,7 @@ cstuff_file_get_lines(const char *src, CStuffFileGetLine get_line)
101 101
 		goto e_syscall;
102 102
 
103 103
 	while ((rs = getdelim(&line, &sz, '\n', f)) != -1) {
104
-		if (!get_line(line, rc)) {
104
+		if (!get_line(line, rs, ctx)) {
105 105
 			continue;
106 106
 		} else {
107 107
 			goto e_input;

+ 1
- 0
file.h View File

@@ -17,6 +17,7 @@
17 17
 #define CSTUFF_FILE_H_3C3D5EDC_A624_4550_98A4_29AD5A41301D
18 18
 
19 19
 #include <stdbool.h>
20
+#include <sys/types.h>
20 21
 #include "retcode.h"
21 22
 
22 23
 

+ 2
- 1
retcode.h View File

@@ -17,7 +17,8 @@
17 17
 #define CSTUFF_RETCODE_H_CBA3C16B_88D6_49F4_966D_DBDA727583FE
18 18
 
19 19
 typedef enum {
20
-	  CSTUFF_MALLOC_ERROR  = -2
20
+	  CSTUFF_INPUT_ERROR = -3
21
+	, CSTUFF_MALLOC_ERROR  = -2
21 22
 	, CSTUFF_SYSCALL_ERROR = -1
22 23
 	, CSTUFF_SUCCESS = 0
23 24
 	, CSTUFF_IDLE    = 1

Loading…
Cancel
Save