put_title.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**************************************************************
  2. * G_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 G_put_cell_title(const char *name, const char *title)
  14. {
  15. 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. sprintf(buf,
  25. _("category information for [%s] in [%s] missing or invalid"),
  26. name, mapset);
  27. G_warning(buf);
  28. return -1;
  29. }
  30. tempfile = G_tempfile();
  31. out = fopen(tempfile, "w");
  32. if (!out) {
  33. fclose(in);
  34. sprintf(buf, _("G_put_title - can't create a temp file"));
  35. G_warning(buf);
  36. return -1;
  37. }
  38. for (line = 0; G_getl(buf, sizeof buf, in); line++) {
  39. if (line == 1) {
  40. strcpy(buf, title);
  41. G_strip(buf);
  42. }
  43. fprintf(out, "%s\n", buf);
  44. }
  45. fclose(in);
  46. fclose(out);
  47. /* must be #cats line, title line, and label for cat 0 */
  48. if (line < 3) {
  49. sprintf(buf, _("category information for [%s] in [%s] invalid"), name,
  50. mapset);
  51. G_warning(buf);
  52. return -1;
  53. }
  54. in = fopen(tempfile, "r");
  55. if (!in) {
  56. sprintf(buf, _("G_put_title - can't reopen temp file"));
  57. G_warning(buf);
  58. return -1;
  59. }
  60. out = G_fopen_new("cats", name);
  61. if (!out) {
  62. fclose(in);
  63. sprintf(buf, _("can't write category information for [%s] in [%s]"),
  64. name, mapset);
  65. G_warning(buf);
  66. return -1;
  67. }
  68. while (fgets(buf, sizeof buf, in))
  69. fprintf(out, "%s", buf);
  70. fclose(in);
  71. fclose(out);
  72. remove(tempfile);
  73. return 1;
  74. }