1 /* This file is part of the Pangolin Project.
2  * http://github.com/stevenlovegrove/Pangolin
3  *
4  * Copyright (c) 2013 Steven Lovegrove
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #pragma once
29 
30 #include <pangolin/platform.h>
31 
32 #include <string>
33 #include <vector>
34 #include <algorithm>
35 
36 namespace pangolin
37 {
38 
39 PANGOLIN_EXPORT
40 std::vector<std::string>& Split(const std::string& s, char delim, std::vector<std::string>& elements);
41 
42 PANGOLIN_EXPORT
43 std::vector<std::string> Split(const std::string &s, char delim);
44 
45 PANGOLIN_EXPORT
46 std::vector<std::string> Expand(const std::string &s, char open='[', char close=']', char delim=',');
47 
48 PANGOLIN_EXPORT
49 std::string SanitizePath(const std::string& path);
50 
51 PANGOLIN_EXPORT
52 std::string PathParent(const std::string& path, int levels = 1);
53 
54 PANGOLIN_EXPORT
55 bool FileExists(const std::string& filename);
56 
57 PANGOLIN_EXPORT
58 std::string FindPath(const std::string& child_path, const std::string& signature_path);
59 
60 PANGOLIN_EXPORT
61 std::string PathExpand(const std::string& sPath);
62 
63 PANGOLIN_EXPORT
64 bool MatchesWildcard(const std::string& str, const std::string& wildcard);
65 
66 // Fill 'file_vec' with the files that match the glob-like 'wildcard_file_path'
67 // ? can be used to match any single charector
68 // * can be used to match any sequence of charectors in a directory
69 // ** can be used to match any directories across any number of levels
70 //   e.g. FilesMatchingWildcard("~/*/code/*.h", vec);
71 //   e.g. FilesMatchingWildcard("~/**/*.png", vec);
72 PANGOLIN_EXPORT
73 bool FilesMatchingWildcard(const std::string& wildcard_file_path, std::vector<std::string>& file_vec);
74 
75 PANGOLIN_EXPORT
76 std::string MakeUniqueFilename(const std::string& filename);
77 
78 PANGOLIN_EXPORT
79 bool IsPipe(const std::string& file);
80 
81 PANGOLIN_EXPORT
82 bool IsPipe(int fd);
83 
84 PANGOLIN_EXPORT
85 int WritablePipeFileDescriptor(const std::string& file);
86 
87 /**
88  * Open the file for reading. Note that it is opened with O_NONBLOCK.  The pipe
89  * open is done in two stages so that the producer knows a reader is waiting
90  * (but not blocked). The reader then checks PipeHasDataToRead() until it
91  * returns true. The file can then be opened. Note that the file descriptor
92  * should be closed after the read stream has been created so that the write
93  * side of the pipe does not get signaled.
94  */
95 PANGOLIN_EXPORT
96 int ReadablePipeFileDescriptor(const std::string& file);
97 
98 PANGOLIN_EXPORT
99 bool PipeHasDataToRead(int fd);
100 
101 PANGOLIN_EXPORT
102 void FlushPipe(const std::string& file);
103 
104 // TODO: Tidy these inlines up / move them
105 
StartsWith(const std::string & str,const std::string & prefix)106 inline bool StartsWith(const std::string& str, const std::string& prefix)
107 {
108     return !str.compare(0, prefix.size(), prefix);
109 }
110 
EndsWith(const std::string & str,const std::string & prefix)111 inline bool EndsWith(const std::string& str, const std::string& prefix)
112 {
113     return !str.compare(str.size() - prefix.size(), prefix.size(), prefix);
114 }
115 
116 inline std::string Trim(const std::string& str, const std::string& delimiters = " \f\n\r\t\v" )
117 {
118     const size_t f = str.find_first_not_of( delimiters );
119     return f == std::string::npos ?
120                 "" :
121                 str.substr( f, str.find_last_not_of( delimiters ) + 1 );
122 }
123 
ToUpper(std::string & str)124 inline void ToUpper( std::string& str )
125 {
126     std::transform(str.begin(), str.end(), str.begin(), ::toupper);
127 }
128 
ToLower(std::string & str)129 inline void ToLower( std::string& str )
130 {
131     std::transform(str.begin(), str.end(), str.begin(), ::tolower);
132 }
133 
ToUpperCopy(const std::string & str)134 inline std::string ToUpperCopy( const std::string& str )
135 {
136     std::string out;
137     out.resize(str.size());
138     std::transform(str.begin(), str.end(), out.begin(), ::toupper);
139     return out;
140 }
141 
ToLowerCopy(const std::string & str)142 inline std::string ToLowerCopy( const std::string& str )
143 {
144     std::string out;
145     out.resize(str.size());
146     std::transform(str.begin(), str.end(), out.begin(), ::tolower);
147     return out;
148 }
149 
150 
151 }
152