gisinit.c 2.9 KB

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