gisinit.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * \file gisinit.c
  3. *
  4. * \brief GIS Library - Handles program initialization.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 2000-2008
  14. */
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20. #include <locale.h>
  21. #include <grass/gis.h>
  22. #include "G.h"
  23. #include <grass/glocale.h>
  24. struct G__ G__;
  25. static int initialized = 0; /** Is set when engine is initialized */
  26. static int gisinit(void);
  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. void G__gisinit(const char *version, const char *pgm)
  37. {
  38. char *mapset;
  39. if (initialized)
  40. return;
  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. 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. }
  58. /**
  59. * \brief Initialize GRASS GIS engine.
  60. *
  61. * Initializes GIS engine, but does not check for a valid mapset.
  62. *
  63. * \return
  64. */
  65. void G__no_gisinit(const char *version)
  66. {
  67. if (initialized)
  68. return;
  69. if (strcmp(version, GIS_H_VERSION) != 0)
  70. G_fatal_error(_("Incompatible library version for module"));
  71. gisinit();
  72. }
  73. /**
  74. * \brief Checks to see if GIS engine is initialized.
  75. *
  76. * \return
  77. */
  78. void G__check_gisinit(void)
  79. {
  80. if (initialized)
  81. return;
  82. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  83. G_sleep(3);
  84. exit(EXIT_FAILURE);
  85. }
  86. static int gisinit(void)
  87. {
  88. /* Mark window as not set */
  89. G__.window_set = 0;
  90. /* no histograms */
  91. G__.want_histogram = 0;
  92. /* set the write type for floating maps */
  93. G__.fp_type = -1;
  94. /* Set masking flag unknown */
  95. G__.auto_mask = -1;
  96. G__.nbytes = sizeof(CELL);
  97. G__.compression_type = 0;
  98. initialized = 1;
  99. setlocale(LC_NUMERIC, "C");
  100. return 0;
  101. }
  102. void G_init_all(void)
  103. {
  104. G__check_gisinit();
  105. G_init_env();
  106. G_init_logging();
  107. G__init_window();
  108. G_get_fp_type();
  109. G_get_compression_type();
  110. G__check_for_auto_masking();
  111. G_init_locale();
  112. G_init_debug();
  113. G_verbose();
  114. G_init_tempfile();
  115. G_init_gdal();
  116. G_get_list_of_mapsets();
  117. G__home();
  118. G__machine_name();
  119. G_whoami();
  120. G_read_datum_table();
  121. G_read_ellipsoid_table(0);
  122. }