get_window.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. *************************************************************************
  3. * G_get_window (window)
  4. * struct Cell_head *window
  5. *
  6. * read the current mapset window
  7. * dies if error
  8. *
  9. *************************************************************************
  10. * G_get_default_window (window)
  11. * struct Cell_head *window
  12. *
  13. * read the default window for the location
  14. * dies if error
  15. *
  16. *************************************************************************
  17. * char *
  18. * G__get_window (window, element, name, mapset)
  19. * read the window 'name' in 'element' in 'mapset'
  20. * returns NULL if ok, error message if not
  21. ************************************************************************/
  22. #include <stdlib.h>
  23. #include "G.h"
  24. #include <grass/gis.h>
  25. #include <grass/glocale.h>
  26. /*!
  27. * \brief read the database region
  28. *
  29. * Reads the database region as stored in the WIND file in the user's
  30. * current mapset <b>into region.</b>
  31. * 3D values are set to defaults if not available in WIND file.
  32. * An error message is printed and exit( ) is called if there is a problem reading
  33. * the region.
  34. * <b>Note.</b> GRASS applications that read or write raster maps should not
  35. * use this routine since its use implies that the active module region will not
  36. * be used. Programs that read or write raster map data (or vector data) can
  37. * query the active module region <i>using G_window_rows and
  38. * G_window_cols..</i>
  39. *
  40. * \param region
  41. * \return int
  42. */
  43. int G_get_window(struct Cell_head *window)
  44. {
  45. static int first = 1;
  46. static struct Cell_head dbwindow;
  47. char *regvar;
  48. /* Optionally read the region from environment variable */
  49. regvar = getenv("GRASS_REGION");
  50. if (regvar) {
  51. char **tokens, *delm = ";";
  52. char *err;
  53. tokens = G_tokenize(regvar, delm);
  54. err = G__read_Cell_head_array(tokens, window, 0);
  55. G_free_tokens(tokens);
  56. if (err) {
  57. G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
  58. err);
  59. G_free(err);
  60. }
  61. return 1;
  62. }
  63. if (first) {
  64. char *wind, *err;
  65. wind = getenv("WIND_OVERRIDE");
  66. if (wind)
  67. err = G__get_window(&dbwindow, "windows", wind, G_mapset());
  68. else
  69. err = G__get_window(&dbwindow, "", "WIND", G_mapset());
  70. if (err) {
  71. G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
  72. err);
  73. G_free(err);
  74. }
  75. }
  76. first = 0;
  77. G_copy(window, &dbwindow, sizeof(dbwindow));
  78. if (!G__.window_set) {
  79. G__.window_set = 1;
  80. G_copy(&G__.window, &dbwindow, sizeof(dbwindow));
  81. }
  82. return 1;
  83. }
  84. /*!
  85. * \brief read the default region
  86. *
  87. * Reads the default region for the location into <b>region.</b>
  88. * 3D values are set to defaults if not available in WIND file.
  89. * An error message is printed and exit( ) is called if there is a problem
  90. * reading the default region.
  91. *
  92. * \param region
  93. * \return int
  94. */
  95. int G_get_default_window(struct Cell_head *window)
  96. {
  97. char *err;
  98. if ((err = G__get_window(window, "", "DEFAULT_WIND", "PERMANENT"))) {
  99. G_fatal_error(_("default region %s"), err);
  100. G_free(err);
  101. }
  102. return 1;
  103. }
  104. char *G__get_window(struct Cell_head *window,
  105. const char *element, const char *name, const char *mapset)
  106. {
  107. FILE *fd;
  108. char *err;
  109. G_zero((char *)window, sizeof(struct Cell_head));
  110. /* Read from file */
  111. if (!(fd = G_fopen_old(element, name, mapset))) {
  112. /*
  113. char path[GPATH_MAX];
  114. G__file_name (path,element,name,mapset);
  115. fprintf (stderr, "G__get_window(%s)\n",path);
  116. */
  117. return G_store(_("is not set"));
  118. }
  119. err = G__read_Cell_head(fd, window, 0);
  120. fclose(fd);
  121. if (err) {
  122. char msg[1024];
  123. sprintf(msg, _("is invalid\n%s"), err);
  124. G_free(err);
  125. return G_store(msg);
  126. }
  127. return NULL;
  128. }