| 65 | | char tmppath[1024]; |
|---|
| 66 | | std::memset(tmppath, 0, sizeof(tmppath)); |
|---|
| 67 | | GetModuleFileName(module, tmppath, sizeof(tmppath)); |
|---|
| 68 | | |
|---|
| 69 | | const fs::path dll_path(tmppath, fs::native); |
|---|
| 70 | | fs::path base_dir = dll_path.branch_path().branch_path(); |
|---|
| | 73 | fs::path base_dir; |
|---|
| | 74 | |
|---|
| | 75 | // If VJ_BASE_DIR is not set, look up the path to this DLL and |
|---|
| | 76 | // use it to provide a default setting for that environment |
|---|
| | 77 | // variable. |
|---|
| | 78 | if ( NULL == env_dir ) |
|---|
| | 79 | { |
|---|
| | 80 | char tmppath[1024]; |
|---|
| | 81 | std::memset(tmppath, 0, sizeof(tmppath)); |
|---|
| | 82 | GetModuleFileName(module, tmppath, sizeof(tmppath)); |
|---|
| | 83 | |
|---|
| | 84 | const fs::path dll_path(tmppath, fs::native); |
|---|
| | 85 | base_dir = dll_path.branch_path().branch_path(); |
|---|
| 157 | | Dl_info info; |
|---|
| 158 | | info.dli_fname = 0; |
|---|
| 159 | | const int result = dladdr(reinterpret_cast<const void*>(&vrjLibraryInit), |
|---|
| 160 | | &info); |
|---|
| 161 | | |
|---|
| 162 | | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| 163 | | if ( 0 != result ) |
|---|
| 164 | | { |
|---|
| 165 | | try |
|---|
| | 174 | fs::path base_dir; |
|---|
| | 175 | const char* env_dir = std::getenv("VJ_BASE_DIR"); |
|---|
| | 176 | |
|---|
| | 177 | // If VJ_BASE_DIR is not set, look up the path to this shared library and |
|---|
| | 178 | // use it to provide a default setting for that environment variable. |
|---|
| | 179 | if ( NULL == env_dir ) |
|---|
| | 180 | { |
|---|
| | 181 | Dl_info info; |
|---|
| | 182 | info.dli_fname = 0; |
|---|
| | 183 | const int result = |
|---|
| | 184 | dladdr(reinterpret_cast<const void*>(&vrjLibraryInit), &info); |
|---|
| | 185 | |
|---|
| | 186 | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| | 187 | if ( 0 != result ) |
|---|
| 174 | | const std::string bit_suffix("64"); |
|---|
| 175 | | #else |
|---|
| 176 | | const std::string bit_suffix(""); |
|---|
| 177 | | #endif |
|---|
| 178 | | |
|---|
| 179 | | // Get the directory containing this shared library. |
|---|
| 180 | | const fs::path lib_path = lib_file.branch_path(); |
|---|
| 181 | | |
|---|
| 182 | | // Start the search for the root of the VR Juggler installation in |
|---|
| 183 | | // the parent of the directory containing this shared library. |
|---|
| 184 | | fs::path base_dir = lib_path.branch_path(); |
|---|
| 185 | | |
|---|
| 186 | | // Use the lib subdirectory to figure out when we have found the root |
|---|
| 187 | | // of the VR Juggler installation tree. |
|---|
| 188 | | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| 189 | | |
|---|
| 190 | | bool found(false); |
|---|
| 191 | | while ( ! found && ! base_dir.empty() ) |
|---|
| | 198 | const std::string bit_suffix("64"); |
|---|
| | 199 | #else |
|---|
| | 200 | const std::string bit_suffix(""); |
|---|
| | 201 | #endif |
|---|
| | 202 | |
|---|
| | 203 | // Get the directory containing this shared library. |
|---|
| | 204 | const fs::path lib_path = lib_file.branch_path(); |
|---|
| | 205 | |
|---|
| | 206 | // Start the search for the root of the VR Juggler installation in |
|---|
| | 207 | // the parent of the directory containing this shared library. |
|---|
| | 208 | base_dir = lib_path.branch_path(); |
|---|
| | 209 | |
|---|
| | 210 | // Use the lib subdirectory to figure out when we have found the |
|---|
| | 211 | // root of the VR Juggler installation tree. |
|---|
| | 212 | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| | 213 | |
|---|
| | 214 | bool found(false); |
|---|
| | 215 | while ( ! found && ! base_dir.empty() ) |
|---|
| | 216 | { |
|---|
| | 217 | try |
|---|
| | 218 | { |
|---|
| | 219 | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| | 220 | { |
|---|
| | 221 | base_dir = base_dir.branch_path(); |
|---|
| | 222 | } |
|---|
| | 223 | else |
|---|
| | 224 | { |
|---|
| | 225 | found = true; |
|---|
| | 226 | } |
|---|
| | 227 | } |
|---|
| | 228 | catch (fs::filesystem_error&) |
|---|
| | 229 | { |
|---|
| | 230 | base_dir = base_dir.branch_path(); |
|---|
| | 231 | } |
|---|
| | 232 | } |
|---|
| | 233 | |
|---|
| | 234 | if ( found ) |
|---|
| | 235 | { |
|---|
| | 236 | setenv("VJ_BASE_DIR", |
|---|
| | 237 | base_dir.native_directory_string().c_str(), 1); |
|---|
| | 238 | } |
|---|
| | 239 | } |
|---|
| | 240 | catch (fs::filesystem_error& ex) |
|---|
| 193 | | try |
|---|
| 194 | | { |
|---|
| 195 | | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| 196 | | { |
|---|
| 197 | | base_dir = base_dir.branch_path(); |
|---|
| 198 | | } |
|---|
| 199 | | else |
|---|
| 200 | | { |
|---|
| 201 | | found = true; |
|---|
| 202 | | } |
|---|
| 203 | | } |
|---|
| 204 | | catch (fs::filesystem_error&) |
|---|
| 205 | | { |
|---|
| 206 | | base_dir = base_dir.branch_path(); |
|---|
| 207 | | } |
|---|
| 208 | | } |
|---|
| 209 | | |
|---|
| 210 | | if ( found ) |
|---|
| 211 | | { |
|---|
| 212 | | const fs::path data_dir = base_dir / VJ_SHARE_DIR; |
|---|
| 213 | | |
|---|
| 214 | | // We use the overwrite value of 0 as a way around testing whether |
|---|
| 215 | | // the environment variables are already set. |
|---|
| 216 | | setenv("VJ_BASE_DIR", base_dir.native_directory_string().c_str(), |
|---|
| 217 | | 0); |
|---|
| 218 | | setenv("VJ_DATA_DIR", data_dir.native_directory_string().c_str(), |
|---|
| 219 | | 0); |
|---|
| | 242 | std::cerr << "Automatic assignment of VJ_BASE_DIR failed:\n" |
|---|
| | 243 | << ex.what() << std::endl; |
|---|
| 222 | | catch (fs::filesystem_error& ex) |
|---|
| 223 | | { |
|---|
| 224 | | std::cerr << "Automatic assignment of VR Juggler environment " |
|---|
| 225 | | << "variables failed:\n" << ex.what() << std::endl; |
|---|
| 226 | | } |
|---|
| | 246 | } |
|---|
| | 247 | else |
|---|
| | 248 | { |
|---|
| | 249 | base_dir = fs::path(env_dir, fs::native); |
|---|
| | 250 | } |
|---|
| | 251 | |
|---|
| | 252 | if ( ! base_dir.empty() ) |
|---|
| | 253 | { |
|---|
| | 254 | // If base_dir were empty, this would result in data_dir being relative |
|---|
| | 255 | // to the current working directory. |
|---|
| | 256 | const fs::path data_dir = base_dir / VJ_SHARE_DIR; |
|---|
| | 257 | |
|---|
| | 258 | // We use the overwrite value of 0 as a way around testing whether the |
|---|
| | 259 | // environment variable is already set. |
|---|
| | 260 | setenv("VJ_DATA_DIR", data_dir.native_directory_string().c_str(), 0); |
|---|