PLearn 0.1
fileutils.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent and Yoshua Bengio
00006 // Copyright (C) 1999-2005 University of Montreal
00007 //
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036  
00037 
00038 /* *******************************************************      
00039  * $Id: fileutils.h 10121 2009-04-15 14:27:39Z nouiz $
00040  * AUTHORS: Pascal Vincent
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 // This file contains useful functions for file manipulation
00045 // that are used in the PLearn Library
00046 
00047 
00050 #ifndef fileutils_INC
00051 #define fileutils_INC
00052 
00053 
00054 //#include <iostream>
00055 #include <map>
00056 #include <string>
00057 #include <vector>
00058 
00059 #include "PPath.h"
00060 #include "PStream.h"
00061 #include <nspr/prlong.h> //< for PRUint64
00062 
00063 namespace PLearn {
00064 using namespace std;
00065   
00067 int chdir(const PPath& path);
00068 
00070 bool pathexists(const PPath& path);
00071 
00073 bool isdir(const PPath& path);
00074 
00076 bool isfile(const PPath& path);
00077 
00079 bool isemptyFile(const PPath& path);
00080 
00082 time_t mtime(const PPath& path);
00083 
00088 vector<string> lsdir(const PPath& dirpath);
00089 
00091 vector<PPath> lsdir_fullpath(const PPath& dirpath);
00092 
00101 bool mkdir_lowlevel(const PPath& dirname);
00102 
00108 bool force_mkdir(const PPath& dirname);
00109   
00112 void force_mkdir_for_file(const PPath& filepath);
00113 
00118 bool force_rmdir(const PPath& dirname);
00119 
00122 PRUint64 filesize64(const PPath& filename);
00123 
00125 inline long filesize(const PPath& filename)
00126 { return long(filesize64(filename)); }
00127 
00129 void cp(const PPath& srcpath, const PPath& destpath);
00130 
00132 bool rm(const PPath& file, bool fail_on_error_if_exist = false);
00133 
00136 PRStatus mv(const PPath& source, const PPath& dest, bool fail_on_error = true);
00137 
00139 PRStatus mvforce(const PPath& source, const PPath& dest, bool fail_on_error = true);
00140 
00142 void touch(const PPath& file);
00143 
00146 void readWhileMatches(PStream& in, const string& s);
00147 
00149 void skipRestOfLine(PStream& in);
00150 
00153 void skipBlanksAndComments(PStream& in);
00154 
00157 void getNextNonBlankLine(PStream& in, string& line);
00158 
00161 int countNonBlankLinesOfFile(const PPath& filename);
00162 
00164 inline int peekAfterSkipBlanks(PStream& in) {
00165     int c;
00166     do {
00167         c = in.get();
00168     } while (c != EOF && isspace(c));
00169     in.unget();
00170     return c;
00171 }
00172 
00174 inline int peekAfterSkipBlanksAndComments(PStream& in)
00175 { skipBlanksAndComments(in); return in.peek(); }
00176 
00178 inline int getAfterSkipBlanks(PStream& in) {
00179     int c;
00180     do {
00181         c = in.get();
00182     } while (c != EOF && isspace(c));
00183     return c;
00184 }
00185 
00187 inline int getAfterSkipBlanksAndComments(PStream& in)
00188 { skipBlanksAndComments(in); return in.get(); }
00189 
00193 PPath newFilename(const PPath& directory = "/tmp/", const string& prefix="", bool is_directory=false);
00194 
00196 PPath makeFileNameValid(const PPath& filename);
00197 
00199 string loadFileAsString(const PPath& filepath);
00200 
00203 void saveStringInFile(const PPath& filepath, const string& text);
00204 
00212 string readAndMacroProcess(PStream& in, map<string, string>& variables, 
00213                            time_t& latest, bool skip_comments= true);
00214 
00219 void addFileAndDateVariables(const PPath& filepath,
00220                              map<string, string>& variables,
00221                              const time_t& latest);
00222     
00243 string readFileAndMacroProcess(const PPath& filepath,
00244                                map<string, string>& variables,
00245                                time_t& latest, bool change_dir = false);
00246 
00247 inline string readFileAndMacroProcess(const PPath& filepath,
00248                                map<string, string>& variables,
00249                                bool change_dir = false)
00250 {
00251     time_t latest = 0;
00252     return readFileAndMacroProcess(filepath, variables, latest, change_dir);
00253 }
00254 
00255 inline string readFileAndMacroProcess(const PPath& filepath, time_t& latest)
00256 {
00257     map<string, string> variables;
00258     return readFileAndMacroProcess(filepath, variables, latest);
00259 }
00260 
00261 inline string readFileAndMacroProcess(const PPath& filepath)
00262 {
00263     map<string, string> variables;
00264     time_t latest = 0;
00265     return readFileAndMacroProcess(filepath, variables, latest);
00266 }
00267 
00274 void addReferenceToFile(const PPath& file);
00275 
00277 void removeReferenceToFile(const PPath& file);
00278 
00280 unsigned int nReferencesToFile(const PPath& file);
00281 
00283 bool noReferenceToFile(const PPath& file);
00284   
00285 } // end of namespace PLearn
00286 
00287 #endif
00288 
00289 
00290 /*
00291   Local Variables:
00292   mode:c++
00293   c-basic-offset:4
00294   c-file-style:"stroustrup"
00295   c-file-offsets:((innamespace . 0)(inline-open . 0))
00296   indent-tabs-mode:nil
00297   fill-column:79
00298   End:
00299 */
00300 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines