root/trunk/whisperlib/common/io/ioutil.h

Revision 7, 7.2 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #ifndef __IO_IOUTIL_H__
33 #define __IO_IOUTIL_H__
34
35 #include <sys/stat.h>
36 #include <vector>
37 #include <string>
38 #include <map>
39 #include <whisperlib/common/base/types.h>
40 #include <whisperlib/common/base/re.h>
41
42 namespace io {
43
44 bool IsDir(const char* name);
45 bool IsDir(const string& name);
46 bool IsReadableFile(const char* name);
47 bool IsReadableFile(const string& name);
48 bool IsSymlink(const string& path);
49 bool Exists(const char* path);
50 bool Exists(const string& path);
51 int64 GetFileSize(const char* name);
52 int64 GetFileSize(const string& name);
53 bool DirList(const string& dir,
54              vector<string>* names,
55              bool list_dirs,
56              re::RE* regex = NULL);
57 bool RecursiveListing(const string& dir,
58                       vector<string>* dir_names,
59                       re::RE* regex = NULL);
60
61 bool CreateRecursiveDirs(
62   const char* dirname,
63   mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
64 bool CreateRecursiveDirs(
65   const string& dirname,
66   mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
67
68
69 // Utility for finding based on a path in a map of paths and structures.
70 // We return C() when not found, and we update path to reflect the path
71 // that triggerd the found objest
72 template<class C>
73 C FindPathBased(const map<string, C>* data, string& path) {
74   if ( data->empty() ) {
75     return C();
76   }
77   string to_search(path);
78   while ( !to_search.empty() ) {
79     const typename map<string, C>::const_iterator it = data->find(to_search);
80     if ( it != data->end() ) {
81       path = to_search;
82       return it->second;
83     }
84     if ( !to_search.empty() && to_search[to_search.size() - 1] == '/' ) {
85       to_search.resize(to_search.size() - 1);
86     } else {
87       const size_t pos_slash = to_search.rfind("/");
88       if ( pos_slash != string::npos ) {
89         to_search.resize(pos_slash + 1);
90       } else {
91         to_search.resize(0);
92       }
93     }
94   }
95   const typename map<string, C>::const_iterator it = data->find(to_search);
96   if ( it != data->end() ) {
97     path = to_search;
98     return it->second;
99   }
100   return C();
101 }
102
103 // Same as above, but adds all matching paths
104 template<class C>
105 int FindAllPathBased(const map<string, C>* data,
106                      const string& path,
107                      map<string, C>* matches) {
108   if ( data->empty() ) {
109     return 0;
110   }
111   string to_search(path);
112   int num_found = 0;
113   while ( !to_search.empty() ) {
114     const typename map<string, C>::const_iterator it = data->find(to_search);
115     if ( it != data->end() ) {
116       ++num_found;
117       matches->insert(make_pair(to_search, it->second));
118     }
119     if ( !to_search.empty() && to_search[to_search.size() - 1] == '/' ) {
120       to_search.resize(to_search.size() - 1);
121     } else {
122       const size_t pos_slash = to_search.rfind("/");
123       if ( pos_slash != string::npos ) {
124         to_search.resize(pos_slash + 1);
125       } else {
126         to_search.resize(0);
127       }
128     }
129   }
130   const typename map<string, C>::const_iterator it = data->find(to_search);
131   if ( it != data->end() ) {
132     ++num_found;
133     matches->insert(make_pair(to_search, it->second));
134   }
135   return num_found;
136 }
137
138
139 // Retrns the last of a set of numbered files placed in the specified
140 // directory. The number of the file can be strtoul from the last
141 // file_num_size chars of the filename.
142 int32 GetLastNumberedFile(const string& dir, re::RE* re, int32 file_num_size);
143
144 // A tool to easy remove files or folders (even non empty folders).
145 // Returns success state. Failure reason can be found in GetLastSystemError().
146 bool Rm(const string& path);
147
148 // Easy remove empty directory.
149 // Fails if the target directory is not empty.
150 bool Rmdir(const string& path);
151
152 // Move file or directory to destination directory.
153 // If a directory with the same name already exists, the source directory
154 // will be integrated in the destionation directory. See function Rename(..) .
155 //
156 // e.g. path = "/tmp/abc.avi"
157 //      dir = "/home/my_folder"
158 //      Will move file "abc.avi" to "/home/my_folder/abc.avi"
159 //      and "/tmp/abc.avi" no longer exists.
160 bool Mv(const string& path, const string& dir, bool overwrite);
161
162 // Rename file or directory moving it between directories if required.
163 // The parent of new_path MUST exist, otherwise this function FAILS.
164 // Symlinks (just like files) are treated as a whole.
165 //
166 // If old_path is a file(or symlink):
167 //   - if new_path is a directory => FAIL.
168 //   - if new_path does not exist => ATOMICAL move.
169 //   - if new_path is a file(or a symlink):
170 //       if overwrite == true => ATOMICAL move.
171 //       else => FAIL.
172 // If old_path is a directory:
173 //   - if new_path is a file(or symlink) => FAIL.
174 //   - if new_path does not exist => ATOMICAL move.
175 //   - if new_path is an existing directory:
176 //     then all the content of old_path is recusively copied to new_path
177 //     according to "overwrite" flag. This function returns true if all
178 //     the content of old_path has been moved to new_path. Otherwise this
179 //     function does its best to move as much as possible and returns false.
180 //
181 // e.g. old_path = "/tmp/abc.avi"
182 //      new_path = "/home/my_folder/123.xyz"
183 //      Will move file "abc.avi" to "/home/my_folder/123.xyz"
184 //      and "/tmp/abc.avi" no longer exists.
185 bool Rename(const string& old_path,
186             const string& new_path,
187             bool overwrite);
188
189 // Creates a directory on disk.
190 // recursive: if true => creates all directories on path "dir"
191 //            if false => creates only "dir"; it's parent must exist.
192 bool Mkdir(const string& dir,
193            bool recursive = false,
194            mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
195
196 // Prepends the current working directory to the provided path, if
197 // necessary, to obtain a fully qualified path
198 string MakeAbsolutePath(const char* path);
199 }
200
201 #endif  // __IO_IOUTIL_H__
Note: See TracBrowser for help on using the browser.