Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3 : // Fork baseline commit: system-00-00-20 (2020-09-25).
4 : // Renamed since fork: yes (from src/Path.cxx to src/Path.cpp).
5 :
6 : /*
7 : * Path.cxx
8 : * Test
9 : *
10 : * Created by Matthias Wiesmann on 09.02.05.
11 : * Copyright 2005 CERN. All rights reserved.
12 : *
13 : */
14 : #include <iostream>
15 : #include <sstream>
16 :
17 : #include "okssystem/Path.hpp"
18 : #include "okssystem/exceptions.hpp"
19 :
20 : const char OksSystem::Path::PATH_SEPARATOR = ':';
21 :
22 : /** Empty constructor
23 : */
24 :
25 0 : OksSystem::Path::Path() {}
26 :
27 : /** Copy constructor
28 : * \param other the original object
29 : */
30 :
31 0 : OksSystem::Path::Path(const Path& other)
32 : {
33 0 : m_directories = other.m_directories;
34 0 : }
35 :
36 : /** Constructor
37 : * \param path_list string containing the list of directories
38 : */
39 :
40 0 : OksSystem::Path::Path(const std::string& path_list)
41 : {
42 0 : parse_path_list(path_list);
43 0 : } // Path
44 :
45 0 : OksSystem::Path::operator std::string() const
46 : {
47 0 : return to_string();
48 : } // operator std::string()
49 :
50 : /** Add a directory to the path
51 : * \param dir the file to add
52 : */
53 :
54 : void
55 0 : OksSystem::Path::add(const OksSystem::File& dir)
56 : {
57 0 : m_directories.push_back(dir);
58 0 : } // add
59 :
60 : /** Parses a string containing the path separated by semi-colons.
61 : * \param path_list the path
62 : */
63 :
64 : void
65 0 : OksSystem::Path::parse_path_list(const std::string& path_list)
66 : {
67 0 : std::string rest = path_list;
68 0 : while (!rest.empty()) {
69 0 : std::string::size_type semi_colon = rest.find(PATH_SEPARATOR);
70 0 : std::string name;
71 :
72 0 : if (semi_colon == std::string::npos) {
73 0 : name = rest;
74 0 : rest.clear();
75 : } else {
76 0 : name = rest.substr(0, semi_colon);
77 0 : rest = rest.substr(semi_colon + 1);
78 : }
79 :
80 0 : if (!name.empty()) {
81 0 : const OksSystem::File directory(name);
82 0 : add(directory);
83 0 : }
84 0 : } // while
85 0 : } // parse_path_list
86 :
87 : /** Prints the path into a stream
88 : * \param stream destination stream
89 : */
90 :
91 : void
92 0 : OksSystem::Path::write_to(std::ostream& stream) const
93 : {
94 0 : bool first = true;
95 0 : for (File::file_list_t::const_iterator pos = m_directories.begin(); pos != m_directories.end(); pos++) {
96 0 : if (!first) {
97 0 : stream << PATH_SEPARATOR;
98 : } else {
99 : first = false;
100 : }
101 0 : stream << pos->full_name();
102 : } // for
103 0 : } // write_to
104 :
105 : /** Prints the path into a string
106 : * \return string containing description
107 : * \see write_to()
108 : */
109 :
110 : std::string
111 0 : OksSystem::Path::to_string() const
112 : {
113 0 : std::ostringstream stream;
114 0 : write_to(stream);
115 0 : return stream.str();
116 0 : } // to_string
117 :
118 : /** Finds the first occurence of a named file in the path.
119 : * \param name the name of the file to search for
120 : * \return a File object representing the file
121 : * \exception EntityNotFoundIssue if no matching file is found
122 : */
123 :
124 : OksSystem::File
125 0 : OksSystem::Path::which(const std::string& name) const
126 : {
127 0 : for (File::file_list_t::const_iterator pos = m_directories.begin(); pos != m_directories.end(); pos++) {
128 0 : File child = pos->child(name);
129 0 : if (child.exists())
130 0 : return child;
131 0 : } // for
132 0 : throw OksSystem::NotFoundIssue(ERS_HERE, name.c_str());
133 : } // which
134 :
135 : /** STL output operator
136 : * \param stream destination stream
137 : * \param path the path to print
138 : */
139 :
140 : std::ostream&
141 0 : operator<<(std::ostream& stream, const OksSystem::Path& path)
142 : {
143 0 : path.write_to(stream);
144 0 : return stream;
145 : } // operator<<
|