GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dhistory.c
Go to the documentation of this file.
1 
2 /**********************************************************************
3  *
4  * G3d_readHistory (name, mapset, hist)
5  * char *name name of map
6  * char *mapset mapset that map belongs to
7  * struct History *hist structure to hold history info
8  *
9  * Reads the history information associated with map layer "map"
10  * in mapset "mapset" into the structure "hist".
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  * G3d_writeHistory (name, hist)
20  * char *name name of map
21  * struct History *hist structure holding history info
22  *
23  * Writes the history information associated with map layer "map"
24  * into current from the structure "hist".
25  *
26  * returns: 0 if successful
27  * -1 on fail
28  **********************************************************************/
29 
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <grass/glocale.h>
34 #include "G3d_intern.h"
35 
36 /*simple error message */
37 void SimpleErrorMessage(FILE * fd, const char *name, const char *mapset)
38 {
39  if (fd != NULL)
40  fclose(fd);
41 
42  G_warning(_("can't get history information for [%s] in mapset [%s]"),
43  name, mapset);
44  return;
45 }
46 
63 int G3d_readHistory(const char *name, const char *mapset,
64  struct History *hist)
65 /* This function is adapted from G_read_history */
66 {
67  FILE *fd;
68  char buff[1024], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
69 
70  G_zero(hist, sizeof(struct History));
71 
72  /*this construct takes care of the correct history file path */
73  if (G__name_is_fully_qualified(name, xname, xmapset)) {
74  sprintf(buff, "%s/%s", G3D_DIRECTORY, xname);
75  sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist@mapset */
76  }
77  else {
78  sprintf(buff, "%s/%s", G3D_DIRECTORY, name);
79  sprintf(buf2, "%s", G3D_HISTORY_ELEMENT);
80  }
81 
82  if (!(fd = G_fopen_old(buff, buf2, mapset)))
83  return -2;
84 
85 
86  if (!G_getl(hist->mapid, sizeof(hist->mapid), fd)) {
87  SimpleErrorMessage(fd, name, mapset);
88  return -1;
89  }
90  G_ascii_check(hist->mapid);
91 
92  if (!G_getl(hist->title, sizeof(hist->title), fd)) {
93  SimpleErrorMessage(fd, name, mapset);
94  return -1;
95  }
96  G_ascii_check(hist->title);
97 
98  if (!G_getl(hist->mapset, sizeof(hist->mapset), fd)) {
99  SimpleErrorMessage(fd, name, mapset);
100  return -1;
101  }
102  G_ascii_check(hist->mapset);
103 
104  if (!G_getl(hist->creator, sizeof(hist->creator), fd)) {
105  SimpleErrorMessage(fd, name, mapset);
106  return -1;
107  }
108  G_ascii_check(hist->creator);
109 
110  if (!G_getl(hist->maptype, sizeof(hist->maptype), fd)) {
111  SimpleErrorMessage(fd, name, mapset);
112  return -1;
113  }
114  G_ascii_check(hist->maptype);
115 
116  if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd)) {
117  SimpleErrorMessage(fd, name, mapset);
118  return -1;
119  }
120  G_ascii_check(hist->datsrc_1);
121 
122  if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd)) {
123  SimpleErrorMessage(fd, name, mapset);
124  return -1;
125  }
126  G_ascii_check(hist->datsrc_2);
127 
128  if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd)) {
129  SimpleErrorMessage(fd, name, mapset);
130  return -1;
131  }
132  G_ascii_check(hist->keywrd);
133 
134  hist->edlinecnt = 0;
135  while ((hist->edlinecnt < MAXEDLINES) &&
136  (G_getl
137  (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
138  G_ascii_check(hist->edhist[hist->edlinecnt]);
139  hist->edlinecnt++;
140  }
141 
142  fclose(fd);
143  return 0;
144 }
145 
146 
162 int G3d_writeHistory(const char *name, struct History *hist)
163 /* This function is adapted from G_write_history */
164 {
165  FILE *fd;
166  int i;
167  char buf[200], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
168 
169  if (G__name_is_fully_qualified(name, xname, xmapset)) {
170  sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
171  sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist@mapset */
172  }
173  else {
174  sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
175  sprintf(buf2, "%s", G3D_HISTORY_ELEMENT);
176  }
177 
178  if (!(fd = G_fopen_new(buf, buf2)))
179  return -1;
180 
181  fprintf(fd, "%s\n", hist->mapid);
182  fprintf(fd, "%s\n", hist->title);
183  fprintf(fd, "%s\n", hist->mapset);
184  fprintf(fd, "%s\n", hist->creator);
185  fprintf(fd, "%s\n", hist->maptype);
186  fprintf(fd, "%s\n", hist->datsrc_1);
187  fprintf(fd, "%s\n", hist->datsrc_2);
188  fprintf(fd, "%s\n", hist->keywrd);
189 
190  for (i = 0; i < hist->edlinecnt; i++)
191  fprintf(fd, "%s\n", hist->edhist[i]);
192 
193  fclose(fd);
194  return 0;
195 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
char xmapset[512]
Definition: g3dcats.c:89
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
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
char buff[1024]
Definition: g3dcats.c:89
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
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)
char buf2[200]
Definition: g3dcats.c:89
int G3d_readHistory(const char *name, const char *mapset, struct History *hist)
read raster3d History file
Definition: g3dhistory.c:63
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
int G3d_writeHistory(const char *name, struct History *hist)
write raster3d History file
Definition: g3dhistory.c:162
char xname[512]
Definition: g3dcats.c:89
void SimpleErrorMessage(FILE *fd, const char *name, const char *mapset)
Definition: g3dhistory.c:37
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57