src/utilities/core/FileReference.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 "FileReference.hpp" | ||
| 7 | |||
| 8 | #include "Assert.hpp" | ||
| 9 | #include "Checksum.hpp" | ||
| 10 | #include "PathHelpers.hpp" | ||
| 11 | #include "../core/FilesystemHelpers.hpp" | ||
| 12 | |||
| 13 | namespace openstudio { | ||
| 14 | |||
| 15 | 4 | FileReference::FileReference(const openstudio::path& p) | |
| 16 | 4 | : m_uuid(createUUID()), | |
| 17 | 4 | m_versionUUID(createUUID()), | |
| 18 | 4 | m_name(toString(p)), | |
| 19 |
1/1✓ Branch 1 taken 4 times.
|
4 | m_displayName(toString(p.filename())), |
| 20 |
1/1✓ Branch 1 taken 4 times.
|
4 | m_path(completeAndNormalize(p)), |
| 21 |
1/1✓ Branch 1 taken 4 times.
|
4 | m_timestampLast(), |
| 22 |
1/1✓ Branch 1 taken 4 times.
|
4 | m_checksumCreate(checksum(m_path)), |
| 23 |
2/2✓ Branch 3 taken 4 times.
✓ Branch 6 taken 4 times.
|
16 | m_checksumLast(m_checksumCreate) { |
| 24 | try { | ||
| 25 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | m_fileType = FileReferenceType(getFileExtension(p)); |
| 26 | ✗ | } catch (...) { | |
| 27 | ✗ | m_fileType = FileReferenceType::Unknown; | |
| 28 | ✗ | } | |
| 29 |
1/1✓ Branch 2 taken 4 times.
|
4 | update(openstudio::path()); |
| 30 | 4 | } | |
| 31 | |||
| 32 | /** De-serialization constructor. Not for general use. */ | ||
| 33 | ✗ | FileReference::FileReference(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name, | |
| 34 | const std::string& displayName, const std::string& description, const openstudio::path& p, | ||
| 35 | const FileReferenceType& fileType, const DateTime& timestampLast, const std::string& checksumCreate, | ||
| 36 | ✗ | const std::string& checksumLast) | |
| 37 | ✗ | : m_uuid(uuid), | |
| 38 | ✗ | m_versionUUID(versionUUID), | |
| 39 | ✗ | m_name(name), | |
| 40 | ✗ | m_displayName(displayName), | |
| 41 | ✗ | m_description(description), | |
| 42 | ✗ | m_path(p), | |
| 43 | ✗ | m_fileType(fileType), | |
| 44 | ✗ | m_timestampLast(timestampLast), | |
| 45 | ✗ | m_checksumCreate(checksumCreate), | |
| 46 | ✗ | m_checksumLast(checksumLast) {} | |
| 47 | |||
| 48 | ✗ | FileReference FileReference::clone() const { | |
| 49 | ✗ | FileReference result(*this); | |
| 50 | ✗ | result.m_uuid = createUUID(); | |
| 51 | ✗ | result.m_versionUUID = createUUID(); | |
| 52 | ✗ | return result; | |
| 53 | ✗ | } | |
| 54 | |||
| 55 | ✗ | openstudio::UUID FileReference::uuid() const { | |
| 56 | ✗ | return m_uuid; | |
| 57 | } | ||
| 58 | |||
| 59 | ✗ | openstudio::UUID FileReference::versionUUID() const { | |
| 60 | ✗ | return m_versionUUID; | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | std::string FileReference::name() const { | |
| 64 | ✗ | return m_name; | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | std::string FileReference::displayName() const { | |
| 68 | ✗ | return m_displayName; | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | std::string FileReference::description() const { | |
| 72 | ✗ | return m_description; | |
| 73 | } | ||
| 74 | |||
| 75 | 8 | openstudio::path FileReference::path() const { | |
| 76 | 8 | return m_path; | |
| 77 | } | ||
| 78 | |||
| 79 | 4 | FileReferenceType FileReference::fileType() const { | |
| 80 | 4 | return m_fileType; | |
| 81 | } | ||
| 82 | |||
| 83 | ✗ | DateTime FileReference::timestampLast() const { | |
| 84 | ✗ | return m_timestampLast; | |
| 85 | } | ||
| 86 | |||
| 87 | ✗ | std::string FileReference::checksumCreate() const { | |
| 88 | ✗ | return m_checksumCreate; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | std::string FileReference::checksumLast() const { | |
| 92 | ✗ | return m_checksumLast; | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | void FileReference::setName(const std::string& newName) { | |
| 96 | ✗ | m_name = newName; | |
| 97 | ✗ | m_versionUUID = createUUID(); | |
| 98 | ✗ | } | |
| 99 | |||
| 100 | ✗ | void FileReference::setDisplayName(const std::string& newDisplayName) { | |
| 101 | ✗ | m_displayName = newDisplayName; | |
| 102 | ✗ | m_versionUUID = createUUID(); | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | ✗ | void FileReference::setDescription(const std::string& newDescription) { | |
| 106 | ✗ | m_description = newDescription; | |
| 107 | ✗ | m_versionUUID = createUUID(); | |
| 108 | ✗ | } | |
| 109 | |||
| 110 | ✗ | void FileReference::setPath(const openstudio::path& newPath) { | |
| 111 | ✗ | if (newPath != m_path) { | |
| 112 | ✗ | m_path = newPath; | |
| 113 | ✗ | m_versionUUID = createUUID(); | |
| 114 | } | ||
| 115 | ✗ | } | |
| 116 | |||
| 117 | 4 | bool FileReference::makePathAbsolute(const openstudio::path& searchDirectory) { | |
| 118 | // trivial completion | ||
| 119 |
1/1✓ Branch 1 taken 4 times.
|
4 | openstudio::path currentPath = path(); |
| 120 |
7/8✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 1 time.
|
4 | if (currentPath.is_absolute() && openstudio::filesystem::exists(currentPath)) { |
| 121 | 3 | return true; | |
| 122 | } | ||
| 123 |
1/1✓ Branch 1 taken 1 time.
|
1 | openstudio::path workingPath(currentPath); |
| 124 | // if currentPath is complete but does not exist, go to extreme measures | ||
| 125 |
2/3✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
|
1 | if (currentPath.is_absolute()) { |
| 126 | 1 | workingPath = currentPath.filename(); | |
| 127 | } | ||
| 128 |
1/2✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
|
1 | if (searchDirectory.empty()) { |
| 129 | 1 | return false; | |
| 130 | } | ||
| 131 | ✗ | openstudio::path newPath = openstudio::filesystem::absolute(workingPath, searchDirectory); | |
| 132 | ✗ | if (newPath.empty() || !openstudio::filesystem::exists(newPath)) { | |
| 133 | ✗ | return false; | |
| 134 | } | ||
| 135 | ✗ | m_path = completeAndNormalize(newPath); | |
| 136 | ✗ | m_versionUUID = createUUID(); | |
| 137 | ✗ | return true; | |
| 138 | 4 | } | |
| 139 | |||
| 140 | ✗ | bool FileReference::makePathRelative(const openstudio::path& basePath) { | |
| 141 | ✗ | openstudio::path newPath; | |
| 142 | ✗ | if (basePath.empty()) { | |
| 143 | ✗ | newPath = path().filename(); | |
| 144 | } else { | ||
| 145 | ✗ | newPath = relativePath(path(), basePath); | |
| 146 | } | ||
| 147 | ✗ | if (newPath.empty()) { | |
| 148 | ✗ | return false; | |
| 149 | } | ||
| 150 | ✗ | m_path = newPath; | |
| 151 | ✗ | m_versionUUID = createUUID(); | |
| 152 | ✗ | return true; | |
| 153 | ✗ | } | |
| 154 | |||
| 155 | 4 | bool FileReference::update(const openstudio::path& searchDirectory) { | |
| 156 |
1/1✓ Branch 1 taken 4 times.
|
4 | makePathAbsolute(searchDirectory); |
| 157 |
1/1✓ Branch 1 taken 4 times.
|
4 | openstudio::path p = path(); |
| 158 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 time.
|
4 | if (openstudio::filesystem::exists(p)) { |
| 159 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | m_timestampLast = DateTime::fromEpoch(openstudio::filesystem::last_write_time_as_time_t(p)); |
| 160 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_checksumLast = checksum(p); |
| 161 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_versionUUID = createUUID(); |
| 162 | 3 | return true; | |
| 163 | } | ||
| 164 | 1 | return false; | |
| 165 | 4 | } | |
| 166 | |||
| 167 | } // namespace openstudio | ||
| 168 |