pst2dii.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2008 Carl Byington - 510 Software Group, released under
00004 the GPL version 2 or any later version at your choice available at
00005 http://www.fsf.org/licenses/gpl.txt
00006 
00007 Based on readpst.c by David Smith
00008 
00009 */
00010 #include <iostream>
00011 #include <string>
00012 #include <vector>
00013 
00014 using namespace std;
00015 
00016 extern "C" {
00017     #include "define.h"
00018     #include "lzfu.h"
00019 }
00020 
00021 struct file_ll {
00022     string  name;
00023     int32_t stored_count;
00024     int32_t email_count;
00025     int32_t skip_count;
00026     int32_t type;
00027     file_ll() {
00028         stored_count = 0;
00029         email_count  = 0;
00030         skip_count   = 0;
00031         type         = 0;
00032     };
00033 };
00034 
00035 // global settings
00036 const char*    convert = "/usr/bin/convert";     // fully qualified path of the convert program from image magick
00037 const char*    prog_name = NULL;          // our arg0 name
00038 const char*    bates_prefix = "";         // string to prefix bates numbers
00039 int            bates_index = 0;           // current bates sequence
00040 const char*    output_directory = ".";
00041 const char*    output_file = "load.dii";
00042 char*    font_file = NULL;
00043 int      bates_color = 0xff0000;    // color of bates header stamp
00044 int      email_sequence = 0;        // current pdf sequence number
00045 char*    pdf_name = NULL;           // current pdf file name
00046 FILE*    dii_file = NULL;           // the output dii load file
00047 pst_file pstfile;                   // the input pst file
00048 
00049 // pdf writer globals
00050 bool     pdf_open = false;          // is pdf writer started
00051 char*    pst_folder;                // current folder name
00052 int      page_sequence;             // current page number
00053 string   conversion;                // conversion command
00054 vector<string>  png_names;
00055 
00056 // png writer globals
00057 bool     png_open = false;          // is current page open
00058 int      line_height;               // in pixels
00059 int      char_width;                // in pixels
00060 int      col_number, col_max;       // in characters
00061 int      line_number, line_max;     // lines per page
00062 int      x_position, y_position;    // in pixels
00063 int      black, red;                // text colors
00064 gdImagePtr  image;                  // current gd image
00065 
00066 const int DPI         = 300;
00067 const double sz       = 10.0;
00068 const int margin      = DPI/2;
00069 const int LINE_SIZE   = 2000;
00070 const int PAGE_WIDTH  = DPI*17/2;
00071 const int PAGE_HEIGHT = DPI*11;
00072 
00073 // max size of the c_time char*. It will store the date of the email
00074 #define C_TIME_SIZE 500
00075 
00076 static void open_png();
00077 static void close_png();
00078 
00079 
00080 static void version();
00081 static void version()
00082 {
00083     printf("pst2dii v%s\n", VERSION);
00084 #if BYTE_ORDER == BIG_ENDIAN
00085     printf("Big Endian implementation being used.\n");
00086 #elif BYTE_ORDER == LITTLE_ENDIAN
00087     printf("Little Endian implementation being used.\n");
00088 #else
00089 #  error "Byte order not supported by this library"
00090 #endif
00091 }
00092 
00093 
00094 static void usage();
00095 static void usage()
00096 {
00097     version();
00098     printf("Usage: %s -f ttf-font-file [OPTIONS] {PST FILENAME}\n", prog_name);
00099     printf("\t-f ttf-font-file  \t- Set the font file\n");
00100     printf("OPTIONS:\n");
00101     printf("\t-B bates-prefix   \t- Set the bates prefix string\n");
00102     printf("\t-O dii-output-file\t- Set the dii load file output filename\n");
00103     printf("\t-V                \t- Version. Display program version\n");
00104     printf("\t-b bates-number   \t- Set the starting bates sequence number\n");
00105     printf("\t-c bates-color    \t- Specify the color of the bates stamps as 6 digit hex\n");
00106     printf("\t-d filename       \t- Debug to file.\n");
00107     printf("\t-h                \t- Help. This screen\n");
00108     printf("\t-o dirname        \t- Output directory to write files to.\n");
00109 }
00110 
00111 
00112 static char *removeCR (char *c);
00113 static char *removeCR (char *c) {
00114     // converts /r/n to /n
00115     char *a, *b;
00116     DEBUG_ENT("removeCR");
00117     a = b = c;
00118     while (*a != '\0') {
00119         *b = *a;
00120         if (*a != '\r')
00121             b++;
00122         a++;
00123     }
00124     *b = '\0';
00125     DEBUG_RET();
00126     return c;
00127 }
00128 
00129 
00130 // The sole purpose of this function is to bypass the pseudo-header prologue
00131 // that Microsoft Outlook inserts at the beginning of the internet email
00132 // headers for emails stored in their "Personal Folders" files.
00133 static char *skip_header_prologue(char *headers);
00134 static char *skip_header_prologue(char *headers) {
00135     const char *bad = "Microsoft Mail Internet Headers";
00136     if (strncmp(headers, bad, strlen(bad)) == 0) {
00137         // Found the offensive header prologue
00138         char *pc = strchr(headers, '\n');
00139         return pc + 1;
00140     }
00141     return headers;
00142 }
00143 
00144 
00145 static void check_filename(string &fname);
00146 static void check_filename(string &fname) {
00147     char *t = strdup(fname.c_str());
00148     DEBUG_ENT("check_filename");
00149     if (!t) {
00150         DEBUG_RET();
00151         return;
00152     }
00153     char *tt = t;
00154     bool fixed = false;
00155     while ((t = strpbrk(t, " /\\:"))) {
00156         // while there are characters in the second string that we don't want
00157         *t = '_'; //replace them with an underscore
00158         fixed = true;
00159     }
00160     if (fixed) fname = string(tt);
00161     free(tt);
00162     DEBUG_RET();
00163 }
00164 
00165 
00166 static string write_separate_attachment(string fname, pst_item_attach* current_attach, int attach_num, pst_file* pst);
00167 static string write_separate_attachment(string fname, pst_item_attach* current_attach, int attach_num, pst_file* pst)
00168 {
00169     FILE *fp = NULL;
00170     int x = 0;
00171     char *temp = NULL;
00172 
00173     // If there is a long filename (filename2) use that, otherwise
00174     // use the 8.3 filename (filename1)
00175     char *attach_filename = (current_attach->filename2.str) ? current_attach->filename2.str
00176                                                             : current_attach->filename1.str;
00177     DEBUG_ENT("write_separate_attachment");
00178     check_filename(fname);
00179     const char* f_name = fname.c_str();
00180     DEBUG_INFO(("dirname=%s, pathname=%s, filename=%s\n", output_directory, f_name, attach_filename));
00181     int len = strlen(output_directory) + 1 + strlen(f_name) + 15;
00182     if (!attach_filename) {
00183         // generate our own (dummy) filename for the attachment
00184         temp = (char*)pst_malloc(len);
00185         sprintf(temp, "%s/%s_attach%i", output_directory, f_name, attach_num);
00186     } else {
00187         // have an attachment name, make sure it's unique
00188         temp = (char*)pst_malloc(len+strlen(attach_filename));
00189         do {
00190             if (fp) fclose(fp);
00191             if (x == 0)
00192                 sprintf(temp, "%s/%s_%s", output_directory, f_name, attach_filename);
00193             else
00194                 sprintf(temp, "%s/%s_%s-%i", output_directory, f_name, attach_filename, x);
00195         } while ((fp = fopen(temp, "r")) && ++x < 99999999);
00196         if (x > 99999999) {
00197             DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
00198         }
00199     }
00200     DEBUG_INFO(("Saving attachment to %s\n", temp));
00201     if (!(fp = fopen(temp, "wb"))) {
00202         DEBUG_WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
00203     } else {
00204         (void)pst_attach_to_file(pst, current_attach, fp);
00205         fclose(fp);
00206     }
00207     string rc(temp);
00208     if (temp) free(temp);
00209     DEBUG_RET();
00210     return rc;
00211 }
00212 
00213 
00214 static void print_pdf_short(const char *line, int len, int color);
00215 static void print_pdf_short(const char *line, int len, int color)
00216 {
00217     if (line_number >= line_max) {
00218         close_png();
00219         open_png();
00220     }
00221     int brect[8];
00222     gdFTStringExtra strex;
00223     strex.flags       = gdFTEX_RESOLUTION;
00224     strex.linespacing = 1.20;
00225     strex.charmap     = 0;
00226     strex.hdpi        = DPI;
00227     strex.vdpi        = DPI;
00228     char xline[len+1];
00229     memcpy(xline, line, len);
00230     xline[len] = '\0';
00231     char *p;
00232     char *l = xline;
00233     while ((p = strchr(l, '&'))) {
00234         *p = '\0';
00235         char *err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, l, &strex);
00236         if (err) printf("%s", err);
00237         x_position += (brect[2]-brect[6]);
00238         l = p+1;
00239         err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, (char*)"&amp;", &strex);
00240         if (err) printf("%s", err);
00241         x_position += (brect[2]-brect[6]);
00242     }
00243     char *err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, l, &strex);
00244     if (err) printf("%s", err);
00245     x_position += (brect[2]-brect[6]);
00246     col_number += len;
00247 }
00248 
00249 
00250 static void new_line();
00251 static void new_line()
00252 {
00253     y_position  += line_height;
00254     line_number += 1;
00255     x_position   = margin;
00256     col_number   = 0;
00257 }
00258 
00259 
00260 static void print_pdf_single(const char *line, int color);
00261 static void print_pdf_single(const char *line, int color)
00262 {
00263     while (*line == '\t') {
00264         char blanks[5];
00265         memset(blanks, ' ', 5);
00266         print_pdf_short(blanks, 4, color);
00267         line++;
00268         if (col_number >= col_max) new_line();
00269     }
00270     int n = strlen(line);
00271     while (n) {
00272         int m = col_max - col_number;   // number of chars that will fit on this line
00273         m = (n > m) ? m : n;
00274         print_pdf_short(line, m, color);
00275         line += m;
00276         n    -= m;
00277         if (n) new_line();
00278     }
00279 }
00280 
00281 
00282 static void print_pdf_only(char *line, int color);
00283 static void print_pdf_only(char *line, int color)
00284 {
00285     char *p;
00286     while ((p = strchr(line, '\n'))) {
00287         *p = '\0';
00288         print_pdf_single(line, color);
00289         *p = '\n';
00290         line = p+1;
00291         new_line();
00292     }
00293     print_pdf_single(line, color);
00294 }
00295 
00296 
00297 static void print_pdf(char *line);
00298 static void print_pdf(char *line)
00299 {
00300     pst_fwrite(line, 1, strlen(line), dii_file);
00301     print_pdf_only(line, black);
00302 }
00303 
00304 
00305 static void open_png()
00306 {
00307     if (!png_open) {
00308         png_open  = true;
00309         int brect[8];
00310         image = gdImageCreate(PAGE_WIDTH, PAGE_HEIGHT);
00311                 gdImageColorAllocate(image, 255, 255, 255); // background color first one allocated
00312         black = gdImageColorAllocate(image, 0, 0, 0);
00313         int r = (bates_color & 0xff0000) >> 16;
00314         int g = (bates_color & 0x00ff00) >> 8;
00315         int b = (bates_color & 0x0000ff);
00316         red   = gdImageColorAllocate(image, r, g, b);
00317 
00318         gdFTStringExtra strex;
00319         strex.flags       = gdFTEX_RESOLUTION;
00320         strex.linespacing = 1.20;
00321         strex.charmap     = 0;
00322         strex.hdpi        = DPI;
00323         strex.vdpi        = DPI;
00324 
00325         char line[LINE_SIZE];
00326         char *err = gdImageStringFTEx(NULL, &brect[0], black, font_file, sz, 0.0, margin, margin, (char*)"LMgqQ", &strex);
00327         if (err) printf("%s", err);
00328         line_height = (brect[3]-brect[7]) * 12/10;
00329         char_width  = (brect[2]-brect[6]) / 5;
00330         col_number  = 0;
00331         col_max     = (PAGE_WIDTH - margin*2) / char_width;
00332         line_number = 0;
00333         line_max    = (PAGE_HEIGHT - margin*2) / line_height;
00334         x_position  = margin;
00335         y_position  = margin + line_height;
00336         snprintf(line, sizeof(line), "%s%06d\n", bates_prefix, bates_index++);
00337         print_pdf_only(line, red);
00338         print_pdf_only(pst_folder, red);
00339     }
00340 }
00341 
00342 
00343 static void close_png()
00344 {
00345     if (png_open) {
00346         png_open = false;
00347         int len = 4 + 11 + 4 +1;
00348         char *fn = (char*)pst_malloc(len);
00349         snprintf(fn, len, "page%d.png", ++page_sequence);
00350         FILE *pngout = fopen(fn, "wb");
00351         if (pngout) {
00352             gdImagePng(image, pngout);
00353             fclose(pngout);
00354         }
00355         gdImageDestroy(image); // free memory
00356         png_names.push_back(fn);
00357         conversion += string(" ") + fn;
00358         free(fn);
00359     }
00360 }
00361 
00362 
00363 static void open_pdf(char *line);
00364 static void open_pdf(char *line)
00365 {
00366     pst_folder    = line;
00367     page_sequence = 0;
00368     conversion    = string(convert);
00369     png_names.clear();
00370     open_png();
00371     /* Note; allocating the largest string length to pdf_name */
00372     int len = strlen(output_directory) + 4 + 6 + 4 + 1;
00373     pdf_name = (char*)pst_malloc(len);
00374     snprintf(pdf_name, 3 + 6 + 1, "dii%06d", ++email_sequence);
00375     fprintf(dii_file, "\n@T %s\n", pdf_name);
00376     snprintf(pdf_name, len, "%s/dii%06d.pdf", output_directory, email_sequence);
00377 }
00378 
00379 
00380 static void close_pdf();
00381 static void close_pdf()
00382 {
00383     close_png();
00384     conversion += string(" ") + pdf_name;
00385     (void)system(conversion.c_str());
00386     for (vector<string>::iterator i=png_names.begin(); i!=png_names.end(); i++) {
00387         remove((*i).c_str());
00388     }
00389     fprintf(dii_file, "@D %s\n", pdf_name);
00390     free(pdf_name);
00391 }
00392 
00393 
00394 static void write_simple(const char *tag, const char *value);
00395 static void write_simple(const char *tag, const char *value)
00396 {
00397     if (value) fprintf(dii_file, "@%s %s\n", tag, value);
00398 }
00399 
00400 
00401 static void write_simple(const char *tag, string value);
00402 static void write_simple(const char *tag, string value)
00403 {
00404     fprintf(dii_file, "@%s %s\n", tag, value.c_str());
00405 }
00406 
00407 
00408 static void write_simple(const char *tag, const char *value, const char *value2);
00409 static void write_simple(const char *tag, const char *value, const char *value2)
00410 {
00411     if (value) {
00412     if (value2) fprintf(dii_file, "@%s \"%s\" <%s>\n", tag, value, value2);
00413     else        fprintf(dii_file, "@%s \"%s\"\n", tag, value);
00414     }
00415 }
00416 
00417 
00418 static string extract_header(char *headers, const char *field);
00419 static string extract_header(char *headers, const char *field)
00420 {
00421     string rc;
00422     int len = strlen(field) + 4;
00423     char f[len];
00424     snprintf(f, len, "\n%s: ", field);
00425     char *p = strstr(headers, f);
00426     if (p) {
00427         p += strlen(f);
00428         char *n = strchr(p, '\n');
00429         if (n) {
00430             *n = '\0';
00431             rc = string(p);
00432             *n = '\n';
00433         }
00434         else {
00435             rc = string(p);
00436         }
00437     }
00438     return rc;
00439 }
00440 
00441 
00442 static void write_normal_email(file_ll &f, pst_item* item, pst_file* pst);
00443 static void write_normal_email(file_ll &f, pst_item* item, pst_file* pst)
00444 {
00445     DEBUG_ENT("write_normal_email");
00446     char *soh = NULL;  // real start of headers.
00447     if (item->email->header.str) {
00448         // some of the headers we get from the file are not properly defined.
00449         // they can contain some email stuff too. We will cut off the header
00450         // when we see a \n\n or \r\n\r\n
00451         removeCR(item->email->header.str);
00452         char *temp = strstr(item->email->header.str, "\n\n");
00453         if (temp) {
00454             DEBUG_INFO(("Found body text in header\n"));
00455             temp[1] = '\0'; // stop after first \n
00456         }
00457         soh = skip_header_prologue(item->email->header.str);
00458     }
00459 
00460     char folder_line[LINE_SIZE];
00461     char line[LINE_SIZE];
00462     // reset pdf writer to new file
00463     int bates = bates_index;    // save starting index
00464     snprintf(folder_line, sizeof(folder_line), "pst folder = %s\n", f.name.c_str());
00465     open_pdf(folder_line);
00466 
00467     // start printing this email
00468     fprintf(dii_file, "@FOLDERNAME %s\n", f.name.c_str());
00469     string myfrom = extract_header(soh, "From");
00470     string myto   = extract_header(soh, "To");
00471     string mycc   = extract_header(soh, "Cc");
00472     string mybcc  = extract_header(soh, "Bcc");
00473     if (myfrom.empty()) write_simple("FROM", item->email->outlook_sender_name.str, item->email->sender_address.str);
00474     else                write_simple("FROM", myfrom);
00475     if (myto.empty())   write_simple("TO",   item->email->sentto_address.str,      item->email->recip_address.str);
00476     else                write_simple("TO", myto);
00477     if (mycc.empty())   write_simple("CC",   item->email->cc_address.str);
00478     else                write_simple("CC", mycc);
00479     if (mybcc.empty())  write_simple("BCC",  item->email->bcc_address.str);
00480     else                write_simple("BCC", mybcc);
00481     if (item->email->sent_date) {
00482         time_t t = pst_fileTimeToUnixTime(item->email->sent_date);
00483         char c_time[C_TIME_SIZE];
00484         strftime(c_time, C_TIME_SIZE, "%F", gmtime(&t));
00485         write_simple("DATESENT", c_time);
00486         strftime(c_time, C_TIME_SIZE, "%T+0000", gmtime(&t));
00487         write_simple("TIMESENT", c_time);
00488     }
00489     if (item->email->arrival_date) {
00490         time_t t = pst_fileTimeToUnixTime(item->email->arrival_date);
00491         char c_time[C_TIME_SIZE];
00492         strftime(c_time, C_TIME_SIZE, "%F", gmtime(&t));
00493         write_simple("DATERCVD", c_time);
00494         strftime(c_time, C_TIME_SIZE, "%T+0000", gmtime(&t));
00495         write_simple("TIMERCVD", c_time);
00496     }
00497     if (item->subject.str) {
00498         write_simple("SUBJECT", item->subject.str);
00499     }
00500     write_simple("MSGID", item->email->messageid.str);
00501     write_simple("READ", (item->flags & 1) ? "Y" : "N");
00502 
00503     DEBUG_INFO(("About to print Header\n"));
00504     fprintf(dii_file, "@HEADER\n");
00505 
00506     if (item && item->subject.str) {
00507         DEBUG_INFO(("item->subject = %s\n", item->subject.str));
00508     }
00509 
00510     if (soh) {
00511         // Now, write out the header...
00512         print_pdf(soh);
00513         int len = strlen(soh);
00514         if (!len || (soh[len-1] != '\n')) {
00515             snprintf(line, sizeof(line), "\n");
00516             print_pdf(line);
00517         }
00518 
00519     } else {
00520         //make up our own headers
00521         const char *temp = item->email->outlook_sender.str;
00522         if (!temp) temp = "";
00523         snprintf(line, sizeof(line), "From: \"%s\" <%s>\n", item->email->outlook_sender_name.str, temp);
00524         print_pdf(line);
00525 
00526         if (item->subject.str) {
00527             snprintf(line, sizeof(line), "Subject: %s\n", item->subject.str);
00528         } else {
00529             snprintf(line, sizeof(line), "Subject: \n");
00530         }
00531         print_pdf(line);
00532 
00533         snprintf(line, sizeof(line), "To: %s\n", item->email->sentto_address.str);
00534         print_pdf(line);
00535 
00536         if (item->email->cc_address.str) {
00537             snprintf(line, sizeof(line), "Cc: %s\n", item->email->cc_address.str);
00538             print_pdf(line);
00539         }
00540 
00541         if (item->email->sent_date) {
00542             time_t em_time = pst_fileTimeToUnixTime(item->email->sent_date);
00543             char c_time[C_TIME_SIZE];
00544             strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", gmtime(&em_time));
00545             snprintf(line, sizeof(line), "Date: %s\n", c_time);
00546             print_pdf(line);
00547         }
00548     }
00549     snprintf(line, sizeof(line), "\n");
00550     print_pdf_only(line, black);
00551     fprintf(dii_file, "@HEADER-END\n");
00552 
00553     DEBUG_INFO(("About to print Body\n"));
00554     fprintf(dii_file, "@EMAIL-BODY\n");
00555     if (item->body.str) {
00556         removeCR(item->body.str);
00557         print_pdf(item->body.str);
00558     } else if (item->email->htmlbody.str) {
00559         removeCR(item->email->htmlbody.str);
00560         print_pdf(item->email->htmlbody.str);
00561     } else if (item->email->encrypted_body.data || item->email->encrypted_htmlbody.data) {
00562         char ln[LINE_SIZE];
00563         snprintf(ln, sizeof(ln), "%s", "The body of this email is encrypted. This isn't supported yet, but the body is now an attachment\n");
00564         print_pdf(ln);
00565     }
00566     fprintf(dii_file, "@EMAIL-END\n");
00567 
00568     int attach_num = 0;
00569     for (pst_item_attach* attach = item->attach; attach; attach = attach->next) {
00570         pst_convert_utf8_null(item, &attach->filename1);
00571         pst_convert_utf8_null(item, &attach->filename2);
00572         pst_convert_utf8_null(item, &attach->mimetype);
00573         DEBUG_INFO(("Attempting Attachment encoding\n"));
00574         if (attach->data.data || attach->i_id) {
00575             string an = write_separate_attachment(f.name, attach, ++attach_num, pst);
00576             fprintf(dii_file, "@EATTACH %s\n", an.c_str());
00577         }
00578     }
00579     close_pdf();
00580     fprintf(dii_file, "@BATESBEG %d\n", bates);
00581     fprintf(dii_file, "@BATESEND %d\n", bates_index-1);
00582     DEBUG_RET();
00583 }
00584 
00585 
00586 static void create_enter_dir(file_ll &f, file_ll *parent, pst_item *item);
00587 static void create_enter_dir(file_ll &f, file_ll *parent, pst_item *item)
00588 {
00589     pst_convert_utf8(item, &item->file_as);
00590     f.type         = item->type;
00591     f.stored_count = (item->folder) ? item->folder->item_count : 0;
00592     f.name         = ((parent) ? parent->name + "/" : "") + string(item->file_as.str);
00593 }
00594 
00595 
00596 static void close_enter_dir(file_ll &f);
00597 static void close_enter_dir(file_ll &f)
00598 {
00599 }
00600 
00601 
00602 static void process(pst_item *outeritem, file_ll *parent, pst_desc_tree *d_ptr);
00603 static void process(pst_item *outeritem, file_ll *parent, pst_desc_tree *d_ptr)
00604 {
00605     file_ll ff;
00606     pst_item *item = NULL;
00607     DEBUG_ENT("process");
00608     create_enter_dir(ff, parent, outeritem);
00609     for (; d_ptr; d_ptr = d_ptr->next) {
00610         if (d_ptr->desc) {
00611             item = pst_parse_item(&pstfile, d_ptr, NULL);
00612             DEBUG_INFO(("item pointer is %p\n", item));
00613             if (item) {
00614                 if (item->folder && item->file_as.str && d_ptr->child )  {
00615                     //if this is a non-empty folder, we want to recurse into it
00616                     fprintf(stderr, "entering folder %s\n", item->file_as.str);
00617                     process(item, &ff, d_ptr->child);
00618                 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_SCHEDULE || item->type == PST_TYPE_REPORT)) {
00619                     ff.email_count++;
00620                     write_normal_email(ff, item, &pstfile);
00621                 }
00622                 else {
00623                     ff.skip_count++;    // other mapi objects
00624                 }
00625                 pst_freeItem(item);
00626             } else {
00627                 ff.skip_count++;
00628                 DEBUG_INFO(("A NULL item was seen\n"));
00629             }
00630         }
00631     }
00632     close_enter_dir(ff);
00633     DEBUG_RET();
00634 }
00635 
00636 
00637 int main(int argc, char* const* argv)
00638 {
00639     pst_desc_tree *d_ptr;
00640     char *fname = NULL;
00641     char c;
00642     char *d_log = NULL;
00643     prog_name = argv[0];
00644     pst_item *item = NULL;
00645 
00646     while ((c = getopt(argc, argv, "B:b:c:d:f:o:O:Vh"))!= -1) {
00647         switch (c) {
00648         case 'B':
00649             bates_prefix = optarg;
00650             break;
00651         case 'b':
00652             bates_index = atoi(optarg);
00653             break;
00654         case 'c':
00655             bates_color = (int)strtol(optarg, (char**)NULL, 16);
00656             break;
00657         case 'f':
00658             font_file = optarg;
00659             break;
00660         case 'o':
00661             output_directory = optarg;
00662             break;
00663         case 'O':
00664             output_file = optarg;
00665             break;
00666         case 'd':
00667             d_log = optarg;
00668             break;
00669         case 'h':
00670             usage();
00671             exit(0);
00672             break;
00673         case 'V':
00674             version();
00675             exit(0);
00676             break;
00677         default:
00678             usage();
00679             exit(1);
00680             break;
00681         }
00682     }
00683 
00684     if (!font_file) {
00685         usage();
00686         exit(1);
00687     }
00688 
00689     if (argc > optind) {
00690         fname = argv[optind];
00691     } else {
00692         usage();
00693         exit(2);
00694     }
00695 
00696 
00697     #ifdef DEBUG_ALL
00698         // force a log file
00699         if (!d_log) d_log = "pst2dii.log";
00700     #endif
00701     DEBUG_INIT(d_log, NULL);
00702     DEBUG_ENT("main");
00703     RET_DERROR(pst_open(&pstfile, fname, NULL), 1, ("Error opening File\n"));
00704     RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
00705 
00706     pst_load_extended_attributes(&pstfile);
00707 
00708     d_ptr = pstfile.d_head; // first record is main record
00709     item  = (pst_item*)pst_parse_item(&pstfile, d_ptr, NULL);
00710     if (!item || !item->message_store) {
00711         DEBUG_RET();
00712         DIE(("Could not get root record\n"));
00713     }
00714 
00715     d_ptr = pst_getTopOfFolders(&pstfile, item);
00716     if (!d_ptr) {
00717         DEBUG_RET();
00718         DIE(("Top of folders record not found. Cannot continue\n"));
00719     }
00720 
00721     dii_file = fopen(output_file, "wb");
00722     if (dii_file) {
00723         process(item, NULL, d_ptr->child);  // do the children of TOPF
00724         pst_freeItem(item);
00725         pst_close(&pstfile);
00726         fclose(dii_file);
00727     }
00728     DEBUG_RET();
00729     return 0;
00730 }

Generated on 6 Jul 2016 for 'LibPst' by  doxygen 1.6.1