Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

gettingStartedCommon.hpp

00001 // File: gettingStartedCommon.hpp
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     /* Initialize our data members */
00024     void clear()
00025     {
00026         price_ = 0.0;
00027         quantity_ = 0;
00028         category_ = "";
00029         name_ = "";
00030         vendor_ = "";
00031         sku_ = "";
00032     }
00033 
00034     // Default constructor
00035     InventoryData() { clear(); }
00036 
00037     // Constructor from a void *
00038     // For use with the data returned from a bdb get
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      * Marshalls this classes data members into a single
00064      * contiguous memory location for the purpose of storing
00065      * the data in a database.
00066      */
00067     char *
00068     getBuffer()
00069     {
00070         // Zero out the buffer
00071         memset(databuf_, 0, 500);
00072         /*
00073          * Now pack the data into a single contiguous memory location for
00074          * storage.
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      * Returns the size of the buffer. Used for storing
00097      * the buffer in a database.
00098      */
00099     inline size_t getBufferSize() { return (bufLen_); }
00100 
00101     /* Utility function used to show the contents of this class */
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      * Utility function that appends a char * to the end of
00116      * the buffer.
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     /* Data members */
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];             /* Vendor name */
00139     char street[MAXFIELD];           /* Street name and number */
00140     char city[MAXFIELD];             /* City */
00141     char state[3];                   /* Two-digit US state code */
00142     char zipcode[6];                 /* US zipcode */
00143     char phone_number[13];           /* Vendor phone number */
00144     char sales_rep[MAXFIELD];        /* Name of sales representative */
00145     char sales_rep_phone[MAXFIELD];  /* Sales rep's phone number */
00146 } VENDOR;
00147 
00148 // Forward declarations
00149 class Db;
00150 class Dbt;
00151 
00152 // Used to extract an inventory item's name from an
00153 // inventory database record. This function is used to create
00154 // keys for secondary database records.
00155 int
00156 get_item_name(Db *dbp, const Dbt *pkey, const Dbt *pdata, Dbt *skey)
00157 {
00158     /*
00159      * First, obtain the buffer location where we placed the item's name. In
00160      * this example, the item's name is located in the primary data. It is the
00161      * first string in the buffer after the price (a double) and the quantity
00162      * (a long).
00163      */
00164     u_int32_t offset = sizeof(double) + sizeof(long);
00165     char *itemname = (char *)pdata->get_data() + offset;
00166 
00167     // unused
00168     (void)pkey;
00169 
00170     /*
00171      * If the offset is beyond the end of the data, then there was a problem
00172      * with the buffer contained in pdata, or there's a programming error in
00173      * how the buffer is marshalled/unmarshalled.  This should never happen!
00174      */
00175     if (offset > pdata->get_size()) {
00176         dbp->errx("get_item_name: buffer sizes do not match!");
00177         // When we return non-zero, the index record is not added/updated.
00178         return (-1);
00179     }
00180 
00181     /* Now set the secondary key's data to be the item name */
00182     skey->set_data(itemname);
00183     skey->set_size((u_int32_t)strlen(itemname) + 1);
00184 
00185     return (0);
00186 };
00187 #endif

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2