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
Rast_put_cell_title(const char * name,const char * title)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 }
81