Mercurial > masqmail
annotate src/header.c @ 371:f122535c589e
Refactoring: early failure exit.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 25 Oct 2011 13:51:43 +0200 |
parents | b27f66555ba8 |
children | 028bc124d744 |
rev | line source |
---|---|
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
1 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
2 ** MasqMail |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
3 ** Copyright (C) 2000 Oliver Kurth |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
4 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
5 ** This program is free software; you can redistribute it and/or modify |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
6 ** it under the terms of the GNU General Public License as published by |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
7 ** the Free Software Foundation; either version 2 of the License, or |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
8 ** (at your option) any later version. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
9 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
10 ** This program is distributed in the hope that it will be useful, |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
13 ** GNU General Public License for more details. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
14 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
15 ** You should have received a copy of the GNU General Public License |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
16 ** along with this program; if not, write to the Free Software |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
0 | 18 */ |
19 #include "masqmail.h" | |
20 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
21 header_name header_names[] = { |
15 | 22 {"From", HEAD_FROM,}, |
23 {"Sender", HEAD_SENDER,}, | |
24 {"To", HEAD_TO,}, | |
25 {"Cc", HEAD_CC,}, | |
26 {"Bcc", HEAD_BCC,}, | |
27 {"Date", HEAD_DATE,}, | |
28 {"Message-Id", HEAD_MESSAGE_ID,}, | |
29 {"Reply-To", HEAD_REPLY_TO,}, | |
30 {"Subject", HEAD_SUBJECT,}, | |
31 {"Return-Path", HEAD_RETURN_PATH,}, | |
32 {"Envelope-To", HEAD_ENVELOPE_TO,}, | |
33 {"Received", HEAD_RECEIVED}, | |
0 | 34 }; |
35 | |
36 /* this was borrowed from exim and slightly changed */ | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 gchar* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
38 rec_timestamp() |
0 | 39 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 static gchar buf[64]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
41 int len; |
0 | 42 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
43 time_t now = time(NULL); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 struct tm *t = localtime(&now); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
45 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 int diff_hour, diff_min; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 struct tm local; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
48 struct tm *gmt; |
0 | 49 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
50 memcpy(&local, t, sizeof(struct tm)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
51 gmt = gmtime(&now); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
52 diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; |
301 | 53 if (local.tm_year != gmt->tm_year) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
54 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440; |
301 | 55 } else if (local.tm_yday != gmt->tm_yday) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
56 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440; |
301 | 57 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
58 diff_hour = diff_min / 60; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
59 diff_min = abs(diff_min - diff_hour * 60); |
0 | 60 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
61 len = strftime(buf, sizeof(buf), "%a, ", &local); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
63 len += strlen(buf + len); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 len += strftime(buf + len, sizeof(buf) - len, "%b %Y %H:%M:%S", &local); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
65 g_snprintf(buf + len, sizeof(buf) - len, " %+03d%02d", diff_hour, diff_min); |
0 | 66 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
67 return buf; |
0 | 68 } |
69 | |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
70 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
71 ** finds list of headers matching id |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
72 ** if id == HEAD_UNKNOWN and header == NULL finds all unknown headers |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
73 ** else finds all headers matching header |
0 | 74 */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
75 GList* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
76 find_header(GList *hdr_list, header_id id, gchar *hdr_str) |
0 | 77 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
78 GList *found_list = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
79 GList *node; |
0 | 80 |
301 | 81 if ((id != HEAD_UNKNOWN) || !hdr_str) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
82 foreach(hdr_list, node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
83 header *hdr = (header *) (node->data); |
301 | 84 if (hdr->id == id) { |
85 found_list = g_list_append(found_list, hdr); | |
86 } | |
87 } | |
88 return found_list; | |
89 } | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
90 |
301 | 91 foreach(hdr_list, node) { |
92 header *hdr = (header *) (node->data); | |
93 gchar buf[64], *q = buf, *p = hdr->header; | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
94 |
301 | 95 while (*p != ':' && q < buf+sizeof(buf)-1 && *p) { |
96 *(q++) = *(p++); | |
97 } | |
98 *q = '\0'; | |
99 | |
100 if (strcasecmp(buf, hdr_str) == 0) { | |
101 found_list = g_list_append(found_list, hdr); | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
102 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
103 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
104 return found_list; |
0 | 105 } |
106 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
107 void |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
108 header_unfold(header *hdr) |
0 | 109 { |
302
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
110 char *src = hdr->header; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
111 char *dest = src; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
112 char *p; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
113 |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
114 p = strchr(src, '\n'); |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
115 if (!p || !p[1]) { |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
116 /* no folded header */ |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
117 return; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
118 } |
0 | 119 |
302
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
120 while (*src) { |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
121 if (*src == '\n') { |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
122 /* ignore */ |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
123 src++; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
124 } else { |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
125 /* copy */ |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
126 *(dest++) = *(src++); |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
127 } |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
128 } |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
129 *(dest++) = '\n'; |
2ffcd38ccf53
improved unused function header_fold()
markus schnalke <meillo@marmaro.de>
parents:
301
diff
changeset
|
130 *(dest++) = '\0'; |
0 | 131 } |
132 | |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
133 /* |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
134 ** fold the header at maxlen chars (newline excluded) |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
135 ** (We exclude the newline because the RFCs deal with it this way) |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
136 */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
137 void |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
138 header_fold(header *hdr, unsigned int maxlen) |
0 | 139 { |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
140 int len = strlen(hdr->header); |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
141 char *src = hdr->header; |
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
142 char *dest; |
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
143 char *tmp; |
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
144 char *p; |
301 | 145 int valueoffset; |
146 | |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
147 if (len <= maxlen) { |
301 | 148 /* we don't need to do anything */ |
149 return; | |
150 } | |
151 | |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
152 /* strip trailing whitespace */ |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
153 for (p=src+len-1; *p==' '||*p=='\t'||*p=='\n'; p--) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
154 *p = '\0'; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
155 len--; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
156 printf(" trailing whitespace\n"); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
157 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
158 printf("stripped len: %d\n", len); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
159 |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
160 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
161 ** FIXME: would be nice to have a better size calculation |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
162 ** (the current size + what we insert as break, twice as often as |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
163 ** we have breaks in the optimal case) |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
164 */ |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
165 tmp = malloc(len + 2 * (len/maxlen) * strlen("\n\t")); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
166 dest = tmp; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
167 |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
168 /* the position in hdr->header where the value part start */ |
301 | 169 valueoffset = hdr->value - hdr->header; |
170 | |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
171 while (strlen(src) > maxlen) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
172 int i, l; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
173 char *pp; |
0 | 174 |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
175 for (pp=src+maxlen; pp>src; pp--) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
176 if (*pp==' ' || *pp=='\t' || *p=='\n') { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
177 break; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
178 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
179 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
180 |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
181 if (src == pp) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
182 /* no potential break point was found within |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
183 maxlen so advance further until the next */ |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
184 for (pp=src+maxlen; *pp; pp++) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
185 if (*pp==' ' || *pp=='\t' || *p=='\n') { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
186 break; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
187 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
188 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
189 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
190 if (!*pp) { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
191 break; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
192 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
193 |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
194 memcpy(dest, src, pp-src); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
195 dest += pp-src; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
196 *(dest++) = '\n'; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
197 *(dest++) = '\t'; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
198 while (*pp == ' ' || *pp == '\t' || *p=='\n') { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
199 pp++; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
200 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
201 src = pp; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
202 } |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
203 memcpy(dest, src, strlen(src)); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
204 dest += strlen(src); |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
205 |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
206 if (*(dest-1) != '\n') { |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
207 *dest = '\n'; |
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
208 *(dest+1) = '\0'; |
301 | 209 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
210 |
303
3e3c280ca5b2
replaced header_fold() with a better implementation
markus schnalke <meillo@marmaro.de>
parents:
302
diff
changeset
|
211 hdr->header = tmp; |
301 | 212 hdr->value = hdr->header + valueoffset; |
0 | 213 } |
214 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
215 header* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
216 create_header(header_id id, gchar *fmt, ...) |
0 | 217 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
218 gchar *p; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
219 header *hdr; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
220 va_list args; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
221 va_start(args, fmt); |
0 | 222 |
301 | 223 /* g_malloc() calls exit on failure */ |
224 hdr = g_malloc(sizeof(header)); | |
0 | 225 |
301 | 226 hdr->id = id; |
227 hdr->header = g_strdup_vprintf(fmt, args); | |
228 hdr->value = NULL; | |
0 | 229 |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
230 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
231 ** value shall point to the first non-whitespace char in the |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
232 ** value part of the header line (i.e. after the first colon) |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
233 */ |
301 | 234 p = strchr(hdr->header, ':'); |
235 if (p) { | |
236 p++; | |
237 while (*p == ' ' || *p == '\t' || *p == '\n') { | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
238 p++; |
301 | 239 } |
240 hdr->value = (*p) ? p : NULL; | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
241 } |
0 | 242 |
301 | 243 DEBUG(3) debugf("create_header(): hdr: `%s'\n", hdr->header); |
244 DEBUG(3) debugf("create_header(): val: `%s'\n", hdr->value); | |
245 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
246 va_end(args); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
247 return hdr; |
0 | 248 } |
249 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
250 void |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
251 destroy_header(header *hdr) |
0 | 252 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
253 if (hdr) { |
301 | 254 if (hdr->header) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
255 g_free(hdr->header); |
301 | 256 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
257 g_free(hdr); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
258 } |
0 | 259 } |
260 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
261 header* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
262 copy_header(header *hdr) |
0 | 263 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
264 header *new_hdr = NULL; |
0 | 265 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
266 if (hdr) { |
301 | 267 new_hdr = g_malloc(sizeof(header)); |
268 new_hdr->id = hdr->id; | |
269 new_hdr->header = g_strdup(hdr->header); | |
270 new_hdr->value = new_hdr->header + (hdr->value - hdr->header); | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
271 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
272 return new_hdr; |
0 | 273 } |
274 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
275 header* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
322
diff
changeset
|
276 get_header(gchar *line) |
0 | 277 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
278 gchar *p = line; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
279 gchar buf[64], *q = buf; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
280 gint i; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
281 header *hdr; |
0 | 282 |
301 | 283 while (*p && (*p != ':') && (q < buf+sizeof(buf)-1)) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
284 *(q++) = *(p++); |
301 | 285 } |
15 | 286 *q = '\0'; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
287 |
301 | 288 if (*p != ':') { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
289 return NULL; |
301 | 290 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
291 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
292 hdr = g_malloc(sizeof(header)); |
0 | 293 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
294 hdr->value = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
295 p++; |
0 | 296 |
301 | 297 while (*p && (*p == ' ' || *p == '\t')) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
298 p++; |
301 | 299 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
300 hdr->value = p; |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
301 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
302 ** Note: an empty value can also mean that it's only the first part |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
303 ** of a folded header line |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
304 */ |
0 | 305 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
306 for (i = 0; i < HEAD_NUM_IDS; i++) { |
301 | 307 if (strcasecmp(header_names[i].header, buf) == 0) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
308 break; |
301 | 309 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
310 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
311 hdr->id = (header_id) i; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
312 hdr->header = g_strdup(line); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
313 hdr->value = hdr->header + (hdr->value - line); |
0 | 314 |
322
01d2f7a17bf0
hinted that the debug output of folded headers prints only the
meillo@marmaro.de
parents:
303
diff
changeset
|
315 DEBUG(4) debugf("header: %d = %s[...]", hdr->id, hdr->header); |
301 | 316 /* Note: This only outputs the first line if the header is folded */ |
0 | 317 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
318 return hdr; |
0 | 319 } |