00001
00002 #include <iostream>
00003 #include <fstream>
00004 #include <cstdlib>
00005
00006 #include "MyDb.hpp"
00007 #include "gettingStartedCommon.hpp"
00008
00009 #ifdef _WIN32
00010 extern "C" {
00011 extern int getopt(int, char * const *, const char *);
00012 extern char *optarg;
00013 }
00014 #else
00015 #include <unistd.h>
00016 #endif
00017
00018
00019 void loadInventoryDB(MyDb &, std::string &);
00020 void loadVendorDB(MyDb &, std::string &);
00021
00022 using namespace std;
00023
00024 int
00025 usage()
00026 {
00027 std::cout << "example_database_load [-b <path to data files>]"
00028 << " [-h <database home directory>]" << std::endl;
00029
00030 std::cout << "Note: If -b -h is specified, then the path must end"
00031 << " with your system's path delimiter (/ or \\)"
00032 << std::endl;
00033 return (-1);
00034 }
00035
00036
00037
00038 int
00039 main (int argc, char *argv[])
00040 {
00041
00042 char ch, lastChar;
00043
00044
00045 std::string basename("./");
00046 std::string databaseHome("./");
00047
00048
00049 std::string vDbName("vendordb.db");
00050 std::string iDbName("inventorydb.db");
00051 std::string itemSDbName("itemname.sdb");
00052
00053
00054 while ((ch = getopt(argc, argv, "b:h:")) != EOF)
00055 switch (ch) {
00056 case 'h':
00057 databaseHome = optarg;
00058 lastChar = databaseHome[databaseHome.size() -1];
00059 if (lastChar != '/' && lastChar != '\\')
00060 return (usage());
00061 break;
00062 case 'b':
00063 basename = optarg;
00064 lastChar = basename[basename.size() -1];
00065 if (lastChar != '/' && lastChar != '\\')
00066 return (usage());
00067 break;
00068 case '?':
00069 default:
00070 return (usage());
00071 break;
00072 }
00073
00074
00075
00076 std::string inventoryFile = basename + "inventory.txt";
00077 std::string vendorFile = basename + "vendors.txt";
00078
00079 try
00080 {
00081
00082 MyDb inventoryDB(databaseHome, iDbName);
00083 MyDb vendorDB(databaseHome, vDbName);
00084 MyDb itemnameSDB(databaseHome, itemSDbName, true);
00085
00086
00087 inventoryDB.getDb().associate(NULL,
00088 &(itemnameSDB.getDb()),
00089 get_item_name,
00090 0);
00091
00092
00093 loadInventoryDB(inventoryDB, inventoryFile);
00094
00095
00096 loadVendorDB(vendorDB, vendorFile);
00097 } catch(DbException &e) {
00098 std::cerr << "Error loading databases. " << std::endl;
00099 std::cerr << e.what() << std::endl;
00100 return (e.get_errno());
00101 } catch(std::exception &e) {
00102 std::cerr << "Error loading databases. " << std::endl;
00103 std::cerr << e.what() << std::endl;
00104 return (-1);
00105 }
00106
00107
00108
00109 return (0);
00110 }
00111
00112
00113
00114 size_t
00115 getNextPound(std::string &theString, std::string &substring)
00116 {
00117 size_t pos = theString.find("#");
00118 substring.assign(theString, 0, pos);
00119 theString.assign(theString, pos + 1, theString.size());
00120 return (pos);
00121 }
00122
00123
00124 void
00125 loadInventoryDB(MyDb &inventoryDB, std::string &inventoryFile)
00126 {
00127 InventoryData inventoryData;
00128 std::string substring;
00129 size_t nextPound;
00130
00131 std::ifstream inFile(inventoryFile.c_str(), std::ios::in);
00132 if ( !inFile )
00133 {
00134 std::cerr << "Could not open file '" << inventoryFile
00135 << "'. Giving up." << std::endl;
00136 throw std::exception();
00137 }
00138
00139 while (!inFile.eof())
00140 {
00141 inventoryData.clear();
00142 std::string stringBuf;
00143 std::getline(inFile, stringBuf);
00144
00145
00146 if (!stringBuf.empty())
00147 {
00148 nextPound = getNextPound(stringBuf, substring);
00149 inventoryData.setName(substring);
00150
00151 nextPound = getNextPound(stringBuf, substring);
00152 inventoryData.setSKU(substring);
00153
00154 nextPound = getNextPound(stringBuf, substring);
00155 inventoryData.setPrice(strtod(substring.c_str(), 0));
00156
00157 nextPound = getNextPound(stringBuf, substring);
00158 inventoryData.setQuantity(strtol(substring.c_str(), 0, 10));
00159
00160 nextPound = getNextPound(stringBuf, substring);
00161 inventoryData.setCategory(substring);
00162
00163 nextPound = getNextPound(stringBuf, substring);
00164 inventoryData.setVendor(substring);
00165
00166 void *buff = (void *)inventoryData.getSKU().c_str();
00167 size_t size = inventoryData.getSKU().size()+1;
00168 Dbt key(buff, (u_int32_t)size);
00169
00170 buff = inventoryData.getBuffer();
00171 size = inventoryData.getBufferSize();
00172 Dbt data(buff, (u_int32_t)size);
00173
00174 inventoryDB.getDb().put(NULL, &key, &data, 0);
00175 }
00176
00177 }
00178
00179 inFile.close();
00180
00181 }
00182
00183
00184 void
00185 loadVendorDB(MyDb &vendorDB, std::string &vendorFile)
00186 {
00187 std::ifstream inFile(vendorFile.c_str(), std::ios::in);
00188 if ( !inFile )
00189 {
00190 std::cerr << "Could not open file '" << vendorFile
00191 << "'. Giving up." << std::endl;
00192 throw std::exception();
00193 }
00194
00195 VENDOR my_vendor;
00196 while (!inFile.eof())
00197 {
00198 std::string stringBuf;
00199 std::getline(inFile, stringBuf);
00200 memset(&my_vendor, 0, sizeof(VENDOR));
00201
00202
00203
00204
00205
00206 sscanf(stringBuf.c_str(),
00207 "%20[^#]#%20[^#]#%20[^#]#%3[^#]#%6[^#]#%13[^#]#%20[^#]#%20[^\n]",
00208 my_vendor.name, my_vendor.street,
00209 my_vendor.city, my_vendor.state,
00210 my_vendor.zipcode, my_vendor.phone_number,
00211 my_vendor.sales_rep, my_vendor.sales_rep_phone);
00212
00213 Dbt key(my_vendor.name, (u_int32_t)strlen(my_vendor.name) + 1);
00214 Dbt data(&my_vendor, sizeof(VENDOR));
00215
00216 vendorDB.getDb().put(NULL, &key, &data, 0);
00217 }
00218
00219 inFile.close();
00220 }