Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: fileutils.h,v 1.14 2004/04/23 14:14:17 plearner Exp $ 00041 * AUTHORS: Pascal Vincent 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 // This file contains useful functions for string manipulation 00046 // that are used in the PLearn Library 00047 00048 00051 #ifndef fileutils_INC 00052 #define fileutils_INC 00053 00054 #include <fstream> 00055 #include <map> 00056 #include <string> 00057 #include <vector> 00058 //#include "general.h" 00059 00060 // norman: set win32 functions 00061 #ifdef WIN32 00062 #include <sys/types.h> 00063 #define stat _stat 00064 #endif 00065 00066 namespace PLearn { 00067 using namespace std; 00068 00070 string getcwd(); 00071 00073 int chdir(const string& path); 00074 00077 string abspath(const string& path); 00078 00080 bool pathexists(const string& path); 00081 00083 bool isdir(const string& path); 00084 00086 bool isfile(const string& path); 00087 00089 time_t mtime(const string& path); 00090 00095 vector<string> lsdir(const string& dirpath); 00096 00098 vector<string> lsdir_fullpath(const string& dirpath); 00099 00105 bool force_mkdir(const string& dirname); 00106 00109 void force_mkdir_for_file(const string& filepath); 00110 00115 bool force_rmdir(const string& dirname); 00116 00118 long filesize(const string& filename); 00119 00121 void cp(const string& srcpath, const string& destpath); 00122 00124 void rm(const string& file); 00125 00127 void mv(const string& file); 00128 00131 void mvforce(const string& file); 00132 00134 void touch(const string& file); 00135 00138 void readWhileMatches(istream& in, const string& s); 00139 00141 void skipRestOfLine(istream& in); 00142 00145 void skipBlanksAndComments(istream& in); 00146 00148 void getNextNonBlankLine(istream& in, string& line); 00149 00152 int countNonBlankLinesOfFile(const string& filename); 00153 00155 int smartReadUntilNext(istream& in, string stoppingsymbols, string& characters_read, bool ignore_brackets=false); 00156 00158 inline char peekAfterSkipBlanks(istream& in) { while(isspace(in.get())); in.unget();return in.peek(); } 00159 00161 inline char peekAfterSkipBlanksAndComments(istream& in) { skipBlanksAndComments(in); return in.peek(); } 00162 00164 inline char getAfterSkipBlanks(istream& in) { char c; while(isspace(c = in.get())); return c; } 00165 00167 inline char getAfterSkipBlanksAndComments(istream& in) { skipBlanksAndComments(in); return in.get(); } 00168 00171 string newFilename(const string directory="/tmp/", const string prefix="", bool is_directory=false); 00172 00173 string makeFileNameValid(const string& filename); 00174 00176 string makeExplicitPath(const string& filename); 00177 00179 string loadFileAsString(const string& filepath); 00180 00183 void saveStringInFile(const string& filepath, const string& text); 00184 00189 string readAndMacroProcess(istream& in, map<string, string>& variables); 00190 00193 00201 string readFileAndMacroProcess(const string& filepath, map<string, string>& variables); 00202 00203 inline string readFileAndMacroProcess(const string& filepath) 00204 { 00205 map<string, string> variables; 00206 return readFileAndMacroProcess(filepath, variables); 00207 } 00208 00209 #ifdef WIN32 00210 #undef stat 00211 #endif 00212 00213 } // end of namespace PLearn 00214 00215 00216 #endif

Generated on Tue Aug 17 15:52:51 2004 for PLearn by doxygen 1.3.7