00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string.h>
00013 #include <grass/gis.h>
00014 #include <grass/glocale.h>
00015
00016 int G_put_cell_title(const char *name, const char *title)
00017 {
00018 char *mapset;
00019 FILE *in, *out;
00020 char *tempfile;
00021 int line;
00022 char buf[1024];
00023
00024 mapset = G_mapset();
00025 in = out = 0;
00026 in = G_fopen_old("cats", name, mapset);
00027 if (!in) {
00028 sprintf(buf,
00029 _("category information for [%s] in [%s] missing or invalid"),
00030 name, mapset);
00031 G_warning(buf);
00032 return -1;
00033 }
00034
00035 tempfile = G_tempfile();
00036 out = fopen(tempfile, "w");
00037 if (!out) {
00038 fclose(in);
00039 sprintf(buf, _("G_put_title - can't create a temp file"));
00040 G_warning(buf);
00041 return -1;
00042 }
00043
00044 for (line = 0; G_getl(buf, sizeof buf, in); line++) {
00045 if (line == 1) {
00046 strcpy(buf, title);
00047 G_strip(buf);
00048 }
00049 fprintf(out, "%s\n", buf);
00050 }
00051 fclose(in);
00052 fclose(out);
00053
00054
00055 if (line < 3) {
00056 sprintf(buf, _("category information for [%s] in [%s] invalid"), name,
00057 mapset);
00058 G_warning(buf);
00059 return -1;
00060 }
00061
00062 in = fopen(tempfile, "r");
00063 if (!in) {
00064 sprintf(buf, _("G_put_title - can't reopen temp file"));
00065 G_warning(buf);
00066 return -1;
00067 }
00068
00069 out = G_fopen_new("cats", name);
00070 if (!out) {
00071 fclose(in);
00072 sprintf(buf, _("can't write category information for [%s] in [%s]"),
00073 name, mapset);
00074 G_warning(buf);
00075 return -1;
00076 }
00077
00078 while (fgets(buf, sizeof buf, in))
00079 fprintf(out, "%s", buf);
00080
00081 fclose(in);
00082 fclose(out);
00083 remove(tempfile);
00084
00085 return 1;
00086 }