executionEnvironment.cxx 3.8 KB

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