annotate lspst.c @ 10:a818f3c2e589

fix tree walk, we now use the item counts in the node blocks
author carl
date Fri, 17 Feb 2006 15:48:38 -0800
parents fac01758bd41
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
1 /***
fac01758bd41 Initial revision
carl
parents:
diff changeset
2 * lspst.c
fac01758bd41 Initial revision
carl
parents:
diff changeset
3 * Part of the LibPST project
fac01758bd41 Initial revision
carl
parents:
diff changeset
4 * Author: Joe Nahmias <joe@nahmias.net>
fac01758bd41 Initial revision
carl
parents:
diff changeset
5 * Based on readpst.c by by David Smith <dave.s@earthcorp.com>
fac01758bd41 Initial revision
carl
parents:
diff changeset
6 *
fac01758bd41 Initial revision
carl
parents:
diff changeset
7 */
fac01758bd41 Initial revision
carl
parents:
diff changeset
8
fac01758bd41 Initial revision
carl
parents:
diff changeset
9 // header file includes {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
10 #include <stdio.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
11 #include <stdlib.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
12 #include <time.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
13 #include <string.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
14 #include <ctype.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
15 #include <errno.h>
fac01758bd41 Initial revision
carl
parents:
diff changeset
16
fac01758bd41 Initial revision
carl
parents:
diff changeset
17 #include "libpst.h"
fac01758bd41 Initial revision
carl
parents:
diff changeset
18 #include "define.h"
fac01758bd41 Initial revision
carl
parents:
diff changeset
19 #include "timeconv.h"
fac01758bd41 Initial revision
carl
parents:
diff changeset
20 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
21 // struct file_ll {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
22 struct file_ll {
fac01758bd41 Initial revision
carl
parents:
diff changeset
23 char *name;
fac01758bd41 Initial revision
carl
parents:
diff changeset
24 char *dname;
fac01758bd41 Initial revision
carl
parents:
diff changeset
25 FILE * output;
fac01758bd41 Initial revision
carl
parents:
diff changeset
26 int32_t stored_count;
fac01758bd41 Initial revision
carl
parents:
diff changeset
27 int32_t email_count;
fac01758bd41 Initial revision
carl
parents:
diff changeset
28 int32_t skip_count;
fac01758bd41 Initial revision
carl
parents:
diff changeset
29 int32_t type;
fac01758bd41 Initial revision
carl
parents:
diff changeset
30 struct file_ll *next;
fac01758bd41 Initial revision
carl
parents:
diff changeset
31 };
fac01758bd41 Initial revision
carl
parents:
diff changeset
32 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
33 // Function Declarations {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
34 void canonicalize_filename(char *fname);
fac01758bd41 Initial revision
carl
parents:
diff changeset
35 int chr_count(char *str, char x);
fac01758bd41 Initial revision
carl
parents:
diff changeset
36 void debug_print(char *fmt, ...);
fac01758bd41 Initial revision
carl
parents:
diff changeset
37 char *rfc2426_escape(char *str);
fac01758bd41 Initial revision
carl
parents:
diff changeset
38 char *rfc2445_datetime_format(FILETIME *ft);
fac01758bd41 Initial revision
carl
parents:
diff changeset
39 // }}}1
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
40 #ifndef DEBUG_MAIN
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
41 #define DEBUG_MAIN(x) debug_print x;
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
42 #endif
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
43 // int main(int argc, char** argv) {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
44 int main(int argc, char** argv) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
45
fac01758bd41 Initial revision
carl
parents:
diff changeset
46 // declarations {{{2
fac01758bd41 Initial revision
carl
parents:
diff changeset
47 pst_item *item = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
48 pst_file pstfile;
fac01758bd41 Initial revision
carl
parents:
diff changeset
49 pst_desc_ll *d_ptr;
fac01758bd41 Initial revision
carl
parents:
diff changeset
50 char *temp = NULL; //temporary char pointer
fac01758bd41 Initial revision
carl
parents:
diff changeset
51 int skip_child = 0;
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
52 struct file_ll *f, *head;
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
53 // }}}2
fac01758bd41 Initial revision
carl
parents:
diff changeset
54
fac01758bd41 Initial revision
carl
parents:
diff changeset
55 if (argc <= 1)
fac01758bd41 Initial revision
carl
parents:
diff changeset
56 DIE(("Missing PST filename.\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
57
fac01758bd41 Initial revision
carl
parents:
diff changeset
58 // Open PST file
fac01758bd41 Initial revision
carl
parents:
diff changeset
59 if ( pst_open(&pstfile, argv[1], "r") )
fac01758bd41 Initial revision
carl
parents:
diff changeset
60 DIE(("Error opening File\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
61 // Load PST index
fac01758bd41 Initial revision
carl
parents:
diff changeset
62 if ( pst_load_index(&pstfile) )
fac01758bd41 Initial revision
carl
parents:
diff changeset
63 DIE(("Index Error\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
64 pst_load_extended_attributes(&pstfile);
fac01758bd41 Initial revision
carl
parents:
diff changeset
65
fac01758bd41 Initial revision
carl
parents:
diff changeset
66 d_ptr = pstfile.d_head; // first record is main record
fac01758bd41 Initial revision
carl
parents:
diff changeset
67 if ((item = _pst_parse_item(&pstfile, d_ptr)) == NULL || item->message_store == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
68 DIE(("main: Could not get root record\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
69 }
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
70
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
71 // default the file_as to the same as the main filename if it doesn't exist
fac01758bd41 Initial revision
carl
parents:
diff changeset
72 if (item->file_as == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
73 if ((temp = strrchr(argv[1], '/')) == NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
74 if ((temp = strrchr(argv[1], '\\')) == NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
75 temp = argv[1];
fac01758bd41 Initial revision
carl
parents:
diff changeset
76 else
fac01758bd41 Initial revision
carl
parents:
diff changeset
77 temp++; // get past the "\\"
fac01758bd41 Initial revision
carl
parents:
diff changeset
78 else
fac01758bd41 Initial revision
carl
parents:
diff changeset
79 temp++; // get past the "/"
fac01758bd41 Initial revision
carl
parents:
diff changeset
80 item->file_as = (char*)xmalloc(strlen(temp)+1);
fac01758bd41 Initial revision
carl
parents:
diff changeset
81 strcpy(item->file_as, temp);
fac01758bd41 Initial revision
carl
parents:
diff changeset
82 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
83 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
fac01758bd41 Initial revision
carl
parents:
diff changeset
84
fac01758bd41 Initial revision
carl
parents:
diff changeset
85 // setup head file_ll
fac01758bd41 Initial revision
carl
parents:
diff changeset
86 head = (struct file_ll*) malloc(sizeof(struct file_ll));
fac01758bd41 Initial revision
carl
parents:
diff changeset
87 memset(head, 0, sizeof(struct file_ll));
fac01758bd41 Initial revision
carl
parents:
diff changeset
88 head->email_count = 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
89 head->skip_count = 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
90 head->next = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
91 head->name = "mbox";
fac01758bd41 Initial revision
carl
parents:
diff changeset
92 head->dname = (char*) malloc(strlen(item->file_as)+1);
fac01758bd41 Initial revision
carl
parents:
diff changeset
93 strcpy(head->dname, item->file_as);
fac01758bd41 Initial revision
carl
parents:
diff changeset
94 head->type = item->type;
fac01758bd41 Initial revision
carl
parents:
diff changeset
95 DEBUG_MAIN(("head @ %p: name = '%s', dname = '%s', next = %p.\n", head, head->name, head->dname, head->next));
fac01758bd41 Initial revision
carl
parents:
diff changeset
96
fac01758bd41 Initial revision
carl
parents:
diff changeset
97 if ((d_ptr = pst_getTopOfFolders(&pstfile, item)) == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
98 DIE(("Top of folders record not found. Cannot continue\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
99 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
100 DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
fac01758bd41 Initial revision
carl
parents:
diff changeset
101
fac01758bd41 Initial revision
carl
parents:
diff changeset
102 if (item){
fac01758bd41 Initial revision
carl
parents:
diff changeset
103 _pst_freeItem(item);
fac01758bd41 Initial revision
carl
parents:
diff changeset
104 item = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
105 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
106
fac01758bd41 Initial revision
carl
parents:
diff changeset
107 d_ptr = d_ptr->child; // do the children of TOPF
fac01758bd41 Initial revision
carl
parents:
diff changeset
108 DEBUG_MAIN(("d_ptr(TOF->child) = %p.\n", d_ptr));
fac01758bd41 Initial revision
carl
parents:
diff changeset
109
fac01758bd41 Initial revision
carl
parents:
diff changeset
110 DEBUG_MAIN(("main: About to do email stuff\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
111 while (d_ptr != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
112 // Process d_ptr {{{2
fac01758bd41 Initial revision
carl
parents:
diff changeset
113 DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr));
fac01758bd41 Initial revision
carl
parents:
diff changeset
114 if (d_ptr->desc == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
115 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
116 f->skip_count++;
fac01758bd41 Initial revision
carl
parents:
diff changeset
117 goto check_parent;
fac01758bd41 Initial revision
carl
parents:
diff changeset
118 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
119 DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id));
fac01758bd41 Initial revision
carl
parents:
diff changeset
120
fac01758bd41 Initial revision
carl
parents:
diff changeset
121 item = _pst_parse_item(&pstfile, d_ptr);
fac01758bd41 Initial revision
carl
parents:
diff changeset
122 DEBUG_MAIN(("main: About to process item @ %p.\n", item));
fac01758bd41 Initial revision
carl
parents:
diff changeset
123 if (item != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
124
fac01758bd41 Initial revision
carl
parents:
diff changeset
125 // there should only be one message_store, and we have already
fac01758bd41 Initial revision
carl
parents:
diff changeset
126 // done it
fac01758bd41 Initial revision
carl
parents:
diff changeset
127 if (item->message_store != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
128 DIE(("ERROR(main): A second message_store has been found.\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
129 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
130
fac01758bd41 Initial revision
carl
parents:
diff changeset
131 if (item->folder != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
132 // Process Folder item {{{3
fac01758bd41 Initial revision
carl
parents:
diff changeset
133 // if this is a folder, we want to recurse into it
fac01758bd41 Initial revision
carl
parents:
diff changeset
134 printf("Folder");
fac01758bd41 Initial revision
carl
parents:
diff changeset
135 if (item->file_as != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
136 printf("\t%s/", item->file_as);
fac01758bd41 Initial revision
carl
parents:
diff changeset
137 printf("\n");
fac01758bd41 Initial revision
carl
parents:
diff changeset
138
fac01758bd41 Initial revision
carl
parents:
diff changeset
139 DEBUG_MAIN(("main: I think I may try to go into folder \"%s\"\n", item->file_as));
fac01758bd41 Initial revision
carl
parents:
diff changeset
140 f = (struct file_ll*) malloc(sizeof(struct file_ll));
fac01758bd41 Initial revision
carl
parents:
diff changeset
141 memset(f, 0, sizeof(struct file_ll));
fac01758bd41 Initial revision
carl
parents:
diff changeset
142 f->next = head;
fac01758bd41 Initial revision
carl
parents:
diff changeset
143 f->email_count = 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
144 f->type = item->type;
fac01758bd41 Initial revision
carl
parents:
diff changeset
145 f->stored_count = item->folder->email_count;
fac01758bd41 Initial revision
carl
parents:
diff changeset
146 head = f;
fac01758bd41 Initial revision
carl
parents:
diff changeset
147 f->name = "mbox";
fac01758bd41 Initial revision
carl
parents:
diff changeset
148 f->dname = (char*) xmalloc(strlen(item->file_as)+1);
fac01758bd41 Initial revision
carl
parents:
diff changeset
149 strcpy(f->dname, item->file_as);
fac01758bd41 Initial revision
carl
parents:
diff changeset
150
fac01758bd41 Initial revision
carl
parents:
diff changeset
151 DEBUG_MAIN(("main: f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as));
fac01758bd41 Initial revision
carl
parents:
diff changeset
152 canonicalize_filename(f->name);
fac01758bd41 Initial revision
carl
parents:
diff changeset
153
fac01758bd41 Initial revision
carl
parents:
diff changeset
154 if (d_ptr->child != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
155 d_ptr = d_ptr->child;
fac01758bd41 Initial revision
carl
parents:
diff changeset
156 skip_child = 1;
fac01758bd41 Initial revision
carl
parents:
diff changeset
157 } else {
fac01758bd41 Initial revision
carl
parents:
diff changeset
158 DEBUG_MAIN(("main: Folder has NO children. Creating directory, and closing again\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
159 // printf("\tNo items to process in folder \"%s\", should have been %i\n", f->dname, f->stored_count);
fac01758bd41 Initial revision
carl
parents:
diff changeset
160 head = f->next;
fac01758bd41 Initial revision
carl
parents:
diff changeset
161 if (f->output != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
162 fclose(f->output);
fac01758bd41 Initial revision
carl
parents:
diff changeset
163 free(f->dname);
fac01758bd41 Initial revision
carl
parents:
diff changeset
164 free(f->name);
fac01758bd41 Initial revision
carl
parents:
diff changeset
165 free(f);
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
166
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
167 f = head;
fac01758bd41 Initial revision
carl
parents:
diff changeset
168 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
169 _pst_freeItem(item);
fac01758bd41 Initial revision
carl
parents:
diff changeset
170 item = NULL; // just for the odd situations!
fac01758bd41 Initial revision
carl
parents:
diff changeset
171 goto check_parent;
fac01758bd41 Initial revision
carl
parents:
diff changeset
172 // }}}3
fac01758bd41 Initial revision
carl
parents:
diff changeset
173 } else if (item->contact != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
174 // Process Contact item {{{3
fac01758bd41 Initial revision
carl
parents:
diff changeset
175 if (f->type != PST_TYPE_CONTACT) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
176 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. "
fac01758bd41 Initial revision
carl
parents:
diff changeset
177 "Will process anyway\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
178 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
179 if (item->type != PST_TYPE_CONTACT) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
180 DEBUG_MAIN(("main: I have an item that has contact info, but doesn't say that"
fac01758bd41 Initial revision
carl
parents:
diff changeset
181 " it is a contact. Type is \"%s\"\n", item->ascii_type));
fac01758bd41 Initial revision
carl
parents:
diff changeset
182 DEBUG_MAIN(("main: Processing anyway\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
183 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
184
fac01758bd41 Initial revision
carl
parents:
diff changeset
185 printf("Contact");
fac01758bd41 Initial revision
carl
parents:
diff changeset
186 if (item->contact->fullname != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
187 printf("\t%s", rfc2426_escape(item->contact->fullname));
fac01758bd41 Initial revision
carl
parents:
diff changeset
188 printf("\n");
fac01758bd41 Initial revision
carl
parents:
diff changeset
189 // }}}3
fac01758bd41 Initial revision
carl
parents:
diff changeset
190 } else if (item->email != NULL &&
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
191 (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
192 // Process Email item {{{3
fac01758bd41 Initial revision
carl
parents:
diff changeset
193 printf("Email");
fac01758bd41 Initial revision
carl
parents:
diff changeset
194 if (item->email->outlook_sender_name != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
195 printf("\tFrom: %s", item->email->outlook_sender_name);
fac01758bd41 Initial revision
carl
parents:
diff changeset
196 if (item->email->subject->subj != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
197 printf("\tSubject: %s", item->email->subject->subj);
fac01758bd41 Initial revision
carl
parents:
diff changeset
198 printf("\n");
fac01758bd41 Initial revision
carl
parents:
diff changeset
199 // }}}3
fac01758bd41 Initial revision
carl
parents:
diff changeset
200 } else if (item->type == PST_TYPE_JOURNAL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
201 // Process Journal item {{{3
fac01758bd41 Initial revision
carl
parents:
diff changeset
202 if (f->type != PST_TYPE_JOURNAL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
203 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
204 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
205
fac01758bd41 Initial revision
carl
parents:
diff changeset
206 printf("Journal\t%s\n", rfc2426_escape(item->email->subject->subj));
fac01758bd41 Initial revision
carl
parents:
diff changeset
207 // }}}3
fac01758bd41 Initial revision
carl
parents:
diff changeset
208 } else if (item->type == PST_TYPE_APPOINTMENT) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
209 // Process Calendar Appointment item {{{3
fac01758bd41 Initial revision
carl
parents:
diff changeset
210 // deal with Calendar appointments
fac01758bd41 Initial revision
carl
parents:
diff changeset
211
fac01758bd41 Initial revision
carl
parents:
diff changeset
212 DEBUG_MAIN(("main: Processing Appointment Entry\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
213 if (f->type != PST_TYPE_APPOINTMENT) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
214 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
215 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
216
fac01758bd41 Initial revision
carl
parents:
diff changeset
217 printf("Appointment");
fac01758bd41 Initial revision
carl
parents:
diff changeset
218 if (item->email != NULL && item->email->subject != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
219 printf("\tSUMMARY: %s", rfc2426_escape(item->email->subject->subj));
fac01758bd41 Initial revision
carl
parents:
diff changeset
220 if (item->appointment != NULL && item->appointment->start != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
221 printf("\tSTART: %s", rfc2445_datetime_format(item->appointment->start));
fac01758bd41 Initial revision
carl
parents:
diff changeset
222 printf("\n");
fac01758bd41 Initial revision
carl
parents:
diff changeset
223
fac01758bd41 Initial revision
carl
parents:
diff changeset
224 // }}}3
fac01758bd41 Initial revision
carl
parents:
diff changeset
225 } else {
fac01758bd41 Initial revision
carl
parents:
diff changeset
226 f->skip_count++;
fac01758bd41 Initial revision
carl
parents:
diff changeset
227 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n", \
fac01758bd41 Initial revision
carl
parents:
diff changeset
228 item->type, item->ascii_type));
fac01758bd41 Initial revision
carl
parents:
diff changeset
229 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
230 } else {
fac01758bd41 Initial revision
carl
parents:
diff changeset
231 f->skip_count++;
fac01758bd41 Initial revision
carl
parents:
diff changeset
232 DEBUG_MAIN(("main: A NULL item was seen\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
233 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
234
fac01758bd41 Initial revision
carl
parents:
diff changeset
235 check_parent:
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
236 // _pst_freeItem(item);
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
237 while (!skip_child && d_ptr->next == NULL && d_ptr->parent != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
238 DEBUG_MAIN(("main: Going to Parent\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
239 head = f->next;
fac01758bd41 Initial revision
carl
parents:
diff changeset
240 if (f->output != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
241 fclose(f->output);
fac01758bd41 Initial revision
carl
parents:
diff changeset
242 DEBUG_MAIN(("main: Email Count for folder %s is %i\n", f->dname, f->email_count));
fac01758bd41 Initial revision
carl
parents:
diff changeset
243 /*
fac01758bd41 Initial revision
carl
parents:
diff changeset
244 printf("\t\"%s\" - %i items done, skipped %i, should have been %i\n", \
fac01758bd41 Initial revision
carl
parents:
diff changeset
245 f->dname, f->email_count, f->skip_count, f->stored_count);
fac01758bd41 Initial revision
carl
parents:
diff changeset
246 */
fac01758bd41 Initial revision
carl
parents:
diff changeset
247
fac01758bd41 Initial revision
carl
parents:
diff changeset
248 free(f->name);
fac01758bd41 Initial revision
carl
parents:
diff changeset
249 free(f->dname);
fac01758bd41 Initial revision
carl
parents:
diff changeset
250 free(f);
fac01758bd41 Initial revision
carl
parents:
diff changeset
251 f = head;
fac01758bd41 Initial revision
carl
parents:
diff changeset
252 if (head == NULL) { //we can't go higher. Must be at start?
fac01758bd41 Initial revision
carl
parents:
diff changeset
253 DEBUG_MAIN(("main: We are now trying to go above the highest level. We must be finished\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
254 break; //from main while loop
fac01758bd41 Initial revision
carl
parents:
diff changeset
255 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
256 d_ptr = d_ptr->parent;
fac01758bd41 Initial revision
carl
parents:
diff changeset
257 skip_child = 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
258 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
259
fac01758bd41 Initial revision
carl
parents:
diff changeset
260 if (item != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
261 DEBUG_MAIN(("main: Freeing memory used by item\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
262 _pst_freeItem(item);
fac01758bd41 Initial revision
carl
parents:
diff changeset
263 item = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
264 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
265
fac01758bd41 Initial revision
carl
parents:
diff changeset
266 if (!skip_child)
fac01758bd41 Initial revision
carl
parents:
diff changeset
267 d_ptr = d_ptr->next;
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
268 else
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
269 skip_child = 0;
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
270
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
271 if (d_ptr == NULL) { DEBUG_MAIN(("main: d_ptr is now NULL\n")); }
fac01758bd41 Initial revision
carl
parents:
diff changeset
272
fac01758bd41 Initial revision
carl
parents:
diff changeset
273 // }}}2
fac01758bd41 Initial revision
carl
parents:
diff changeset
274 } // end while(d_ptr != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
275 DEBUG_MAIN(("main: Finished.\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
276
fac01758bd41 Initial revision
carl
parents:
diff changeset
277 // Cleanup {{{2
fac01758bd41 Initial revision
carl
parents:
diff changeset
278 pst_close(&pstfile);
fac01758bd41 Initial revision
carl
parents:
diff changeset
279 while (f != NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
280 if (f->output != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
281 fclose(f->output);
fac01758bd41 Initial revision
carl
parents:
diff changeset
282 free(f->name);
fac01758bd41 Initial revision
carl
parents:
diff changeset
283 free(f->dname);
fac01758bd41 Initial revision
carl
parents:
diff changeset
284
fac01758bd41 Initial revision
carl
parents:
diff changeset
285 head = f->next;
fac01758bd41 Initial revision
carl
parents:
diff changeset
286 free(f);
fac01758bd41 Initial revision
carl
parents:
diff changeset
287 f = head;
fac01758bd41 Initial revision
carl
parents:
diff changeset
288 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
289 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
290 // }}}2
fac01758bd41 Initial revision
carl
parents:
diff changeset
291
fac01758bd41 Initial revision
carl
parents:
diff changeset
292 return 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
293 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
294 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
295 // void canonicalize_filename(char *fname) {{{1
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
296 // This function will make sure that a filename is in cannonical form. That
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
297 // is, it will replace any slashes, backslashes, or colons with underscores.
fac01758bd41 Initial revision
carl
parents:
diff changeset
298 void canonicalize_filename(char *fname) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
299 DEBUG_ENT("canonicalize_filename");
fac01758bd41 Initial revision
carl
parents:
diff changeset
300 if (fname == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
301 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
302 return;
fac01758bd41 Initial revision
carl
parents:
diff changeset
303 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
304 while ((fname = strpbrk(fname, "/\\:")) != NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
305 *fname = '_';
fac01758bd41 Initial revision
carl
parents:
diff changeset
306 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
307 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
308 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
309 // int chr_count(char *str, char x) {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
310 int chr_count(char *str, char x) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
311 int r = 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
312 if (str == NULL) return 0;
fac01758bd41 Initial revision
carl
parents:
diff changeset
313 while (*str != '\0') {
fac01758bd41 Initial revision
carl
parents:
diff changeset
314 if (*str == x)
fac01758bd41 Initial revision
carl
parents:
diff changeset
315 r++;
fac01758bd41 Initial revision
carl
parents:
diff changeset
316 str++;
fac01758bd41 Initial revision
carl
parents:
diff changeset
317 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
318 return r;
fac01758bd41 Initial revision
carl
parents:
diff changeset
319 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
320 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
321 // void debug_print(char *fmt, ...) {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
322 void debug_print(char *fmt, ...) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
323 // shamlessly stolen from minprintf() in K&R pg. 156
fac01758bd41 Initial revision
carl
parents:
diff changeset
324 va_list ap;
fac01758bd41 Initial revision
carl
parents:
diff changeset
325 char *p, *sval;
fac01758bd41 Initial revision
carl
parents:
diff changeset
326 void *pval;
fac01758bd41 Initial revision
carl
parents:
diff changeset
327 int ival;
fac01758bd41 Initial revision
carl
parents:
diff changeset
328 double dval;
fac01758bd41 Initial revision
carl
parents:
diff changeset
329 FILE *fp = stderr;
fac01758bd41 Initial revision
carl
parents:
diff changeset
330
fac01758bd41 Initial revision
carl
parents:
diff changeset
331 va_start(ap, fmt);
fac01758bd41 Initial revision
carl
parents:
diff changeset
332 for(p = fmt; *p; p++) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
333 if (*p != '%') {
fac01758bd41 Initial revision
carl
parents:
diff changeset
334 fputc(*p, fp);
fac01758bd41 Initial revision
carl
parents:
diff changeset
335 continue;
fac01758bd41 Initial revision
carl
parents:
diff changeset
336 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
337 switch (tolower(*++p)) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
338 case 'd': case 'i':
fac01758bd41 Initial revision
carl
parents:
diff changeset
339 ival = va_arg(ap, int);
fac01758bd41 Initial revision
carl
parents:
diff changeset
340 fprintf(fp, "%d", ival);
fac01758bd41 Initial revision
carl
parents:
diff changeset
341 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
342 case 'f':
fac01758bd41 Initial revision
carl
parents:
diff changeset
343 dval = va_arg(ap, double);
fac01758bd41 Initial revision
carl
parents:
diff changeset
344 fprintf(fp, "%f", dval);
fac01758bd41 Initial revision
carl
parents:
diff changeset
345 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
346 case 's':
fac01758bd41 Initial revision
carl
parents:
diff changeset
347 for (sval = va_arg(ap, char *); *sval; ++sval)
fac01758bd41 Initial revision
carl
parents:
diff changeset
348 fputc(*sval, fp);
fac01758bd41 Initial revision
carl
parents:
diff changeset
349 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
350 case 'p':
fac01758bd41 Initial revision
carl
parents:
diff changeset
351 pval = va_arg(ap, void *);
fac01758bd41 Initial revision
carl
parents:
diff changeset
352 fprintf(fp, "%p", pval);
fac01758bd41 Initial revision
carl
parents:
diff changeset
353 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
354 case 'x':
fac01758bd41 Initial revision
carl
parents:
diff changeset
355 ival = va_arg(ap, int);
fac01758bd41 Initial revision
carl
parents:
diff changeset
356 fprintf(fp, "%#010x", ival);
fac01758bd41 Initial revision
carl
parents:
diff changeset
357 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
358 default:
fac01758bd41 Initial revision
carl
parents:
diff changeset
359 fputc(*p, fp);
fac01758bd41 Initial revision
carl
parents:
diff changeset
360 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
361 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
362 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
363 va_end(ap);
fac01758bd41 Initial revision
carl
parents:
diff changeset
364 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
365 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
366 // char *rfc2426_escape(char *str) {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
367 char *rfc2426_escape(char *str) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
368 static char *buf = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
369 char *a, *b;
fac01758bd41 Initial revision
carl
parents:
diff changeset
370 int y, z;
fac01758bd41 Initial revision
carl
parents:
diff changeset
371
fac01758bd41 Initial revision
carl
parents:
diff changeset
372 DEBUG_ENT("rfc2426_escape");
fac01758bd41 Initial revision
carl
parents:
diff changeset
373 if (str == NULL) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
374 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
375 return NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
376 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
377
fac01758bd41 Initial revision
carl
parents:
diff changeset
378 // calculate space required to escape all the commas, semi-colons, backslashes, and newlines
fac01758bd41 Initial revision
carl
parents:
diff changeset
379 y = chr_count(str, ',') + chr_count(str, '\\') + chr_count(str, ';') + chr_count(str, '\n');
fac01758bd41 Initial revision
carl
parents:
diff changeset
380 // count how many carriage-returns we have to skip
fac01758bd41 Initial revision
carl
parents:
diff changeset
381 z = chr_count(str, '\r');
fac01758bd41 Initial revision
carl
parents:
diff changeset
382
fac01758bd41 Initial revision
carl
parents:
diff changeset
383 if (y == 0 && z == 0) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
384 // there isn't any work required
fac01758bd41 Initial revision
carl
parents:
diff changeset
385 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
386 return str;
fac01758bd41 Initial revision
carl
parents:
diff changeset
387 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
388
fac01758bd41 Initial revision
carl
parents:
diff changeset
389 buf = (char *) realloc( buf, strlen(str) + y - z + 1 );
fac01758bd41 Initial revision
carl
parents:
diff changeset
390 for (a = str, b = buf; *a != '\0'; ++a, ++b)
fac01758bd41 Initial revision
carl
parents:
diff changeset
391 switch (*a) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
392 case ',' : case '\\': case ';' : case '\n':
fac01758bd41 Initial revision
carl
parents:
diff changeset
393 // insert backslash to escape
fac01758bd41 Initial revision
carl
parents:
diff changeset
394 *(b++) = '\\';
fac01758bd41 Initial revision
carl
parents:
diff changeset
395 *b = *a;
fac01758bd41 Initial revision
carl
parents:
diff changeset
396 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
397 case '\r':
fac01758bd41 Initial revision
carl
parents:
diff changeset
398 // skip
fac01758bd41 Initial revision
carl
parents:
diff changeset
399 break;
fac01758bd41 Initial revision
carl
parents:
diff changeset
400 default:
fac01758bd41 Initial revision
carl
parents:
diff changeset
401 *b = *a;
fac01758bd41 Initial revision
carl
parents:
diff changeset
402 }
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
403 *b = '\0'; // NUL-terminate the string
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
404
fac01758bd41 Initial revision
carl
parents:
diff changeset
405 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
406 return buf;
10
a818f3c2e589 fix tree walk, we now use the item counts in the node blocks
carl
parents: 3
diff changeset
407 }
3
fac01758bd41 Initial revision
carl
parents:
diff changeset
408 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
409 // char *rfc2445_datetime_format(FILETIME *ft) {{{1
fac01758bd41 Initial revision
carl
parents:
diff changeset
410 char *rfc2445_datetime_format(FILETIME *ft) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
411 static char* buffer = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
412 struct tm *stm = NULL;
fac01758bd41 Initial revision
carl
parents:
diff changeset
413 DEBUG_ENT("rfc2445_datetime_format");
fac01758bd41 Initial revision
carl
parents:
diff changeset
414 if (buffer == NULL)
fac01758bd41 Initial revision
carl
parents:
diff changeset
415 buffer = malloc(30); // should be enough
fac01758bd41 Initial revision
carl
parents:
diff changeset
416 stm = fileTimeToStructTM(ft);
fac01758bd41 Initial revision
carl
parents:
diff changeset
417 if (strftime(buffer, 30, "%Y%m%dT%H%M%SZ", stm)==0) {
fac01758bd41 Initial revision
carl
parents:
diff changeset
418 DEBUG_INFO(("Problem occured formatting date\n"));
fac01758bd41 Initial revision
carl
parents:
diff changeset
419 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
420 DEBUG_RET();
fac01758bd41 Initial revision
carl
parents:
diff changeset
421 return buffer;
fac01758bd41 Initial revision
carl
parents:
diff changeset
422 }
fac01758bd41 Initial revision
carl
parents:
diff changeset
423 // }}}1
fac01758bd41 Initial revision
carl
parents:
diff changeset
424
fac01758bd41 Initial revision
carl
parents:
diff changeset
425 // vim:sw=4 ts=4:
fac01758bd41 Initial revision
carl
parents:
diff changeset
426 // vim600: set foldlevel=0 foldmethod=marker: