gdrsclib
main.c
Go to the documentation of this file.
1 /***************************************************
2 Apache License Version 2.0
3 
4 Copyright 2007-2011 EcoEquity and Stockholm Environment Institute
5 
6 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
7 file except in compliance with the License. You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software distributed under
12 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 KIND, either express or implied. See the License for the specific language governing
14 permissions and limitations under the License.
15  ****************************************************/
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include "gdrs.h"
23 #ifdef DMALLOC
24 #include <dmalloc.h>
25 #endif
26 
27 int main(int argc, char *argv[]) {
28  struct gdrs_struct gdrs;
29  int i, retcode, opt;
30  int sql3ret, optndx;
31  char *db = NULL;
32 
33  static struct option long_options[] ={
34  {"db", required_argument, NULL, 'd'},
35  {"help", no_argument, NULL, 'h'},
36  {0, 0, 0, 0}
37  };
38 
39  while ((opt = getopt_long(argc, argv, "d:", long_options, &optndx)) != -1) {
40  switch (opt) {
41  case 'd':
42  db = strdup(optarg);
43  break;
44  case 'h':
45  db = NULL;
46  break;
47  }
48  }
49 
50  if (db != NULL) {
51  if ((retcode = loadgdrs(&gdrs, db))) {
52  fprintf(stderr, "Program returned with error code %d\n", retcode);
53  } else {
54  if ((sql3ret = dump2sql3(db, gdrs))) {
55  fprintf(stderr, "Error writing to sqlite database. Error code: %d\n", sql3ret);
56  }
57  cleargdrs(gdrs);
58  }
59  free(db);
60  } else {
61  // Write out usage message
62  printf("Usage:\n");
63  printf(" gdrsclib [-h] [--help] [-d database] [--db database]\n");
64  printf("where\n");
65  printf(" database: an sqlite3 database with the expected tables\n");
66  retcode = GDRS_DIDNOTRUN;
67  }
68 
69  return retcode;
70 }
int loadgdrs(struct gdrs_struct *gdrs, const char *fname)
Function to translate from SQLite3 database to internal data structures.
Definition: gdrs.c:54
int main(int argc, char *argv[])
Definition: main.c:27
int dump2sql3(const char *fname, struct gdrs_struct gdrs)
After the calculation, write all results to the SQLite3 database.
Definition: gdrs.c:1800
This is the main struct that contains data from the sqlite3 database and calculated values...
Definition: gdrs.h:92
void cleargdrs(struct gdrs_struct gdrs)
Clear out the GDRs data structure.
Definition: gdrs.c:621
#define GDRS_DIDNOTRUN
Definition: gdrs.h:35