| 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: gettingStartedCommon.hpp |
| 10 |
|
| 11 |
#ifndef GETTINGSTARTEDCOMMON_H |
| 12 |
#define GETTINGSTARTEDCOMMON_H |
| 13 |
|
| 14 |
#include <cstdlib> |
| 15 |
#include <cstring> |
| 16 |
|
| 17 |
class InventoryData |
| 18 |
{ |
| 19 |
public: |
| 20 |
inline void setPrice(double price) {price_ = price;} |
| 21 |
inline void setQuantity(long quantity) {quantity_ = quantity;} |
| 22 |
inline void setCategory(std::string &category) {category_ = category;} |
| 23 |
inline void setName(std::string &name) {name_ = name;} |
| 24 |
inline void setVendor(std::string &vendor) {vendor_ = vendor;} |
| 25 |
inline void setSKU(std::string &sku) {sku_ = sku;} |
| 26 |
|
| 27 |
inline double& getPrice() {return(price_);} |
| 28 |
inline long& getQuantity() {return(quantity_);} |
| 29 |
inline std::string& getCategory() {return(category_);} |
| 30 |
inline std::string& getName() {return(name_);} |
| 31 |
inline std::string& getVendor() {return(vendor_);} |
| 32 |
inline std::string& getSKU() {return(sku_);} |
| 33 |
|
| 34 |
/* Initialize our data members */ |
| 35 |
void clear() |
| 36 |
{ |
| 37 |
price_ = 0.0; |
| 38 |
quantity_ = 0; |
| 39 |
category_ = ""; |
| 40 |
name_ = ""; |
| 41 |
vendor_ = ""; |
| 42 |
sku_ = ""; |
| 43 |
} |
| 44 |
|
| 45 |
// Default constructor |
| 46 |
InventoryData() { clear(); } |
| 47 |
|
| 48 |
// Constructor from a void * |
| 49 |
// For use with the data returned from a bdb get |
| 50 |
InventoryData(void *buffer) |
| 51 |
{ |
| 52 |
char *buf = (char *)buffer; |
| 53 |
|
| 54 |
price_ = *((double *)buf); |
| 55 |
bufLen_ = sizeof(double); |
| 56 |
|
| 57 |
quantity_ = *((long *)(buf + bufLen_)); |
| 58 |
bufLen_ += sizeof(long); |
| 59 |
|
| 60 |
name_ = buf + bufLen_; |
| 61 |
bufLen_ += name_.size() + 1; |
| 62 |
|
| 63 |
sku_ = buf + bufLen_; |
| 64 |
bufLen_ += sku_.size() + 1; |
| 65 |
|
| 66 |
category_ = buf + bufLen_; |
| 67 |
bufLen_ += category_.size() + 1; |
| 68 |
|
| 69 |
vendor_ = buf + bufLen_; |
| 70 |
bufLen_ += vendor_.size() + 1; |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Marshalls this classes data members into a single |
| 75 |
* contiguous memory location for the purpose of storing |
| 76 |
* the data in a database. |
| 77 |
*/ |
| 78 |
char * |
| 79 |
getBuffer() |
| 80 |
{ |
| 81 |
// Zero out the buffer |
| 82 |
memset(databuf_, 0, 500); |
| 83 |
/* |
| 84 |
* Now pack the data into a single contiguous memory location for |
| 85 |
* storage. |
| 86 |
*/ |
| 87 |
bufLen_ = 0; |
| 88 |
int dataLen = 0; |
| 89 |
|
| 90 |
dataLen = sizeof(double); |
| 91 |
memcpy(databuf_, &price_, dataLen); |
| 92 |
bufLen_ += dataLen; |
| 93 |
|
| 94 |
dataLen = sizeof(long); |
| 95 |
memcpy(databuf_ + bufLen_, &quantity_, dataLen); |
| 96 |
bufLen_ += dataLen; |
| 97 |
|
| 98 |
packString(databuf_, name_); |
| 99 |
packString(databuf_, sku_); |
| 100 |
packString(databuf_, category_); |
| 101 |
packString(databuf_, vendor_); |
| 102 |
|
| 103 |
return (databuf_); |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Returns the size of the buffer. Used for storing |
| 108 |
* the buffer in a database. |
| 109 |
*/ |
| 110 |
inline size_t getBufferSize() { return (bufLen_); } |
| 111 |
|
| 112 |
/* Utility function used to show the contents of this class */ |
| 113 |
void |
| 114 |
show() { |
| 115 |
std::cout << "\nName: " << name_ << std::endl; |
| 116 |
std::cout << " SKU: " << sku_ << std::endl; |
| 117 |
std::cout << " Price: " << price_ << std::endl; |
| 118 |
std::cout << " Quantity: " << quantity_ << std::endl; |
| 119 |
std::cout << " Category: " << category_ << std::endl; |
| 120 |
std::cout << " Vendor: " << vendor_ << std::endl; |
| 121 |
} |
| 122 |
|
| 123 |
private: |
| 124 |
|
| 125 |
/* |
| 126 |
* Utility function that appends a char * to the end of |
| 127 |
* the buffer. |
| 128 |
*/ |
| 129 |
void |
| 130 |
packString(char *buffer, std::string &theString) |
| 131 |
{ |
| 132 |
size_t string_size = theString.size() + 1; |
| 133 |
memcpy(buffer+bufLen_, theString.c_str(), string_size); |
| 134 |
bufLen_ += string_size; |
| 135 |
} |
| 136 |
|
| 137 |
/* Data members */ |
| 138 |
std::string category_, name_, vendor_, sku_; |
| 139 |
double price_; |
| 140 |
long quantity_; |
| 141 |
size_t bufLen_; |
| 142 |
char databuf_[500]; |
| 143 |
|
| 144 |
}; |
| 145 |
|
| 146 |
#define MAXFIELD 20 |
| 147 |
|
| 148 |
typedef struct vendor { |
| 149 |
char name[MAXFIELD]; /* Vendor name */ |
| 150 |
char street[MAXFIELD]; /* Street name and number */ |
| 151 |
char city[MAXFIELD]; /* City */ |
| 152 |
char state[3]; /* Two-digit US state code */ |
| 153 |
char zipcode[6]; /* US zipcode */ |
| 154 |
char phone_number[13]; /* Vendor phone number */ |
| 155 |
char sales_rep[MAXFIELD]; /* Name of sales representative */ |
| 156 |
char sales_rep_phone[MAXFIELD]; /* Sales rep's phone number */ |
| 157 |
} VENDOR; |
| 158 |
|
| 159 |
// Forward declarations |
| 160 |
class Db; |
| 161 |
class Dbt; |
| 162 |
|
| 163 |
// Used to extract an inventory item's name from an |
| 164 |
// inventory database record. This function is used to create |
| 165 |
// keys for secondary database records. |
| 166 |
int |
| 167 |
get_item_name(Db *dbp, const Dbt *pkey, const Dbt *pdata, Dbt *skey) |
| 168 |
{ |
| 169 |
/* |
| 170 |
* First, obtain the buffer location where we placed the item's name. In |
| 171 |
* this example, the item's name is located in the primary data. It is the |
| 172 |
* first string in the buffer after the price (a double) and the quantity |
| 173 |
* (a long). |
| 174 |
*/ |
| 175 |
u_int32_t offset = sizeof(double) + sizeof(long); |
| 176 |
char *itemname = (char *)pdata->get_data() + offset; |
| 177 |
|
| 178 |
// unused |
| 179 |
(void)pkey; |
| 180 |
|
| 181 |
/* |
| 182 |
* If the offset is beyond the end of the data, then there was a problem |
| 183 |
* with the buffer contained in pdata, or there's a programming error in |
| 184 |
* how the buffer is marshalled/unmarshalled. This should never happen! |
| 185 |
*/ |
| 186 |
if (offset > pdata->get_size()) { |
| 187 |
dbp->errx("get_item_name: buffer sizes do not match!"); |
| 188 |
// When we return non-zero, the index record is not added/updated. |
| 189 |
return (-1); |
| 190 |
} |
| 191 |
|
| 192 |
/* Now set the secondary key's data to be the item name */ |
| 193 |
skey->set_data(itemname); |
| 194 |
skey->set_size((u_int32_t)strlen(itemname) + 1); |
| 195 |
|
| 196 |
return (0); |
| 197 |
}; |
| 198 |
#endif |