int main(int argc, char *argv[]) { struct GModule *module; struct Option *map, *date; struct TimeStamp ts; char *name; int modify; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("metadata")); G_add_keyword(_("timestamp")); module->label = _("Modifies a timestamp for a raster map."); module->description = _("Print/add/remove a timestamp for a raster map."); map = G_define_standard_option(G_OPT_R_MAP); date = G_define_option(); date->key = "date"; date->key_desc = "timestamp"; date->required = NO; date->type = TYPE_STRING; date->label = _("Datetime, datetime1/datetime2, or 'none' to remove"); date->description = _("Format: '15 jan 1994' (absolute) or '2 years' (relative)"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); name = map->answer; modify = date->answer != NULL; if (!modify) { if (G_read_raster_timestamp(name, "", &ts) == 1) { G__write_timestamp(stdout, &ts); exit(EXIT_SUCCESS); } else exit(EXIT_FAILURE); } if (strcmp(date->answer, "none") == 0) { G_remove_raster_timestamp(name); exit(EXIT_SUCCESS); } if (1 == G_scan_timestamp(&ts, date->answer)) { G_write_raster_timestamp(name, &ts); exit(EXIT_SUCCESS); } else G_fatal_error(_("Invalid timestamp")); exit(EXIT_SUCCESS); }
/*- * Writes site_head struct. */ int G_site_put_head(struct Map_info *Map, Site_head * head) { static char buf[128]; if (head->name != NULL) Vect_set_map_name(Map, head->name); /* crashes: if (head->desc!=NULL) Vect_set_comment (Map, head->desc); */ /* if (head->form!=NULL) fprintf(ptr,"form|%s\n",head->form); if (head->labels!=NULL) fprintf(ptr,"labels|%s\n",head->labels); */ /* time could be in (char *) stime, (struct TimeStamp *) time, both, or neither */ if (head->stime != NULL || head->time != NULL) { if (head->time != NULL) { /* TimeStamp struct has precendence */ G_format_timestamp(head->time, buf); Vect_set_date(Map, buf); } else if (head->stime != NULL) { /* next check string */ if (head->time == NULL) { if ((head->time = (struct TimeStamp *)G_malloc(sizeof(struct TimeStamp))) == NULL) G_fatal_error(_("Memory error in writing timestamp")); else if (G_scan_timestamp(head->time, head->stime) < 0) { G_warning(_("Illegal TimeStamp string")); return -1; /* added to prevent crash 5/2000 MN */ } } G_format_timestamp(head->time, buf); head->stime = G_store(buf); Vect_set_date(Map, head->stime); } } return 0; }
/*- * Fills in site_head struct. */ int G_site_get_head(struct Map_info *Map, Site_head * head) { head->name = Vect_get_name(Map); head->desc = Vect_get_comment(Map); head->form = NULL; head->labels = NULL; /* head->stime = Vect_get_date(Map); *//* crashes, G_scan_timestamp() needed? */ head->stime = NULL; head->time = NULL; if (head->stime && strlen(head->stime) > 0) { if ((head->time = (struct TimeStamp *)G_malloc(sizeof(struct TimeStamp))) == NULL) G_fatal_error(_("Memory error in allocating timestamp")); if (G_scan_timestamp(head->time, head->stime) < 0) { G_warning(datetime_error_msg()); head->time = NULL; head->stime = NULL; } } return 0; }
int main(int argc, char *argv[]) { char *me; char *output, *input; char *fs; int dims, i, has_cat; struct GModule *module; FILE *in_fd, *out_fd; Site *site; Site_head shead; struct TimeStamp ts; struct { struct Option *input, *output, *dims, *fs, *date; } parm; G_gisinit(me = argv[0]); module = G_define_module(); G_add_keyword(_("sites")); module->description = "Convert an ASCII listing of site locations " "into a GRASS site list file."; parm.output = G_define_option(); parm.output->key = "output"; parm.output->type = TYPE_STRING; parm.output->required = YES; parm.output->description = "vector map to be created"; parm.output->gisprompt = "any,vector,vector"; parm.input = G_define_option(); parm.input->key = "input"; parm.input->type = TYPE_STRING; parm.input->required = NO; parm.input->description = "unix file containing sites"; parm.dims = G_define_option(); parm.dims->key = "d"; parm.dims->type = TYPE_INTEGER; parm.dims->required = NO; parm.dims->description = "number of dimensions (default=2)"; parm.fs = G_define_option(); parm.fs->key = "fs"; parm.fs->key_desc = "character|space|tab"; parm.fs->type = TYPE_STRING; parm.fs->required = NO; parm.fs->description = "input field separator"; parm.fs->answer = "space"; parm.date = G_define_option(); parm.date->key = "date"; parm.date->key_desc = "timestamp"; parm.date->required = NO; parm.date->type = TYPE_STRING; parm.date->description = "datetime or datetime1/datetime2"; if (G_parser(argc, argv)) exit(EXIT_FAILURE); if ((input = parm.input->answer)) { in_fd = fopen(input, "r"); if (NULL == in_fd) { fprintf(stderr, "%s - ", me); perror(input); exit(1); } } else in_fd = stdin; output = parm.output->answer; shead.name = G_store(parm.output->answer); shead.desc = G_store(G_recreate_command()); shead.form = shead.labels = shead.stime = (char *)NULL; /* add here time parameter */ if (parm.date->answer) { if (1 == G_scan_timestamp(&ts, parm.date->answer)) shead.time = &ts; else G_fatal_error("Invalid timestamp"); } else shead.time = (struct TimeStamp *)NULL; dims = 2; loop = 1; /* added 11/99 MNeteler */ if (parm.dims->answer != NULL) if ((i = sscanf(parm.dims->answer, "%d", &dims)) != 1) G_fatal_error("error scanning number of dimensions"); if (dims < 2) G_fatal_error("number of dimensions must be greater than 1"); if (strlen(parm.fs->answer) < 1) G_fatal_error("field separator cannot be empty"); else { fs = parm.fs->answer; if (strcmp(fs, "space") == 0) fs = NULL; else if (strcmp(fs, "tab") == 0) fs = NULL; } out_fd = G_fopen_sites_new(output); if (out_fd == NULL) G_fatal_error("can't create sites file [%s].", output); G_site_put_head(out_fd, &shead); while ((site = get_site(in_fd, dims, fs, &has_cat))) G_site_put(out_fd, site); G_sites_close(out_fd); exit(0); }