put_window.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. \file lib/gis/put_window.c
  3. \brief GIS Library - Modify window (i.e. GRASS region)
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdlib.h>
  10. #include <grass/gis.h>
  11. #include "gis_local_proto.h"
  12. /*!
  13. * \brief Writes the region (window)
  14. *
  15. * Writes the region file (WIND) in the user's current mapset
  16. * or when environmental variable \c WIND_OVERRIDE is set,
  17. * it writes the region to file specified by \c WIND_OVERRIDE variable.
  18. *
  19. * When \c WIND_OVERRIDE is set the current process and child processes
  20. * are affected.
  21. * Otherwise the whole GRASS session is affected.
  22. *
  23. * \warning When environmental variable \c WIND_OVERRIDE is not set,
  24. * this routine actually changes the region.
  25. * So in this case it should only be called by modules which the user knows
  26. * will change the region. It is probably fair to say that only the
  27. * \gmod{g.region} should call this routine unless \c WIND_OVERRIDE is set.
  28. *
  29. * This function does not adjust the \p window before setting the region
  30. * so you should call G_adjust_Cell_head() before calling this function.
  31. *
  32. * \param[in,out] window pointer to Cell_head
  33. *
  34. * \return 1 on success
  35. * \return -1 on error (no diagnostic message is printed)
  36. *
  37. * \sa G_get_window(), G_set_window(), python.core.use_temp_region()
  38. */
  39. int G_put_window(const struct Cell_head *window)
  40. {
  41. char *wind = getenv("WIND_OVERRIDE");
  42. return wind ? G_put_element_window(window, "windows", wind)
  43. : G_put_element_window(window, "", "WIND");
  44. }
  45. /*!
  46. * \brief Write the region
  47. *
  48. * Writes the region file (WIND) in the user's current mapset
  49. * from region.
  50. * <b>Warning:</b> Since this routine actually changes the
  51. * region, it should only be called by modules which the user knows
  52. * will change the region. It is probably fair to say that only the
  53. * <tt>g.region</tt> should call this routine.
  54. *
  55. * \param[in,out] window pointer to Cell_head
  56. * \param dir directory name
  57. * \param name file name
  58. *
  59. * \return 1 on success
  60. * \return -1 on error (no diagnostic message is printed)
  61. *
  62. * \sa G_put_window()
  63. */
  64. int G_put_element_window(const struct Cell_head *window, const char *dir, const char *name)
  65. {
  66. FILE *fd;
  67. if (!(fd = G_fopen_new(dir, name)))
  68. return -1;
  69. G__write_Cell_head3(fd, window, 0);
  70. fclose(fd);
  71. return 1;
  72. }