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