1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2004, 2015 Oracle and/or its affiliates. All rights reserved.
*/
#include "gettingstarted_common.h"
/* Forward declarations */
int usage(void);
char *show_inventory_item(void *);
int show_all_records(STOCK_DBS *);
int show_records(STOCK_DBS *, char *);
int show_vendor_record(char *, DB *);
int
usage()
{
fprintf(stderr, "example_database_read [-i <item name>]");
fprintf(stderr, " [-h <database home>]\n");
fprintf(stderr,
"\tNote: Any path specified to the -h parameter must end\n");
fprintf(stderr, " with your system's path delimiter (/ or \\)\n");
return (-1);
}
/*
* Searches for a inventory item based on that item's name. The search is
* performed using the item name secondary database. Displays all
* inventory items that use the specified name, as well as the vendor
* associated with that inventory item.
*
* If no item name is provided, then all inventory items are displayed.
*/
int
main(int argc, char *argv[])
{
STOCK_DBS my_stock;
int ch, ret;
char *itemname;
/* Initialize the STOCK_DBS struct */
initialize_stockdbs(&my_stock);
/* Parse the command line arguments */
itemname = NULL;
while ((ch = getopt(argc, argv, "h:i:?")) != EOF)
switch (ch) {
case 'h':
if (optarg[strlen(optarg)-1] != '/' &&
optarg[strlen(optarg)-1] != '\\')
return (usage());
my_stock.db_home_dir = optarg;
break;
case 'i':
itemname = optarg;
break;
case '?':
default:
return (usage());
}
/* Identify the files that hold our databases */
set_db_filenames(&my_stock);
/* Open all databases */
ret = databases_setup(&my_stock, "example_database_read", stderr);
if (ret != 0) {
fprintf(stderr, "Error opening databases\n");
databases_close(&my_stock);
return (ret);
}
/*
* Show either a single item or all items, depending
* on whether itemname is set to a value.
*/
if (itemname == NULL)
ret = show_all_records(&my_stock);
else
ret = show_records(&my_stock, itemname);
/* close our databases */
databases_close(&my_stock);
return (ret);
}
int show_all_records(STOCK_DBS *my_stock)
{
DBC *inventory_cursorp;
DBT key, data;
char *the_vendor;
int exit_value, ret;
/* Initialize our DBTs. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Get a cursor to the inventory db */
my_stock->inventory_dbp->cursor(my_stock->inventory_dbp, NULL,
&inventory_cursorp, 0);
/*
* Iterate over the inventory database, from the first record
* to the last, displaying each in turn.
*/
exit_value = 0;
while ((ret =
inventory_cursorp->get(inventory_cursorp, &key, &data, DB_NEXT)) == 0)
{
the_vendor = show_inventory_item(data.data);
ret = show_vendor_record(the_vendor, my_stock->vendor_dbp);
if (ret) {
exit_value = ret;
break;
}
}
/* Close the cursor */
inventory_cursorp->close(inventory_cursorp);
return (exit_value);
}
/*
* Search for an inventory item given its name (using the inventory item
* secondary database) and display that record and any duplicates that may
* exist.
*/
int
show_records(STOCK_DBS *my_stock, char *itemname)
{
DBC *itemname_cursorp;
DBT key, data;
char *the_vendor;
int ret, exit_value;
/* Initialize our DBTs. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Get a cursor to the itemname db */
my_stock->itemname_sdbp->cursor(my_stock->itemname_sdbp, NULL,
&itemname_cursorp, 0);
/*
* Get the search key. This is the name on the inventory
* record that we want to examine.
*/
key.data = itemname;
key.size = (u_int32_t)strlen(itemname) + 1;
/*
* Position our cursor to the first record in the secondary
* database that has the appropriate key.
*/
exit_value = 0;
ret = itemname_cursorp->get(itemname_cursorp, &key, &data, DB_SET);
if (!ret) {
do {
/*
* Show the inventory record and the vendor responsible
* for this inventory item.
*/
the_vendor = show_inventory_item(data.data);
ret = show_vendor_record(the_vendor, my_stock->vendor_dbp);
if (ret) {
exit_value = ret;
break;
}
/*
* Our secondary allows duplicates, so we need to loop over
* the next duplicate records and show them all. This is done
* because an inventory item's name is not a unique value.
*/
} while (itemname_cursorp->get(itemname_cursorp, &key, &data,
DB_NEXT_DUP) == 0);
} else {
printf("No records found for '%s'\n", itemname);
}
/* Close the cursor */
itemname_cursorp->close(itemname_cursorp);
return (exit_value);
}
/*
* Shows an inventory item. How we retrieve the inventory
* item values from the provided buffer is strictly dependent
* on the order that those items were originally stored in the
* DBT. See load_inventory_database in example_database_load
* for how this was done.
*/
char *
show_inventory_item(void *vBuf)
{
float price;
int quantity;
size_t buf_pos;
char *category, *name, *sku, *vendor_name;
char *buf = (char *)vBuf;
/* Get the price. */
price = *((float *)buf);
buf_pos = sizeof(float);
/* Get the quantity. */
quantity = *((int *)(buf + buf_pos));
buf_pos += sizeof(int);
/* Get the inventory item's name */
name = buf + buf_pos;
buf_pos += strlen(name) + 1;
/* Get the inventory item's sku */
sku = buf + buf_pos;
buf_pos += strlen(sku) + 1;
/*
* Get the category (fruits, vegetables, desserts) that this
* item belongs to.
*/
category = buf + buf_pos;
buf_pos += strlen(category) + 1;
/* Get the vendor's name */
vendor_name = buf + buf_pos;
/* Display all this information */
printf("name: %s\n", name);
printf("\tSKU: %s\n", sku);
printf("\tCategory: %s\n", category);
printf("\tPrice: %.2f\n", price);
printf("\tQuantity: %i\n", quantity);
printf("\tVendor:\n");
/* Return the vendor's name */
return (vendor_name);
}
/*
* Shows a vendor record. Each vendor record is an instance of
* a vendor structure. See load_vendor_database() in
* example_database_load for how this structure was originally
* put into the database.
*/
int
show_vendor_record(char *vendor_name, DB *vendor_dbp)
{
DBT key, data;
VENDOR my_vendor;
int ret;
/* Zero our DBTs */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Set the search key to the vendor's name */
key.data = vendor_name;
key.size = (u_int32_t)strlen(vendor_name) + 1;
/*
* Make sure we use the memory we set aside for the VENDOR
* structure rather than the memory that DB allocates.
* Some systems may require structures to be aligned in memory
* in a specific way, and DB may not get it right.
*/
data.data = &my_vendor;
data.ulen = sizeof(VENDOR);
data.flags = DB_DBT_USERMEM;
/* Get the record */
ret = vendor_dbp->get(vendor_dbp, NULL, &key, &data, 0);
if (ret != 0) {
vendor_dbp->err(vendor_dbp, ret, "Error searching for vendor: '%s'",
vendor_name);
return (ret);
} else {
printf("\t\t%s\n", my_vendor.name);
printf("\t\t%s\n", my_vendor.street);
printf("\t\t%s, %s\n", my_vendor.city, my_vendor.state);
printf("\t\t%s\n\n", my_vendor.zipcode);
printf("\t\t%s\n\n", my_vendor.phone_number);
printf("\t\tContact: %s\n", my_vendor.sales_rep);
printf("\t\t%s\n", my_vendor.sales_rep_phone);
}
return (0);
}
|