-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
74 lines (60 loc) · 1.3 KB
/
parse.c
File metadata and controls
74 lines (60 loc) · 1.3 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define MAX_SIZE 1024
int parse(const char *filename, int (*action)(const char *name, const char *value));
int parse(const char *filename, int (*action)(const char *name, const char *value))
{
char buf[MAX_SIZE];
char *tmp;
char name[128];
char val[384];
FILE *fp;
size_t i = 0;
fp = fopen(filename, "r");
if (fp == NULL)
{
return -EBADF;
}
while (fgets(buf, MAX_SIZE, fp) != NULL)
{
for (tmp = buf; *tmp == ' ' || *tmp == '\t'; tmp++)
;
if ((*tmp == '#') || (*tmp == '=') || (*tmp == '\0') || (*tmp == '\n'))
continue;
for (i = 0; *tmp != '='; tmp++, i++)
{
name[i] = *tmp;
}
while (name[i - 1] == ' ' || name[i - 1] == '\t')
i--;
name[i] = '\0';
for (tmp++, i = 0; (*tmp != '\0') && (*tmp != '\n'); tmp++, i++)
{
if (*tmp == ' ' || *tmp == '\t')
{
i--;
continue;
}
val[i] = *tmp;
}
while (val[i - 1] == ' ' || val[i - 1] == '\t')
i--;
val[i] = '\0';
action(name, val);
}
return 0;
}
int msg(const char *name, const char *value)
{
// printf("%s=%s\n", name, value);
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
return -EINVAL;
parse(argv[1], msg);
return 0;
}