GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
history.c
Go to the documentation of this file.
1 
2 /**********************************************************************
3  *
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
8  *
9  * Reads the history information associated with map layer "map"
10  * in mapset "mapset" into the structure "phist".
11  *
12  * returns: 0 if successful
13  * -1 on fail
14  *
15  * note: a warning message is printed if the file is incorrect
16  *
17  **********************************************************************
18  *
19  * G_write_history (name, phist)
20  * char *name name of map
21  * struct History *phist structure holding history info
22  *
23  * Writes the history information associated with map layer "map"
24  * into current from the structure "phist".
25  *
26  * returns: 0 if successful
27  * -1 on fail
28  ***********************************************************************
29  *
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
34  *
35  * Puts local information like time and date, user's name, map name,
36  * and current mapset name into the hist structure
37  *
38  * NOTE: use G_write_history() to write the structure.
39  **********************************************************************
40  *
41  * G_command_history (hist)
42  * struct History *hist History structure to be filled in
43  *
44  * Appends (parsed) command line to history structure's comments
45  *
46  * Returns:
47  * 0 success
48  * 1 failure (history file full, no change)
49  * 2 failure (history file full, added as much as we could)
50  *
51  * NOTE: initialize structure with G_short_history() first.
52  * NOTE: use G_write_history() to write the structure.
53  **********************************************************************/
54 
55 #include <string.h>
56 #include <grass/gis.h>
57 #include <grass/glocale.h>
58 
59 
75 int G_read_history(const char *name, const char *mapset, struct History *hist)
76 {
77  FILE *fd;
78 
79  G_zero(hist, sizeof(struct History));
80  fd = G_fopen_old("hist", name, mapset);
81  if (!fd)
82  goto error;
83 
84 
85  if (!G_getl(hist->mapid, sizeof(hist->mapid), fd))
86  goto error;
87  G_ascii_check(hist->mapid);
88 
89  if (!G_getl(hist->title, sizeof(hist->title), fd))
90  goto error;
91  G_ascii_check(hist->title);
92 
93  if (!G_getl(hist->mapset, sizeof(hist->mapset), fd))
94  goto error;
95  G_ascii_check(hist->mapset);
96 
97  if (!G_getl(hist->creator, sizeof(hist->creator), fd))
98  goto error;
99  G_ascii_check(hist->creator);
100 
101  if (!G_getl(hist->maptype, sizeof(hist->maptype), fd))
102  goto error;
103  G_ascii_check(hist->maptype);
104 
105  if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd))
106  goto error;
107  G_ascii_check(hist->datsrc_1);
108 
109  if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd))
110  goto error;
111  G_ascii_check(hist->datsrc_2);
112 
113  if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd))
114  goto error;
115  G_ascii_check(hist->keywrd);
116 
117  hist->edlinecnt = 0;
118  while ((hist->edlinecnt < MAXEDLINES) &&
119  (G_getl
120  (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
121  G_ascii_check(hist->edhist[hist->edlinecnt]);
122  hist->edlinecnt++;
123  }
124 
125 
126  fclose(fd);
127  return 0;
128 
129  error:
130  if (fd != NULL)
131  fclose(fd);
132  G_warning(_("can't get history information for [%s] in mapset [%s]"),
133  name, mapset);
134  return -1;
135 }
136 
137 
153 int G_write_history(const char *name, struct History *hist)
154 {
155  FILE *fd;
156  int i;
157 
158  fd = G_fopen_new("hist", name);
159  if (!fd)
160  goto error;
161 
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);
170 
171  for (i = 0; i < hist->edlinecnt; i++)
172  fprintf(fd, "%s\n", hist->edhist[i]);
173 
174  fclose(fd);
175  return 0;
176 
177  error:
178  if (fd)
179  fclose(fd);
180  G_warning(_("can't write history information for [%s]"), name);
181  return -1;
182 }
183 
184 
185 
202 int G_short_history(const char *name, const char *type, struct History *hist)
203 {
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);
209 
210  sprintf(hist->keywrd, "generated by %s", G_program_name());
211  strcpy(hist->datsrc_1, "");
212  strcpy(hist->datsrc_2, "");
213  hist->edlinecnt = 0;
214 
215  return 1;
216 }
217 
254 int G_command_history(struct History *hist)
255 {
256  int j, cmdlen;
257  char *cmdlin;
258 
259  cmdlin = G_recreate_command();
260  cmdlen = strlen(cmdlin);
261 
262  if (hist->edlinecnt > MAXEDLINES - 2) {
263  G_warning(_("Not enough room in history file to record command line."));
264  return 1;
265  }
266 
267  if (hist->edlinecnt > 0) { /* add a blank line if preceding history exists */
268  strcpy(hist->edhist[hist->edlinecnt], "");
269  hist->edlinecnt++;
270  }
271 
272  if (cmdlen < 70) { /* ie if it will fit on a single line */
273  sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
274  hist->edlinecnt++;
275  }
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], "\\");
282  j += 68;
283  hist->edlinecnt++;
284  if (hist->edlinecnt > MAXEDLINES - 2) {
285  G_warning(_("Not enough room in history file for command line (truncated)."));
286  return 2;
287  }
288  }
289  if ((cmdlen - j) > 0) { /* ie anything left */
290  strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);
291  hist->edlinecnt++;
292  }
293  }
294  return 0;
295 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_short_history(const char *name, const char *type, struct History *hist)
initialize history structure
Definition: history.c:202
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
int G_ascii_check(char *string)
Removes non-ascii characters from buffer.
Definition: ascii_chk.c:34
char * G_recreate_command(void)
Creates command to run non-interactive.
Definition: parser.c:2829
int G_write_history(const char *name, struct History *hist)
write raster history file
Definition: history.c:153
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
int G_command_history(struct History *hist)
Save command line to raster history structure.
Definition: history.c:254
const char * G_program_name(void)
return module name
Definition: progrm_nme.c:33
char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:40
int G_getl(char *buf, int n, FILE *fd)
gets a line of text from a file
Definition: getl.c:17
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:197
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
int G_read_history(const char *name, const char *mapset, struct History *hist)
read raster history file
Definition: history.c:75
char * G_date(void)
Current date and time.
Definition: date.c:30
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226