gisinit.c 2.9 KB

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