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

util.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: util.c,v 1.17 2005/10/14 12:50:37 bostic Exp $
00008  */
00009 
00010 #include "csv.h"
00011 #include "csv_local.h"
00012 #include "csv_extern.h"
00013 
00014 /*
00015  * entry_print --
00016  *      Display the primary database's data item.
00017  */
00018 int
00019 entry_print(void *data, size_t len, u_int32_t field_count)
00020 {
00021         u_int32_t a, *offset;
00022         u_int i;
00023         char *raw;
00024 
00025         memcpy(&a, data, sizeof(u_int32_t));
00026         printf("\tversion: %lu\n", (u_long)a);
00027 
00028         offset = (u_int32_t *)data + 1;
00029         if (field_count == 0) {
00030                 memcpy(&a, offset++, sizeof(u_int32_t));
00031                 printf("\tcolumn map: %lu fields: {%.*s}\n", (u_long)a,
00032                     (int)(len - 2 * sizeof(u_int32_t)),
00033                     (u_int8_t *)data + 2 * sizeof(u_int32_t));
00034         } else {
00035                 raw = (char *)(offset + (field_count + 1));
00036                 for (i = 0; i < field_count; ++i) {
00037                         memcpy(&a, &offset[i], sizeof(u_int32_t));
00038                         len = OFFSET_LEN(offset, i);
00039                         printf("\toffset %4lu: len %4lu: {%.*s}\n",
00040                             (u_long)offset[i],
00041                             (u_long)len, (int)len, raw + a);
00042                 }
00043         }
00044 
00045         return (0);
00046 }
00047 
00048 /*
00049  * strtod_err --
00050  *      strtod(3) with error checking.
00051  */
00052 int
00053 strtod_err(char *input, double *valp)
00054 {
00055         double val;
00056         char *end;
00057 
00058         /*
00059          * strtoul requires setting errno to detect errors.
00060          */
00061         errno = 0;
00062         val = strtod(input, &end);
00063         if (errno == ERANGE) {
00064                 dbenv->err(dbenv, ERANGE, "%s", input);
00065                 return (1);
00066         }
00067         if (input[0] == '\0' ||
00068             (end[0] != '\0' && end[0] != '\n' && !isspace(end[0]))) {
00069                 dbenv->errx(dbenv,
00070                     "%s: invalid floating point argument", input);
00071                 return (1);
00072         }
00073 
00074         *valp = val;
00075         return (0);
00076 }
00077 
00078 /*
00079  * strtoul_err --
00080  *      strtoul(3) with error checking.
00081  */
00082 int
00083 strtoul_err(char *input, u_long *valp)
00084 {
00085         u_long val;
00086         char *end;
00087 
00088         /*
00089          * strtoul requires setting errno to detect errors.
00090          */
00091         errno = 0;
00092         val = strtoul(input, &end, 10);
00093         if (errno == ERANGE) {
00094                 dbenv->err(dbenv, ERANGE, "%s", input);
00095                 return (1);
00096         }
00097         if (input[0] == '\0' ||
00098             (end[0] != '\0' && end[0] != '\n' && !isspace(end[0]))) {
00099                 dbenv->errx(dbenv, "%s: invalid unsigned long argument", input);
00100                 return (1);
00101         }
00102 
00103         *valp = val;
00104         return (0);
00105 }
00106 
00107 int
00108 secondary_callback(DB *db_arg, const DBT *key, const DBT *data, DBT *result)
00109 {
00110         DbField *f;
00111         DbRecord record;
00112         void *faddr, *addr;
00113 
00114         /* Populate the field. */
00115         if (DbRecord_init(key, data, &record) != 0)
00116                 return (-1);
00117 
00118         f = db_arg->app_private;
00119         faddr = (u_int8_t *)&record + f->offset;
00120 
00121         /*
00122          * If necessary, copy the field into separate memory.
00123          * Set up the result DBT.
00124          */
00125         switch (f->type) {
00126         case STRING:
00127                 result->data = *(char **)faddr;
00128                 result->size = strlen(*(char **)faddr) + 1;
00129                 break;
00130         case DOUBLE:
00131                 if ((addr = malloc(sizeof(double))) == NULL)
00132                         return (-1);
00133                 result->data = addr;
00134                 result->size = sizeof(double);
00135                 result->flags = DB_DBT_APPMALLOC;
00136                 memcpy(addr, faddr, sizeof(double));
00137                 break;
00138         case ULONG:
00139                 if ((addr = malloc(sizeof(u_long))) == NULL)
00140                         return (-1);
00141                 result->data = addr;
00142                 result->size = sizeof(u_long);
00143                 result->flags = DB_DBT_APPMALLOC;
00144                 memcpy(addr, faddr, sizeof(u_long));
00145                 break;
00146         default:
00147         case NOTSET:
00148                 abort();
00149                 /* NOTREACHED */
00150         }
00151 
00152         return (0);
00153 }
00154 
00155 /*
00156  * compare_double --
00157  *      Compare two keys.
00158  */
00159 int
00160 compare_double(DB *db_arg, const DBT *a_arg, const DBT *b_arg)
00161 {
00162         double a, b;
00163 
00164         db_arg = db_arg;                        /* Quiet compiler. */
00165 
00166         memcpy(&a, a_arg->data, sizeof(double));
00167         memcpy(&b, b_arg->data, sizeof(double));
00168         return (a > b ? 1 : ((a < b) ? -1 : 0));
00169 }
00170 
00171 /*
00172  * compare_ulong --
00173  *      Compare two keys.
00174  */
00175 int
00176 compare_ulong(DB *db_arg, const DBT *a_arg, const DBT *b_arg)
00177 {
00178         u_long a, b;
00179 
00180         db_arg = db_arg;                        /* Quiet compiler. */
00181 
00182         memcpy(&a, a_arg->data, sizeof(u_long));
00183         memcpy(&b, b_arg->data, sizeof(u_long));
00184         return (a > b ? 1 : ((a < b) ? -1 : 0));
00185 }
00186 
00187 /*
00188  * field_cmp_double --
00189  *      Compare two double.
00190  */
00191 int
00192 field_cmp_double(void *a, void *b, OPERATOR op)
00193 {
00194         switch (op) {
00195         case GT:
00196                 return (*(double *)a > *(double *)b);
00197         case GTEQ:
00198                 return (*(double *)a >= *(double *)b);
00199         case LT:
00200                 return (*(double *)a < *(double *)b);
00201         case LTEQ:
00202                 return (*(double *)a <= *(double *)b);
00203         case NEQ:
00204                 return (*(double *)a != *(double *)b);
00205         case EQ:
00206                 return (*(double *)a == *(double *)b);
00207         case WC:
00208         case NWC:
00209                 break;
00210         }
00211 
00212         abort();
00213         /* NOTREACHED */
00214 }
00215 
00216 /*
00217  * field_cmp_re --
00218  *      Compare against regular expression.
00219  */
00220 int
00221 field_cmp_re(void *a, void *b, OPERATOR op)
00222 {
00223         op = op;                                /* Quiet compiler. */
00224 
00225         switch (op) {
00226 #ifdef HAVE_WILDCARD_SUPPORT
00227         case WC:
00228                 return (regexec(b, *(char **)a, 0, NULL, 0) == 0);
00229         case NWC:
00230                 return (regexec(b, *(char **)a, 0, NULL, 0) != 0);
00231 #else
00232         case WC:
00233         case NWC:
00234                 a = a;
00235                 b = b;                          /* Quiet compiler. */
00236                 /* FALLTHROUGH */
00237 #endif
00238         case GT:
00239         case GTEQ:
00240         case LT:
00241         case LTEQ:
00242         case NEQ:
00243         case EQ:
00244                 break;
00245         }
00246 
00247         abort();
00248         /* NOTREACHED */
00249 }
00250 
00251 /*
00252  * field_cmp_string --
00253  *      Compare two strings.
00254  */
00255 int
00256 field_cmp_string(void *a, void *b, OPERATOR op)
00257 {
00258         int v;
00259 
00260         v = strcasecmp(*(char **)a, b);
00261         switch (op) {
00262         case GT:
00263                 return (v > 0 ? 1 : 0);
00264         case GTEQ:
00265                 return (v >= 0 ? 1 : 0);
00266         case LT:
00267                 return (v < 0 ? 1 : 0);
00268         case LTEQ:
00269                 return (v <= 0 ? 1 : 0);
00270         case NEQ:
00271                 return (v ? 1 : 0);
00272         case EQ:
00273                 return (v ? 0 : 1);
00274         case WC:
00275         case NWC:
00276                 break;
00277         }
00278 
00279         abort();
00280         /* NOTREACHED */
00281 }
00282 
00283 /*
00284  * field_cmp_ulong --
00285  *      Compare two ulongs.
00286  */
00287 int
00288 field_cmp_ulong(void *a, void *b, OPERATOR op)
00289 {
00290         switch (op) {
00291         case GT:
00292                 return (*(u_long *)a > *(u_long *)b);
00293         case GTEQ:
00294                 return (*(u_long *)a >= *(u_long *)b);
00295         case LT:
00296                 return (*(u_long *)a < *(u_long *)b);
00297         case LTEQ:
00298                 return (*(u_long *)a <= *(u_long *)b);
00299         case NEQ:
00300                 return (*(u_long *)a != *(u_long *)b);
00301         case EQ:
00302                 return (*(u_long *)a == *(u_long *)b);
00303         case WC:
00304         case NWC:
00305                 break;
00306         }
00307 
00308         abort();
00309         /* NOTREACHED */
00310 }

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