00001
00002
00003 #ifndef GETTINGSTARTEDCOMMON_H
00004 #define GETTINGSTARTEDCOMMON_H
00005
00006 class InventoryData
00007 {
00008 public:
00009 inline void setPrice(double price) {price_ = price;}
00010 inline void setQuantity(long quantity) {quantity_ = quantity;}
00011 inline void setCategory(std::string &category) {category_ = category;}
00012 inline void setName(std::string &name) {name_ = name;}
00013 inline void setVendor(std::string &vendor) {vendor_ = vendor;}
00014 inline void setSKU(std::string &sku) {sku_ = sku;}
00015
00016 inline double& getPrice() {return(price_);}
00017 inline long& getQuantity() {return(quantity_);}
00018 inline std::string& getCategory() {return(category_);}
00019 inline std::string& getName() {return(name_);}
00020 inline std::string& getVendor() {return(vendor_);}
00021 inline std::string& getSKU() {return(sku_);}
00022
00023
00024 void clear()
00025 {
00026 price_ = 0.0;
00027 quantity_ = 0;
00028 category_ = "";
00029 name_ = "";
00030 vendor_ = "";
00031 sku_ = "";
00032 }
00033
00034
00035 InventoryData() { clear(); }
00036
00037
00038
00039 InventoryData(void *buffer)
00040 {
00041 char *buf = (char *)buffer;
00042
00043 price_ = *((double *)buf);
00044 bufLen_ = sizeof(double);
00045
00046 quantity_ = *((long *)(buf + bufLen_));
00047 bufLen_ += sizeof(long);
00048
00049 name_ = buf + bufLen_;
00050 bufLen_ += name_.size() + 1;
00051
00052 sku_ = buf + bufLen_;
00053 bufLen_ += sku_.size() + 1;
00054
00055 category_ = buf + bufLen_;
00056 bufLen_ += category_.size() + 1;
00057
00058 vendor_ = buf + bufLen_;
00059 bufLen_ += vendor_.size() + 1;
00060 }
00061
00062
00063
00064
00065
00066
00067 char *
00068 getBuffer()
00069 {
00070
00071 memset(databuf_, 0, 500);
00072
00073
00074
00075
00076 bufLen_ = 0;
00077 int dataLen = 0;
00078
00079 dataLen = sizeof(double);
00080 memcpy(databuf_, &price_, dataLen);
00081 bufLen_ += dataLen;
00082
00083 dataLen = sizeof(long);
00084 memcpy(databuf_ + bufLen_, &quantity_, dataLen);
00085 bufLen_ += dataLen;
00086
00087 packString(databuf_, name_);
00088 packString(databuf_, sku_);
00089 packString(databuf_, category_);
00090 packString(databuf_, vendor_);
00091
00092 return (databuf_);
00093 }
00094
00095
00096
00097
00098
00099 inline size_t getBufferSize() { return (bufLen_); }
00100
00101
00102 void
00103 show() {
00104 std::cout << "\nName: " << name_ << std::endl;
00105 std::cout << " SKU: " << sku_ << std::endl;
00106 std::cout << " Price: " << price_ << std::endl;
00107 std::cout << " Quantity: " << quantity_ << std::endl;
00108 std::cout << " Category: " << category_ << std::endl;
00109 std::cout << " Vendor: " << vendor_ << std::endl;
00110 }
00111
00112 private:
00113
00114
00115
00116
00117
00118 void
00119 packString(char *buffer, std::string &theString)
00120 {
00121 size_t string_size = theString.size() + 1;
00122 memcpy(buffer+bufLen_, theString.c_str(), string_size);
00123 bufLen_ += string_size;
00124 }
00125
00126
00127 std::string category_, name_, vendor_, sku_;
00128 double price_;
00129 long quantity_;
00130 size_t bufLen_;
00131 char databuf_[500];
00132
00133 };
00134
00135 #define MAXFIELD 20
00136
00137 typedef struct vendor {
00138 char name[MAXFIELD];
00139 char street[MAXFIELD];
00140 char city[MAXFIELD];
00141 char state[3];
00142 char zipcode[6];
00143 char phone_number[13];
00144 char sales_rep[MAXFIELD];
00145 char sales_rep_phone[MAXFIELD];
00146 } VENDOR;
00147
00148
00149 class Db;
00150 class Dbt;
00151
00152
00153
00154
00155 int
00156 get_item_name(Db *dbp, const Dbt *pkey, const Dbt *pdata, Dbt *skey)
00157 {
00158
00159
00160
00161
00162
00163
00164 u_int32_t offset = sizeof(double) + sizeof(long);
00165 char *itemname = (char *)pdata->get_data() + offset;
00166
00167
00168 (void)pkey;
00169
00170
00171
00172
00173
00174
00175 if (offset > pdata->get_size()) {
00176 dbp->errx("get_item_name: buffer sizes do not match!");
00177
00178 return (-1);
00179 }
00180
00181
00182 skey->set_data(itemname);
00183 skey->set_size((u_int32_t)strlen(itemname) + 1);
00184
00185 return (0);
00186 };
00187 #endif