void load_config(void) { if ((lang = malloc(strlen(DEFAULT_LANGUAGE)+1))==NULL) print_error("Out of memory"); strcpy(lang, DEFAULT_LANGUAGE); if ((locale = malloc(strlen(DEFAULT_LOCALE)+1))==NULL) print_error("Out of memory"); strcpy(locale, DEFAULT_LOCALE); units = DEFAULT_UNITS; ifaces = NULL; FILE *stream; if ((stream = fopen(CONFIG_PATH, "r")) == NULL) print_error("Can't open config file");; while (!feof(stream)) { char *line = read_line(stream); char *directive, *value; parse_line(line, &directive, &value); if (directive!=NULL) { lowerstr(directive); if (strcmp(directive, "language")== 0) { parse_language(value); } else if (strcmp(directive, "locale")== 0) { parse_locale(value); } else if (strcmp(directive, "units")==0) { parse_units(value); } else if (strcmp(directive, "interface")==0) { parse_interface(value); } free(directive); free(value); } free(line); } if (ifaces == NULL) { print_error("No interface specified in config file."); } }
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL parse_bytes (const char *s, const char *def_unit) { return parse_units (s, bytes_units, def_unit); }
int ROKEN_LIB_FUNCTION parse_time (const char *s, const char *def_unit) { return parse_units (s, time_units, def_unit); }
int parse_command_line(int argc, char *argv[]) { char pl_desc[256]; char pw_desc[256]; int i; struct { struct Option *cell; struct Option *units; struct Option *pl; /* page length */ struct Option *pw; /* page width */ struct Option *outfile; struct Option *nv; struct Option *nsteps; } parms; struct { struct Flag *f; struct Flag *m; struct Flag *h; struct Flag *q; struct Flag *e; struct Flag *n; struct Flag *N; struct Flag *i; /* use quant rules for fp map, i.e. read it as int */ struct Flag *C; /* report for fp ranges in Cats file (fp maps only) */ } flags; parms.cell = G_define_standard_option(G_OPT_R_MAPS); parms.cell->description = _("Raster map(s) to report on"); parms.units = G_define_option(); parms.units->key = "units"; parms.units->type = TYPE_STRING; parms.units->required = NO; parms.units->multiple = YES; parms.units->description = _("Units"); parms.units->descriptions = _("mi;miles;me;meters;k;kilometers;a;acres;" "h;hectares;c;cell counts;p;percent cover"); parms.units->options = "mi,me,k,a,h,c,p"; parms.units->guisection = _("Output settings"); parms.nv = G_define_option(); parms.nv->key = "null"; parms.nv->type = TYPE_STRING; parms.nv->required = NO; parms.nv->multiple = NO; parms.nv->answer = "*"; parms.nv->description = _("Character representing no data cell value"); parms.nv->guisection = _("Formatting"); parms.pl = G_define_option(); parms.pl->key = "pl"; parms.pl->type = TYPE_INTEGER; parms.pl->required = NO; sprintf(pl_desc, _("Page length (default: %d lines)"), DEFAULT_PAGE_LENGTH); parms.pl->description = pl_desc; parms.pl->guisection = _("Formatting"); parms.pw = G_define_option(); parms.pw->key = "pw"; parms.pw->type = TYPE_INTEGER; parms.pw->required = NO; sprintf(pw_desc, _("Page width (default: %d characters)"), DEFAULT_PAGE_WIDTH); parms.pw->description = pw_desc; parms.pw->guisection = _("Formatting"); parms.outfile = G_define_standard_option(G_OPT_F_OUTPUT); parms.outfile->key = "output"; parms.outfile->required = NO; parms.outfile->label = _("Name for output file to hold the report"); parms.outfile->description = _("If no output given report is printed to standard output"); parms.outfile->guisection = _("Output settings"); parms.nsteps = G_define_option(); parms.nsteps->key = "nsteps"; parms.nsteps->type = TYPE_INTEGER; parms.nsteps->required = NO; parms.nsteps->multiple = NO; parms.nsteps->answer = "255"; parms.nsteps->description = _("Number of fp subranges to collect stats from"); parms.nsteps->guisection = _("FP maps"); flags.h = G_define_flag(); flags.h->key = 'h'; flags.h->description = _("Suppress page headers"); flags.h->guisection = _("Formatting"); flags.f = G_define_flag(); flags.f->key = 'f'; flags.f->description = _("Use formfeeds between pages"); flags.f->guisection = _("Formatting"); flags.e = G_define_flag(); flags.e->key = 'e'; flags.e->description = _("Scientific format"); flags.e->guisection = _("Formatting"); flags.n = G_define_flag(); flags.n->key = 'n'; flags.n->description = _("Filter out all no data cells"); flags.N = G_define_flag(); flags.N->key = 'N'; flags.N->description = _("Filter out cells where all maps have no data"); flags.C = G_define_flag(); flags.C->key = 'C'; flags.C->description = _("Report for cats fp ranges (fp maps only)"); flags.C->guisection = _("FP maps"); flags.i = G_define_flag(); flags.i->key = 'i'; flags.i->description = _("Read fp map as integer (use map's quant rules)"); flags.i->guisection = _("FP maps"); /* hidden feature. * if first arg is >file just run r.stats into this file and quit * if first arg is <file, run report from stats in file * (this feature is for the interactive version of this program - * to get more than one report without re-running r.stats) */ stats_flag = EVERYTHING; if (argc > 1) { if (argv[1][0] == '<' || argv[1][0] == '>') { stats_file = argv[1] + 1; if (argv[1][0] == '<') stats_flag = REPORT_ONLY; else { unlink(stats_file); stats_flag = STATS_ONLY; } argc--; argv++; } } if (G_parser(argc, argv)) exit(EXIT_FAILURE); use_formfeed = flags.f->answer; with_headers = !flags.h->answer; e_format = flags.e->answer; no_nulls = flags.n->answer; no_nulls_all = flags.N->answer; cat_ranges = flags.C->answer; as_int = flags.i->answer; for (i = 0; parms.cell->answers[i]; i++) parse_layer(parms.cell->answers[i]); if (parms.units->answers) for (i = 0; parms.units->answers[i]; i++) parse_units(parms.units->answers[i]); sscanf(parms.nsteps->answer, "%d", &nsteps); if (nsteps <= 0) { G_warning(_("nsteps has to be > 0; using nsteps=255")); nsteps = 255; } if (parms.pl->answer) { if (sscanf(parms.pl->answer, "%d", &page_length) != 1 || page_length < 0) { G_fatal_error(_("Illegal page length")); } } if (parms.pw->answer) { if (sscanf(parms.pw->answer, "%d", &page_width) != 1 || page_width < 1) { G_fatal_error(_("Illegal page width")); } } if (parms.outfile->answer) { if (freopen(parms.outfile->answer, "w", stdout) == NULL) { perror(parms.outfile->answer); exit(EXIT_FAILURE); } } no_data_str = parms.nv->answer; return 0; }
int parse_command_line(int argc, char *argv[]) { int i; char *desc; struct { struct Option *cell; struct Option *units; struct Option *pl; /* page length */ struct Option *pw; /* page width */ struct Option *outfile; struct Option *nv; struct Option *nsteps; struct Option *sort; } parms; struct { struct Flag *f; struct Flag *m; struct Flag *h; struct Flag *q; struct Flag *e; struct Flag *n; struct Flag *N; struct Flag *i; /* use quant rules for fp map, i.e. read it as int */ struct Flag *C; /* report for fp ranges in Cats file (fp maps only) */ } flags; parms.cell = G_define_standard_option(G_OPT_R_MAPS); parms.cell->description = _("Name of raster map(s) to report on"); parms.units = G_define_option(); parms.units->key = "units"; parms.units->type = TYPE_STRING; parms.units->required = NO; parms.units->multiple = YES; parms.units->description = _("Units to report"); desc = NULL; G_asprintf(&desc, "mi;%s;me;%s;k;%s;a;%s;h;%s;c;%s;p;%s", _("area in square miles"), _("area in square meters"), _("area in square kilometers"), _("area in acres"), _("area in hectares"), _("number of cells"), _("percent cover")); parms.units->descriptions = desc; parms.units->options = "mi,me,k,a,h,c,p"; parms.units->guisection = _("Statistics"); parms.outfile = G_define_standard_option(G_OPT_F_OUTPUT); parms.outfile->required = NO; parms.outfile->label = _("Name for output file to hold the report"); parms.outfile->description = _("If no output file given report is printed to standard output"); parms.nv = G_define_standard_option(G_OPT_M_NULL_VALUE); parms.nv->answer = "*"; parms.nv->guisection = _("Formatting"); parms.pl = G_define_option(); parms.pl->key = "page_length"; parms.pl->type = TYPE_INTEGER; parms.pl->required = NO; parms.pl->description = _("Page length"); parms.pl->answer = DEFAULT_PAGE_LENGTH; parms.pl->guisection = _("Formatting"); parms.pw = G_define_option(); parms.pw->key = "page_width"; parms.pw->type = TYPE_INTEGER; parms.pw->required = NO; parms.pw->description = _("Page width"); parms.pw->answer = DEFAULT_PAGE_WIDTH; parms.pw->guisection = _("Formatting"); parms.nsteps = G_define_option(); parms.nsteps->key = "nsteps"; parms.nsteps->type = TYPE_INTEGER; parms.nsteps->required = NO; parms.nsteps->multiple = NO; parms.nsteps->answer = "255"; parms.nsteps->description = _("Number of floating-point subranges to collect stats from"); parms.nsteps->guisection = _("Floating point"); parms.sort = G_define_option(); parms.sort->key = "sort"; parms.sort->type = TYPE_STRING; parms.sort->required = NO; parms.sort->multiple = NO; parms.sort->label = _("Sort output statistics by cell counts"); parms.sort->description = _("Default: sorted by categories or intervals"); parms.sort->options = "asc,desc"; G_asprintf((char **)&(parms.sort->descriptions), "asc;%s;desc;%s", _("Sort by cell counts in ascending order"), _("Sort by cell counts in descending order")); parms.sort->guisection = _("Formatting"); flags.h = G_define_flag(); flags.h->key = 'h'; flags.h->description = _("Suppress page headers"); flags.h->guisection = _("Formatting"); flags.f = G_define_flag(); flags.f->key = 'f'; flags.f->description = _("Use formfeeds between pages"); flags.f->guisection = _("Formatting"); flags.e = G_define_flag(); flags.e->key = 'e'; flags.e->description = _("Scientific format"); flags.e->guisection = _("Formatting"); flags.n = G_define_flag(); flags.n->key = 'n'; flags.n->description = _("Do not report no data value"); flags.n->guisection = _("No data"); flags.N = G_define_flag(); flags.N->key = 'a'; flags.N->description = _("Do not report cells where all maps have no data"); flags.N->guisection = _("No data"); flags.C = G_define_flag(); flags.C->key = 'c'; flags.C->description = _("Report for cats floating-point ranges (floating-point maps only)"); flags.C->guisection = _("Floating point"); flags.i = G_define_flag(); flags.i->key = 'i'; flags.i->description = _("Read floating-point map as integer (use map's quant rules)"); flags.i->guisection = _("Floating point"); /* hidden feature. * if first arg is >file just run r.stats into this file and quit * if first arg is <file, run report from stats in file * (this feature is for the interactive version of this program - * to get more than one report without re-running r.stats) */ stats_flag = EVERYTHING; if (argc > 1) { if (argv[1][0] == '<' || argv[1][0] == '>') { stats_file = argv[1] + 1; if (argv[1][0] == '<') stats_flag = REPORT_ONLY; else { unlink(stats_file); stats_flag = STATS_ONLY; } argc--; argv++; } } if (G_parser(argc, argv)) exit(EXIT_FAILURE); use_formfeed = flags.f->answer; with_headers = !flags.h->answer; e_format = flags.e->answer; no_nulls = flags.n->answer; no_nulls_all = flags.N->answer; cat_ranges = flags.C->answer; as_int = flags.i->answer; for (i = 0; parms.cell->answers[i]; i++) parse_layer(parms.cell->answers[i]); if (parms.units->answers) for (i = 0; parms.units->answers[i]; i++) parse_units(parms.units->answers[i]); sscanf(parms.nsteps->answer, "%d", &nsteps); if (nsteps <= 0) { G_warning(_("nsteps has to be > 0; using nsteps=255")); nsteps = 255; } if (sscanf(parms.pl->answer, "%d", &page_length) != 1 || page_length < 0) { G_fatal_error(_("Illegal page length")); } if (sscanf(parms.pw->answer, "%d", &page_width) != 1 || page_width < 1) { G_fatal_error(_("Illegal page width")); } if (parms.outfile->answer) { if (freopen(parms.outfile->answer, "w", stdout) == NULL) { perror(parms.outfile->answer); exit(EXIT_FAILURE); } } no_data_str = parms.nv->answer; /* determine sorting method */ do_sort = SORT_DEFAULT; /* sort by cats by default */ if (parms.sort->answer) { switch(parms.sort->answer[0]) { case 'a': do_sort = SORT_ASC; break; case 'd': do_sort = SORT_DESC; break; default: G_debug(1, "Sorting by '%s' not supported", parms.sort->answer); break; } } return 0; }
// Main... int main(int argc, char *argv[]) { opts.verbose = FALSE; opts.daemon = FALSE; bzero(opts.output_file, FILENAME_MAX); strcpy(opts.config_file, "temper1.conf"); opts.units = 'C'; strcpy(opts.dt_format, "%d-%b-%Y %H:%M"); bzero(opts.only_device, 40); static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"daemon", required_argument, 0, 'D'}, {"config", required_argument, 0, 'C'}, {"output", required_argument, 0, 'o'}, {"units", required_argument, 0, 'u'}, {"device", required_argument, 0, 'd'}, {0, 0, 0, 0} }; int options_index = 0, c = 0, proceed = TRUE; while ((c = getopt_long(argc, argv, "hVvD:C:o:u:d:", long_options, &options_index)) != -1) { switch (c) { case 'C': strcpy(opts.config_file, optarg); break; } } load_configuration(); optind = 1; while ((c = getopt_long(argc, argv, "hVvD:C:o:u:d:", long_options, &options_index)) != -1) { switch (c) { case 'h': fprintf(stdout, "usage: temper1 [--version|-V] [--help|-h] [--daemon|-D [seconds]]\n"); fprintf(stdout, " [--verbose|-v] [--config|-C [file]] [--output|-o [file]]\n"); fprintf(stdout, " [--units|-u [C|F|K]] [--device|-d [bus_no-port_no]]\n"); proceed = FALSE; break; case 'V': proceed = FALSE; fprintf(stdout, "temper1 version %s\n", VERSION); break; case 'v': opts.verbose = TRUE; break; case 'D': opts.daemon = TRUE; break; case 'd': strcpy(opts.only_device, optarg); break; case 'o': strcpy(opts.output_file, optarg); break; case 'u': parse_units(optarg); break; case 'C': // Just ignore as we've done this already break; case '?': proceed = FALSE; break; default: break; } } if (proceed) { initialise_usb(opts.verbose); load_calibrations(); if (!opts.daemon) { // This is the one shot read iterate_usb(is_device_temper1, initialise_temper1, use_temper1, close_temper1); } else { // These separate iterations allow for the use_temper1 to do loops over all devices (daemon mode) // by simply using a different use_temper1 method pointer. int r = iterate_usb(is_device_temper1, initialise_temper1, NULL, NULL); if (r >= 0) r = iterate_usb(is_device_temper1, NULL, use_temper1, NULL); // TODO install signal handler to run this if (r >= 0) r = iterate_usb(is_device_temper1, NULL, NULL, close_temper1); } } return (!proceed); }