diff options
Diffstat (limited to 'docs/gsg/C/DbUsage.html')
-rw-r--r-- | docs/gsg/C/DbUsage.html | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/docs/gsg/C/DbUsage.html b/docs/gsg/C/DbUsage.html index c272b9de..a7432990 100644 --- a/docs/gsg/C/DbUsage.html +++ b/docs/gsg/C/DbUsage.html @@ -14,7 +14,7 @@ <body> <div xmlns="" class="navheader"> <div class="libver"> - <p>Library Version 11.2.5.3</p> + <p>Library Version 12.1.6.1</p> </div> <table width="100%" summary="Navigation header"> <tr> @@ -46,7 +46,7 @@ Again, remember that you can find the complete implementation for these functions in: </p> - <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_c/getting_started</pre> + <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples/c/getting_started</pre> <p> where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you placed your DB distribution. @@ -109,7 +109,7 @@ this example program. However, as always you can find the complete implementation for this program here: </p> - <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_c/getting_started</pre> + <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples/c/getting_started</pre> <p> where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you placed your DB distribution. @@ -123,7 +123,7 @@ /* Forward declarations */ int load_vendors_database(STOCK_DBS, char *); -int pack_string(char *, char *, int); +size_t pack_string(char *, char *, size_t); int load_inventory_database(STOCK_DBS, char *); </pre> <p> Next we begin our <code class="function">main()</code> function with the variable @@ -139,7 +139,8 @@ int main(int argc, char *argv[]) { STOCK_DBS my_stock; - int ret, size; + int ret; + size_t size; char *basename, *inventory_file, *vendor_file; /* Initialize the STOCK_DBS struct */ @@ -177,20 +178,20 @@ main(int argc, char *argv[]) /* Open all databases */ ret = databases_setup(&my_stock, "example_database_load", stderr); - if (ret != 0) { - fprintf(stderr, "Error opening databases\n"); + if (ret) { + fprintf(stderr, "Error opening databases.\n"); databases_close(&my_stock); return (ret); } ret = load_vendors_database(my_stock, vendor_file); - if (!ret) { + if (ret) { fprintf(stderr, "Error loading vendors database.\n"); databases_close(&my_stock); return (ret); } ret = load_inventory_database(my_stock, inventory_file); - if (!ret) { + if (ret) { fprintf(stderr, "Error loading inventory database.\n"); databases_close(&my_stock); return (ret); @@ -233,9 +234,9 @@ int load_vendors_database(STOCK_DBS my_stock, char *vendor_file) { DBT key, data; + char buf[MAXLINE]; FILE *ifp; VENDOR my_vendor; - char buf[MAXLINE]; /* Open the vendor file for read access */ ifp = fopen(vendor_file, "r"); @@ -272,11 +273,11 @@ load_vendors_database(STOCK_DBS my_stock, char *vendor_file) /* Set up the database record's key */ key.data = my_vendor.name; - key.size = strlen(my_vendor.name) + 1; + key.size = (u_int32_t)strlen(my_vendor.name) + 1; /* Set up the database record's data */ data.data = &my_vendor; - data.size = sizeof(my_vendor); + data.size = sizeof(VENDOR); /* * Note that given the way we built our struct, there are extra @@ -330,10 +331,11 @@ load_vendors_database(STOCK_DBS my_stock, char *vendor_file) * appropriate location. Used to ensure that all our strings * are contained in a single contiguous chunk of memory. */ -int -pack_string(char *buffer, char *string, int start_pos) +size_t +pack_string(char *buffer, char *string, size_t start_pos) { - int string_size = strlen(string) + 1; + size_t string_size; + string_size = strlen(string) + 1; memcpy(buffer+start_pos, string, string_size); @@ -345,15 +347,19 @@ pack_string(char *buffer, char *string, int start_pos) <a id="c_dbt15"></a> <pre class="programlisting">/* * Loads the contents of the inventory.txt file into - * a database. + * a database. Note that because the itemname + * secondary database is associated to the inventorydb + * (see env_setup() in gettingstarted_common.c), the + * itemname index is automatically created when this + * database is loaded. */ int load_inventory_database(STOCK_DBS my_stock, char *inventory_file) { DBT key, data; char buf[MAXLINE]; - void *databuf; - int bufLen, dataLen; + char databuf[MAXDATABUF]; + size_t bufLen, dataLen; FILE *ifp; /* @@ -374,9 +380,6 @@ load_inventory_database(STOCK_DBS my_stock, char *inventory_file) return(-1); } - /* Get our buffer. MAXDATABUF is some suitably large number */ - databuf = malloc(MAXDATABUF); - /* * Read the inventory.txt file line by line, saving each line off to * the database as we go. @@ -438,11 +441,11 @@ load_inventory_database(STOCK_DBS my_stock, char *inventory_file) * not support duplicates for this database. */ key.data = sku; - key.size = strlen(sku) + 1; + key.size = (u_int32_t)strlen(sku) + 1; /* The data is the information that we packed into databuf. */ data.data = databuf; - data.size = bufLen; + data.size = (u_int32_t)bufLen; /* Put the data into the database */ my_stock.vendor_dbp->put(my_stock.inventory_dbp, 0, @@ -451,8 +454,6 @@ load_inventory_database(STOCK_DBS my_stock, char *inventory_file) /* Cleanup */ fclose(ifp); - if (databuf != NULL) - free(databuf); return(0); } </pre> |