00001 #include "importbibtex.h"
00002 #include <qstring.h>
00003 #include <stdio.h>
00004 #include <qlineedit.h>
00005 #include <importfile.h>
00006
00007 ImportBibTex::ImportBibTex(QWidget *parent): ImportBase(parent), c(0) {
00008 view = new ImportFile(parent);
00009 }
00010
00011 void ImportBibTex::process() {
00012 bt_initialize ();
00013 processFile(view->filename->text());
00014 bt_cleanup ();
00015 }
00016
00017 bool ImportBibTex::processFile(QString filename)
00018 {
00019 FILE * infile;
00020 AST * entry;
00021 boolean status, overall_status;
00022
00023
00024 infile = fopen (filename, "r");
00025 if (infile == NULL)
00026 {
00027 perror (filename);
00028 return false;
00029 }
00030
00031 char *fname=strdup(filename);
00032 overall_status = true;
00033 while (1)
00034 {
00035 c++;
00036 entry = bt_parse_entry (infile, fname, 0, &status);
00037 overall_status &= status;
00038 if (!entry) break;
00039 fillDict(entry);
00040 bt_free_ast (entry);
00041 }
00042 delete fname;
00043
00044 return overall_status;
00045 }
00046
00047
00048
00049 void ImportBibTex::fillDict (AST * entry)
00050 {
00051 AST * field;
00052 char * fname;
00053 char * value;
00054 int i;
00055 bt_stringlist *namelist;
00056 bt_name * name;
00057
00058
00059 if (bt_entry_metatype (entry) != BTE_REGULAR)
00060 {
00061 printf ("skipping %s entry\n", bt_entry_type (entry));
00062 return;
00063 }
00064
00065 printf ("parsing %s: %s\n", bt_entry_key (entry), bt_entry_type (entry));
00066
00067 dict["bibtextype_"+QString().setNum(count())]=bt_entry_type(entry);
00068
00069 field = NULL;
00070 while ((field = bt_next_field (entry, field, &fname)))
00071 {
00072 if (strcmp (fname, "author") == 0 ||
00073 strcmp (fname, "editor") == 0)
00074 {
00075 value = bt_get_text (field);
00076
00077 printf ("field: %s:\n", fname);
00078 printf (" %s\n", value);
00079
00080 namelist = bt_split_list (value, "and", NULL, 0, "name");
00081 if (namelist != NULL)
00082 {
00083 printf (" splits into %d names:\n", namelist->num_items);
00084 for (i = 0; i < namelist->num_items; i++)
00085 {
00086 printf (" %s\n", namelist->items[i]);
00087
00088 name = bt_split_name (namelist->items[i], NULL, 0, i);
00089 printf (" ");
00090
00091 QString firstnamekey="firstname"+QString().setNum(i)+"_"+QString().setNum(count());
00092 dict[firstnamekey]=extractComponent ("first", name, BTN_FIRST, "; ");
00093
00094 QString lastname=extractComponent ("von", name, BTN_VON, "; ");
00095 lastname+=extractComponent ("last", name, BTN_LAST, "; ");
00096 lastname+=extractComponent ("jr", name, BTN_JR, NULL);
00097
00098 QString lastnamekey="lastname"+QString().setNum(i)+"_"+QString().setNum(count());
00099 dict[lastnamekey]=lastname;
00100 printf ("\n");
00101 printf ("%s=>%s", lastnamekey.ascii(), lastname.ascii());
00102 }
00103 }
00104 } else {
00105 dict[QString(fname)+"_"+QString().setNum(count())]=bt_get_text(field);
00106 printf("found %s => %s\n", fname, bt_get_text(field));
00107 }
00108 }
00109 }
00110
00111
00112 QString ImportBibTex::extractComponent(char * comp, bt_name * name, int part, char * tail)
00113 {
00114 int i;
00115 char **tokens;
00116 int num_tokens;
00117 QString s;
00118
00119 tokens = name->parts[part];
00120 num_tokens = name->part_len[part];
00121
00122 if(num_tokens > 0)
00123 {
00124 printf ("%s: ", comp);
00125 for (i = 0; i < num_tokens; i++)
00126 {
00127 printf ("%s/", tokens[i]);
00128 s+=tokens[i];
00129 s+=" ";
00130 }
00131 }
00132 return s;
00133 }
00134
00135 int ImportBibTex::count() {
00136 return c;
00137 }
00138
00139 QWidget *ImportBibTex::customDlg() {
00140 return view;
00141 }