2
/**********************************************************************
4
* G_read_history (name, mapset, phist)
5
* char *name name of map
6
* char *mapset mapset that map belongs to
7
* struct History *phist structure to hold history info
9
* Reads the history information associated with map layer "map"
10
* in mapset "mapset" into the structure "phist".
12
* returns: 0 if successful
15
* note: a warning message is printed if the file is incorrect
17
**********************************************************************
19
* G_write_history (name, phist)
20
* char *name name of map
21
* struct History *phist structure holding history info
23
* Writes the history information associated with map layer "map"
24
* into current from the structure "phist".
26
* returns: 0 if successful
28
***********************************************************************
30
* G_short_history (name, type, hist)
31
* char *name name of cell file
32
* char *type type of cell file
33
* struct History *hist History structure to be filled in
35
* Puts local information like time and date, user's name, map name,
36
* and current mapset name into the hist structure
38
* NOTE: use G_write_history() to write the structure.
39
**********************************************************************
41
* G_command_history (hist)
42
* struct History *hist History structure to be filled in
44
* Appends (parsed) command line to history structure's comments
48
* 1 failure (history file full, no change)
49
* 2 failure (history file full, added as much as we could)
51
* NOTE: initialize structure with G_short_history() first.
52
* NOTE: use G_write_history() to write the structure.
53
**********************************************************************/
56
#include <grass/gis.h>
57
#include <grass/glocale.h>
61
* \brief read raster history file
63
* This routine reads the history file for
64
* the raster map <b>name</b> in <b>mapset</b> into the <b>history</b>
66
* A diagnostic message is printed and -1 is returned if there is an error
67
* reading the history file. Otherwise, 0 is returned.
75
int G_read_history(const char *name, const char *mapset, struct History *hist)
79
G_zero(hist, sizeof(struct History));
80
fd = G_fopen_old("hist", name, mapset);
85
if (!G_getl(hist->mapid, sizeof(hist->mapid), fd))
87
G_ascii_check(hist->mapid);
89
if (!G_getl(hist->title, sizeof(hist->title), fd))
91
G_ascii_check(hist->title);
93
if (!G_getl(hist->mapset, sizeof(hist->mapset), fd))
95
G_ascii_check(hist->mapset);
97
if (!G_getl(hist->creator, sizeof(hist->creator), fd))
99
G_ascii_check(hist->creator);
101
if (!G_getl(hist->maptype, sizeof(hist->maptype), fd))
103
G_ascii_check(hist->maptype);
105
if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd))
107
G_ascii_check(hist->datsrc_1);
109
if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd))
111
G_ascii_check(hist->datsrc_2);
113
if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd))
115
G_ascii_check(hist->keywrd);
118
while ((hist->edlinecnt < MAXEDLINES) &&
120
(hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
121
G_ascii_check(hist->edhist[hist->edlinecnt]);
132
G_warning(_("can't get history information for [%s] in mapset [%s]"),
139
* \brief write raster history file
141
* This routine writes the history file for the raster map
142
* <b>name</b> in the current mapset from the <b>history</b> structure.
143
* A diagnostic message is printed and -1 is returned if there is an error
144
* writing the history file. Otherwise, 0 is returned.
145
* <b>Note.</b> The <b>history</b> structure should first be initialized
146
* using <i>G_short_history.</i>
153
int G_write_history(const char *name, struct History *hist)
158
fd = G_fopen_new("hist", name);
162
fprintf(fd, "%s\n", hist->mapid);
163
fprintf(fd, "%s\n", hist->title);
164
fprintf(fd, "%s\n", hist->mapset);
165
fprintf(fd, "%s\n", hist->creator);
166
fprintf(fd, "%s\n", hist->maptype);
167
fprintf(fd, "%s\n", hist->datsrc_1);
168
fprintf(fd, "%s\n", hist->datsrc_2);
169
fprintf(fd, "%s\n", hist->keywrd);
171
for (i = 0; i < hist->edlinecnt; i++)
172
fprintf(fd, "%s\n", hist->edhist[i]);
180
G_warning(_("can't write history information for [%s]"), name);
187
* \brief initialize history structure
189
* This routine initializes the
190
* <b>history</b> structure, recording the date, user, module name and the
191
* raster map <b>name</b> structure. The <b>type</b> is an anachronism from
192
* earlier versions of GRASS and should be specified as "raster".
193
* <b>Note.</b> This routine only initializes the data structure. It does not
194
* write the history file.
202
int G_short_history(const char *name, const char *type, struct History *hist)
204
strncpy(hist->mapid, G_date(), RECORD_LEN);
205
strncpy(hist->title, name, RECORD_LEN);
206
strncpy(hist->mapset, G_mapset(), RECORD_LEN);
207
strncpy(hist->creator, G_whoami(), RECORD_LEN);
208
strncpy(hist->maptype, type, RECORD_LEN);
210
sprintf(hist->keywrd, "generated by %s", G_program_name());
211
strcpy(hist->datsrc_1, "");
212
strcpy(hist->datsrc_2, "");
219
* \brief Save command line to raster history structure
221
* This routine takes an existing (run <i>G_short_history first</i>) history
222
* structure and adds the command line to the end of the comments array, as
223
* cleaned & expanded by the parser.
225
* History file is limited to [80]x[50], as defined in include/gis.h
227
* * First version had for loops of [i][j] character assignments and ending
228
* nulls, but using the string libraries is cleaner and less bug prone.
229
* * Second version had white space detection, intelligent wrapping, and
230
* indentation of continued lines, but this proved a pain in the neck for
231
* things like r.patch which can have long strings without any
232
* parser-acceptable breaks.
233
* * This is MK-III, simplified, but that's good: it's cut & paste-able.
235
* NOTE: use G_write_history() to write the structure.
239
* struct History history;
240
* G_short_history(rasterfile, "raster", &history);
241
* G_command_history(&history);
242
* G_write_history(rasterfile, &history);
246
* 1 failure (history file full, no change)
247
* 2 failure (history file full, added as much as we could)
254
int G_command_history(struct History *hist)
259
cmdlin = G_recreate_command();
260
cmdlen = strlen(cmdlin);
262
if (hist->edlinecnt > MAXEDLINES - 2) {
263
G_warning(_("Not enough room in history file to record command line."));
267
if (hist->edlinecnt > 0) { /* add a blank line if preceding history exists */
268
strcpy(hist->edhist[hist->edlinecnt], "");
272
if (cmdlen < 70) { /* ie if it will fit on a single line */
273
sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
276
else { /* multi-line required */
277
j = 0; /* j is the current position in the command line string */
278
while ((cmdlen - j) > 70) {
279
strncpy(hist->edhist[hist->edlinecnt], &cmdlin[j], 68);
280
hist->edhist[hist->edlinecnt][68] = '\0';
281
strcat(hist->edhist[hist->edlinecnt], "\\");
284
if (hist->edlinecnt > MAXEDLINES - 2) {
285
G_warning(_("Not enough room in history file for command line (truncated)."));
289
if ((cmdlen - j) > 0) { /* ie anything left */
290
strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);