| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
// File: excxx_example_database_load.cpp |
| 10 |
#include <iostream> |
| 11 |
#include <fstream> |
| 12 |
#include <cstdlib> |
| 13 |
|
| 14 |
#include "MyDb.hpp" |
| 15 |
#include "gettingStartedCommon.hpp" |
| 16 |
|
| 17 |
#ifdef _WIN32 |
| 18 |
extern "C" { |
| 19 |
extern int getopt(int, char * const *, const char *); |
| 20 |
extern char *optarg; |
| 21 |
} |
| 22 |
#else |
| 23 |
#include <unistd.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
// Forward declarations |
| 27 |
void loadInventoryDB(MyDb &, std::string &); |
| 28 |
void loadVendorDB(MyDb &, std::string &); |
| 29 |
|
| 30 |
using namespace std; |
| 31 |
|
| 32 |
int |
| 33 |
usage() |
| 34 |
{ |
| 35 |
std::cout << "example_database_load [-b <path to data files>]" |
| 36 |
<< " [-h <database home directory>]" << std::endl; |
| 37 |
|
| 38 |
std::cout << "Note: If -b -h is specified, then the path must end" |
| 39 |
<< " with your system's path delimiter (/ or \\)" |
| 40 |
<< std::endl; |
| 41 |
return (-1); |
| 42 |
} |
| 43 |
|
| 44 |
// Loads the contents of vendors.txt and inventory.txt into |
| 45 |
// Berkeley DB databases. |
| 46 |
int |
| 47 |
main (int argc, char *argv[]) |
| 48 |
{ |
| 49 |
|
| 50 |
int ch; |
| 51 |
char lastChar; |
| 52 |
|
| 53 |
// Initialize the path to the database files |
| 54 |
std::string basename("./"); |
| 55 |
std::string databaseHome("./"); |
| 56 |
|
| 57 |
// Database names |
| 58 |
std::string vDbName("vendordb.db"); |
| 59 |
std::string iDbName("inventorydb.db"); |
| 60 |
std::string itemSDbName("itemname.sdb"); |
| 61 |
|
| 62 |
// Parse the command line arguments |
| 63 |
while ((ch = getopt(argc, argv, "b:h:")) != EOF) |
| 64 |
switch (ch) { |
| 65 |
case 'h': |
| 66 |
databaseHome = optarg; |
| 67 |
lastChar = databaseHome[databaseHome.size() -1]; |
| 68 |
if (lastChar != '/' && lastChar != '\\') |
| 69 |
return (usage()); |
| 70 |
break; |
| 71 |
case 'b': |
| 72 |
basename = optarg; |
| 73 |
lastChar = basename[basename.size() -1]; |
| 74 |
if (lastChar != '/' && lastChar != '\\') |
| 75 |
return (usage()); |
| 76 |
break; |
| 77 |
case '?': |
| 78 |
default: |
| 79 |
return (usage()); |
| 80 |
break; |
| 81 |
} |
| 82 |
|
| 83 |
// Identify the full name for our input files, which should |
| 84 |
// also include some path information. |
| 85 |
std::string inventoryFile = basename + "inventory.txt"; |
| 86 |
std::string vendorFile = basename + "vendors.txt"; |
| 87 |
|
| 88 |
try |
| 89 |
{ |
| 90 |
// Open all databases. |
| 91 |
MyDb inventoryDB(databaseHome, iDbName); |
| 92 |
MyDb vendorDB(databaseHome, vDbName); |
| 93 |
MyDb itemnameSDB(databaseHome, itemSDbName, true); |
| 94 |
|
| 95 |
// Associate the primary and the secondary |
| 96 |
inventoryDB.getDb().associate(NULL, |
| 97 |
&(itemnameSDB.getDb()), |
| 98 |
get_item_name, |
| 99 |
0); |
| 100 |
|
| 101 |
// Load the inventory database |
| 102 |
loadInventoryDB(inventoryDB, inventoryFile); |
| 103 |
|
| 104 |
// Load the vendor database |
| 105 |
loadVendorDB(vendorDB, vendorFile); |
| 106 |
} catch(DbException &e) { |
| 107 |
std::cerr << "Error loading databases. " << std::endl; |
| 108 |
std::cerr << e.what() << std::endl; |
| 109 |
return (e.get_errno()); |
| 110 |
} catch(std::exception &e) { |
| 111 |
std::cerr << "Error loading databases. " << std::endl; |
| 112 |
std::cerr << e.what() << std::endl; |
| 113 |
return (-1); |
| 114 |
} |
| 115 |
|
| 116 |
// MyDb class constructors will close the databases when they |
| 117 |
// go out of scope. |
| 118 |
return (0); |
| 119 |
} // End main |
| 120 |
|
| 121 |
// Used to locate the first pound sign (a field delimiter) |
| 122 |
// in the input string. |
| 123 |
size_t |
| 124 |
getNextPound(std::string &theString, std::string &substring) |
| 125 |
{ |
| 126 |
size_t pos = theString.find("#"); |
| 127 |
substring.assign(theString, 0, pos); |
| 128 |
theString.assign(theString, pos + 1, theString.size()); |
| 129 |
return (pos); |
| 130 |
} |
| 131 |
|
| 132 |
// Loads the contents of the inventory.txt file into a database |
| 133 |
void |
| 134 |
loadInventoryDB(MyDb &inventoryDB, std::string &inventoryFile) |
| 135 |
{ |
| 136 |
InventoryData inventoryData; |
| 137 |
std::string substring; |
| 138 |
size_t nextPound; |
| 139 |
|
| 140 |
std::ifstream inFile(inventoryFile.c_str(), std::ios::in); |
| 141 |
if ( !inFile ) |
| 142 |
{ |
| 143 |
std::cerr << "Could not open file '" << inventoryFile |
| 144 |
<< "'. Giving up." << std::endl; |
| 145 |
throw std::exception(); |
| 146 |
} |
| 147 |
|
| 148 |
while (!inFile.eof()) |
| 149 |
{ |
| 150 |
inventoryData.clear(); |
| 151 |
std::string stringBuf; |
| 152 |
std::getline(inFile, stringBuf); |
| 153 |
|
| 154 |
// Now parse the line |
| 155 |
if (!stringBuf.empty()) |
| 156 |
{ |
| 157 |
nextPound = getNextPound(stringBuf, substring); |
| 158 |
inventoryData.setName(substring); |
| 159 |
|
| 160 |
nextPound = getNextPound(stringBuf, substring); |
| 161 |
inventoryData.setSKU(substring); |
| 162 |
|
| 163 |
nextPound = getNextPound(stringBuf, substring); |
| 164 |
inventoryData.setPrice(strtod(substring.c_str(), 0)); |
| 165 |
|
| 166 |
nextPound = getNextPound(stringBuf, substring); |
| 167 |
inventoryData.setQuantity(strtol(substring.c_str(), 0, 10)); |
| 168 |
|
| 169 |
nextPound = getNextPound(stringBuf, substring); |
| 170 |
inventoryData.setCategory(substring); |
| 171 |
|
| 172 |
nextPound = getNextPound(stringBuf, substring); |
| 173 |
inventoryData.setVendor(substring); |
| 174 |
|
| 175 |
void *buff = (void *)inventoryData.getSKU().c_str(); |
| 176 |
size_t size = inventoryData.getSKU().size()+1; |
| 177 |
Dbt key(buff, (u_int32_t)size); |
| 178 |
|
| 179 |
buff = inventoryData.getBuffer(); |
| 180 |
size = inventoryData.getBufferSize(); |
| 181 |
Dbt data(buff, (u_int32_t)size); |
| 182 |
|
| 183 |
inventoryDB.getDb().put(NULL, &key, &data, 0); |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
inFile.close(); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
// Loads the contents of the vendors.txt file into a database |
| 193 |
void |
| 194 |
loadVendorDB(MyDb &vendorDB, std::string &vendorFile) |
| 195 |
{ |
| 196 |
std::ifstream inFile(vendorFile.c_str(), std::ios::in); |
| 197 |
if ( !inFile ) |
| 198 |
{ |
| 199 |
std::cerr << "Could not open file '" << vendorFile |
| 200 |
<< "'. Giving up." << std::endl; |
| 201 |
throw std::exception(); |
| 202 |
} |
| 203 |
|
| 204 |
VENDOR my_vendor; |
| 205 |
while (!inFile.eof()) |
| 206 |
{ |
| 207 |
std::string stringBuf; |
| 208 |
std::getline(inFile, stringBuf); |
| 209 |
memset(&my_vendor, 0, sizeof(VENDOR)); |
| 210 |
|
| 211 |
// Scan the line into the structure. |
| 212 |
// Convenient, but not particularly safe. |
| 213 |
// In a real program, there would be a lot more |
| 214 |
// defensive code here. |
| 215 |
sscanf(stringBuf.c_str(), |
| 216 |
"%20[^#]#%20[^#]#%20[^#]#%3[^#]#%6[^#]#%13[^#]#%20[^#]#%20[^\n]", |
| 217 |
my_vendor.name, my_vendor.street, |
| 218 |
my_vendor.city, my_vendor.state, |
| 219 |
my_vendor.zipcode, my_vendor.phone_number, |
| 220 |
my_vendor.sales_rep, my_vendor.sales_rep_phone); |
| 221 |
|
| 222 |
Dbt key(my_vendor.name, (u_int32_t)strlen(my_vendor.name) + 1); |
| 223 |
Dbt data(&my_vendor, sizeof(VENDOR)); |
| 224 |
|
| 225 |
vendorDB.getDb().put(NULL, &key, &data, 0); |
| 226 |
} |
| 227 |
|
| 228 |
inFile.close(); |
| 229 |
} |