executionEnvironment.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Filename: executionEnvironment.cxx
  2. // Created by: drose (15May00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "executionEnvironment.h"
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <errno.h>
  23. #include <stdio.h> // for perror
  24. #ifdef WIN32_VC
  25. // Windows requires this for getcwd().
  26. #include <direct.h>
  27. #define getcwd _getcwd
  28. #endif
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: ExecutionEnvironment::get_environment_variable
  31. // Access: Public, Static
  32. // Description: Returns the definition of the indicated environment
  33. // variable, or the empty string if the variable is
  34. // undefined. The nonstatic implementation.
  35. ////////////////////////////////////////////////////////////////////
  36. string ExecutionEnvironment::
  37. get_environment_variable(const string &var) {
  38. const char *def = getenv(var.c_str());
  39. if (def != (char *)NULL) {
  40. return def;
  41. }
  42. return string();
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: ExecutionEnviroment::expand_string
  46. // Access: Public, Static
  47. // Description: Reads the string, looking for environment variable
  48. // names marked by a $. Expands all such variable
  49. // names. A repeated dollar sign ($$) is mapped to a
  50. // single dollar sign.
  51. //
  52. // Returns the expanded string.
  53. ////////////////////////////////////////////////////////////////////
  54. string ExecutionEnvironment::
  55. expand_string(const string &str) {
  56. string result;
  57. size_t last = 0;
  58. size_t dollar = str.find('$');
  59. while (dollar != string::npos && dollar + 1 < str.length()) {
  60. size_t start = dollar + 1;
  61. if (str[start] == '$') {
  62. // A double dollar sign maps to a single dollar sign.
  63. result += str.substr(last, start - last);
  64. last = start + 1;
  65. } else {
  66. string varname;
  67. size_t end = start;
  68. if (str[start] == '{') {
  69. // Curly braces delimit the variable name explicitly.
  70. end = str.find('}', start + 1);
  71. if (end != string::npos) {
  72. varname = str.substr(start + 1, end - (start + 1));
  73. end++;
  74. }
  75. }
  76. if (end == start) {
  77. // Scan for the end of the variable name.
  78. while (end < str.length() && (isalnum(str[end]) || str[end] == '_')) {
  79. end++;
  80. }
  81. varname = str.substr(start, end - start);
  82. }
  83. string subst =
  84. result += str.substr(last, dollar - last);
  85. result += get_environment_variable(varname);
  86. last = end;
  87. }
  88. dollar = str.find('$', last);
  89. }
  90. result += str.substr(last);
  91. return result;
  92. }
  93. ////////////////////////////////////////////////////////////////////
  94. // Function: ExecutionEnviroment::get_cwd
  95. // Access: Public, Static
  96. // Description: Returns the name of the current working directory.
  97. ////////////////////////////////////////////////////////////////////
  98. Filename ExecutionEnvironment::
  99. get_cwd() {
  100. // getcwd() requires us to allocate a dynamic buffer and grow it on
  101. // demand.
  102. static size_t bufsize = 1024;
  103. static char *buffer = NULL;
  104. if (buffer == (char *)NULL) {
  105. buffer = new char[bufsize];
  106. }
  107. while (getcwd(buffer, bufsize) == (char *)NULL) {
  108. if (errno != ERANGE) {
  109. perror("getcwd");
  110. return string();
  111. }
  112. delete[] buffer;
  113. bufsize = bufsize * 2;
  114. buffer = new char[bufsize];
  115. assert(buffer != (char *)NULL);
  116. }
  117. return Filename::from_os_specific(buffer);
  118. }