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

load_main.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: load_main.c,v 1.8 2005/04/12 20:21:19 bostic Exp $
00008  */
00009 
00010 #include "csv.h"
00011 #include "csv_local.h"
00012 #include "csv_extern.h"
00013 
00014 static int usage(void);
00015 
00016 /*
00017  * Globals
00018  */
00019 DB_ENV   *dbenv;                        /* Database environment */
00020 DB       *db;                           /* Primary database */
00021 DB      **secondary;                    /* Secondaries */
00022 int       verbose;                      /* Program verbosity */
00023 char     *progname;                     /* Program name */
00024 
00025 int
00026 main(int argc, char *argv[])
00027 {
00028         input_fmt ifmt;
00029         u_long version;
00030         int ch, ret, t_ret;
00031         char *home;
00032 
00033         /* Initialize globals. */
00034         dbenv = NULL;
00035         db = NULL;
00036         if ((progname = strrchr(argv[0], '/')) == NULL)
00037                 progname = argv[0];
00038         else
00039                 ++progname;
00040         verbose = 0;
00041 
00042         /* Initialize arguments. */
00043         home = NULL;
00044         ifmt = FORMAT_NL;
00045         version = 1;
00046 
00047         /* Process arguments. */
00048         while ((ch = getopt(argc, argv, "F:f:h:V:v")) != EOF)
00049                 switch (ch) {
00050                 case 'f':
00051                         if (freopen(optarg, "r", stdin) == NULL) {
00052                                 fprintf(stderr,
00053                                     "%s: %s\n", optarg, db_strerror(errno));
00054                                 return (EXIT_FAILURE);
00055                         }
00056                         break;
00057                 case 'F':
00058                         if (strcasecmp(optarg, "excel") == 0) {
00059                                 ifmt = FORMAT_EXCEL;
00060                                 break;
00061                         }
00062                         return (usage());
00063                 case 'h':
00064                         home = optarg;
00065                         break;
00066                 case 'V':
00067                         if (strtoul_err(optarg, &version))
00068                                 return (EXIT_FAILURE);
00069                         break;
00070                 case 'v':
00071                         ++verbose;
00072                         break;
00073                 case '?':
00074                 default:
00075                         return (usage());
00076                 }
00077         argc -= optind;
00078         argv += optind;
00079 
00080         if (*argv != NULL)
00081                 return (usage());
00082 
00083         /*
00084          * The home directory may not exist -- try and create it.  We don't
00085          * bother to distinguish between failure to create it and it already
00086          * existing, as the database environment open will fail if we aren't
00087          * successful.
00088          */
00089         if (home == NULL)
00090                 home = getenv("DB_HOME");
00091         if (home != NULL)
00092                 (void)mkdir(home, S_IRWXU);
00093 
00094         /* Create or join the database environment. */
00095         if (csv_env_open(home, 0) != 0)
00096                 return (EXIT_FAILURE);
00097 
00098         /* Load records into the database. */
00099         ret = input_load(ifmt, version);
00100 
00101         /* Close the database environment. */
00102         if ((t_ret = csv_env_close()) != 0 && ret == 0)
00103                 ret = t_ret;
00104 
00105         return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00106 }
00107 
00108 /*
00109  * usage --
00110  *      Program usage message.
00111  */
00112 static int
00113 usage(void)
00114 {
00115         (void)fprintf(stderr,
00116             "usage: %s [-v] [-F excel] [-f csv-file] [-h home]\n", progname);
00117         return (EXIT_FAILURE);
00118 }

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