1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2014 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <cstring> |
---|
9 | #include <cstdio> |
---|
10 | #include <cerrno> |
---|
11 | |
---|
12 | #include <string> |
---|
13 | |
---|
14 | #include <osgDB/FileUtils> |
---|
15 | #include <osgDB/FileNameUtils> |
---|
16 | |
---|
17 | #include "FileUtil.h" |
---|
18 | #include "Trace.h" |
---|
19 | |
---|
20 | using namespace GeoVis; |
---|
21 | |
---|
22 | void DirectoryVisitor::traverse(const std::string& path) |
---|
23 | { |
---|
24 | if (osgDB::fileType(path) == osgDB::DIRECTORY) { |
---|
25 | if (handleDir(path)) { |
---|
26 | osgDB::DirectoryContents files = osgDB::getDirectoryContents(path); |
---|
27 | for (osgDB::DirectoryContents::const_iterator f = files.begin(); f != files.end(); ++f) { |
---|
28 | if (f->compare(".") == 0 || f->compare("..") == 0) |
---|
29 | continue; |
---|
30 | |
---|
31 | std::string filepath = osgDB::concatPaths(path, *f); |
---|
32 | traverse(filepath); |
---|
33 | } |
---|
34 | } |
---|
35 | handlePostDir(path); |
---|
36 | } else if (osgDB::fileType(path) == osgDB::REGULAR_FILE) { |
---|
37 | handleFile(path); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | void GeoVis::removeDirectory(const char *path) |
---|
42 | { |
---|
43 | TRACE("%s", path); |
---|
44 | |
---|
45 | CollectFilesVisitor cfv; |
---|
46 | cfv.traverse(std::string(path)); |
---|
47 | for (std::vector<std::string>::const_iterator itr = cfv.filenames.begin(); |
---|
48 | itr != cfv.filenames.end(); ++itr) { |
---|
49 | if (remove(itr->c_str()) < 0) { |
---|
50 | ERROR("remove: %s", strerror(errno)); |
---|
51 | } |
---|
52 | } |
---|
53 | for (std::vector<std::string>::const_iterator itr = cfv.dirnames.begin(); |
---|
54 | itr != cfv.dirnames.end(); ++itr) { |
---|
55 | if (remove(itr->c_str()) < 0) { |
---|
56 | ERROR("remove: %s", strerror(errno)); |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * \brief Get the file path part of a local:// URL |
---|
63 | * |
---|
64 | * This implementation assumes a relative path if there is no leading slash, |
---|
65 | * rather than assuming there is a server name. |
---|
66 | */ |
---|
67 | std::string GeoVis::getLocalFilePath(const std::string& url) |
---|
68 | { |
---|
69 | std::string::size_type pos(url.find("://")); |
---|
70 | |
---|
71 | if (pos != std::string::npos) { |
---|
72 | return url.substr(pos+3, std::string::npos); |
---|
73 | } |
---|
74 | return url; |
---|
75 | } |
---|