MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / ExternalFile

Method ExternalFile

src/model/ExternalFile.cpp:216–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

214 }
215
216 ExternalFile::ExternalFile(const Model& model, const std::string& filename, bool copyFile) : ResourceObject(ExternalFile::iddObjectType(), model) {
217 OS_ASSERT(getImpl<detail::ExternalFile_Impl>());
218
219 WorkflowJSON workflow = model.workflowJSON();
220
221 // we expect all strings to be UTF-8 encoded
222 path p = toPath(filename);
223 if (!exists(p)) {
224 boost::optional<path> op = workflow.findFile(filename);
225 if (!op) {
226 this->remove();
227 LOG_AND_THROW("Cannot find file \"" << filename << "\" for " << this->briefDescription());
228 }
229 p = op.get();
230 }
231 OS_ASSERT(exists(p));
232
233 bool ok;
234 if (copyFile) {
235 path destDir;
236 std::vector<path> absoluteFilePaths = workflow.absoluteFilePaths();
237 if (absoluteFilePaths.empty()) {
238 destDir = workflow.absoluteRootDir();
239 } else {
240 destDir = absoluteFilePaths[0];
241 }
242 path dest = destDir / p.filename();
243
244 if (exists(dest)) {
245 if (checksum(p) != checksum(dest)) {
246 this->remove();
247 LOG_AND_THROW("File \"" << p.filename() << "\" already exists in \"" << destDir << "\"");
248 }
249 } else {
250
251 try {
252 makeParentFolder(dest, path(), true);
253 } catch (std::exception&) {
254 this->remove();
255 LOG_AND_THROW("Failed to created parent folder at \"" << dest << "\"");
256 }
257 try {
258 boost::filesystem::copy(p, dest);
259 } catch (std::exception&) {
260 this->remove();
261 LOG_AND_THROW("Failed to copy file from \"" << p << "\" to \"" << dest << "\"");
262 }
263 }
264 OS_ASSERT(exists(dest));
265
266 ok = setFileName(toString(dest.filename()));
267 OS_ASSERT(ok);
268 } else {
269 ok = setFileName(filename);
270 OS_ASSERT(ok);
271 }
272 }
273

Callers

nothing calls this directly

Calls 14

makeParentFolderFunction · 0.85
workflowJSONMethod · 0.80
findFileMethod · 0.80
briefDescriptionMethod · 0.80
absoluteFilePathsMethod · 0.80
absoluteRootDirMethod · 0.80
filenameMethod · 0.80
iddObjectTypeFunction · 0.70
toPathFunction · 0.50
checksumFunction · 0.50
toStringFunction · 0.50
removeMethod · 0.45

Tested by

no test coverage detected