Mercurial > libpst
comparison pst2ldif.cpp @ 8:2b58cf15aaf7
pst is case sensitive, ldap is not
author | carl |
---|---|
date | Thu, 23 Dec 2004 15:49:19 -0800 |
parents | |
children | 8c724896a28a |
comparison
equal
deleted
inserted
replaced
7:3e3d8b576630 | 8:2b58cf15aaf7 |
---|---|
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); | |
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) | |
277 printf("mail: %s\n", single(item->contact->address1)); | |
278 if (item->contact->address2) | |
279 printf("mail: %s\n", single(item->contact->address2)); | |
280 if (item->contact->address3) | |
281 printf("mail: %s\n", single(item->contact->address3)); | |
282 if (item->contact->business_address) { | |
283 if (item->contact->business_po_box) | |
284 printf("postalAddress: %s\n", single(item->contact->business_po_box)); | |
285 if (item->contact->business_street) | |
286 multi("postalAddress: %s\n", item->contact->business_street); | |
287 if (item->contact->business_city) | |
288 printf("l: %s\n", single(item->contact->business_city)); | |
289 if (item->contact->business_state) | |
290 printf("st: %s\n", single(item->contact->business_state)); | |
291 if (item->contact->business_postal_code) | |
292 printf("postalCode: %s\n", single(item->contact->business_postal_code)); | |
293 } | |
294 else if (item->contact->home_address) { | |
295 if (item->contact->home_po_box) | |
296 printf("postalAddress: %s\n", single(item->contact->home_po_box)); | |
297 if (item->contact->home_street) | |
298 multi("postalAddress: %s\n", item->contact->home_street); | |
299 if (item->contact->home_city) | |
300 printf("l: %s\n", single(item->contact->home_city)); | |
301 if (item->contact->home_state) | |
302 printf("st: %s\n", single(item->contact->home_state)); | |
303 if (item->contact->home_postal_code) | |
304 printf("postalCode: %s\n", single(item->contact->home_postal_code)); | |
305 } | |
306 else if (item->contact->other_address) { | |
307 if (item->contact->other_po_box) | |
308 printf("postalAddress: %s\n", single(item->contact->other_po_box)); | |
309 if (item->contact->other_street) | |
310 multi("postalAddress: %s\n", item->contact->other_street); | |
311 if (item->contact->other_city) | |
312 printf("l: %s\n", single(item->contact->other_city)); | |
313 if (item->contact->other_state) | |
314 printf("st: %s\n", single(item->contact->other_state)); | |
315 if (item->contact->other_postal_code) | |
316 printf("postalCode: %s\n", single(item->contact->other_postal_code)); | |
317 } | |
318 if (item->contact->business_fax) | |
319 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax)); | |
320 else if (item->contact->home_fax) | |
321 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax)); | |
322 | |
323 if (item->contact->business_phone) | |
324 printf("telephoneNumber: %s\n", single(item->contact->business_phone)); | |
325 if (item->contact->home_phone) | |
326 printf("homePhone: %s\n", single(item->contact->home_phone)); | |
327 | |
328 if (item->contact->car_phone) | |
329 printf("mobile: %s\n", single(item->contact->car_phone)); | |
330 else if (item->contact->mobile_phone) | |
331 printf("mobile: %s\n", single(item->contact->mobile_phone)); | |
332 else if (item->contact->other_phone) | |
333 printf("mobile: %s\n", single(item->contact->other_phone)); | |
334 | |
335 | |
336 if (item->comment) | |
337 printf("description: %s\n", single(item->comment)); | |
338 | |
339 printf("objectClass: %s\n\n", ldap_class); | |
340 } | |
341 } | |
342 } | |
343 } | |
344 _pst_freeItem(item); | |
345 } | |
346 d_ptr = d_ptr->next; | |
347 } | |
348 } | |
349 | |
350 | |
351 int main(int argc, char** argv) { | |
352 pst_desc_ll *d_ptr; | |
353 char *fname = NULL; | |
354 char *temp = NULL; //temporary char pointer | |
355 char c; | |
356 prog_name = argv[0]; | |
357 pst_item *item = NULL; | |
358 | |
359 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) { | |
360 switch (c) { | |
361 case 'b': | |
362 ldap_base = optarg; | |
363 temp = strchr(ldap_base, ','); | |
364 if (temp) { | |
365 *temp = '\0'; | |
366 ldap_org = strdup(ldap_base); | |
367 *temp = ','; | |
368 } | |
369 break; | |
370 case 'c': | |
371 ldap_class = optarg; | |
372 break; | |
373 case 'h': | |
374 usage(); | |
375 exit(0); | |
376 break; | |
377 case 'V': | |
378 version(); | |
379 exit(0); | |
380 break; | |
381 default: | |
382 usage(); | |
383 exit(1); | |
384 break; | |
385 } | |
386 } | |
387 | |
388 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) { | |
389 fname = argv[optind]; | |
390 } else { | |
391 usage(); | |
392 exit(2); | |
393 } | |
394 | |
395 DEBUG_INIT("pst2ldif.log"); | |
396 DEBUG_REGISTER_CLOSE(); | |
397 DEBUG_ENT("main"); | |
398 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n")); | |
399 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n")); | |
400 | |
401 pst_load_extended_attributes(&pstfile); | |
402 | |
403 d_ptr = pstfile.d_head; // first record is main record | |
404 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr); | |
405 if (!item || !item->message_store) { | |
406 DIE(("main: Could not get root record\n")); | |
407 } | |
408 | |
409 d_ptr = pst_getTopOfFolders(&pstfile, item); | |
410 if (!d_ptr) { | |
411 DIE(("Top of folders record not found. Cannot continue\n")); | |
412 } | |
413 | |
414 _pst_freeItem(item); | |
415 | |
416 // write the ldap header | |
417 printf("dn: %s\n", ldap_base); | |
418 printf("o: %s\n", ldap_org); | |
419 printf("objectClass: organization\n\n"); | |
420 printf("dn: cn=root, %s\n", ldap_base); | |
421 printf("cn: root\n"); | |
422 printf("objectClass: %s\n\n", ldap_class); | |
423 | |
424 process(d_ptr->child); // do the children of TOPF | |
425 pst_close(&pstfile); | |
426 return 0; | |
427 } | |
428 | |
429 | |
430 int usage() { | |
431 version(); | |
432 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name); | |
433 printf("OPTIONS:\n"); | |
434 printf("\t-h\t- Help. This screen\n"); | |
435 printf("\t-V\t- Version. Display program version\n"); | |
436 printf("\t-b ldapbase\t- set the ldap base value\n"); | |
437 printf("\t-c class \t- set the class of the ldap objects\n"); | |
438 return 0; | |
439 } | |
440 | |
441 | |
442 int version() { | |
443 printf("pst2ldif v%s using LibPST v%s\n", VERSION, PST_VERSION); | |
444 #if BYTE_ORDER == BIG_ENDIAN | |
445 printf("Big Endian implementation being used.\n"); | |
446 #elif BYTE_ORDER == LITTLE_ENDIAN | |
447 printf("Little Endian implementation being used.\n"); | |
448 #else | |
449 # error "Byte order not supported by this library" | |
450 #endif | |
451 #ifdef __GNUC__ | |
452 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__); | |
453 #endif | |
454 return 0; | |
455 } | |
456 | |
457 | |
458 // my_stristr varies from strstr in that its searches are case-insensitive | |
459 char * my_stristr(char *haystack, char *needle) { | |
460 char *x=haystack, *y=needle, *z = NULL; | |
461 if (haystack == NULL || needle == NULL) | |
462 return NULL; | |
463 while (*y != '\0' && *x != '\0') { | |
464 if (tolower(*y) == tolower(*x)) { | |
465 // move y on one | |
466 y++; | |
467 if (z == NULL) { | |
468 z = x; // store first position in haystack where a match is made | |
469 } | |
470 } else { | |
471 y = needle; // reset y to the beginning of the needle | |
472 z = NULL; // reset the haystack storage point | |
473 } | |
474 x++; // advance the search in the haystack | |
475 } | |
476 return z; | |
477 } | |
478 | |
479 | |
480 char *check_filename(char *fname) { | |
481 char *t = fname; | |
482 if (t == NULL) { | |
483 return fname; | |
484 } | |
485 while ((t = strpbrk(t, "/\\:"))) { | |
486 // while there are characters in the second string that we don't want | |
487 *t = '_'; //replace them with an underscore | |
488 } | |
489 return fname; | |
490 } | |
491 | |
492 | |
493 char *single(char *str) { | |
494 if (!str) return ""; | |
495 char *ret = rfc2426_escape(str); | |
496 char *n = strchr(ret, '\n'); | |
497 if (n) *n = '\0'; | |
498 return ret; | |
499 } | |
500 | |
501 | |
502 char *folded(char *str) { | |
503 if (!str) return ""; | |
504 char *ret = rfc2426_escape(str); | |
505 char *n = ret; | |
506 while (n = strchr(n, '\n')) { | |
507 *n = ' '; | |
508 } | |
509 n = ret; | |
510 while (n = strchr(n, ',')) { | |
511 *n = ' '; | |
512 } | |
513 return ret; | |
514 } | |
515 | |
516 | |
517 void multi(char *fmt, char *str) { | |
518 if (!str) return; | |
519 char *ret = rfc2426_escape(str); | |
520 char *n = ret; | |
521 while (n = strchr(ret, '\n')) { | |
522 *n = '\0'; | |
523 printf(fmt, ret); | |
524 ret = n+1; | |
525 } | |
526 if (*ret) printf(fmt, ret); | |
527 } | |
528 | |
529 | |
530 char *rfc2426_escape(char *str) { | |
531 static char* buf = NULL; | |
532 char *ret, *a, *b; | |
533 int x = 0, y, z; | |
534 if (str == NULL) | |
535 ret = str; | |
536 else { | |
537 | |
538 // calculate space required to escape all the following characters | |
539 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2)); | |
540 z = chr_count(str, '\r'); | |
541 if (y == 0 && z == 0) | |
542 // there isn't any extra space required | |
543 ret = str; | |
544 else { | |
545 buf = (char*) realloc(buf, x+1); | |
546 a = str; | |
547 b = buf; | |
548 while (*a != '\0') { | |
549 switch(*a) { | |
550 // case ',' : | |
551 case '\\': | |
552 case ';' : | |
553 // case '\n': | |
554 *(b++)='\\'; | |
555 *b=*a; | |
556 break; | |
557 case '\r': | |
558 break; | |
559 default: | |
560 *b=*a; | |
561 } | |
562 b++; | |
563 a++; | |
564 } | |
565 *b = '\0'; | |
566 ret = buf; | |
567 } | |
568 } | |
569 return ret; | |
570 } | |
571 | |
572 | |
573 int chr_count(char *str, char x) { | |
574 int r = 0; | |
575 while (*str != '\0') { | |
576 if (*str == x) | |
577 r++; | |
578 str++; | |
579 } | |
580 return r; | |
581 } | |
582 |