Initial import from http://www.droids-corp.org/hg/libcmdline/rev/db316e4289a1
[libcmdline.git] / src / genconf / parser_common.c
1 #include <ctype.h>
2
3 /*
4  * return true if line is empty or contains only spaces/comments
5  */
6 int line_is_empty(const char *buf)
7 {
8         while (*buf != '\0' && *buf != '#') {
9                 if (!isspace(*buf))
10                         return 0;
11                 buf++;
12         }
13         return 1;
14 }
15
16 /* if buf is a string surrounded by simple or double quotes, return 0
17  * and fill *start and *end to the position of quotes. Else, return
18  * -1. */
19 int remove_quote(const char *buf, int *start, int *end)
20 {
21         const char *c = buf;
22
23         while (isspace(*c))
24                 c ++;
25
26         /* if it starts with double quote, remove them */
27         if (c[0] == '"') {
28                 *start = c - buf;
29                 c ++;
30                 while (*c != '\0' ) {
31                         if ((*c == '"') && (*(c-1) != '\\')) {
32                                 *end = c - buf;
33                                 return 0;
34                         }
35                         c ++;
36                 }
37                 return -1;
38         }
39         /* if it starts with simple quote, remove them */
40         if (c[0] == '\'') {
41                 *start = c - buf;
42                 c ++;
43                 while (*c != '\0' ) {
44                         if ((*c == '\'') && (*(c-1) != '\\')) {
45                                 *end = c - buf;
46                                 return 0;
47                         }
48                         c ++;
49                 }
50                 return -1;
51         }
52         return -1;
53 }