GRASS Programmer's Manual  6.4.2(2012)
 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 }