Mercurial > libpst
annotate pst2ldif.cpp @ 13:80ae7e5cb562
patches from debian
author | carl |
---|---|
date | Sun, 19 Feb 2006 14:35:22 -0800 |
parents | 3f627519a92d |
children | 3f2fedec798c |
rev | line source |
---|---|
11 | 1 /* |
2 | |
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under | |
4 the GPL version 2 or any later version at your choice available at | |
5 http://www.fsf.org/licenses/gpl.txt | |
6 | |
7 Based on readpst.c by David Smith | |
8 | |
9 */ | |
10 | |
11 using namespace std; | |
12 | |
13 #include <stdio.h> | |
14 #include <stdlib.h> | |
15 #include <time.h> | |
16 #include <string.h> | |
17 #include <ctype.h> | |
18 #include <limits.h> | |
19 #include <errno.h> | |
20 | |
21 #ifndef _WIN32 | |
22 # include <unistd.h> | |
23 # include <sys/stat.h> //mkdir | |
24 | |
25 // for reading of directory and clearing in function mk_separate_dir | |
26 # include <sys/types.h> | |
27 # include <dirent.h> | |
28 #else | |
29 # include <direct.h> | |
30 # define chdir _chdir | |
31 # define int32_t __int32 | |
32 #endif | |
33 | |
34 #ifndef __GNUC__ | |
35 # include "XGetopt.h" | |
36 #endif | |
37 | |
38 // needed for std c++ collections | |
39 #include <set> | |
40 | |
41 extern "C" { | |
42 #include "libstrfunc.h" // for base64_encoding | |
43 #include "define.h" | |
44 #include "libpst.h" | |
45 #include "common.h" | |
46 #include "timeconv.h" | |
47 #include "lzfu.h" | |
48 } | |
49 | |
50 #define VERSION "0.2" | |
51 | |
52 int32_t usage(); | |
53 int32_t version(); | |
54 char *my_stristr(char *haystack, char *needle); | |
55 char *check_filename(char *fname); | |
56 char *single(char *str); | |
57 char *folded(char *str); | |
58 void multi(char *fmt, char *str); | |
59 char *rfc2426_escape(char *str); | |
60 int32_t chr_count(char *str, char x); | |
61 | |
62 char *prog_name; | |
63 pst_file pstfile; | |
64 char *ldap_base = NULL; // 'o=some.domain.tld, c=US' | |
65 char *ldap_class = NULL; // 'newPerson' | |
66 char *ldap_org = NULL; // 'o=some.domain.tld', computed from ldap_base | |
67 | |
68 | |
69 //////////////////////////////////////////////// | |
70 // define our ordering | |
71 struct ltstr { | |
72 bool operator()(char* s1, char* s2) const { | |
73 return strcasecmp(s1, s2) < 0; | |
74 } | |
75 }; | |
76 // define our set | |
77 typedef set<char *, ltstr> string_set; | |
78 // make a static set to hold the cn values | |
79 static string_set all_strings; | |
80 | |
81 | |
82 //////////////////////////////////////////////// | |
83 // helper to register a string in a string set | |
84 // | |
85 static char* register_string(string_set &s, char *name); | |
86 static char* register_string(string_set &s, char *name) { | |
87 string_set::iterator i = s.find(name); | |
88 if (i != s.end()) return *i; | |
89 char *x = strdup(name); | |
90 s.insert(x); | |
91 return x; | |
92 } | |
93 | |
94 //////////////////////////////////////////////// | |
95 // register a global string | |
96 // | |
97 static char* register_string(char *name); | |
98 static char* register_string(char *name) { | |
99 return register_string(all_strings, name); | |
100 } | |
101 | |
102 | |
103 //////////////////////////////////////////////// | |
104 // make a unique string | |
105 // | |
106 static char* unique_string(char *name); | |
107 static char* unique_string(char *name) { | |
108 int unique = 2; | |
109 string_set::iterator i = all_strings.find(name); | |
110 if (i == all_strings.end()) return register_string(name); | |
111 while (true) { | |
112 char n[strlen(name)+10]; | |
113 snprintf(n, sizeof(n), "%s %d", name, unique++); | |
114 string_set::iterator i = all_strings.find(n); | |
115 if (i == all_strings.end()) return register_string(n); | |
116 } | |
117 } | |
118 | |
119 | |
120 //////////////////////////////////////////////// | |
121 // remove leading and trailing blanks | |
122 // | |
123 static char *trim(char *name); | |
124 static char *trim(char *name) { | |
125 char *p; | |
126 while (*name == ' ') name++; | |
127 p = name + strlen(name) - 1; | |
128 while ((p >= name) && (*p == ' ')) *p-- = '\0'; | |
129 return name; | |
130 } | |
131 | |
132 | |
133 static void process(pst_desc_ll *d_ptr); | |
134 static void process(pst_desc_ll *d_ptr) { | |
135 pst_item *item = NULL; | |
136 while (d_ptr) { | |
137 if (d_ptr->desc) { | |
138 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr); | |
12
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
139 DEBUG_INFO(("item pointer is %p\n", item)); |
11 | 140 if (item) { |
141 if (item->message_store) { | |
142 // there should only be one message_store, and we have already done it | |
143 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n")); | |
144 } | |
145 | |
146 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) { | |
147 //if this is a non-empty folder other than deleted items, we want to recurse into it | |
148 fprintf(stderr, "entering folder %s\n", item->file_as); | |
149 process(d_ptr->child); | |
150 } else if (item->contact) { | |
151 // deal with a contact | |
152 if (item->type != PST_TYPE_CONTACT) { | |
153 DIE(("type should be contact\n")); | |
154 } | |
155 else if (item->contact == NULL) { // this is an incorrect situation. Inform user | |
156 DIE(("null item contact\n")); | |
157 } else { | |
158 char cn[1000]; | |
159 snprintf(cn, sizeof(cn), "%s %s %s %s", | |
160 single(item->contact->display_name_prefix), | |
161 single(item->contact->first_name), | |
162 single(item->contact->surname), | |
163 single(item->contact->suffix)); | |
164 if (strcmp(cn, " ")) { | |
165 // fprintf(stderr, "\n\n\n"); | |
166 // fprintf(stderr, "access_method %s\n", item->contact->access_method); | |
167 // fprintf(stderr, "account_name %s\n", item->contact->account_name); | |
168 // fprintf(stderr, "address1 %s\n", item->contact->address1); | |
169 // fprintf(stderr, "address1_desc %s\n", item->contact->address1_desc); | |
170 // fprintf(stderr, "address1_transport %s\n", item->contact->address1_transport); | |
171 // fprintf(stderr, "address2 %s\n", item->contact->address2); | |
172 // fprintf(stderr, "address2_desc %s\n", item->contact->address2_desc); | |
173 // fprintf(stderr, "address2_transport %s\n", item->contact->address2_transport); | |
174 // fprintf(stderr, "address3 %s\n", item->contact->address3); | |
175 // fprintf(stderr, "address3_desc %s\n", item->contact->address3_desc); | |
176 // fprintf(stderr, "address3_transport %s\n", item->contact->address3_transport); | |
177 // fprintf(stderr, "assistant_name %s\n", item->contact->assistant_name); | |
178 // fprintf(stderr, "assistant_phone %s\n", item->contact->assistant_phone); | |
179 // fprintf(stderr, "billing_information %s\n", item->contact->billing_information); | |
180 // fprintf(stderr, "business_address %s\n", item->contact->business_address); | |
181 // fprintf(stderr, "business_city %s\n", item->contact->business_city); | |
182 // fprintf(stderr, "business_country %s\n", item->contact->business_country); | |
183 // fprintf(stderr, "business_fax %s\n", item->contact->business_fax); | |
184 // fprintf(stderr, "business_homepage %s\n", item->contact->business_homepage); | |
185 // fprintf(stderr, "business_phone %s\n", item->contact->business_phone); | |
186 // fprintf(stderr, "business_phone2 %s\n", item->contact->business_phone2); | |
187 // fprintf(stderr, "business_po_box %s\n", item->contact->business_po_box); | |
188 // fprintf(stderr, "business_postal_code %s\n", item->contact->business_postal_code); | |
189 // fprintf(stderr, "business_state %s\n", item->contact->business_state); | |
190 // fprintf(stderr, "business_street %s\n", item->contact->business_street); | |
191 // fprintf(stderr, "callback_phone %s\n", item->contact->callback_phone); | |
192 // fprintf(stderr, "car_phone %s\n", item->contact->car_phone); | |
193 // fprintf(stderr, "company_main_phone %s\n", item->contact->company_main_phone); | |
194 // fprintf(stderr, "company_name %s\n", item->contact->company_name); | |
195 // fprintf(stderr, "computer_name %s\n", item->contact->computer_name); | |
196 // fprintf(stderr, "customer_id %s\n", item->contact->customer_id); | |
197 // fprintf(stderr, "def_postal_address %s\n", item->contact->def_postal_address); | |
198 // fprintf(stderr, "department %s\n", item->contact->department); | |
199 // fprintf(stderr, "display_name_prefix %s\n", item->contact->display_name_prefix); | |
200 // fprintf(stderr, "first_name %s\n", item->contact->first_name); | |
201 // fprintf(stderr, "followup %s\n", item->contact->followup); | |
202 // fprintf(stderr, "free_busy_address %s\n", item->contact->free_busy_address); | |
203 // fprintf(stderr, "ftp_site %s\n", item->contact->ftp_site); | |
204 // fprintf(stderr, "fullname %s\n", item->contact->fullname); | |
205 // fprintf(stderr, "gov_id %s\n", item->contact->gov_id); | |
206 // fprintf(stderr, "hobbies %s\n", item->contact->hobbies); | |
207 // fprintf(stderr, "home_address %s\n", item->contact->home_address); | |
208 // fprintf(stderr, "home_city %s\n", item->contact->home_city); | |
209 // fprintf(stderr, "home_country %s\n", item->contact->home_country); | |
210 // fprintf(stderr, "home_fax %s\n", item->contact->home_fax); | |
211 // fprintf(stderr, "home_phone %s\n", item->contact->home_phone); | |
212 // fprintf(stderr, "home_phone2 %s\n", item->contact->home_phone2); | |
213 // fprintf(stderr, "home_po_box %s\n", item->contact->home_po_box); | |
214 // fprintf(stderr, "home_postal_code %s\n", item->contact->home_postal_code); | |
215 // fprintf(stderr, "home_state %s\n", item->contact->home_state); | |
216 // fprintf(stderr, "home_street %s\n", item->contact->home_street); | |
217 // fprintf(stderr, "initials %s\n", item->contact->initials); | |
218 // fprintf(stderr, "isdn_phone %s\n", item->contact->isdn_phone); | |
219 // fprintf(stderr, "job_title %s\n", item->contact->job_title); | |
220 // fprintf(stderr, "keyword %s\n", item->contact->keyword); | |
221 // fprintf(stderr, "language %s\n", item->contact->language); | |
222 // fprintf(stderr, "location %s\n", item->contact->location); | |
223 // fprintf(stderr, "manager_name %s\n", item->contact->manager_name); | |
224 // fprintf(stderr, "middle_name %s\n", item->contact->middle_name); | |
225 // fprintf(stderr, "mileage %s\n", item->contact->mileage); | |
226 // fprintf(stderr, "mobile_phone %s\n", item->contact->mobile_phone); | |
227 // fprintf(stderr, "nickname %s\n", item->contact->nickname); | |
228 // fprintf(stderr, "office_loc %s\n", item->contact->office_loc); | |
229 // fprintf(stderr, "org_id %s\n", item->contact->org_id); | |
230 // fprintf(stderr, "other_address %s\n", item->contact->other_address); | |
231 // fprintf(stderr, "other_city %s\n", item->contact->other_city); | |
232 // fprintf(stderr, "other_country %s\n", item->contact->other_country); | |
233 // fprintf(stderr, "other_phone %s\n", item->contact->other_phone); | |
234 // fprintf(stderr, "other_po_box %s\n", item->contact->other_po_box); | |
235 // fprintf(stderr, "other_postal_code %s\n", item->contact->other_postal_code); | |
236 // fprintf(stderr, "other_state %s\n", item->contact->other_state); | |
237 // fprintf(stderr, "other_street %s\n", item->contact->other_street); | |
238 // fprintf(stderr, "pager_phone %s\n", item->contact->pager_phone); | |
239 // fprintf(stderr, "personal_homepage %s\n", item->contact->personal_homepage); | |
240 // fprintf(stderr, "pref_name %s\n", item->contact->pref_name); | |
241 // fprintf(stderr, "primary_fax %s\n", item->contact->primary_fax); | |
242 // fprintf(stderr, "primary_phone %s\n", item->contact->primary_phone); | |
243 // fprintf(stderr, "profession %s\n", item->contact->profession); | |
244 // fprintf(stderr, "radio_phone %s\n", item->contact->radio_phone); | |
245 // fprintf(stderr, "spouse_name %s\n", item->contact->spouse_name); | |
246 // fprintf(stderr, "suffix %s\n", item->contact->suffix); | |
247 // fprintf(stderr, "surname %s\n", item->contact->surname); | |
248 // fprintf(stderr, "telex %s\n", item->contact->telex); | |
249 // fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name); | |
250 // fprintf(stderr, "ttytdd_phone %s\n", item->contact->ttytdd_phone); | |
251 // have a valid cn | |
252 char *ucn = unique_string(folded(trim(cn))); | |
253 printf("dn: cn=%s, %s\n", ucn, ldap_base); | |
254 printf("cn: %s\n", ucn); | |
255 if (item->contact->first_name) { | |
256 snprintf(cn, sizeof(cn), "%s %s", | |
257 single(item->contact->display_name_prefix), | |
258 single(item->contact->first_name)); | |
259 printf("givenName: %s\n", trim(cn)); | |
260 } | |
261 if (item->contact->surname) { | |
262 snprintf(cn, sizeof(cn), "%s %s", | |
263 single(item->contact->surname), | |
264 single(item->contact->suffix)); | |
265 printf("sn: %s\n", trim(cn)); | |
266 } | |
267 else if (item->contact->company_name) { | |
268 printf("sn: %s\n", single(item->contact->company_name)); | |
269 } | |
270 else | |
271 printf("sn: %s\n", ucn); // use cn as sn if we cannot find something better | |
272 | |
273 if (item->contact->job_title) | |
274 printf("personalTitle: %s\n", single(item->contact->job_title)); | |
275 if (item->contact->company_name) | |
276 printf("company: %s\n", single(item->contact->company_name)); | |
277 if (item->contact->address1 && *item->contact->address1) | |
278 printf("mail: %s\n", single(item->contact->address1)); | |
279 if (item->contact->address2 && *item->contact->address2) | |
280 printf("mail: %s\n", single(item->contact->address2)); | |
281 if (item->contact->address3 && *item->contact->address3) | |
282 printf("mail: %s\n", single(item->contact->address3)); | |
283 if (item->contact->address1a && *item->contact->address1a) | |
284 printf("mail: %s\n", single(item->contact->address1a)); | |
285 if (item->contact->address2a && *item->contact->address2a) | |
286 printf("mail: %s\n", single(item->contact->address2a)); | |
287 if (item->contact->address3a && *item->contact->address3a) | |
288 printf("mail: %s\n", single(item->contact->address3a)); | |
289 if (item->contact->business_address) { | |
290 if (item->contact->business_po_box) | |
291 printf("postalAddress: %s\n", single(item->contact->business_po_box)); | |
292 if (item->contact->business_street) | |
293 multi("postalAddress: %s\n", item->contact->business_street); | |
294 if (item->contact->business_city) | |
295 printf("l: %s\n", single(item->contact->business_city)); | |
296 if (item->contact->business_state) | |
297 printf("st: %s\n", single(item->contact->business_state)); | |
298 if (item->contact->business_postal_code) | |
299 printf("postalCode: %s\n", single(item->contact->business_postal_code)); | |
300 } | |
301 else if (item->contact->home_address) { | |
302 if (item->contact->home_po_box) | |
303 printf("postalAddress: %s\n", single(item->contact->home_po_box)); | |
304 if (item->contact->home_street) | |
305 multi("postalAddress: %s\n", item->contact->home_street); | |
306 if (item->contact->home_city) | |
307 printf("l: %s\n", single(item->contact->home_city)); | |
308 if (item->contact->home_state) | |
309 printf("st: %s\n", single(item->contact->home_state)); | |
310 if (item->contact->home_postal_code) | |
311 printf("postalCode: %s\n", single(item->contact->home_postal_code)); | |
312 } | |
313 else if (item->contact->other_address) { | |
314 if (item->contact->other_po_box) | |
315 printf("postalAddress: %s\n", single(item->contact->other_po_box)); | |
316 if (item->contact->other_street) | |
317 multi("postalAddress: %s\n", item->contact->other_street); | |
318 if (item->contact->other_city) | |
319 printf("l: %s\n", single(item->contact->other_city)); | |
320 if (item->contact->other_state) | |
321 printf("st: %s\n", single(item->contact->other_state)); | |
322 if (item->contact->other_postal_code) | |
323 printf("postalCode: %s\n", single(item->contact->other_postal_code)); | |
324 } | |
325 if (item->contact->business_fax) | |
326 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax)); | |
327 else if (item->contact->home_fax) | |
328 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax)); | |
329 | |
330 if (item->contact->business_phone) | |
331 printf("telephoneNumber: %s\n", single(item->contact->business_phone)); | |
332 if (item->contact->home_phone) | |
333 printf("homePhone: %s\n", single(item->contact->home_phone)); | |
334 | |
335 if (item->contact->car_phone) | |
336 printf("mobile: %s\n", single(item->contact->car_phone)); | |
337 else if (item->contact->mobile_phone) | |
338 printf("mobile: %s\n", single(item->contact->mobile_phone)); | |
339 else if (item->contact->other_phone) | |
340 printf("mobile: %s\n", single(item->contact->other_phone)); | |
341 | |
342 | |
343 if (item->comment) | |
344 printf("description: %s\n", single(item->comment)); | |
345 | |
346 printf("objectClass: %s\n\n", ldap_class); | |
347 } | |
348 } | |
349 } | |
12
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
350 else { |
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
351 DEBUG_INFO(("item is not a contact\n")); |
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
352 } |
11 | 353 } |
354 _pst_freeItem(item); | |
355 } | |
356 d_ptr = d_ptr->next; | |
357 } | |
358 } | |
359 | |
360 | |
361 int main(int argc, char** argv) { | |
362 pst_desc_ll *d_ptr; | |
363 char *fname = NULL; | |
364 char *temp = NULL; //temporary char pointer | |
365 char c; | |
366 prog_name = argv[0]; | |
367 pst_item *item = NULL; | |
368 | |
369 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) { | |
370 switch (c) { | |
371 case 'b': | |
372 ldap_base = optarg; | |
373 temp = strchr(ldap_base, ','); | |
374 if (temp) { | |
375 *temp = '\0'; | |
376 ldap_org = strdup(ldap_base+2); // assume first 2 chars are o= | |
377 *temp = ','; | |
378 } | |
379 break; | |
380 case 'c': | |
381 ldap_class = optarg; | |
382 break; | |
383 case 'h': | |
384 usage(); | |
385 exit(0); | |
386 break; | |
387 case 'V': | |
388 version(); | |
389 exit(0); | |
390 break; | |
391 default: | |
392 usage(); | |
393 exit(1); | |
394 break; | |
395 } | |
396 } | |
397 | |
398 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) { | |
399 fname = argv[optind]; | |
400 } else { | |
401 usage(); | |
402 exit(2); | |
403 } | |
404 | |
405 DEBUG_INIT("pst2ldif.log"); | |
406 DEBUG_REGISTER_CLOSE(); | |
407 DEBUG_ENT("main"); | |
408 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n")); | |
409 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n")); | |
410 | |
411 pst_load_extended_attributes(&pstfile); | |
412 | |
413 d_ptr = pstfile.d_head; // first record is main record | |
414 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr); | |
415 if (!item || !item->message_store) { | |
12
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
416 DEBUG_RET(); |
11 | 417 DIE(("main: Could not get root record\n")); |
418 } | |
419 | |
420 d_ptr = pst_getTopOfFolders(&pstfile, item); | |
421 if (!d_ptr) { | |
12
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
422 DEBUG_RET(); |
11 | 423 DIE(("Top of folders record not found. Cannot continue\n")); |
424 } | |
425 | |
426 _pst_freeItem(item); | |
427 | |
428 // write the ldap header | |
429 printf("dn: %s\n", ldap_base); | |
430 printf("o: %s\n", ldap_org); | |
431 printf("objectClass: organization\n\n"); | |
432 printf("dn: cn=root, %s\n", ldap_base); | |
433 printf("cn: root\n"); | |
434 printf("objectClass: %s\n\n", ldap_class); | |
435 | |
436 process(d_ptr->child); // do the children of TOPF | |
437 pst_close(&pstfile); | |
12
3f627519a92d
properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
carl
parents:
11
diff
changeset
|
438 DEBUG_RET(); |
11 | 439 return 0; |
440 } | |
441 | |
442 | |
443 int usage() { | |
444 version(); | |
445 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name); | |
446 printf("OPTIONS:\n"); | |
447 printf("\t-h\t- Help. This screen\n"); | |
448 printf("\t-V\t- Version. Display program version\n"); | |
449 printf("\t-b ldapbase\t- set the ldap base value\n"); | |
450 printf("\t-c class \t- set the class of the ldap objects\n"); | |
451 return 0; | |
452 } | |
453 | |
454 | |
455 int version() { | |
13 | 456 printf("pst2ldif v%s\n", VERSION); |
11 | 457 #if BYTE_ORDER == BIG_ENDIAN |
458 printf("Big Endian implementation being used.\n"); | |
459 #elif BYTE_ORDER == LITTLE_ENDIAN | |
460 printf("Little Endian implementation being used.\n"); | |
461 #else | |
462 # error "Byte order not supported by this library" | |
463 #endif | |
464 #ifdef __GNUC__ | |
465 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__); | |
466 #endif | |
467 return 0; | |
468 } | |
469 | |
470 | |
471 // my_stristr varies from strstr in that its searches are case-insensitive | |
472 char * my_stristr(char *haystack, char *needle) { | |
473 char *x=haystack, *y=needle, *z = NULL; | |
474 if (haystack == NULL || needle == NULL) | |
475 return NULL; | |
476 while (*y != '\0' && *x != '\0') { | |
477 if (tolower(*y) == tolower(*x)) { | |
478 // move y on one | |
479 y++; | |
480 if (z == NULL) { | |
481 z = x; // store first position in haystack where a match is made | |
482 } | |
483 } else { | |
484 y = needle; // reset y to the beginning of the needle | |
485 z = NULL; // reset the haystack storage point | |
486 } | |
487 x++; // advance the search in the haystack | |
488 } | |
489 return z; | |
490 } | |
491 | |
492 | |
493 char *check_filename(char *fname) { | |
494 char *t = fname; | |
495 if (t == NULL) { | |
496 return fname; | |
497 } | |
498 while ((t = strpbrk(t, "/\\:"))) { | |
499 // while there are characters in the second string that we don't want | |
500 *t = '_'; //replace them with an underscore | |
501 } | |
502 return fname; | |
503 } | |
504 | |
505 | |
506 char *single(char *str) { | |
507 if (!str) return ""; | |
508 char *ret = rfc2426_escape(str); | |
509 char *n = strchr(ret, '\n'); | |
510 if (n) *n = '\0'; | |
511 return ret; | |
512 } | |
513 | |
514 | |
515 char *folded(char *str) { | |
516 if (!str) return ""; | |
517 char *ret = rfc2426_escape(str); | |
518 char *n = ret; | |
519 while (n = strchr(n, '\n')) { | |
520 *n = ' '; | |
521 } | |
522 n = ret; | |
523 while (n = strchr(n, ',')) { | |
524 *n = ' '; | |
525 } | |
526 return ret; | |
527 } | |
528 | |
529 | |
530 void multi(char *fmt, char *str) { | |
531 if (!str) return; | |
532 char *ret = rfc2426_escape(str); | |
533 char *n = ret; | |
534 while (n = strchr(ret, '\n')) { | |
535 *n = '\0'; | |
536 printf(fmt, ret); | |
537 ret = n+1; | |
538 } | |
539 if (*ret) printf(fmt, ret); | |
540 } | |
541 | |
542 | |
543 char *rfc2426_escape(char *str) { | |
544 static char* buf = NULL; | |
545 char *ret, *a, *b; | |
546 int x = 0, y, z; | |
547 if (str == NULL) | |
548 ret = str; | |
549 else { | |
550 | |
551 // calculate space required to escape all the following characters | |
552 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2)); | |
553 z = chr_count(str, '\r'); | |
554 if (y == 0 && z == 0) | |
555 // there isn't any extra space required | |
556 ret = str; | |
557 else { | |
558 buf = (char*) realloc(buf, x+1); | |
559 a = str; | |
560 b = buf; | |
561 while (*a != '\0') { | |
562 switch(*a) { | |
563 // case ',' : | |
564 case '\\': | |
565 case ';' : | |
566 // case '\n': | |
567 *(b++)='\\'; | |
568 *b=*a; | |
569 break; | |
570 case '\r': | |
571 break; | |
572 default: | |
573 *b=*a; | |
574 } | |
575 b++; | |
576 a++; | |
577 } | |
578 *b = '\0'; | |
579 ret = buf; | |
580 } | |
581 } | |
582 return ret; | |
583 } | |
584 | |
585 | |
586 int chr_count(char *str, char x) { | |
587 int r = 0; | |
588 while (*str != '\0') { | |
589 if (*str == x) | |
590 r++; | |
591 str++; | |
592 } | |
593 return r; | |
594 } | |
595 |