src/generateiddfactory/GeneratorApplicationPathHelpers.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include <sstream> | ||
| 7 | #include <boost/algorithm/string/case_conv.hpp> | ||
| 8 | #include <boost/filesystem/operations.hpp> | ||
| 9 | #include <boost/filesystem/path.hpp> | ||
| 10 | #include <iostream> | ||
| 11 | |||
| 12 | #ifdef _WIN32 | ||
| 13 | # include <Windows.h> | ||
| 14 | #else | ||
| 15 | # include <dlfcn.h> | ||
| 16 | #endif | ||
| 17 | |||
| 18 | namespace openstudio { | ||
| 19 | |||
| 20 | using path = boost::filesystem::path; | ||
| 21 | |||
| 22 | ✗ | openstudio::path getApplicationOutputDirectory(const std::string& outdir) { | |
| 23 | ✗ | openstudio::path result(outdir); | |
| 24 | ✗ | std::stringstream ss; | |
| 25 | ✗ | std::stringstream tss; | |
| 26 | |||
| 27 | // turn relative path into absolute path | ||
| 28 | ✗ | result = boost::filesystem::system_complete(result); | |
| 29 | ✗ | std::string dirStr = result.string(); | |
| 30 | // create string used in error messages | ||
| 31 | ✗ | if (dirStr != outdir) { | |
| 32 | ✗ | tss << " (" << dirStr << ")"; | |
| 33 | } | ||
| 34 | |||
| 35 | // result cannot be a file | ||
| 36 | ✗ | if (boost::filesystem::is_regular_file(result)) { | |
| 37 | ✗ | ss << "Invalid outdir, " << outdir << tss.str() << ", is a file instead of a directory with a valid parent_path."; | |
| 38 | ✗ | throw std::runtime_error(ss.str().c_str()); | |
| 39 | } | ||
| 40 | |||
| 41 | // if result is not yet a directory | ||
| 42 | ✗ | if (!boost::filesystem::is_directory(result)) { | |
| 43 | // parent path must be an existing directory | ||
| 44 | ✗ | if (!boost::filesystem::is_directory(result.parent_path())) { | |
| 45 | ✗ | ss << "Invalid outdir, " << outdir << tss.str() << ", has a parent_path, " << result.parent_path().string() | |
| 46 | ✗ | << ", that is either invalid, or corresponds to a non-existent directory."; | |
| 47 | ✗ | throw std::runtime_error(ss.str().c_str()); | |
| 48 | } | ||
| 49 | // make the output directory, if possible | ||
| 50 | ✗ | std::cout << "Creating output directory " << dirStr << "." << '\n'; | |
| 51 | ✗ | bool ok = boost::filesystem::create_directory(result); | |
| 52 | ✗ | if (!ok) { | |
| 53 | ✗ | ss << "Unable to create outdir " << outdir << tss.str() << "."; | |
| 54 | ✗ | throw std::runtime_error(ss.str().c_str()); | |
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | ✗ | std::cout << '\n' << "All output will be directed to " << dirStr << "." << '\n' << '\n'; | |
| 59 | ✗ | return result; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | } // namespace openstudio | ||
| 63 |