]>
git.cameronkatri.com Git - cgit.git/blob - parsing.c
1 /* config.c: parsing of config files
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 int next_char(FILE *f
)
24 void skip_line(FILE *f
)
28 while((c
=next_char(f
)) && c
!='\n' && c
!=EOF
)
32 int read_config_line(FILE *f
, char *line
, const char **value
, int bufsize
)
34 int i
= 0, isname
= 0;
39 if (!isname
&& (c
=='#' || c
==';')) {
43 if (!isname
&& isspace(c
))
46 if (c
=='=' && !*value
) {
49 } else if (c
=='\n' && !isname
) {
52 } else if (c
=='\n' || c
==EOF
) {
65 int cgit_read_config(const char *filename
, configfn fn
)
70 FILE *f
= fopen(filename
, "r");
75 while((len
= read_config_line(f
, line
, &value
, sizeof(line
))) > 0)
82 char *convert_query_hexchar(char *txt
)
85 if (strlen(txt
) < 3) {
89 d1
= hextoint(*(txt
+1));
90 d2
= hextoint(*(txt
+2));
101 int cgit_parse_query(char *txt
, configfn fn
)
103 char *t
, *value
= NULL
, c
;
108 t
= txt
= xstrdup(txt
);
110 while((c
=*t
) != '\0') {
117 t
= convert_query_hexchar(t
);
131 char *substr(const char *head
, const char *tail
)
135 buf
= xmalloc(tail
- head
+ 1);
136 strncpy(buf
, head
, tail
- head
);
137 buf
[tail
- head
] = '\0';
141 struct commitinfo
*cgit_parse_commit(struct commit
*commit
)
143 struct commitinfo
*ret
;
144 char *p
= commit
->buffer
, *t
= commit
->buffer
;
146 ret
= xmalloc(sizeof(*ret
));
147 ret
->commit
= commit
;
149 if (strncmp(p
, "tree ", 5))
150 die("Bad commit: %s", sha1_to_hex(commit
->object
.sha1
));
152 p
+= 46; // "tree " + hex[40] + "\n"
154 while (!strncmp(p
, "parent ", 7))
155 p
+= 48; // "parent " + hex[40] + "\n"
157 if (!strncmp(p
, "author ", 7)) {
159 t
= strchr(p
, '<') - 1;
160 ret
->author
= substr(p
, t
);
162 t
= strchr(t
, '>') + 1;
163 ret
->author_email
= substr(p
, t
);
164 ret
->author_date
= atol(++t
);
165 p
= strchr(t
, '\n') + 1;
168 if (!strncmp(p
, "committer ", 9)) {
170 t
= strchr(p
, '<') - 1;
171 ret
->committer
= substr(p
, t
);
173 t
= strchr(t
, '>') + 1;
174 ret
->committer_email
= substr(p
, t
);
175 ret
->committer_date
= atol(++t
);
176 p
= strchr(t
, '\n') + 1;
180 p
= strchr(p
, '\n') + 1;
183 ret
->subject
= substr(p
, t
);
187 p
= strchr(p
, '\n') + 1;