GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-ed80a6eaeb
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  G_free(tempfile);
39  return -1;
40  }
41 
42  for (line = 0; G_getl(buf, sizeof buf, in); line++) {
43  if (line == 1) {
44  strcpy(buf, title);
45  G_strip(buf);
46  }
47  fprintf(out, "%s\n", buf);
48  }
49  fclose(in);
50  fclose(out);
51 
52  /* must be #cats line, title line, and label for cat 0 */
53  if (line < 3) {
54  G_warning(_("category information for [%s] in [%s] invalid"), name,
55  mapset);
56  remove(tempfile);
57  G_free(tempfile);
58  return -1;
59  }
60 
61  in = fopen(tempfile, "r");
62  if (!in) {
63  G_warning(_("G_put_title - can't reopen temp file"));
64  G_free(tempfile);
65  return -1;
66  }
67 
68  out = G_fopen_new("cats", name);
69  if (!out) {
70  fclose(in);
71  G_warning(_("can't write category information for [%s] in [%s]"), name,
72  mapset);
73  remove(tempfile);
74  G_free(tempfile);
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  G_free(tempfile);
85 
86  return 1;
87 }
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition: gis/open.c:251
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
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