history.c

Go to the documentation of this file.
00001 /**********************************************************************
00002  *
00003  *  G_read_history (name, mapset, phist)
00004  *      char *name                   name of map
00005  *      char *mapset                 mapset that map belongs to
00006  *      struct History *phist        structure to hold history info
00007  *
00008  *  Reads the history information associated with map layer "map"
00009  *  in mapset "mapset" into the structure "phist".
00010  *
00011  *   returns:    0  if successful
00012  *              -1  on fail
00013  *
00014  *  note:   a warning message is printed if the file is incorrect
00015  *
00016  **********************************************************************
00017  *
00018  *  G_write_history (name, phist)
00019  *      char *name                   name of map
00020  *      struct History *phist        structure holding history info
00021  *
00022  *  Writes the history information associated with map layer "map"
00023  *  into current from the structure "phist".
00024  *
00025  *   returns:    0  if successful
00026  *              -1  on fail
00027  ***********************************************************************
00028  *
00029  *  G_short_history (name, type, hist)
00030  *     char *name             name of cell file
00031  *     char *type             type of cell file
00032  *     struct History *hist   History structure to be filled in
00033  *
00034  *  Puts local information like time and date, user's name, map name,
00035  *  and current mapset name into the hist structure
00036  *
00037  *  NOTE: use G_write_history() to write the structure.
00038  **********************************************************************
00039  *
00040  *  G_command_history (hist)
00041  *     struct History *hist   History structure to be filled in
00042  *
00043  *  Appends (parsed) command line to history structure's comments
00044  *
00045  * Returns:
00046  *     0 success
00047  *     1 failure (history file full, no change)
00048  *     2 failure (history file full, added as much as we could)
00049  *
00050  *  NOTE: initialize structure with G_short_history() first.
00051  *  NOTE: use G_write_history() to write the structure.
00052  **********************************************************************/
00053 
00054 #include <string.h>
00055 #include <grass/gis.h>
00056 #include <grass/glocale.h>
00057 
00058 
00074 int G_read_history (
00075     char *name,
00076     char *mapset,
00077     struct History *hist)
00078 {
00079     FILE *fd;
00080 
00081     G_zero (hist, sizeof (struct History));
00082     fd = G_fopen_old ("hist", name, mapset);
00083     if (!fd)
00084         goto error;
00085 
00086 
00087     if (!G_getl(hist->mapid, sizeof(hist->mapid), fd))
00088         goto error;
00089     G_ascii_check(hist->mapid) ;
00090 
00091     if (!G_getl(hist->title, sizeof(hist->title), fd))
00092         goto error;
00093     G_ascii_check(hist->title) ;
00094 
00095     if (!G_getl(hist->mapset, sizeof(hist->mapset), fd))
00096         goto error;
00097     G_ascii_check(hist->mapset) ;
00098 
00099     if (!G_getl(hist->creator, sizeof(hist->creator), fd))
00100         goto error;
00101     G_ascii_check(hist->creator) ;
00102 
00103     if (!G_getl(hist->maptype, sizeof(hist->maptype), fd))
00104         goto error;
00105     G_ascii_check(hist->maptype) ;
00106 
00107     if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd))
00108         goto error;
00109     G_ascii_check(hist->datsrc_1) ;
00110 
00111     if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd))
00112         goto error;
00113     G_ascii_check(hist->datsrc_2) ;
00114 
00115     if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd))
00116         goto error;
00117     G_ascii_check(hist->keywrd) ;
00118 
00119     hist->edlinecnt = 0;
00120     while ((hist->edlinecnt < MAXEDLINES) &&
00121         (G_getl( hist->edhist[hist->edlinecnt], sizeof (hist->edhist[0]), fd)))
00122     {
00123         G_ascii_check( hist->edhist[hist->edlinecnt]) ;
00124         hist->edlinecnt++;
00125     }
00126 
00127 
00128     fclose(fd) ;
00129     return 0;
00130 
00131 error:
00132     if (fd != NULL)
00133         fclose(fd) ;
00134     G_warning (_("can't get history information for [%s] in mapset [%s]"),
00135             name, mapset);
00136     return -1;
00137 }
00138 
00139 
00155 int G_write_history (
00156     char *name,
00157     struct History *hist)
00158 {
00159     FILE *fd;
00160     int i;
00161 
00162     fd = G_fopen_new ("hist", name);
00163     if (!fd)
00164             goto error;
00165 
00166     fprintf (fd, "%s\n", hist->mapid)    ; 
00167     fprintf (fd, "%s\n", hist->title)    ; 
00168     fprintf (fd, "%s\n", hist->mapset)  ; 
00169     fprintf (fd, "%s\n", hist->creator)  ; 
00170     fprintf (fd, "%s\n", hist->maptype)  ; 
00171     fprintf (fd, "%s\n", hist->datsrc_1) ; 
00172     fprintf (fd, "%s\n", hist->datsrc_2) ; 
00173     fprintf (fd, "%s\n", hist->keywrd)   ; 
00174 
00175     for(i=0; i < hist->edlinecnt; i++) 
00176             fprintf (fd, "%s\n", hist->edhist[i]) ;
00177 
00178     fclose (fd) ;
00179     return 0;
00180 
00181 error:
00182     if (fd)
00183             fclose(fd) ;
00184     G_warning (_("can't write history information for [%s]"), name);
00185     return -1;
00186 }
00187 
00188 
00189 
00206 int G_short_history (
00207     char *name,
00208     char *type,
00209     struct History *hist)
00210 {
00211     strncpy(hist->mapid, G_date(), RECORD_LEN);
00212     strncpy(hist->title, name, RECORD_LEN);
00213     strncpy(hist->mapset, G_mapset(), RECORD_LEN);
00214     strncpy(hist->creator, G_whoami(), RECORD_LEN);
00215     strncpy(hist->maptype, type, RECORD_LEN);
00216 
00217     sprintf(hist->keywrd, "generated by %s", G_program_name());
00218     strcpy(hist->datsrc_1, "");
00219     strcpy(hist->datsrc_2, "");
00220     hist->edlinecnt = 0;
00221 
00222     return 1;
00223 }
00224 
00261 int G_command_history(struct History *hist) {
00262     int j, cmdlen;
00263     char *cmdlin;
00264 
00265     cmdlin = G_recreate_command();
00266     cmdlen = strlen(cmdlin);
00267 
00268     if(hist->edlinecnt > MAXEDLINES -2) {
00269         G_warning(_("Not enough room in history file to record command line."));
00270         return 1;
00271     }
00272 
00273     if(hist->edlinecnt > 0) {    /* add a blank line if preceding history exists */
00274         strcpy(hist->edhist[hist->edlinecnt], "");
00275         hist->edlinecnt++;
00276     }
00277 
00278     if(cmdlen < 70) {    /* ie if it will fit on a single line */
00279         sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
00280         hist->edlinecnt++;
00281     }
00282     else {    /* multi-line required */
00283         j = 0;    /* j is the current position in the command line string */
00284         while((cmdlen - j) > 70) {
00285             strncpy(hist->edhist[hist->edlinecnt], &cmdlin[j], 68);
00286             hist->edhist[hist->edlinecnt][68] = '\0';
00287             strcat(hist->edhist[hist->edlinecnt], "\\");
00288             j+=68;
00289             hist->edlinecnt++;
00290             if(hist->edlinecnt > MAXEDLINES -2) {
00291                 G_warning(_("Not enough room in history file for command line (truncated)."));
00292                 return 2;
00293             }
00294         }
00295         if((cmdlen - j) > 0) {    /* ie anything left */
00296             strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);
00297             hist->edlinecnt++;
00298         }
00299     }
00300     return 0;
00301 }

Generated on Fri Nov 21 11:02:18 2008 for GRASS by  doxygen 1.5.1