comparison src/header.c @ 303:3e3c280ca5b2

replaced header_fold() with a better implementation
author markus schnalke <meillo@marmaro.de>
date Thu, 09 Dec 2010 18:01:46 -0300
parents 2ffcd38ccf53
children 01d2f7a17bf0
comparison
equal deleted inserted replaced
302:2ffcd38ccf53 303:3e3c280ca5b2
126 } 126 }
127 *(dest++) = '\n'; 127 *(dest++) = '\n';
128 *(dest++) = '\0'; 128 *(dest++) = '\0';
129 } 129 }
130 130
131 #define MAX_HDR_LEN 72 131 /*
132 fold the header at maxlen chars (newline excluded)
133 (We exclude the newline because the RFCs deal with it this way)
134 */
132 void 135 void
133 header_fold(header * hdr) 136 header_fold(header* hdr, unsigned int maxlen)
134 { 137 {
135 gint len = strlen(hdr->header); 138 int len = strlen(hdr->header);
136 gchar *p, *q; 139 char* src = hdr->header;
137 gchar *tmp_hdr; 140 char* dest;
141 char* tmp;
142 char* p;
138 int valueoffset; 143 int valueoffset;
139 144
140 if (len < MAX_HDR_LEN) { 145 if (len <= maxlen) {
141 /* we don't need to do anything */ 146 /* we don't need to do anything */
142 return; 147 return;
143 } 148 }
144 149
145 /* the position in hdr->header where the value part starts */ 150 /* strip trailing whitespace */
151 for (p=src+len-1; *p==' '||*p=='\t'||*p=='\n'; p--) {
152 *p = '\0';
153 len--;
154 printf(" trailing whitespace\n");
155 }
156 printf("stripped len: %d\n", len);
157
158 /* FIXME: would be nice to have a better size calculation */
159 /* (the current size + what we insert as break, twice as often as
160 we have breaks in the optimal case) */
161 tmp = malloc(len + 2 * (len/maxlen) * strlen("\n\t"));
162 dest = tmp;
163
164 /* the position in hdr->header where the value part start */
146 valueoffset = hdr->value - hdr->header; 165 valueoffset = hdr->value - hdr->header;
147 166
148 /* TODO: size is only calculated roughly */ 167 while (strlen(src) > maxlen) {
149 /* size is probably overestimated, but so we are on the safe side */ 168 int i, l;
150 /* (as much as we already have + chars inserted per break * number 169 char *pp;
151 of breaks + some more) */ 170
152 tmp_hdr = g_malloc(len + 2 * (len/MAX_HDR_LEN) + 10); 171 for (pp=src+maxlen; pp>src; pp--) {
153 172 if (*pp==' ' || *pp=='\t' || *p=='\n') {
154 p = hdr->header; 173 break;
155 q = tmp_hdr;
156
157 if (p[len - 1] == '\n') {
158 p[len - 1] = '\0';
159 }
160
161 while (*p) {
162 gint i, l;
163 gchar *pp;
164
165 /* look forward and find potential break points */
166 i = 0;
167 l = -1;
168 pp = p;
169 while (*pp && (i < MAX_HDR_LEN)) {
170 if ((*pp == ' ') || (*pp == '\t')) {
171 l = i;
172 } 174 }
175 }
176
177 if (src == pp) {
178 /* no potential break point was found within
179 maxlen so advance further until the next */
180 for (pp=src+maxlen; *pp; pp++) {
181 if (*pp==' ' || *pp=='\t' || *p=='\n') {
182 break;
183 }
184 }
185 }
186 if (!*pp) {
187 break;
188 }
189
190 memcpy(dest, src, pp-src);
191 dest += pp-src;
192 *(dest++) = '\n';
193 *(dest++) = '\t';
194 while (*pp == ' ' || *pp == '\t' || *p=='\n') {
173 pp++; 195 pp++;
174 i++; 196 }
175 } 197 src = pp;
176 if (!*pp) { 198 }
177 l = pp - p; /* take rest, if EOS found */ 199 memcpy(dest, src, strlen(src));
178 } 200 dest += strlen(src);
179 201
180 if (l == -1) { 202 if (*(dest-1) != '\n') {
181 /* no potential break point was found within 203 *dest = '\n';
182 MAX_HDR_LEN so advance further until the next */ 204 *(dest+1) = '\0';
183 while (*pp && *pp != ' ' && *pp != '\t') { 205 }
184 pp++; 206
185 i++; 207 hdr->header = tmp;
186 }
187 l = i;
188 }
189
190 /* copy */
191 i = 0;
192 while (i < l) {
193 *(q++) = *(p++);
194 i++;
195 }
196 *(q++) = '\n';
197 *(q++) = *(p++); /* this is either space, tab or 0 */
198 /* *(q++) = '\t'; */
199 }
200
201 g_free(hdr->header);
202 hdr->header = tmp_hdr;
203 hdr->value = hdr->header + valueoffset; 208 hdr->value = hdr->header + valueoffset;
204 } 209 }
205 210
206 header* 211 header*
207 create_header(header_id id, gchar * fmt, ...) 212 create_header(header_id id, gchar * fmt, ...)