GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
put_title.c
Go to the documentation of this file.
1 
2 /**************************************************************
3  * G_put_cell_title (name, title)
4  * char *name name of map file
5  * char *title new title
6  *
7  * changes the title for the cell file 'name' in current mapset
8  *
9  * returns 1 if ok, -1 if error
10  *************************************************************/
11 
12 #include <string.h>
13 #include <grass/gis.h>
14 #include <grass/glocale.h>
15 
16 int G_put_cell_title(const char *name, const char *title)
17 {
18  char *mapset;
19  FILE *in, *out;
20  char *tempfile;
21  int line;
22  char buf[1024];
23 
24  mapset = G_mapset();
25  in = out = 0;
26  in = G_fopen_old("cats", name, mapset);
27  if (!in) {
28  sprintf(buf,
29  _("category information for [%s] in [%s] missing or invalid"),
30  name, mapset);
31  G_warning(buf);
32  return -1;
33  }
34 
35  tempfile = G_tempfile();
36  out = fopen(tempfile, "w");
37  if (!out) {
38  fclose(in);
39  sprintf(buf, _("G_put_title - can't create a temp file"));
40  G_warning(buf);
41  return -1;
42  }
43 
44  for (line = 0; G_getl(buf, sizeof buf, in); line++) {
45  if (line == 1) {
46  strcpy(buf, title);
47  G_strip(buf);
48  }
49  fprintf(out, "%s\n", buf);
50  }
51  fclose(in);
52  fclose(out);
53 
54  /* must be #cats line, title line, and label for cat 0 */
55  if (line < 3) {
56  sprintf(buf, _("category information for [%s] in [%s] invalid"), name,
57  mapset);
58  G_warning(buf);
59  return -1;
60  }
61 
62  in = fopen(tempfile, "r");
63  if (!in) {
64  sprintf(buf, _("G_put_title - can't reopen temp file"));
65  G_warning(buf);
66  return -1;
67  }
68 
69  out = G_fopen_new("cats", name);
70  if (!out) {
71  fclose(in);
72  sprintf(buf, _("can't write category information for [%s] in [%s]"),
73  name, mapset);
74  G_warning(buf);
75  return -1;
76  }
77 
78  while (fgets(buf, sizeof buf, in))
79  fprintf(out, "%s", buf);
80 
81  fclose(in);
82  fclose(out);
83  remove(tempfile);
84 
85  return 1;
86 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
string name
Definition: render.py:1314
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:47
char * mapset
Definition: G.h:64
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
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
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
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 G_put_cell_title(const char *name, const char *title)
Definition: put_title.c:16