Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/io/filesys.h

Go to the documentation of this file.
00001 //  Copyright (C) 1999--2002  Petter Urkedal (petter.urkedal@matfys.lth.se)
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 //
00027 //  $Id: filesys.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00028 
00029 
00030 
00031 #ifndef MORE_FILESYS_H
00032 #define MORE_FILESYS_H
00033 
00034 #include <string>
00035 #include <list>
00036 #include <stdexcept>
00037 
00038 
00039 namespace more {
00040 namespace io {
00041 
00042   /** Rename a file or a directory.  Returns true iff successful. */
00043   bool rename_node(std::string const& oldpath, std::string const& newpath);
00044 
00045   /** Remove a file or a directory.  Returns true iff successful. */
00046   bool remove_node(std::string const& path);
00047 
00048   /** Remove all files in a directory and the directory itself, USE
00049       WITH CARE! Returns true iff successful. */
00050   bool remove_node_rec(std::string const& path);
00051 
00052   /** Calls remove(path) at exit. */
00053   void remove_node_at_exit(std::string const& path);
00054 
00055   /** Calls remove_recursive(path) at exit, USE WITH CARE! */
00056   void remove_node_rec_at_exit(std::string const& path);
00057 
00058   /** Copy node \a srcpath to \a dstpath.  Directories will only be
00059    ** created without copying contents. */
00060   bool copy_node(std::string const& srcpath, std::string const& dstpath);
00061 
00062   /** Copy node \a srcpath to \a dstpath, recursively copying
00063    ** directory contents.  */
00064   bool copy_node_rec(std::string const& srcpath, std::string const& dstpath);
00065 
00066   /** Create a directory.  Returns true iff successful. */
00067   bool create_dir(std::string path);
00068 
00069   /** Create a directory after first creating all nonexisting parents
00070       directories.  Returns true iff successful. */
00071   bool create_dir_rec(std::string path);
00072 
00073   /** Returns true iff the file at path contains str. This version
00074       searches line by line, so str should not contain any
00075       newlines. */
00076   bool grep_string(std::string const& path, std::string const& str);
00077 
00078   /** Attempts to reserve a unique file name starting with \a name.
00079       If it succeeds, an empty file with permissions 0600 (Unix) will
00080       be created, name is modified to the name of this file and true
00081       is returned.  Otherwise false is returned.  The file will be
00082       removed at exit if remove_p=true.  If remove_p == true, do not
00083       remove the file yourself, as some other application may then
00084       reuse the file name and loose its file the file when your
00085       program exits! */
00086   bool reserve_tmp_file(std::string& name, bool remove_p = true);
00087 
00088   /** A handle to a unique name of a file that will be removed when
00089       the handle is destructed.  An empty file with permissions 0600
00090       is created. */
00091   struct tmp_file_reservation
00092   {
00093       /** Attempt to reserve the file. */
00094       explicit tmp_file_reservation(std::string const& name)
00095           : m_name(name)
00096       {
00097           if (!reserve_tmp_file(m_name, false))
00098               m_name.clear();
00099       }
00100 
00101       /** Remove the file. */
00102       ~tmp_file_reservation()
00103       {
00104           if (!m_name.empty())
00105               remove_node(m_name);
00106       }
00107 
00108       /** Returns the reserved file name, or throws a runtime_error if
00109           not reserved. */
00110       std::string const& name()
00111       {
00112           if (m_name.empty())
00113               throw std::runtime_error("more::tmp_file_reservation::name: "
00114                                        "Could not reserve a tmp file.");
00115           return m_name;
00116       }
00117 
00118       /** True of a file name was reserved. */
00119       bool is_reserved() const { return !m_name.empty(); }
00120 
00121     private:
00122       std::string m_name;
00123   };
00124 
00125   /** Attemts to create a directory with a name starting with name.
00126       If it succeeds, returns true and there will be a newly created
00127       directory with permissions 0700 (Unix).  Otherwise returns
00128       false.  The directory and its contents will be removed at
00129       exit.  */
00130   bool create_tmp_dir(std::string& name);
00131 
00132 //   std::string mkpath(const std::string, const std::string);
00133 //   std::string path_file(const std::string);
00134 //   std::string path_directory(const std::string);
00135 //   std::string path_parent(const std::string);
00136 
00137   /** Aquires various information about the a file. */
00138   struct file_status
00139   {
00140       static const int readable_flag = 4;
00141       static const int writable_flag = 2;
00142       static const int executable_flag = 1;
00143 
00144       file_status(std::string);
00145       ~file_status();
00146 
00147       bool good() const { return m_err == 0; }
00148       bool bad() const { return m_err != 0; }
00149 
00150       int user_permissions() const;
00151       int permissions() const;
00152       unsigned int uid() const;
00153       unsigned int gid() const;
00154 
00155       bool exists() const { return good(); }
00156       bool is_readable() const { return user_permissions() & readable_flag; }
00157       bool is_writable() const { return user_permissions() & writable_flag; }
00158       bool is_executable() const {return user_permissions()&executable_flag;}
00159 
00160       bool is_regular_file() const;
00161       bool is_directory() const;
00162       bool is_symbolic_link() const;
00163 
00164       double atime() const;
00165       double mtime() const;
00166 
00167     private:
00168       void*     m_stat;
00169       int       m_err;
00170   };
00171 
00172 
00173   /** A list of path names matched by a globbing pattern. */
00174   struct file_glob_list
00175   {
00176       /** The iterator should be at least a bidirectional iterator. */
00177       typedef char const* const* iterator;
00178       /** The iterator should be at least a bidirectional iterator. */
00179       typedef char const* const* const_iterator;
00180 
00181       file_glob_list(std::string patt);
00182       ~file_glob_list();
00183 
00184       /** True unless a read error occured.  Note that no matches
00185           results in an empty list, but good() still returns true. */
00186       bool good() const { return m_data; }
00187 
00188       const_iterator begin() const;
00189       const_iterator end() const;
00190 
00191     private:
00192       void* m_data;
00193   };
00194 
00195   typedef std::list<std::string> list_of_string;
00196   typedef list_of_string::iterator list_of_string_iterator;
00197   typedef list_of_string::const_iterator const_list_of_string_iterator;
00198 
00199 
00200   // scsh style file-name manipulation
00201 
00202   bool          is_file_name_directory(std::string fname);
00203   bool          is_file_name_non_directory(std::string fname);
00204   std::string   file_name_as_directory(std::string fname);
00205   std::string   directory_as_file_name(std::string fname);
00206   bool          is_file_name_absolute(std::string fname);
00207   std::string   file_name_directory(std::string fname);
00208   std::string   file_name_nondirectory(std::string fname);
00209   std::pair<std::string, std::string>
00210                 split_file_name(std::string fname);
00211   std::string   path_list_to_file_name(const_list_of_string_iterator first,
00212                                        const_list_of_string_iterator past_end,
00213                                        std::string dir = ".");
00214   std::string   file_name_extension(std::string fname);
00215   std::string   file_name_sans_extension(std::string fname);
00216   std::string   replace_extension(std::string fname, std::string ext);
00217   std::string   resolve_file_name(std::string fname, std::string dir = ".");
00218   std::string   absolute_file_name(std::string fname, std::string dir = ".");
00219   std::string   relative_file_name(std::string fpath, std::string dir);
00220   std::string   current_directory();
00221   std::string   home_dir(/* XXX std::string user = "" */);
00222   std::string   home_file(std::string fname);
00223 
00224 }}
00225 
00226 #endif

Generated on Sat Sep 7 19:11:16 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.