summaryrefslogtreecommitdiffabout
authorJonathan Bastien-Filiatrault <joe@x2a.org>2007-10-26 22:11:26 (UTC)
committer Jonathan Bastien-Filiatrault <joe@x2a.org>2007-11-05 23:13:30 (UTC)
commitaf0819830445e39584a0137034562086a55deaf2 (patch) (unidiff)
treea9da08806d706be633b63d0fc2f9dbe12824536b
parentaa5cc328f4894ba6972842b4edbca3017f169050 (diff)
downloadcgit-af0819830445e39584a0137034562086a55deaf2.zip
cgit-af0819830445e39584a0137034562086a55deaf2.tar.gz
cgit-af0819830445e39584a0137034562086a55deaf2.tar.bz2
Add iconv_msg function.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--parsing.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/parsing.c b/parsing.c
index 16b4db7..f156c12 100644
--- a/parsing.c
+++ b/parsing.c
@@ -1,40 +1,42 @@
1/* config.c: parsing of config files 1/* config.c: parsing of config files
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include <iconv.h>
10
9#include "cgit.h" 11#include "cgit.h"
10 12
11int next_char(FILE *f) 13int next_char(FILE *f)
12{ 14{
13 int c = fgetc(f); 15 int c = fgetc(f);
14 if (c=='\r') { 16 if (c=='\r') {
15 c = fgetc(f); 17 c = fgetc(f);
16 if (c!='\n') { 18 if (c!='\n') {
17 ungetc(c, f); 19 ungetc(c, f);
18 c = '\r'; 20 c = '\r';
19 } 21 }
20 } 22 }
21 return c; 23 return c;
22} 24}
23 25
24void skip_line(FILE *f) 26void skip_line(FILE *f)
25{ 27{
26 int c; 28 int c;
27 29
28 while((c=next_char(f)) && c!='\n' && c!=EOF) 30 while((c=next_char(f)) && c!='\n' && c!=EOF)
29 ; 31 ;
30} 32}
31 33
32int read_config_line(FILE *f, char *line, const char **value, int bufsize) 34int read_config_line(FILE *f, char *line, const char **value, int bufsize)
33{ 35{
34 int i = 0, isname = 0; 36 int i = 0, isname = 0;
35 37
36 *value = NULL; 38 *value = NULL;
37 while(i<bufsize-1) { 39 while(i<bufsize-1) {
38 int c = next_char(f); 40 int c = next_char(f);
39 if (!isname && (c=='#' || c==';')) { 41 if (!isname && (c=='#' || c==';')) {
40 skip_line(f); 42 skip_line(f);
@@ -147,64 +149,120 @@ void cgit_parse_url(const char *url)
147 if (!url || url[0] == '\0') 149 if (!url || url[0] == '\0')
148 return; 150 return;
149 151
150 cgit_repo = cgit_get_repoinfo(url); 152 cgit_repo = cgit_get_repoinfo(url);
151 if (cgit_repo) { 153 if (cgit_repo) {
152 cgit_query_repo = cgit_repo->url; 154 cgit_query_repo = cgit_repo->url;
153 return; 155 return;
154 } 156 }
155 157
156 cmd = strchr(url, '/'); 158 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) { 159 while (!cgit_repo && cmd) {
158 cmd[0] = '\0'; 160 cmd[0] = '\0';
159 cgit_repo = cgit_get_repoinfo(url); 161 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) { 162 if (cgit_repo == NULL) {
161 cmd[0] = '/'; 163 cmd[0] = '/';
162 cmd = strchr(cmd + 1, '/'); 164 cmd = strchr(cmd + 1, '/');
163 continue; 165 continue;
164 } 166 }
165 167
166 cgit_query_repo = cgit_repo->url; 168 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/'); 169 p = strchr(cmd + 1, '/');
168 if (p) { 170 if (p) {
169 p[0] = '\0'; 171 p[0] = '\0';
170 if (p[1]) 172 if (p[1])
171 cgit_query_path = trim_end(p + 1, '/'); 173 cgit_query_path = trim_end(p + 1, '/');
172 } 174 }
173 cgit_cmd = cgit_get_cmd_index(cmd + 1); 175 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1); 176 cgit_query_page = xstrdup(cmd + 1);
175 return; 177 return;
176 } 178 }
177} 179}
178 180
181static char *iconv_msg(char *msg, const char *encoding)
182{
183 iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding);
184 size_t inlen = strlen(msg);
185 char *in;
186 char *out;
187 size_t inleft;
188 size_t outleft;
189 char *buf;
190 char *ret;
191 size_t buf_sz;
192 int again, fail;
193
194 if(msg_conv == (iconv_t)-1)
195 return NULL;
196
197 buf_sz = inlen * 2;
198 buf = xmalloc(buf_sz+1);
199 do {
200 in = msg;
201 inleft = inlen;
202
203 out = buf;
204 outleft = buf_sz;
205 iconv(msg_conv, &in, &inleft, &out, &outleft);
206
207 if(inleft == 0) {
208 fail = 0;
209 again = 0;
210 } else if(inleft != 0 && errno == E2BIG) {
211 fail = 0;
212 again = 1;
213
214 buf_sz *= 2;
215 free(buf);
216 buf = xmalloc(buf_sz+1);
217 } else {
218 fail = 1;
219 again = 0;
220 }
221 } while(again && !fail);
222
223 if(fail) {
224 free(buf);
225 ret = NULL;
226 } else {
227 buf = xrealloc(buf, out - buf);
228 *out = 0;
229 ret = buf;
230 }
231
232 iconv_close(msg_conv);
233
234 return ret;
235}
236
179char *substr(const char *head, const char *tail) 237char *substr(const char *head, const char *tail)
180{ 238{
181 char *buf; 239 char *buf;
182 240
183 buf = xmalloc(tail - head + 1); 241 buf = xmalloc(tail - head + 1);
184 strncpy(buf, head, tail - head); 242 strncpy(buf, head, tail - head);
185 buf[tail - head] = '\0'; 243 buf[tail - head] = '\0';
186 return buf; 244 return buf;
187} 245}
188 246
189struct commitinfo *cgit_parse_commit(struct commit *commit) 247struct commitinfo *cgit_parse_commit(struct commit *commit)
190{ 248{
191 struct commitinfo *ret; 249 struct commitinfo *ret;
192 char *p = commit->buffer, *t = commit->buffer; 250 char *p = commit->buffer, *t = commit->buffer;
193 251
194 ret = xmalloc(sizeof(*ret)); 252 ret = xmalloc(sizeof(*ret));
195 ret->commit = commit; 253 ret->commit = commit;
196 ret->author = NULL; 254 ret->author = NULL;
197 ret->author_email = NULL; 255 ret->author_email = NULL;
198 ret->committer = NULL; 256 ret->committer = NULL;
199 ret->committer_email = NULL; 257 ret->committer_email = NULL;
200 ret->subject = NULL; 258 ret->subject = NULL;
201 ret->msg = NULL; 259 ret->msg = NULL;
202 ret->msg_encoding = NULL; 260 ret->msg_encoding = NULL;
203 261
204 if (p == NULL) 262 if (p == NULL)
205 return ret; 263 return ret;
206 264
207 if (strncmp(p, "tree ", 5)) 265 if (strncmp(p, "tree ", 5))
208 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 266 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
209 else 267 else
210 p += 46; // "tree " + hex[40] + "\n" 268 p += 46; // "tree " + hex[40] + "\n"