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