GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
put_title.c
Go to the documentation of this file.
1 /**************************************************************
2  * Rast_put_cell_title (name, title)
3  * char *name name of map file
4  * char *title new title
5  *
6  * changes the title for the cell file 'name' in current mapset
7  *
8  * returns 1 if ok, -1 if error
9  *************************************************************/
10 
11 #include <string.h>
12 #include <grass/gis.h>
13 #include <grass/glocale.h>
14 
15 int Rast_put_cell_title(const char *name, const char *title)
16 {
17  const char *mapset;
18  FILE *in, *out;
19  char *tempfile;
20  int line;
21  char buf[1024];
22 
23  mapset = G_mapset();
24  in = out = 0;
25  in = G_fopen_old("cats", name, mapset);
26  if (!in) {
27  G_warning(_("category information for [%s] in [%s]"
28  " missing or invalid"),
29  name, mapset);
30  return -1;
31  }
32 
33  tempfile = G_tempfile();
34  out = fopen(tempfile, "w");
35  if (!out) {
36  fclose(in);
37  G_warning(_("G_put_title - can't create a temp file"));
38  return -1;
39  }
40 
41  for (line = 0; G_getl(buf, sizeof buf, in); line++) {
42  if (line == 1) {
43  strcpy(buf, title);
44  G_strip(buf);
45  }
46  fprintf(out, "%s\n", buf);
47  }
48  fclose(in);
49  fclose(out);
50 
51  /* must be #cats line, title line, and label for cat 0 */
52  if (line < 3) {
53  G_warning(_("category information for [%s] in [%s] invalid"), name,
54  mapset);
55  return -1;
56  }
57 
58  in = fopen(tempfile, "r");
59  if (!in) {
60  G_warning(_("G_put_title - can't reopen temp file"));
61  return -1;
62  }
63 
64  out = G_fopen_new("cats", name);
65  if (!out) {
66  fclose(in);
67  G_warning(_("can't write category information for [%s] in [%s]"), name,
68  mapset);
69  return -1;
70  }
71 
72  while (fgets(buf, sizeof buf, in))
73  fprintf(out, "%s", buf);
74 
75  fclose(in);
76  fclose(out);
77  remove(tempfile);
78 
79  return 1;
80 }
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition: gis/open.c:251
void G_warning(const char *,...) __attribute__((format(printf
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition: strings.c:300
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
Definition: getl.c:33
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition: gis/open.c:219
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:6
#define strcpy
Definition: parson.c:62
int Rast_put_cell_title(const char *name, const char *title)
Definition: put_title.c:15