gisinit.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * \file gisinit.c
  3. *
  4. * \brief Handles program initialization.
  5. *
  6. * This program is free software under the GNU General Public License
  7. * (>=v2). Read the file COPYING that comes with GRASS for details.
  8. *
  9. * \author GRASS GIS Development Team
  10. *
  11. * \date 2000-2006
  12. */
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include <grass/gis.h>
  19. #include "G.h"
  20. #include <grass/glocale.h>
  21. struct G__ G__ ;
  22. static int initialized = 0; /** Is set when engine is initialized */
  23. static int gisinit(void);
  24. /**
  25. * \fn int G_gisinit (const char *pgm)
  26. *
  27. * \brief Initialize GRASS GIS engine.
  28. *
  29. * Initializes GIS engine and ensures a valid mapset is available.
  30. *
  31. * \param[in] pgm Program (module) name
  32. * \return always returns 0 on success
  33. * \return exit() is called on error
  34. */
  35. int G__gisinit(const char *version, const char *pgm)
  36. {
  37. char *mapset;
  38. if ( initialized )
  39. return 0;
  40. G_set_program_name (pgm);
  41. if (strcmp(version, GIS_H_VERSION) != 0)
  42. G_fatal_error(_("Incompatible library version for module"));
  43. /* Make sure location and mapset are set */
  44. G_location_path();
  45. switch (G__mapset_permissions (mapset = G_mapset()))
  46. {
  47. case 1:
  48. break;
  49. case 0:
  50. G_fatal_error (_("MAPSET %s - permission denied"), mapset);
  51. break;
  52. default:
  53. G_fatal_error (_("MAPSET %s not found"), mapset);
  54. break;
  55. }
  56. gisinit();
  57. return 0;
  58. }
  59. /**
  60. * \fn int G_no_gisinit (void)
  61. *
  62. * \brief Initialize GRASS GIS engine.
  63. *
  64. * Initializes GIS engine, but does not check for a valid mapset.
  65. *
  66. * \return always returns 0 on success
  67. */
  68. int G__no_gisinit(const char *version)
  69. {
  70. if ( initialized )
  71. return 0;
  72. if (strcmp(version, GIS_H_VERSION) != 0)
  73. G_fatal_error(_("Incompatible library version for module"));
  74. gisinit();
  75. return 0;
  76. }
  77. /**
  78. * \fn int G__check_gisinit (void)
  79. *
  80. * \brief Checks to see if GIS engine is initialized.
  81. *
  82. * \return 1 on success
  83. * \return exit() is called on error
  84. */
  85. int G__check_gisinit(void)
  86. {
  87. if (initialized) return 1;
  88. G_warning (_("System not initialized. Programmer forgot to call G_gisinit()."));
  89. G_sleep(3);
  90. exit(EXIT_FAILURE);
  91. }
  92. static int gisinit(void)
  93. {
  94. /* Mark window as not set */
  95. G__.window_set = 0 ;
  96. /* no histograms */
  97. G__.want_histogram = 0;
  98. /* Set compressed data buffer size to zero */
  99. G__.compressed_buf_size = 0;
  100. G__.work_buf_size = 0;
  101. G__.null_buf_size = 0;
  102. G__.mask_buf_size = 0;
  103. G__.temp_buf_size = 0;
  104. /* mask buf we always want to keep allocated */
  105. G__reallocate_mask_buf();
  106. /* set the write type for floating maps */
  107. G__.fp_type = FCELL_TYPE;
  108. G__.fp_nbytes = XDR_FLOAT_NBYTES;
  109. /* Set masking flag unknown */
  110. G__.auto_mask = -1 ;
  111. /* set architecture dependant bit patterns for embeded null vals */
  112. G__init_null_patterns();
  113. initialized = 1;
  114. return 0;
  115. }