gisinit.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (strcmp(version, GIS_H_VERSION) != 0)
  38. G_fatal_error(_("Module built against version %s but "
  39. "trying to use version %s. "
  40. "You need to rebuild GRASS GIS or untangle multiple installations."),
  41. version, GIS_H_VERSION);
  42. /* Make sure location and mapset are set */
  43. G_location_path();
  44. mapset = G_mapset();
  45. switch (G_mapset_permissions(mapset)) {
  46. case 1:
  47. break;
  48. case 0:
  49. G_fatal_error(_("MAPSET %s - permission denied"), mapset);
  50. break;
  51. default:
  52. G_fatal_error(_("MAPSET %s not found at %s"), mapset, G_location_path());
  53. break;
  54. }
  55. gisinit();
  56. }
  57. /*!
  58. \brief Initialize GIS Library
  59. Initializes GIS engine, but does not check for a valid mapset.
  60. */
  61. void G__no_gisinit(const char *version)
  62. {
  63. if (initialized)
  64. return;
  65. if (strcmp(version, GIS_H_VERSION) != 0)
  66. G_fatal_error(_("Module built against version %s but "
  67. "trying to use version %s. "
  68. "You need to rebuild GRASS GIS or untangle multiple installations."),
  69. version, GIS_H_VERSION);
  70. gisinit();
  71. }
  72. /*!
  73. \brief Checks to see if GIS engine is initialized.
  74. */
  75. void G__check_gisinit(void)
  76. {
  77. if (initialized)
  78. return;
  79. G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
  80. G_sleep(3);
  81. exit(EXIT_FAILURE);
  82. }
  83. static int gisinit(void)
  84. {
  85. char *zlib;
  86. #ifdef __MINGW32__
  87. _fmode = O_BINARY;
  88. #endif
  89. /* Mark window as not set */
  90. G__.window_set = 0;
  91. /* byte order */
  92. G__.little_endian = G_is_little_endian();
  93. zlib = getenv("GRASS_ZLIB_LEVEL");
  94. G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : -2;
  95. initialized = 1;
  96. setlocale(LC_NUMERIC, "C");
  97. return 0;
  98. }
  99. /*!
  100. \brief Initialize environment
  101. */
  102. void G_init_all(void)
  103. {
  104. G__check_gisinit();
  105. G_init_env();
  106. G_init_logging();
  107. G__init_window();
  108. G_init_locale();
  109. G_init_debug();
  110. G_verbose();
  111. G_init_tempfile();
  112. G__get_list_of_mapsets();
  113. G__home();
  114. G__machine_name();
  115. G_whoami();
  116. G_read_datum_table();
  117. G_read_ellipsoid_table(0);
  118. }