filesystem.hpp

Go to the documentation of this file.
00001 /* $Id: filesystem.hpp 26481 2008-05-09 11:33:38Z suokko $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file filesystem.hpp 
00016 //! Declarations for File-IO.
00017 
00018 #ifndef FILESYSTEM_HPP_INCLUDED
00019 #define FILESYSTEM_HPP_INCLUDED
00020 
00021 #include <time.h>
00022 
00023 #include <iosfwd>
00024 #include <string>
00025 #include <vector>
00026 
00027 //! An exception object used when an IO error occurs
00028 struct io_exception : public std::exception {
00029     io_exception() : message("") {}
00030     io_exception(const std::string& msg) : message(msg) {}
00031     virtual ~io_exception() throw() {}
00032 
00033     virtual const char* what() const throw() { return message.c_str(); }
00034 private:
00035     std::string message;
00036 };
00037 
00038 enum FILE_NAME_MODE { ENTIRE_FILE_PATH, FILE_NAME_ONLY };
00039 enum FILE_FILTER { NO_FILTER, SKIP_MEDIA_DIR};
00040 enum FILE_REORDER_OPTION { DONT_REORDER, DO_REORDER };
00041 
00042 //! Populates 'files' with all the files and 
00043 //! 'dirs' with all the directories in dir. 
00044 //! If files or dirs are NULL they will not be used.
00045 //!
00046 //! Mode determines whether the entire path or just the filename is retrieved.
00047 void get_files_in_dir(const std::string dir,
00048                       std::vector<std::string>* files,
00049                       std::vector<std::string>* dirs=NULL,
00050                       FILE_NAME_MODE mode=FILE_NAME_ONLY,
00051                       FILE_FILTER = NO_FILTER,
00052                       FILE_REORDER_OPTION reorder=DONT_REORDER);
00053 
00054 std::string get_dir(const std::string &dir);
00055 
00056 // The location of various important files:
00057 std::string get_prefs_file();
00058 std::string get_save_index_file();
00059 std::string get_saves_dir();
00060 std::string get_cache_dir();
00061 std::string get_intl_dir();
00062 std::string get_screenshot_dir();
00063 
00064 //! Get the next free filename using "name + number (3 digits) + extension"
00065 //! maximum 1000 files then start always giving 999
00066 std::string get_next_filename(const std::string& name, const std::string& extension);
00067 std::string get_upload_dir();
00068 std::string get_user_data_dir();
00069 
00070 std::string get_cwd();
00071 
00072 bool make_directory(const std::string& dirname);
00073 bool delete_directory(const std::string& dirname);
00074 
00075 // Basic disk I/O:
00076 
00077 //! Basic disk I/O - read file.
00078 std::string read_file(const std::string& fname);
00079 std::istream *istream_file(std::string const &fname);
00080 std::ostream *ostream_file(std::string const &fname);
00081 //! Throws io_exception if an error occurs.
00082 void write_file(const std::string& fname, const std::string& data);
00083 
00084 std::string read_map(const std::string& name);
00085 
00086 //! Returns true if the given file is a directory.
00087 bool is_directory(const std::string& fname);
00088 
00089 //! Returns true if file with name already exists.
00090 bool file_exists(const std::string& name);
00091 
00092 //! Get the creation time of a file.
00093 time_t file_create_time(const std::string& fname);
00094 
00095 //! Return the next ordered full filename within this directory.
00096 std::string next_filename(const std::string &dirname, unsigned int max = 0);
00097 
00098 //! Returns true if the file ends with '.gz'.
00099 bool is_gzip_file(const std::string& filename);
00100 
00101 struct file_tree_checksum
00102 {
00103     file_tree_checksum();
00104     explicit file_tree_checksum(const class config& cfg);
00105     void write(class config& cfg) const;
00106     void reset() {nfiles = 0;modified = 0;sum_size=0;};
00107     // @todo: make variables private!
00108     size_t nfiles, sum_size;
00109     time_t modified;
00110 };
00111 
00112 bool operator==(const file_tree_checksum& lhs, const file_tree_checksum& rhs);
00113 bool operator!=(const file_tree_checksum& lhs, const file_tree_checksum& rhs);
00114 
00115 
00116 //! Get the time at which the data/ tree was last modified at.
00117 const file_tree_checksum& data_tree_checksum(bool reset = false);
00118 
00119 //! Returns the size of a file, or -1 if the file doesn't exist.
00120 int file_size(const std::string& fname);
00121 
00122 bool ends_with(const std::string& str, const std::string& suffix);
00123 
00124 //! Returns the base filename of a file, with directory name stripped. 
00125 //! Equivalent to a portable basename() function.
00126 std::string file_name(const std::string& file);
00127 
00128 //! Returns the directory name of a file, with filename stripped. 
00129 //! Equivalent to a portable dirname()
00130 std::string directory_name(const std::string& file);
00131 
00132 /*! The paths manager is responsible for recording the various paths 
00133  *  that binary files may be located at. 
00134  *  It should be passed a config object which holds binary path information. 
00135  *  This is in the format
00136  *@verbatim
00137  *    [binary_path]
00138  *      path=<path>
00139  *    [/binary_path]
00140  *  Binaries will be searched for in [wesnoth-path]/data/<path>/images/
00141  *@endverbatim
00142  */
00143 struct binary_paths_manager
00144 {
00145     binary_paths_manager();
00146     binary_paths_manager(const class config& cfg);
00147     ~binary_paths_manager();
00148 
00149     void set_paths(const class config& cfg);
00150 
00151 private:
00152     binary_paths_manager(const binary_paths_manager& o);
00153     binary_paths_manager& operator=(const binary_paths_manager& o);
00154 
00155     void cleanup();
00156 
00157     std::vector<std::string> paths_;
00158 };
00159 
00160 void clear_binary_paths_cache();
00161 
00162 //! Returns a vector with all possible paths to a given type of binary,
00163 //! e.g. 'images', 'sounds', etc,
00164 const std::vector<std::string>& get_binary_paths(const std::string& type);
00165 
00166 //! Returns a complete path to the actual file of a given a type of binary, 
00167 //! or an empty string if the file isn't present.
00168 std::string get_binary_file_location(const std::string& type, const std::string& filename);
00169 
00170 class scoped_istream {
00171     std::istream *stream;
00172 public:
00173     scoped_istream(std::istream *s): stream(s) {}
00174     scoped_istream& operator=(std::istream *);
00175     std::istream &operator*() { return *stream; }
00176     std::istream *operator->() { return stream; }
00177     ~scoped_istream();
00178 };
00179 
00180 class scoped_ostream {
00181     std::ostream *stream;
00182 public:
00183     scoped_ostream(std::ostream *s): stream(s) {}
00184     scoped_ostream& operator=(std::ostream *);
00185     std::ostream &operator*() { return *stream; }
00186     std::ostream *operator->() { return stream; }
00187     ~scoped_ostream();
00188 };
00189 
00190 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs