gisinit.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <locale.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. #include "G.h"
  25. struct G__ G__;
  26. static int initialized = 0; /** Is set when engine is initialized */
  27. static int gisinit(void);
  28. /**
  29. * \brief Initialize GRASS GIS engine.
  30. *
  31. * Initializes GIS engine and ensures a valid mapset is available.
  32. *
  33. * \param[in] pgm Program (module) name
  34. * \return always returns 0 on success
  35. * \return exit() is called on error
  36. */
  37. void G__gisinit(const char *version, const char *pgm)
  38. {
  39. const char *mapset;
  40. if (initialized)
  41. return;
  42. G_set_program_name(pgm);
  43. if (strcmp(version, GIS_H_VERSION) != 0)
  44. G_fatal_error(_("Incompatible library version for module. "
  45. "You need to rebuild GRASS or untangle multiple installations."));
  46. /* Make sure location and mapset are set */
  47. G_location_path();
  48. mapset = G_mapset();
  49. switch (G__mapset_permissions(mapset)) {
  50. case 1:
  51. break;
  52. case 0:
  53. G_fatal_error(_("MAPSET %s - permission denied"), mapset);
  54. break;
  55. default:
  56. G_fatal_error(_("MAPSET %s not found"), mapset);
  57. break;
  58. }
  59. gisinit();
  60. }
  61. /**
  62. * \brief Initialize GRASS GIS engine.
  63. *
  64. * Initializes GIS engine, but does not check for a valid mapset.
  65. *
  66. * \return
  67. */
  68. void G__no_gisinit(const char *version)
  69. {
  70. if (initialized)
  71. return;
  72. if (strcmp(version, GIS_H_VERSION) != 0)
  73. G_fatal_error(_("Incompatible library version for module. "
  74. "You need to rebuild GRASS or untangle multiple installations."));
  75. gisinit();
  76. }
  77. /**
  78. * \brief Checks to see if GIS engine is initialized.
  79. *
  80. * \return
  81. */
  82. void G__check_gisinit(void)
  83. {
  84. if (initialized)
  85. return;
  86. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  87. G_sleep(3);
  88. exit(EXIT_FAILURE);
  89. }
  90. static int gisinit(void)
  91. {
  92. #ifdef __MINGW32__
  93. _fmode = O_BINARY;
  94. #endif
  95. /* Mark window as not set */
  96. G__.window_set = 0;
  97. initialized = 1;
  98. setlocale(LC_NUMERIC, "C");
  99. return 0;
  100. }
  101. void G_init_all(void)
  102. {
  103. G__check_gisinit();
  104. G_init_env();
  105. G_init_logging();
  106. G__init_window();
  107. G_init_locale();
  108. G_init_debug();
  109. G_verbose();
  110. G_init_tempfile();
  111. G_get_list_of_mapsets();
  112. G__home();
  113. G__machine_name();
  114. G_whoami();
  115. G_read_datum_table();
  116. G_read_ellipsoid_table(0);
  117. }