db5.c
Go to the documentation of this file.00001
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <ham/hamsterdb.h>
00021
00022 int
00023 main(int argc, char **argv)
00024 {
00025 ham_status_t st;
00026 ham_db_t *db;
00027 ham_cursor_t *cursor;
00028 char line[1024*4];
00029 unsigned lineno=0;
00030 ham_key_t key;
00031 ham_record_t record;
00032
00033 memset(&key, 0, sizeof(key));
00034 memset(&record, 0, sizeof(record));
00035
00036 printf("This sample uses hamsterdb and duplicate keys to list all words "
00037 "in the\noriginal order, together with their line number.\n");
00038 printf("Reading from stdin...\n");
00039
00040
00041
00042
00043 st=ham_new(&db);
00044 if (st!=HAM_SUCCESS) {
00045 printf("ham_new() failed with error %d\n", st);
00046 return (-1);
00047 }
00048
00049
00050
00051
00052
00053
00054 st=ham_create_ex(db, "test.db", HAM_ENABLE_DUPLICATES, 0664, 0);
00055 if (st!=HAM_SUCCESS) {
00056 printf("ham_create_ex() failed with error %d\n", st);
00057 return (-1);
00058 }
00059
00060
00061
00062
00063
00064 while (fgets(line, sizeof(line), stdin)) {
00065 char *start=line, *p;
00066 lineno++;
00067
00068
00069
00070
00071
00072 while ((p=strtok(start, " \t\r\n"))) {
00073 key.data=p;
00074 key.size=(ham_size_t)strlen(p)+1;
00075
00076 record.data=&lineno;
00077 record.size=sizeof(lineno);
00078
00079 st=ham_insert(db, 0, &key, &record, HAM_DUPLICATE);
00080 if (st!=HAM_SUCCESS) {
00081 printf("ham_insert() failed with error %d\n", st);
00082 return (-1);
00083 }
00084 printf(".");
00085
00086 start=0;
00087 }
00088 }
00089
00090
00091
00092
00093 st=ham_cursor_create(db, 0, 0, &cursor);
00094 if (st!=HAM_SUCCESS) {
00095 printf("ham_cursor_create() failed with error %d\n", st);
00096 return (-1);
00097 }
00098
00099
00100
00101
00102 while (1) {
00103 st=ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
00104 if (st!=HAM_SUCCESS) {
00105
00106 if (st==HAM_KEY_NOT_FOUND)
00107 break;
00108 else {
00109 printf("ham_cursor_next() failed with error %d\n", st);
00110 return (-1);
00111 }
00112 }
00113
00114
00115
00116
00117 printf("%s: appeared in line %u\n", (const char *)key.data,
00118 *(unsigned *)record.data);
00119 }
00120
00121
00122
00123
00124
00125
00126 st=ham_close(db, HAM_AUTO_CLEANUP);
00127 if (st!=HAM_SUCCESS) {
00128 printf("ham_close() failed with error %d\n", st);
00129 return (-1);
00130 }
00131
00132
00133
00134
00135 ham_delete(db);
00136
00137
00138
00139
00140 return (0);
00141 }
00142