gisinit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <ctype.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <sys/stat.h>
  16. #include <locale.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. #include "G.h"
  20. #include "gis_local_proto.h"
  21. struct G__ G__;
  22. static int initialized = 0; /** Is set when engine is initialized */
  23. static int gisinit(void);
  24. /*!
  25. \brief Initialize GIS Library and ensures a valid mapset is available.
  26. \param version
  27. \param pgm program (module) name
  28. \return always returns 0 on success
  29. \return G_fatal_error() is called on error
  30. */
  31. void G__gisinit(const char *version, const char *pgm)
  32. {
  33. const char *mapset;
  34. if (initialized)
  35. return;
  36. G_set_program_name(pgm);
  37. /* verify version of GRASS headers (and anything else in include) */
  38. if (strcmp(version, GIS_H_VERSION) != 0)
  39. G_fatal_error(_("Module built against version %s but "
  40. "trying to use version %s. "
  41. "You need to rebuild GRASS GIS or untangle multiple installations."),
  42. version, GIS_H_VERSION);
  43. /* Make sure location and mapset are set */
  44. G_location_path();
  45. mapset = G_mapset();
  46. switch (G_mapset_permissions(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 at %s"), mapset, G_location_path());
  54. break;
  55. }
  56. gisinit();
  57. }
  58. /*!
  59. \brief Initialize GIS Library
  60. Initializes GIS engine, but does not check for a valid mapset.
  61. */
  62. void G__no_gisinit(const char *version)
  63. {
  64. if (initialized)
  65. return;
  66. /* verify version of GRASS headers (and anything else in include) */
  67. if (strcmp(version, GIS_H_VERSION) != 0)
  68. G_fatal_error(_("Module built against version %s but "
  69. "trying to use version %s. "
  70. "You need to rebuild GRASS GIS or untangle multiple installations."),
  71. version, GIS_H_VERSION);
  72. gisinit();
  73. }
  74. /*!
  75. \brief Checks to see if GIS engine is initialized.
  76. */
  77. void G__check_gisinit(void)
  78. {
  79. if (initialized)
  80. return;
  81. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  82. G_sleep(3);
  83. exit(EXIT_FAILURE);
  84. }
  85. static int gisinit(void)
  86. {
  87. char *zlib;
  88. #ifdef __MINGW32__
  89. _fmode = O_BINARY;
  90. #endif
  91. /* Mark window as not set */
  92. G__.window_set = 0;
  93. /* byte order */
  94. G__.little_endian = G_is_little_endian();
  95. zlib = getenv("GRASS_ZLIB_LEVEL");
  96. /* Valid zlib compression levels -1 - 9 */
  97. /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
  98. * level 0 means no compression
  99. * as used here, 1 gives the best compromise between speed and compression */
  100. G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
  101. if (G__.compression_level < -1 || G__.compression_level > 9)
  102. G__.compression_level = 1;
  103. initialized = 1;
  104. setlocale(LC_NUMERIC, "C");
  105. return 0;
  106. }
  107. /*!
  108. \brief Initialize environment
  109. */
  110. void G_init_all(void)
  111. {
  112. G__check_gisinit();
  113. G_init_env();
  114. G_init_logging();
  115. G__init_window();
  116. G_init_locale();
  117. G_init_debug();
  118. G_verbose();
  119. G_init_tempfile();
  120. G__get_list_of_mapsets();
  121. G__home();
  122. G__machine_name();
  123. G_whoami();
  124. G_read_datum_table();
  125. G_read_ellipsoid_table(0);
  126. }