timestamp.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * provides DateTime functions for timestamp management:
00004  *
00005  * Authors: Michael Shapiro & Bill Brown, CERL
00006  *          grid3 functions by Michael Pelizzari, LMCO
00007  *            
00008  * G_init_timestamp()
00009  * G_set_timestamp()
00010  * G_set_timestamp_range()
00011  * G_format_timestamp()
00012  * G_scan_timestamp()
00013  * G_get_timestamps()
00014  * G_read_raster_timestamp()
00015  * G_remove_raster_timestamp()
00016  * G_read_vector_timestamp()
00017  * G_remove_vector_timestamp()
00018  * G_read_grid3_timestamp()
00019  * G_remove_grid3_timestamp()
00020  * G_write_raster_timestamp()
00021  * G_write_vector_timestamp()
00022  * G_write_grid3_timestamp()
00023  *
00024  * COPYRIGHT:    (C) 2000 by the GRASS Development Team
00025  *
00026  *               This program is free software under the GNU General Public
00027  *               License (>=v2). Read the file COPYING that comes with GRASS
00028  *               for details.
00029  *
00030  */
00031 
00032 #include <string.h>
00033 #include "gis.h"
00034 #include "glocale.h"
00035 
00036 void G_init_timestamp (struct TimeStamp *ts)
00037 {
00038     ts->count = 0;
00039 }
00040 
00041 void G_set_timestamp (struct TimeStamp *ts, DateTime *dt)
00042 {
00043     datetime_copy (&ts->dt[0],dt);
00044     ts->count = 1;
00045 }
00046 
00047 void G_set_timestamp_range (
00048     struct TimeStamp *ts,
00049     DateTime *dt1,DateTime *dt2)
00050 {
00051     datetime_copy (&ts->dt[0], dt1);
00052     datetime_copy (&ts->dt[1], dt2);
00053     ts->count = 2;
00054 }
00055 
00056 int G__read_timestamp (FILE *fd, struct TimeStamp *ts)
00057 {
00058     char buf[1024];
00059     char comment[2];
00060 
00061     while (fgets(buf, sizeof(buf), fd))
00062     {
00063         if (sscanf (buf, "%1s", comment) != 1 || *comment == '#')
00064             continue;
00065         return (G_scan_timestamp (ts, buf) > 0 ? 0 : -1);
00066     }
00067     return -2; /* nothing in the file */
00068 }
00069 
00070 int G__write_timestamp ( FILE *fd, struct TimeStamp *ts)
00071 {
00072     char buf[1024];
00073 
00074     if (G_format_timestamp (ts, buf) < 0)
00075         return -1;
00076     fprintf (fd, "%s\n", buf);
00077     return 0;
00078 }
00079 
00080 
00105 int G_format_timestamp (
00106     struct TimeStamp *ts,
00107     char *buf)
00108 {
00109     char temp1[128], temp2[128];
00110     *buf = 0;
00111     if (ts->count > 0)
00112     {
00113         if (datetime_format (&ts->dt[0],temp1) != 0)
00114             return -1;
00115     }
00116     if (ts->count > 1)
00117     {
00118         if (datetime_format (&ts->dt[1],temp2) != 0)
00119             return -1;
00120     }
00121     if (ts->count == 1)
00122         strcpy (buf, temp1);
00123     else if (ts->count == 2)
00124         sprintf (buf, "%s / %s", temp1, temp2);
00125 
00126     return 1;
00127 }
00128 
00129 
00154 int G_scan_timestamp (
00155     struct TimeStamp *ts,
00156     char *buf)
00157 {
00158     char temp[1024], *t;
00159     char *slash;
00160     DateTime dt1, dt2;
00161 
00162     G_init_timestamp(ts);
00163     for (slash = buf; *slash; slash++)
00164         if (*slash == '/')
00165             break;
00166     if (*slash)
00167     {
00168         t = temp;
00169         while (buf != slash)
00170             *t++ = *buf++;
00171         *t = 0;
00172         buf++;
00173         if (datetime_scan(&dt1,temp) != 0 || datetime_scan(&dt2,buf) != 0)
00174                 return -1;
00175         G_set_timestamp_range (ts, &dt1, &dt2);
00176     }
00177     else
00178     {
00179         if(datetime_scan (&dt2, buf) != 0 )
00180             return -1;
00181         G_set_timestamp (ts, &dt2);
00182     }
00183     return 1;
00184 }
00185         
00186 
00221 int G_get_timestamps (
00222     struct TimeStamp *ts,
00223     DateTime *dt1,DateTime *dt2,
00224     int *count)
00225 {
00226     *count = 0;
00227     if (ts->count > 0)
00228     {
00229         datetime_copy (dt1, &ts->dt[0]);
00230         *count = 1;
00231     }
00232     if (ts->count > 1)
00233     {
00234         datetime_copy (dt2, &ts->dt[1]);
00235         *count = 2;
00236     }
00237 
00238     return 0;
00239 }
00240 
00241 /* write timestamp file
00242  * 1 ok
00243  * -1 error - can't create timestamp file
00244  * -2 error - invalid datetime in ts
00245  */
00246 static int write_timestamp (
00247     char *maptype,char *mapname,char *element,char *filename,
00248     struct TimeStamp *ts)
00249 {
00250     FILE *fd;
00251     int stat;
00252 
00253     fd = G_fopen_new (element, filename);
00254     if (fd == NULL)
00255     {
00256         G_warning (
00257                 _("Can't create timestamp file for %s map %s in mapset %s"),
00258                 maptype, mapname, G_mapset());
00259         return -1;
00260     }
00261 
00262     stat = G__write_timestamp (fd, ts);
00263     fclose (fd);
00264     if (stat == 0)
00265         return 1;
00266     G_warning (
00267             _("Invalid timestamp specified for %s map %s in mapset %s"),
00268             maptype, mapname, G_mapset());
00269     return -2;
00270 }
00271 
00272 /* read timestamp file
00273  * 0 no timestamp file
00274  * 1 ok
00275  * -1 error - can't open timestamp file
00276  * -2 error - invalid datetime values in timestamp file
00277  */
00278 static int read_timestamp (
00279     char *maptype,char *mapname,char *mapset,
00280     char *element,char *filename,
00281     struct TimeStamp *ts)
00282 {
00283     FILE *fd;
00284     int stat;
00285 
00286     if (!G_find_file2 (element, filename, mapset))
00287         return 0;
00288     fd = G_fopen_old (element, filename, mapset);
00289     if (fd == NULL)
00290     {
00291         G_warning (
00292                 _("Can't open timestamp file for %s map %s in mapset %s"),
00293                 maptype, mapname, mapset);
00294         return -1;
00295     }
00296 
00297     stat = G__read_timestamp (fd, ts);
00298     fclose (fd);
00299     if (stat == 0)
00300         return 1;
00301     G_warning (
00302             _("Invalid timestamp file for %s map %s in mapset %s"),
00303             maptype, mapname, mapset);
00304     return -2;
00305 }
00306 
00307 #define RAST_MISC "cell_misc"
00308 #define VECT_MISC "dig_misc"
00309 #define GRID3     "grid3"
00310 
00311 
00335 int G_read_raster_timestamp (
00336     char *name,char *mapset,
00337     struct TimeStamp *ts)
00338 {
00339     char element[128];
00340 
00341     sprintf (element, "%s/%s", RAST_MISC, name);
00342     return read_timestamp ("raster", name, mapset, element, "timestamp", ts);
00343 }
00344 
00345 
00372 int G_remove_raster_timestamp (char *name)
00373 {
00374     char element[128];
00375 
00376     sprintf (element, "%s/%s", RAST_MISC, name);
00377     return G_remove(element, "timestamp");
00378 }
00379 
00380 
00404 int G_read_vector_timestamp (
00405     char *name,char *mapset,
00406     struct TimeStamp *ts)
00407 {
00408     char element[128];
00409 
00410     sprintf (element, "%s/%s", VECT_MISC, name);
00411     return read_timestamp ("vector", name, mapset, element, "timestamp", ts);
00412 }
00413 
00414 
00441 int G_remove_vector_timestamp (char *name)
00442 {
00443     char element[128];
00444 
00445     sprintf (element, "%s/%s", VECT_MISC, name);
00446     return G_remove(element, "timestamp");
00447 }
00448 
00449 
00462 int G_read_grid3_timestamp (
00463     char *name,char *mapset,
00464     struct TimeStamp *ts)
00465 {
00466     char element[128];
00467 
00468     sprintf (element, "%s/%s", GRID3, name);
00469     return read_timestamp ("grid3", name, mapset, element, "timestamp", ts);
00470 }
00471 
00472 
00486 int G_remove_grid3_timestamp (char *name)
00487 {
00488     char element[128];
00489 
00490     sprintf (element, "%s/%s", GRID3, name);
00491     return G_remove(element, "timestamp");
00492 }
00493 
00494 
00521 int G_write_raster_timestamp (
00522     char *name,
00523     struct TimeStamp *ts)
00524 {
00525     char element[128];
00526 
00527     sprintf (element, "%s/%s", RAST_MISC, name);
00528     return write_timestamp ("raster", name, element, "timestamp", ts);
00529 }
00530 
00531 
00558 int G_write_vector_timestamp (
00559     char *name,
00560     struct TimeStamp *ts)
00561 {
00562     char element[128];
00563 
00564     sprintf (element, "%s/%s", VECT_MISC, name);
00565     return write_timestamp ("vector", name, element, "timestamp", ts);
00566 }
00567 
00568 
00582 int G_write_grid3_timestamp (
00583     char *name,
00584     struct TimeStamp *ts)
00585 {
00586     char element[128];
00587 
00588     sprintf (element, "%s/%s", GRID3, name);
00589     return write_timestamp ("grid3", name, element, "timestamp", ts);
00590 }

Generated on Wed Aug 23 17:49:23 2006 for GRASS by  doxygen 1.4.7