gisinit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /* temporary disabled since svn keywords are not maintained by git
  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. */
  44. /* Make sure location and mapset are set */
  45. G_location_path();
  46. mapset = G_mapset();
  47. switch (G_mapset_permissions(mapset)) {
  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 at %s"), mapset, G_location_path());
  55. break;
  56. }
  57. gisinit();
  58. }
  59. /*!
  60. \brief Initialize GIS Library
  61. Initializes GIS engine, but does not check for a valid mapset.
  62. */
  63. void G__no_gisinit(const char *version)
  64. {
  65. if (initialized)
  66. return;
  67. /* temporary disabled since svn keywords are not maintained by git
  68. if (strcmp(version, GIS_H_VERSION) != 0)
  69. G_fatal_error(_("Module built against version %s but "
  70. "trying to use version %s. "
  71. "You need to rebuild GRASS GIS or untangle multiple installations."),
  72. version, GIS_H_VERSION);
  73. */
  74. gisinit();
  75. }
  76. /*!
  77. \brief Checks to see if GIS engine is initialized.
  78. */
  79. void G__check_gisinit(void)
  80. {
  81. if (initialized)
  82. return;
  83. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  84. G_sleep(3);
  85. exit(EXIT_FAILURE);
  86. }
  87. static int gisinit(void)
  88. {
  89. char *zlib;
  90. #ifdef __MINGW32__
  91. _fmode = O_BINARY;
  92. #endif
  93. /* Mark window as not set */
  94. G__.window_set = 0;
  95. /* byte order */
  96. G__.little_endian = G_is_little_endian();
  97. zlib = getenv("GRASS_ZLIB_LEVEL");
  98. /* Valid zlib compression levels -1 - 9 */
  99. /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
  100. * level 0 means no compression
  101. * as used here, 1 gives the best compromise between speed and compression */
  102. G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
  103. if (G__.compression_level < -1 || G__.compression_level > 9)
  104. G__.compression_level = 1;
  105. initialized = 1;
  106. setlocale(LC_NUMERIC, "C");
  107. return 0;
  108. }
  109. /*!
  110. \brief Initialize environment
  111. */
  112. void G_init_all(void)
  113. {
  114. G__check_gisinit();
  115. G_init_env();
  116. G_init_logging();
  117. G__init_window();
  118. G_init_locale();
  119. G_init_debug();
  120. G_verbose();
  121. G_init_tempfile();
  122. G__get_list_of_mapsets();
  123. G__home();
  124. G__machine_name();
  125. G_whoami();
  126. G_read_datum_table();
  127. G_read_ellipsoid_table(0);
  128. }