GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
timestamp.c
Go to the documentation of this file.
1 
79 #include <string.h>
80 #include <unistd.h>
81 #include <grass/gis.h>
82 #include <grass/vect/dig_defines.h>
83 #include <grass/glocale.h>
84 
85 #define RAST_MISC "cell_misc"
86 #define GRID3 "grid3"
87 
93 void G_init_timestamp(struct TimeStamp *ts)
94 {
95  ts->count = 0;
96 }
97 
104 void G_set_timestamp(struct TimeStamp *ts, const DateTime * dt)
105 {
106  datetime_copy(&ts->dt[0], dt);
107  ts->count = 1;
108 }
109 
116 void G_set_timestamp_range(struct TimeStamp *ts,
117  const DateTime * dt1, const DateTime * dt2)
118 {
119  datetime_copy(&ts->dt[0], dt1);
120  datetime_copy(&ts->dt[1], dt2);
121  ts->count = 2;
122 }
123 
134 int G__read_timestamp(FILE * fd, struct TimeStamp *ts)
135 {
136  char buf[1024];
137  char comment[2];
138 
139  while (fgets(buf, sizeof(buf), fd)) {
140  if (sscanf(buf, "%1s", comment) != 1 || *comment == '#')
141  continue;
142  return (G_scan_timestamp(ts, buf) > 0 ? 0 : -1);
143  }
144  return -2; /* nothing in the file */
145 }
146 
158 int G_write_timestamp(FILE * fd, const struct TimeStamp *ts)
159 {
160  char buf[1024];
161 
162  if (G_format_timestamp(ts, buf) < 0)
163  return -1;
164  fprintf(fd, "%s\n", buf);
165  return 0;
166 }
167 
181 int G_format_timestamp(const struct TimeStamp *ts, char *buf)
182 {
183  char temp1[128], temp2[128];
184 
185  *buf = 0;
186  if (ts->count > 0) {
187  if (datetime_format(&ts->dt[0], temp1) != 0)
188  return -1;
189  }
190  if (ts->count > 1) {
191  if (datetime_format(&ts->dt[1], temp2) != 0)
192  return -1;
193  }
194  if (ts->count == 1)
195  strcpy(buf, temp1);
196  else if (ts->count == 2)
197  sprintf(buf, "%s / %s", temp1, temp2);
198 
199  return 1;
200 }
201 
215 int G_scan_timestamp(struct TimeStamp *ts, const char *buf)
216 {
217  char temp[1024], *t;
218  const char *slash;
219  DateTime dt1, dt2;
220 
221  G_init_timestamp(ts);
222  for (slash = buf; *slash; slash++)
223  if (*slash == '/')
224  break;
225  if (*slash) {
226  t = temp;
227  while (buf != slash)
228  *t++ = *buf++;
229  *t = 0;
230  buf++;
231  if (datetime_scan(&dt1, temp) != 0 || datetime_scan(&dt2, buf) != 0)
232  return -1;
233  G_set_timestamp_range(ts, &dt1, &dt2);
234  }
235  else {
236  if (datetime_scan(&dt2, buf) != 0)
237  return -1;
238  G_set_timestamp(ts, &dt2);
239  }
240  return 1;
241 }
242 
258 void G_get_timestamps(const struct TimeStamp *ts,
259  DateTime * dt1, DateTime * dt2, int *count)
260 {
261  *count = 0;
262  if (ts->count > 0) {
263  datetime_copy(dt1, &ts->dt[0]);
264  *count = 1;
265  }
266  if (ts->count > 1) {
267  datetime_copy(dt2, &ts->dt[1]);
268  *count = 2;
269  }
270 }
271 
284 static int write_timestamp(const char *maptype, const char *dir,
285  const char *name, const struct TimeStamp *ts)
286 {
287  FILE *fd;
288  int stat;
289 
290  fd = G_fopen_new_misc(dir, "timestamp", name);
291  if (fd == NULL) {
292  G_warning(_("Unable to create timestamp file for %s map <%s@%s>"),
293  maptype, name, G_mapset());
294  return -1;
295  }
296 
297  stat = G_write_timestamp(fd, ts);
298  fclose(fd);
299  if (stat == 0)
300  return 1;
301  G_warning(_("Invalid timestamp specified for %s map <%s@%s>"),
302  maptype, name, G_mapset());
303  return -2;
304 }
305 
320 static int read_timestamp(const char *maptype, const char *dir,
321  const char *name, const char *mapset,
322  struct TimeStamp *ts)
323 {
324  FILE *fd;
325  int stat;
326 
327  if (!G_find_file2_misc(dir, "timestamp", name, mapset))
328  return 0;
329  fd = G_fopen_old_misc(dir, "timestamp", name, mapset);
330  if (fd == NULL) {
331  G_warning(_("Unable to open timestamp file for %s map <%s@%s>"),
332  maptype, name, mapset);
333  return -1;
334  }
335 
336  stat = G__read_timestamp(fd, ts);
337  fclose(fd);
338  if (stat == 0)
339  return 1;
340  G_warning(_("Invalid timestamp file for %s map <%s@%s>"),
341  maptype, name, mapset);
342  return -2;
343 }
344 
354 int G_has_raster_timestamp(const char *name, const char *mapset)
355 {
356  if (!G_find_file2_misc(RAST_MISC, "timestamp", name, mapset))
357  return 0;
358 
359  return 1;
360 }
361 
372 int G_read_raster_timestamp(const char *name, const char *mapset,
373  struct TimeStamp *ts)
374 {
375  return read_timestamp("raster", RAST_MISC, name, mapset, ts);
376 }
377 
389 int G_write_raster_timestamp(const char *name, const struct TimeStamp *ts)
390 {
391  return write_timestamp("raster", RAST_MISC, name, ts);
392 }
393 
406 {
407  return G_remove_misc(RAST_MISC, "timestamp", name);
408 }
409 
420 int G_has_vector_timestamp(const char *name, const char *layer, const char *mapset)
421 {
422  char dir[GPATH_MAX];
423  char path[GPATH_MAX + GNAME_MAX];
424  char ele[GNAME_MAX];
425 
426  if(layer != NULL)
427  G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
428  else
429  G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
430 
431  G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
432  G_file_name(path, dir, ele, mapset);
433 
434  G_debug(1, "Check for timestamp <%s>", path);
435 
436  if (access(path, R_OK) != 0)
437  return 0;
438 
439  return 1;
440 }
441 
455 int G_read_vector_timestamp(const char *name, const char *layer,
456  const char *mapset, struct TimeStamp *ts)
457 {
458  FILE *fd;
459  int stat;
460  char dir[GPATH_MAX];
461  char ele[GNAME_MAX];
462 
463  /* In case no timestamp file is present return 0 */
464  if (G_has_vector_timestamp(name, layer, mapset) != 1)
465  return 0;
466 
467  if(layer != NULL)
468  G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
469  else
470  G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
471 
472  G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
473 
474  G_debug(1, "Read timestamp <%s/%s>", dir, ele);
475 
476  fd = G_fopen_old(dir, ele, mapset);
477 
478  if (fd == NULL) {
479  G_warning(_("Unable to open timestamp file for vector map <%s@%s>"),
480  name, G_mapset());
481  return -1;
482  }
483 
484  stat = G__read_timestamp(fd, ts);
485  fclose(fd);
486  if (stat == 0)
487  return 1;
488  G_warning(_("Invalid timestamp file for vector map <%s@%s>"),
489  name, mapset);
490  return -2;
491 }
492 
505 int G_write_vector_timestamp(const char *name, const char *layer, const struct TimeStamp *ts)
506 {
507  FILE *fd;
508  int stat;
509  char dir[GPATH_MAX];
510  char ele[GNAME_MAX];
511 
512  if(layer != NULL)
513  G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
514  else
515  G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
516 
517  G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
518 
519  G_debug(1, "Write timestamp <%s/%s>", dir, ele);
520 
521  fd = G_fopen_new(dir, ele);
522 
523  if (fd == NULL) {
524  G_warning(_("Unable to create timestamp file for vector map <%s@%s>"),
525  name, G_mapset());
526  return -1;
527  }
528 
529  stat = G_write_timestamp(fd, ts);
530  fclose(fd);
531  if (stat == 0)
532  return 1;
533  G_warning(_("Invalid timestamp specified for vector map <%s@%s>"),
534  name, G_mapset());
535  return -2;
536 }
537 
550 int G_remove_vector_timestamp(const char *name, const char *layer)
551 {
552  char dir[GPATH_MAX];
553  char ele[GNAME_MAX];
554 
555  if(layer)
556  G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
557  else
558  G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
559 
560  G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
561  return G_remove(dir, ele);
562 }
563 
573 int G_has_raster3d_timestamp(const char *name, const char *mapset)
574 {
575  if (!G_find_file2_misc(GRID3, "timestamp", name, mapset))
576  return 0;
577 
578  return 1;
579 }
580 
591 int G_read_raster3d_timestamp(const char *name, const char *mapset,
592  struct TimeStamp *ts)
593 {
594  return read_timestamp("raster3d", GRID3, name, mapset, ts);
595 }
596 
608 int G_write_raster3d_timestamp(const char *name, const struct TimeStamp *ts)
609 {
610  return write_timestamp("raster3d", GRID3, name, ts);
611 }
612 
625 {
626  return G_remove_misc(GRID3, "timestamp", name);
627 }
628 
int G_read_raster_timestamp(const char *name, const char *mapset, struct TimeStamp *ts)
Read timestamp from raster map.
Definition: timestamp.c:372
int G_write_timestamp(FILE *fd, const struct TimeStamp *ts)
Output TimeStamp structure to a file as a formatted string.
Definition: timestamp.c:158
const char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) ...
Definition: find_file.c:267
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
void G_set_timestamp(struct TimeStamp *ts, const DateTime *dt)
Set timestamp (single)
Definition: timestamp.c:104
int datetime_format(const DateTime *dt, char *buf)
formats DateTime structure as a human-readable string returns 0 when successful and &#39;buf&#39; is filled w...
int G_scan_timestamp(struct TimeStamp *ts, const char *buf)
Fill a TimeStamp structure from a datetime string.
Definition: timestamp.c:215
int count
int G_write_raster3d_timestamp(const char *name, const struct TimeStamp *ts)
Write timestamp of 3D raster map.
Definition: timestamp.c:608
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
Definition: open_misc.c:210
#define GRID3
Definition: timestamp.c:86
#define NULL
Definition: ccmath.h:32
int G_has_raster_timestamp(const char *name, const char *mapset)
Check if timestamp for raster map exists.
Definition: timestamp.c:354
void datetime_copy(DateTime *dst, const DateTime *src)
Copies the DateTime [into/from ???] src.
Definition: copy.c:20
int G_write_raster_timestamp(const char *name, const struct TimeStamp *ts)
Write timestamp of raster map.
Definition: timestamp.c:389
int G_snprintf(char *str, size_t size, const char *fmt,...)
snprintf() clone.
Definition: snprintf.c:43
int G__read_timestamp(FILE *fd, struct TimeStamp *ts)
Read timestamp.
Definition: timestamp.c:134
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
int G_remove_vector_timestamp(const char *name, const char *layer)
Remove timestamp from vector map.
Definition: timestamp.c:550
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
double t
void G_get_timestamps(const struct TimeStamp *ts, DateTime *dt1, DateTime *dt2, int *count)
Copy TimeStamp into [two] Datetimes structs.
Definition: timestamp.c:258
#define RAST_MISC
Definition: timestamp.c:85
int G_remove(const char *element, const char *name)
Remove a database file.
Definition: remove.c:45
int G_has_vector_timestamp(const char *name, const char *layer, const char *mapset)
Check if timestamp for vector map exists.
Definition: timestamp.c:420
int G_remove_raster3d_timestamp(const char *name)
Remove timestamp from 3D raster map.
Definition: timestamp.c:624
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:207
void G_set_timestamp_range(struct TimeStamp *ts, const DateTime *dt1, const DateTime *dt2)
Set timestamp (range)
Definition: timestamp.c:116
Definition: path.h:16
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database file
Definition: open_misc.c:182
int G_write_vector_timestamp(const char *name, const char *layer, const struct TimeStamp *ts)
Write timestamp of vector map.
Definition: timestamp.c:505
int G_read_vector_timestamp(const char *name, const char *layer, const char *mapset, struct TimeStamp *ts)
Read timestamp from vector map.
Definition: timestamp.c:455
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition: remove.c:66
int G_remove_raster_timestamp(const char *name)
Remove timestamp from raster map.
Definition: timestamp.c:405
int G_has_raster3d_timestamp(const char *name, const char *mapset)
Check if timestamp for 3D raster map exists.
Definition: timestamp.c:573
int datetime_scan(DateTime *dt, const char *buf)
Convert the ascii string into a DateTime. This determines the mode/from/to based on the string...
Definition: scan.c:43
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:240
const char * name
Definition: named_colr.c:7
int G_format_timestamp(const struct TimeStamp *ts, char *buf)
Create text string from TimeStamp structure.
Definition: timestamp.c:181
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
int G_read_raster3d_timestamp(const char *name, const char *mapset, struct TimeStamp *ts)
Read timestamp from 3D raster map.
Definition: timestamp.c:591
void G_init_timestamp(struct TimeStamp *ts)
Initialize timestamp structure.
Definition: timestamp.c:93