put_title.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <string.h>
  11. #include <grass/gis.h>
  12. #include <grass/glocale.h>
  13. int Rast_put_cell_title(const char *name, const char *title)
  14. {
  15. const char *mapset;
  16. FILE *in, *out;
  17. char *tempfile;
  18. int line;
  19. char buf[1024];
  20. mapset = G_mapset();
  21. in = out = 0;
  22. in = G_fopen_old("cats", name, mapset);
  23. if (!in) {
  24. G_warning(_("category information for [%s] in [%s]"
  25. " missing or invalid"), name, mapset);
  26. return -1;
  27. }
  28. tempfile = G_tempfile();
  29. out = fopen(tempfile, "w");
  30. if (!out) {
  31. fclose(in);
  32. G_warning(_("G_put_title - can't create a temp file"));
  33. return -1;
  34. }
  35. for (line = 0; G_getl(buf, sizeof buf, in); line++) {
  36. if (line == 1) {
  37. strcpy(buf, title);
  38. G_strip(buf);
  39. }
  40. fprintf(out, "%s\n", buf);
  41. }
  42. fclose(in);
  43. fclose(out);
  44. /* must be #cats line, title line, and label for cat 0 */
  45. if (line < 3) {
  46. G_warning(_("category information for [%s] in [%s] invalid"),
  47. name, mapset);
  48. return -1;
  49. }
  50. in = fopen(tempfile, "r");
  51. if (!in) {
  52. G_warning(_("G_put_title - can't reopen temp file"));
  53. return -1;
  54. }
  55. out = G_fopen_new("cats", name);
  56. if (!out) {
  57. fclose(in);
  58. G_warning(_("can't write category information for [%s] in [%s]"),
  59. name, mapset);
  60. return -1;
  61. }
  62. while (fgets(buf, sizeof buf, in))
  63. fprintf(out, "%s", buf);
  64. fclose(in);
  65. fclose(out);
  66. remove(tempfile);
  67. return 1;
  68. }