summaryrefslogtreecommitdiffabout
path: root/parsing.c
authorLars Hjemli <hjemli@gmail.com>2008-12-06 10:33:05 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-12-06 10:33:05 (UTC)
commit3157ea3b7e5204dba2892ffb54d641746c4c7ca4 (patch) (unidiff)
tree93198341d83438fce50ffeda19b2510138b7b2e5 /parsing.c
parent7b5cee65fd9cf31e4f19ce4ff613778cb95512a9 (diff)
parentd2592a2cb5ca202d9825d8f1ca6f4a712ea8fe2b (diff)
downloadcgit-3157ea3b7e5204dba2892ffb54d641746c4c7ca4.zip
cgit-3157ea3b7e5204dba2892ffb54d641746c4c7ca4.tar.gz
cgit-3157ea3b7e5204dba2892ffb54d641746c4c7ca4.tar.bz2
Merge branch 'stable'
Diffstat (limited to 'parsing.c') (more/less context) (ignore whitespace changes)
-rw-r--r--parsing.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/parsing.c b/parsing.c
index c8f3048..f3f3b15 100644
--- a/parsing.c
+++ b/parsing.c
@@ -51,110 +51,114 @@ void cgit_parse_url(const char *url)
51 return; 51 return;
52 } 52 }
53} 53}
54 54
55char *substr(const char *head, const char *tail) 55char *substr(const char *head, const char *tail)
56{ 56{
57 char *buf; 57 char *buf;
58 58
59 buf = xmalloc(tail - head + 1); 59 buf = xmalloc(tail - head + 1);
60 strncpy(buf, head, tail - head); 60 strncpy(buf, head, tail - head);
61 buf[tail - head] = '\0'; 61 buf[tail - head] = '\0';
62 return buf; 62 return buf;
63} 63}
64 64
65char *parse_user(char *t, char **name, char **email, unsigned long *date) 65char *parse_user(char *t, char **name, char **email, unsigned long *date)
66{ 66{
67 char *p = t; 67 char *p = t;
68 int mode = 1; 68 int mode = 1;
69 69
70 while (p && *p) { 70 while (p && *p) {
71 if (mode == 1 && *p == '<') { 71 if (mode == 1 && *p == '<') {
72 *name = substr(t, p - 1); 72 *name = substr(t, p - 1);
73 t = p; 73 t = p;
74 mode++; 74 mode++;
75 } else if (mode == 1 && *p == '\n') { 75 } else if (mode == 1 && *p == '\n') {
76 *name = substr(t, p); 76 *name = substr(t, p);
77 p++; 77 p++;
78 break; 78 break;
79 } else if (mode == 2 && *p == '>') { 79 } else if (mode == 2 && *p == '>') {
80 *email = substr(t, p + 1); 80 *email = substr(t, p + 1);
81 t = p; 81 t = p;
82 mode++; 82 mode++;
83 } else if (mode == 2 && *p == '\n') { 83 } else if (mode == 2 && *p == '\n') {
84 *email = substr(t, p); 84 *email = substr(t, p);
85 p++; 85 p++;
86 break; 86 break;
87 } else if (mode == 3 && isdigit(*p)) { 87 } else if (mode == 3 && isdigit(*p)) {
88 *date = atol(p); 88 *date = atol(p);
89 mode++; 89 mode++;
90 } else if (*p == '\n') { 90 } else if (*p == '\n') {
91 p++; 91 p++;
92 break; 92 break;
93 } 93 }
94 p++; 94 p++;
95 } 95 }
96 return p; 96 return p;
97} 97}
98 98
99#ifdef NO_ICONV
100#define reencode(a, b, c)
101#else
99const char *reencode(char **txt, const char *src_enc, const char *dst_enc) 102const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
100{ 103{
101 char *tmp; 104 char *tmp;
102 105
103 if (!txt || !*txt || !src_enc || !dst_enc) 106 if (!txt || !*txt || !src_enc || !dst_enc)
104 return *txt; 107 return *txt;
105 108
106 tmp = reencode_string(*txt, src_enc, dst_enc); 109 tmp = reencode_string(*txt, src_enc, dst_enc);
107 if (tmp) { 110 if (tmp) {
108 free(*txt); 111 free(*txt);
109 *txt = tmp; 112 *txt = tmp;
110 } 113 }
111 return *txt; 114 return *txt;
112} 115}
116#endif
113 117
114struct commitinfo *cgit_parse_commit(struct commit *commit) 118struct commitinfo *cgit_parse_commit(struct commit *commit)
115{ 119{
116 struct commitinfo *ret; 120 struct commitinfo *ret;
117 char *p = commit->buffer, *t = commit->buffer; 121 char *p = commit->buffer, *t = commit->buffer;
118 122
119 ret = xmalloc(sizeof(*ret)); 123 ret = xmalloc(sizeof(*ret));
120 ret->commit = commit; 124 ret->commit = commit;
121 ret->author = NULL; 125 ret->author = NULL;
122 ret->author_email = NULL; 126 ret->author_email = NULL;
123 ret->committer = NULL; 127 ret->committer = NULL;
124 ret->committer_email = NULL; 128 ret->committer_email = NULL;
125 ret->subject = NULL; 129 ret->subject = NULL;
126 ret->msg = NULL; 130 ret->msg = NULL;
127 ret->msg_encoding = NULL; 131 ret->msg_encoding = NULL;
128 132
129 if (p == NULL) 133 if (p == NULL)
130 return ret; 134 return ret;
131 135
132 if (strncmp(p, "tree ", 5)) 136 if (strncmp(p, "tree ", 5))
133 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 137 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
134 else 138 else
135 p += 46; // "tree " + hex[40] + "\n" 139 p += 46; // "tree " + hex[40] + "\n"
136 140
137 while (!strncmp(p, "parent ", 7)) 141 while (!strncmp(p, "parent ", 7))
138 p += 48; // "parent " + hex[40] + "\n" 142 p += 48; // "parent " + hex[40] + "\n"
139 143
140 if (p && !strncmp(p, "author ", 7)) { 144 if (p && !strncmp(p, "author ", 7)) {
141 p = parse_user(p + 7, &ret->author, &ret->author_email, 145 p = parse_user(p + 7, &ret->author, &ret->author_email,
142 &ret->author_date); 146 &ret->author_date);
143 } 147 }
144 148
145 if (p && !strncmp(p, "committer ", 9)) { 149 if (p && !strncmp(p, "committer ", 9)) {
146 p = parse_user(p + 9, &ret->committer, &ret->committer_email, 150 p = parse_user(p + 9, &ret->committer, &ret->committer_email,
147 &ret->committer_date); 151 &ret->committer_date);
148 } 152 }
149 153
150 if (p && !strncmp(p, "encoding ", 9)) { 154 if (p && !strncmp(p, "encoding ", 9)) {
151 p += 9; 155 p += 9;
152 t = strchr(p, '\n'); 156 t = strchr(p, '\n');
153 if (t) { 157 if (t) {
154 ret->msg_encoding = substr(p, t + 1); 158 ret->msg_encoding = substr(p, t + 1);
155 p = t + 1; 159 p = t + 1;
156 } 160 }
157 } 161 }
158 162
159 // skip unknown header fields 163 // skip unknown header fields
160 while (p && *p && (*p != '\n')) { 164 while (p && *p && (*p != '\n')) {