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 <sstream>
15 : #include <iostream>
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 0 : m_directories = other.m_directories;
33 0 : }
34 :
35 : /** Constructor
36 : * \param path_list string containing the list of directories
37 : */
38 :
39 0 : OksSystem::Path::Path(const std::string &path_list) {
40 0 : parse_path_list(path_list);
41 0 : } // Path
42 :
43 0 : OksSystem::Path::operator std::string() const {
44 0 : return to_string();
45 : } // operator std::string()
46 :
47 :
48 :
49 : /** Add a directory to the path
50 : * \param dir the file to add
51 : */
52 :
53 0 : void OksSystem::Path::add(const OksSystem::File &dir) {
54 0 : m_directories.push_back(dir);
55 0 : } // add
56 :
57 :
58 : /** Parses a string containing the path separated by semi-colons.
59 : * \param path_list the path
60 : */
61 :
62 0 : void OksSystem::Path::parse_path_list(const std::string &path_list) {
63 0 : std::string rest = path_list;
64 0 : while(! rest.empty()) {
65 0 : std::string::size_type semi_colon = rest.find(PATH_SEPARATOR);
66 0 : std::string name;
67 :
68 0 : if(semi_colon==std::string::npos) {
69 0 : name = rest;
70 0 : rest.clear();
71 : } else {
72 0 : name = rest.substr(0,semi_colon);
73 0 : rest = rest.substr(semi_colon+1);
74 : }
75 :
76 0 : if(!name.empty()) {
77 0 : const OksSystem::File directory(name);
78 0 : add(directory);
79 0 : }
80 0 : } // while
81 0 : } // parse_path_list
82 :
83 : /** Prints the path into a stream
84 : * \param stream destination stream
85 : */
86 :
87 0 : void OksSystem::Path::write_to(std::ostream &stream) const {
88 0 : bool first = true;
89 0 : for(File::file_list_t::const_iterator pos = m_directories.begin(); pos!=m_directories.end();pos++) {
90 0 : if (!first) {
91 0 : stream << PATH_SEPARATOR;
92 : } else {
93 : first = false;
94 : }
95 0 : stream << pos->full_name();
96 : } // for
97 0 : } // write_to
98 :
99 : /** Prints the path into a string
100 : * \return string containing description
101 : * \see write_to()
102 : */
103 :
104 0 : std::string OksSystem::Path::to_string() const {
105 0 : std::ostringstream stream;
106 0 : write_to(stream);
107 0 : return stream.str();
108 0 : } // to_string
109 :
110 : /** Finds the first occurence of a named file in the path.
111 : * \param name the name of the file to search for
112 : * \return a File object representing the file
113 : * \exception EntityNotFoundIssue if no matching file is found
114 : */
115 :
116 0 : OksSystem::File OksSystem::Path::which(const std::string &name) const {
117 0 : for(File::file_list_t::const_iterator pos = m_directories.begin(); pos!=m_directories.end();pos++) {
118 0 : File child = pos->child(name);
119 0 : if (child.exists()) return child;
120 0 : } // for
121 0 : throw OksSystem::NotFoundIssue( ERS_HERE, name.c_str() );
122 : } // which
123 :
124 : /** STL output operator
125 : * \param stream destination stream
126 : * \param path the path to print
127 : */
128 :
129 0 : std::ostream& operator<<(std::ostream& stream, const OksSystem::Path& path) {
130 0 : path.write_to(stream);
131 0 : return stream;
132 : } // operator<<
133 :
134 :
|