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