filesystem.hpp

Go to the documentation of this file.
00001 /* $Id: filesystem.hpp 53931 2012-04-14 23:43:23Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 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 as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Declarations for File-IO.
00019  */
00020 
00021 #ifndef FILESYSTEM_HPP_INCLUDED
00022 #define FILESYSTEM_HPP_INCLUDED
00023 
00024 #include <time.h>
00025 
00026 #include <iosfwd>
00027 #include <string>
00028 #include <vector>
00029 
00030 #include "exceptions.hpp"
00031 
00032 /** An exception object used when an IO error occurs */
00033 struct io_exception : public game::error {
00034     io_exception() : game::error("") {}
00035     io_exception(const std::string& msg) : game::error(msg) {}
00036 };
00037 
00038 struct file_tree_checksum;
00039 
00040 enum file_name_option { ENTIRE_FILE_PATH, FILE_NAME_ONLY };
00041 enum file_filter_option { NO_FILTER, SKIP_MEDIA_DIR, SKIP_PBL_FILES };
00042 enum file_reorder_option { DONT_REORDER, DO_REORDER };
00043 
00044 /**
00045  * Populates 'files' with all the files and
00046  * 'dirs' with all the directories in dir.
00047  * If files or dirs are NULL they will not be used.
00048  *
00049  * mode: determines whether the entire path or just the filename is retrieved.
00050  * filter: determines if we skip images and sounds directories
00051  * reorder: triggers the special handling of _main.cfg and _final.cfg
00052  * checksum: can be used to store checksum info
00053  */
00054 void get_files_in_dir(const std::string &dir,
00055                       std::vector<std::string>* files,
00056                       std::vector<std::string>* dirs=NULL,
00057                       file_name_option mode = FILE_NAME_ONLY,
00058                       file_filter_option filter = NO_FILTER,
00059                       file_reorder_option reorder = DONT_REORDER,
00060                       file_tree_checksum* checksum = NULL);
00061 
00062 std::string get_dir(const std::string &dir);
00063 
00064 // The location of various important files:
00065 std::string get_prefs_file();
00066 std::string get_default_prefs_file();
00067 std::string get_save_index_file();
00068 std::string get_saves_dir();
00069 std::string get_intl_dir();
00070 std::string get_screenshot_dir();
00071 std::string get_addon_campaigns_dir();
00072 
00073 /**
00074  * Get the next free filename using "name + number (3 digits) + extension"
00075  * maximum 1000 files then start always giving 999
00076  */
00077 std::string get_next_filename(const std::string& name, const std::string& extension);
00078 void set_preferences_dir(std::string path);
00079 
00080 const std::string &get_user_config_dir();
00081 const std::string &get_user_data_dir();
00082 const std::string &get_cache_dir();
00083 
00084 std::string get_cwd();
00085 std::string get_exe_dir();
00086 
00087 bool make_directory(const std::string& dirname);
00088 bool delete_directory(const std::string& dirname, const bool keep_pbl = false);
00089 
00090 bool looks_like_pbl(const std::string& file);
00091 
00092 // Basic disk I/O:
00093 
00094 /** Basic disk I/O - read file.
00095  * The bool relative_from_game_path determines whether relative paths should be treated as relative
00096  * to the game path (true) or to the current directory from which Wesnoth was run (false).
00097  */
00098 std::string read_file(const std::string &fname);
00099 std::istream *istream_file(const std::string &fname);
00100 std::ostream *ostream_file(std::string const &fname);
00101 /** Throws io_exception if an error occurs. */
00102 void write_file(const std::string& fname, const std::string& data);
00103 
00104 std::string read_map(const std::string& name);
00105 
00106 /**
00107  * Creates a directory if it does not exist already.
00108  *
00109  * @param dirname                 Path to directory. All parents should exist.
00110  * @returns                       True if the directory exists or could be
00111  *                                successfully created; false otherwise.
00112  */
00113 bool create_directory_if_missing(const std::string& dirname);
00114 /**
00115  * Creates a recursive directory tree if it does not exist already
00116  * @param dirname                 Full path of target directory. Non existing parents
00117  *                                will be created
00118  * @return                        True if the directory exists or could be
00119  *                                successfully created; false otherwise.
00120  */
00121 bool create_directory_if_missing_recursive(const std::string& dirname);
00122 
00123 /** Returns true if the given file is a directory. */
00124 bool is_directory(const std::string& fname);
00125 
00126 /** Returns true if a file or directory with such name already exists. */
00127 bool file_exists(const std::string& name);
00128 
00129 /** Get the creation time of a file. */
00130 time_t file_create_time(const std::string& fname);
00131 
00132 /** Returns true if the file ends with '.gz'. */
00133 bool is_gzip_file(const std::string& filename);
00134 
00135 struct file_tree_checksum
00136 {
00137     file_tree_checksum();
00138     explicit file_tree_checksum(const class config& cfg);
00139     void write(class config& cfg) const;
00140     void reset() {nfiles = 0;modified = 0;sum_size=0;};
00141     // @todo make variables private!
00142     size_t nfiles, sum_size;
00143     time_t modified;
00144     bool operator==(const file_tree_checksum &rhs) const;
00145     bool operator!=(const file_tree_checksum &rhs) const
00146     { return !operator==(rhs); }
00147 };
00148 
00149 /** Get the time at which the data/ tree was last modified at. */
00150 const file_tree_checksum& data_tree_checksum(bool reset = false);
00151 
00152 /** Returns the size of a file, or -1 if the file doesn't exist. */
00153 int file_size(const std::string& fname);
00154 
00155 bool ends_with(const std::string& str, const std::string& suffix);
00156 
00157 /**
00158  * Returns the base filename of a file, with directory name stripped.
00159  * Equivalent to a portable basename() function.
00160  */
00161 std::string file_name(const std::string& file);
00162 
00163 /**
00164  * Returns the directory name of a file, with filename stripped.
00165  * Equivalent to a portable dirname()
00166  */
00167 std::string directory_name(const std::string& file);
00168 
00169 /**
00170  * Returns the absolute path of a file.
00171  */
00172 std::string normalize_path(const std::string &path);
00173 
00174 /**
00175  *  The paths manager is responsible for recording the various paths
00176  *  that binary files may be located at.
00177  *  It should be passed a config object which holds binary path information.
00178  *  This is in the format
00179  *@verbatim
00180  *    [binary_path]
00181  *      path=<path>
00182  *    [/binary_path]
00183  *  Binaries will be searched for in [wesnoth-path]/data/<path>/images/
00184  *@endverbatim
00185  */
00186 struct binary_paths_manager
00187 {
00188     binary_paths_manager();
00189     binary_paths_manager(const class config& cfg);
00190     ~binary_paths_manager();
00191 
00192     void set_paths(const class config& cfg);
00193 
00194 private:
00195     binary_paths_manager(const binary_paths_manager& o);
00196     binary_paths_manager& operator=(const binary_paths_manager& o);
00197 
00198     void cleanup();
00199 
00200     std::vector<std::string> paths_;
00201 };
00202 
00203 void clear_binary_paths_cache();
00204 
00205 /**
00206  * Returns a vector with all possible paths to a given type of binary,
00207  * e.g. 'images', 'sounds', etc,
00208  */
00209 const std::vector<std::string>& get_binary_paths(const std::string& type);
00210 
00211 /**
00212  * Returns a complete path to the actual file of a given @a type
00213  * or an empty string if the file isn't present.
00214  */
00215 std::string get_binary_file_location(const std::string& type, const std::string& filename);
00216 
00217 /**
00218  * Returns a complete path to the actual directory of a given @a type
00219  * or an empty string if the directory isn't present.
00220  */
00221 std::string get_binary_dir_location(const std::string &type, const std::string &filename);
00222 
00223 /**
00224  * Returns a complete path to the actual WML file or directory
00225  * or an empty string if the file isn't present.
00226  */
00227 std::string get_wml_location(const std::string &filename,
00228     const std::string &current_dir = std::string());
00229 
00230 /**
00231  * Returns a short path to @a filename, skipping the (user) data directory.
00232  */
00233 std::string get_short_wml_path(const std::string &filename);
00234 
00235 /**
00236  * Returns an image path to @a filename for binary path-independent use in saved games.
00237  *
00238  * Example:
00239  *   units/konrad-fighter.png ->
00240  *   data/campaigns/Heir_To_The_Throne/images/units/konrad-fighter.png
00241  */
00242 std::string get_independent_image_path(const std::string &filename);
00243 
00244 /**
00245  * Returns the appropriate invocation for a Wesnoth-related binary, assuming
00246  * that it is located in the same directory as the running wesnoth binary.
00247  * This is just a string-transformation based on argv[0], so the returned
00248  * program is not guaranteed to actaully exist.  '-debug' variants are handled
00249  * correctly.
00250  */
00251 std::string get_program_invocation(const std::string &program_name);
00252 
00253 class scoped_istream {
00254     std::istream *stream;
00255 public:
00256     scoped_istream(std::istream *s): stream(s) {}
00257     scoped_istream& operator=(std::istream *);
00258     std::istream &operator*() { return *stream; }
00259     std::istream *operator->() { return stream; }
00260     ~scoped_istream();
00261 };
00262 
00263 class scoped_ostream {
00264     std::ostream *stream;
00265 public:
00266     scoped_ostream(std::ostream *s): stream(s) {}
00267     scoped_ostream& operator=(std::ostream *);
00268     std::ostream &operator*() { return *stream; }
00269     std::ostream *operator->() { return stream; }
00270     ~scoped_ostream();
00271 };
00272 
00273 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:36 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs