| 74 | | char tmppath[1024]; |
|---|
| 75 | | std::memset(tmppath, 0, sizeof(tmppath)); |
|---|
| 76 | | GetModuleFileName(module, tmppath, sizeof(tmppath)); |
|---|
| 77 | | |
|---|
| 78 | | const fs::path dll_path(tmppath, fs::native); |
|---|
| 79 | | fs::path base_dir = dll_path.branch_path().branch_path(); |
|---|
| | 82 | fs::path base_dir; |
|---|
| | 83 | |
|---|
| | 84 | // If SNX_BASE_DIR is not set, look up the path to this DLL and |
|---|
| | 85 | // use it to provide a default setting for that environment |
|---|
| | 86 | // variable. |
|---|
| | 87 | if ( NULL == env_dir ) |
|---|
| | 88 | { |
|---|
| | 89 | char tmppath[1024]; |
|---|
| | 90 | std::memset(tmppath, 0, sizeof(tmppath)); |
|---|
| | 91 | GetModuleFileName(module, tmppath, sizeof(tmppath)); |
|---|
| | 92 | |
|---|
| | 93 | const fs::path dll_path(tmppath, fs::native); |
|---|
| | 94 | base_dir = dll_path.branch_path().branch_path(); |
|---|
| 166 | | Dl_info info; |
|---|
| 167 | | info.dli_fname = 0; |
|---|
| 168 | | const int result = dladdr(reinterpret_cast<const void*>(&snxLibraryInit), |
|---|
| 169 | | &info); |
|---|
| 170 | | |
|---|
| 171 | | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| 172 | | if ( 0 != result ) |
|---|
| 173 | | { |
|---|
| 174 | | try |
|---|
| | 183 | fs::path base_dir; |
|---|
| | 184 | const char* env_dir = std::getenv("SNX_BASE_DIR"); |
|---|
| | 185 | |
|---|
| | 186 | // If SNX_BASE_DIR is not set, look up the path to this shared library and |
|---|
| | 187 | // use it to provide a default setting for that environment variable. |
|---|
| | 188 | if ( NULL == env_dir ) |
|---|
| | 189 | { |
|---|
| | 190 | Dl_info info; |
|---|
| | 191 | info.dli_fname = 0; |
|---|
| | 192 | const int result = |
|---|
| | 193 | dladdr(reinterpret_cast<const void*>(&snxLibraryInit), &info); |
|---|
| | 194 | |
|---|
| | 195 | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| | 196 | if ( 0 != result ) |
|---|
| 183 | | const std::string bit_suffix("64"); |
|---|
| 184 | | #else |
|---|
| 185 | | const std::string bit_suffix(""); |
|---|
| 186 | | #endif |
|---|
| 187 | | |
|---|
| 188 | | // Get the directory containing this shared library. |
|---|
| 189 | | const fs::path lib_path = lib_file.branch_path(); |
|---|
| 190 | | |
|---|
| 191 | | // Start the search for the root of the Sonix installation in the |
|---|
| 192 | | // parent of the directory containing this shared library. |
|---|
| 193 | | fs::path base_dir = lib_path.branch_path(); |
|---|
| 194 | | |
|---|
| 195 | | // Use the lib subdirectory to figure out when we have found the root |
|---|
| 196 | | // of the Sonix installation tree. |
|---|
| 197 | | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| 198 | | |
|---|
| 199 | | bool found(false); |
|---|
| 200 | | while ( ! found && ! base_dir.empty() ) |
|---|
| | 207 | const std::string bit_suffix("64"); |
|---|
| | 208 | #else |
|---|
| | 209 | const std::string bit_suffix(""); |
|---|
| | 210 | #endif |
|---|
| | 211 | |
|---|
| | 212 | // Get the directory containing this shared library. |
|---|
| | 213 | const fs::path lib_path = lib_file.branch_path(); |
|---|
| | 214 | |
|---|
| | 215 | // Start the search for the root of the Sonix installation in the |
|---|
| | 216 | // parent of the directory containing this shared library. |
|---|
| | 217 | base_dir = lib_path.branch_path(); |
|---|
| | 218 | |
|---|
| | 219 | // Use the lib subdirectory to figure out when we have found the |
|---|
| | 220 | // root of the Sonix installation tree. |
|---|
| | 221 | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| | 222 | |
|---|
| | 223 | bool found(false); |
|---|
| | 224 | while ( ! found && ! base_dir.empty() ) |
|---|
| | 225 | { |
|---|
| | 226 | try |
|---|
| | 227 | { |
|---|
| | 228 | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| | 229 | { |
|---|
| | 230 | base_dir = base_dir.branch_path(); |
|---|
| | 231 | } |
|---|
| | 232 | else |
|---|
| | 233 | { |
|---|
| | 234 | found = true; |
|---|
| | 235 | } |
|---|
| | 236 | } |
|---|
| | 237 | catch (fs::filesystem_error&) |
|---|
| | 238 | { |
|---|
| | 239 | base_dir = base_dir.branch_path(); |
|---|
| | 240 | } |
|---|
| | 241 | } |
|---|
| | 242 | |
|---|
| | 243 | if ( found ) |
|---|
| | 244 | { |
|---|
| | 245 | setenv("SNX_BASE_DIR", |
|---|
| | 246 | base_dir.native_directory_string().c_str(), 1); |
|---|
| | 247 | } |
|---|
| | 248 | } |
|---|
| | 249 | catch (fs::filesystem_error& ex) |
|---|
| 202 | | try |
|---|
| 203 | | { |
|---|
| 204 | | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| 205 | | { |
|---|
| 206 | | base_dir = base_dir.branch_path(); |
|---|
| 207 | | } |
|---|
| 208 | | else |
|---|
| 209 | | { |
|---|
| 210 | | found = true; |
|---|
| 211 | | } |
|---|
| 212 | | } |
|---|
| 213 | | catch (fs::filesystem_error&) |
|---|
| 214 | | { |
|---|
| 215 | | base_dir = base_dir.branch_path(); |
|---|
| 216 | | } |
|---|
| 217 | | } |
|---|
| 218 | | |
|---|
| 219 | | if ( found ) |
|---|
| 220 | | { |
|---|
| 221 | | const fs::path data_dir = base_dir / SNX_SHARE_DIR; |
|---|
| 222 | | |
|---|
| 223 | | // We use the overwrite value of 0 as a way around testing whether |
|---|
| 224 | | // the environment variables are already set. |
|---|
| 225 | | setenv("SNX_BASE_DIR", base_dir.native_directory_string().c_str(), |
|---|
| 226 | | 0); |
|---|
| 227 | | setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), |
|---|
| 228 | | 0); |
|---|
| | 251 | std::cerr << "Automatic assignment of SNX_BASE_DIR failed:\n" |
|---|
| | 252 | << ex.what() << std::endl; |
|---|
| 231 | | catch (fs::filesystem_error& ex) |
|---|
| 232 | | { |
|---|
| 233 | | std::cerr << "Automatic assignment of Sonix environment " |
|---|
| 234 | | << "variables failed:\n" << ex.what() << std::endl; |
|---|
| 235 | | } |
|---|
| | 255 | } |
|---|
| | 256 | else |
|---|
| | 257 | { |
|---|
| | 258 | base_dir = fs::path(env_dir, fs::native); |
|---|
| | 259 | } |
|---|
| | 260 | |
|---|
| | 261 | if ( ! base_dir.empty() ) |
|---|
| | 262 | { |
|---|
| | 263 | // If base_dir were empty, this would result in data_dir being relative |
|---|
| | 264 | // to the current working directory. |
|---|
| | 265 | const fs::path data_dir = base_dir / SNX_SHARE_DIR; |
|---|
| | 266 | |
|---|
| | 267 | // We use the overwrite value of 0 as a way around testing whether the |
|---|
| | 268 | // environment variable is already set. |
|---|
| | 269 | setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), 0); |
|---|