GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72269
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
put_title.c
Go to the documentation of this file.
1 
2 /**************************************************************
3  * Rast_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 Rast_put_cell_title(const char *name, const char *title)
17 {
18  const 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  G_warning(_("category information for [%s] in [%s]"
29  " missing or invalid"), 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"),
54  name, 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]"),
68  name, 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 }
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:258
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
int Rast_put_cell_title(const char *name, const char *title)
Definition: put_title.c:16
char * mapset
Definition: R.h:64
fclose(fd)
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition: getl.c:31
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:220
#define _(str)
Definition: glocale.h:13
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:253
const char * name
Definition: named_colr.c:7
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204