gisinit.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. struct G__ G__;
  21. static int initialized = 0; /** Is set when engine is initialized */
  22. static int gisinit(void);
  23. /*!
  24. \brief Initialize GIS Library and ensures a valid mapset is available.
  25. \param version
  26. \param pgm program (module) name
  27. \return always returns 0 on success
  28. \return G_fatal_error() is called on error
  29. */
  30. void G__gisinit(const char *version, const char *pgm)
  31. {
  32. const char *mapset;
  33. if (initialized)
  34. return;
  35. G_set_program_name(pgm);
  36. if (strcmp(version, GIS_H_VERSION) != 0)
  37. G_fatal_error(_("Module built against version %s but "
  38. "trying to use version %s. "
  39. "You need to rebuild GRASS GIS or untangle multiple installations."),
  40. version, GIS_H_VERSION);
  41. /* Make sure location and mapset are set */
  42. G_location_path();
  43. mapset = G_mapset();
  44. switch (G__mapset_permissions(mapset)) {
  45. case 1:
  46. break;
  47. case 0:
  48. G_fatal_error(_("MAPSET %s - permission denied"), mapset);
  49. break;
  50. default:
  51. G_fatal_error(_("MAPSET %s not found at %s"), mapset, G_location_path());
  52. break;
  53. }
  54. gisinit();
  55. }
  56. /*!
  57. \brief Initialize GIS Library
  58. Initializes GIS engine, but does not check for a valid mapset.
  59. */
  60. void G__no_gisinit(const char *version)
  61. {
  62. if (initialized)
  63. return;
  64. if (strcmp(version, GIS_H_VERSION) != 0)
  65. G_fatal_error(_("Module built against version %s but "
  66. "trying to use version %s. "
  67. "You need to rebuild GRASS GIS or untangle multiple installations."),
  68. version, GIS_H_VERSION);
  69. gisinit();
  70. }
  71. /*!
  72. \brief Checks to see if GIS engine is initialized.
  73. */
  74. void G__check_gisinit(void)
  75. {
  76. if (initialized)
  77. return;
  78. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  79. G_sleep(3);
  80. exit(EXIT_FAILURE);
  81. }
  82. static int gisinit(void)
  83. {
  84. char *zlib;
  85. #ifdef __MINGW32__
  86. _fmode = O_BINARY;
  87. #endif
  88. /* Mark window as not set */
  89. G__.window_set = 0;
  90. /* byte order */
  91. G__.little_endian = G_is_little_endian();
  92. zlib = getenv("GRASS_ZLIB_LEVEL");
  93. G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : -2;
  94. initialized = 1;
  95. setlocale(LC_NUMERIC, "C");
  96. return 0;
  97. }
  98. /*!
  99. \brief Initialize environment
  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. }