Upstream tarball 10013
[amule.git] / src / libs / common / Path.h
blob4becbb50a69f5e74b621435dc0bda90c9503f539
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifndef FILENAME_H
26 #define FILENAME_H
28 #include "../../Types.h"
29 #include "Format.h"
32 /**
33 * This class wraps a path/filename, serving a purpose much
34 * like wxFileName. But in addition CPath serves to enable
35 * the handling of "broken" filenames, allowing these to be
36 * printed in a meaningful manner, while still allowing
37 * access to the actual files in the filesystem.
39 * This class is thread-safe, in that the class is read-only,
40 * and any function that returns wxStrings or CPath objects,
41 * ensure that an entirely new wxString object is created to
42 * circumvent the thread-unsafe reference counting of that
43 * class.
45 * This class, and its static functions should be used in
46 * preference of wxFileName, for the above reason, and so
47 * that cross-platform issues can be worked around in a
48 * single place.
50 class CPath : public CPrintable
52 public:
53 /** Default constructor. */
54 CPath();
56 /** Constuctor. */
57 explicit CPath(const wxString& path);
59 /** Copy constructor. Creates a deep-copy of the passed object. */
60 CPath(const CPath& other);
62 /** Destructor. */
63 ~CPath();
66 /**
67 * Creates a path from one saved in the 'universial' format.
69 * These are to be used when the filenames/paths are saved to
70 * the local machine, and ensure that locale-changes does not
71 * affect our ability to find previously known files. This
72 * should not be used when sending filenames to other clients
73 * or (currently) the core/gui.
74 **/
75 static CPath FromUniv(const wxString& path);
76 /** Creates an 'universial' path from the specifed CPath. */
77 static wxString ToUniv(const CPath& path);
80 /** Assignment operator. */
81 CPath& operator=(const CPath& other);
82 /** Less-than operator. */
83 bool operator<(const CPath& other) const;
84 /** Note that only exact matches are considered equal, see IsSameDir. */
85 bool operator==(const CPath& other) const;
86 /** Note that only exact matches are considered equal, see IsSameDir. */
87 bool operator!=(const CPath& other) const;
90 /** Returns true if the filename is valid, false otherwise. */
91 bool IsOk() const;
92 /** Returns true if the path exists and is a file, false otherwise. */
93 bool FileExists() const;
94 /** Returns true if the path exists and is a directory, false otherwise. */
95 bool DirExists() const;
98 enum EAccess {
99 //! Only check of the file or dir exists.
100 exists,
101 //! If set, checks if a file is readable, or if a dir can be accessed.
102 readable,
103 //! If set, checks if a file or directory can be written to,
104 writable,
105 //! Combination of the two checks above.
106 readwritable = readable | writable
109 /** Returns if if the path is a dir, and the checks were positive. */
110 bool IsDir(EAccess mode) const;
111 /** Returns if if the path is a dir, and the checks were positive. */
112 bool IsFile(EAccess mode) const;
115 /** Returns the contents of the object in a form suitable for use with wx system-calls. */
116 wxString GetRaw() const;
117 /** Returns the contents of the object in a form suitable for use in the UI. */
118 wxString GetPrintable() const;
119 /** Returns the (raw) last extension, empty if none is found. */
120 wxString GetExt() const;
122 /** Returns the full path, exluding the filename. */
123 CPath GetPath() const;
124 /** Returns the full filename, excluding the path. */
125 CPath GetFullName() const;
127 /** Returns the size of the specified file, or wxInvalidSize on failure. */
128 sint64 GetFileSize() const;
131 * Compares under the assumption that both objects are dirs, even if
132 * one or the other lacks a terminal directory-seperator. However, an
133 * empty CPath object will not be considered equal to a path to the root.
135 bool IsSameDir(const CPath& other) const;
137 /** Returns a CPath created from joining the two objects. */
138 CPath JoinPaths(const CPath& other) const;
139 /** Returns a CPath with invalid chars removed, and spaces escaped if specified. */
140 CPath Cleanup(bool keepSpaces = true, bool isFAT32 = false) const;
141 /** Returns a CPath with a postfix before the file-extension. Must be ASCII. */
142 CPath AddPostfix(const wxString& postfix) const;
144 /** Returns a CPath object with the extension appended. Empty strings are ignored. */
145 CPath AppendExt(const wxString& ext) const;
146 /** Returns a CPath with the (last, if multiple) extension removed. */
147 CPath RemoveExt() const;
148 /** Returns a CPath object with all extensions removed. */
149 CPath RemoveAllExt() const;
152 /** Returns true if the the passed path makes up an prefix of this object. */
153 bool StartsWith(const CPath& other) const;
155 /** @see cprintable::getprintablestring */
156 wxString GetPrintableString() const;
159 /**
160 * Renames the file 'src' to the file 'dst', overwriting if specified. Note that
161 * renaming cannot be done across volumes. For that CopyFile is required.
163 static bool RenameFile(const CPath& src, const CPath& dst, bool overwrite = false);
165 * Copies the file 'src' to the file 'dst', overwriting if specified.
166 * The silly name is used to avoid conflicts with the #define CopyFile,
167 * which is set on MSW.
169 static bool CloneFile(const CPath& src, const CPath& dst, bool overwrite = false);
171 /** Makes a backup of a file, by copying the original file to 'src' + 'appendix' */
172 static bool BackupFile(const CPath& src, const wxString& appendix);
174 /** Deletes the specified file, returning true on success. */
175 static bool RemoveFile(const CPath& file);
176 /** Deletes the specified directory, returning true on success. */
177 static bool RemoveDir(const CPath& file);
178 /** Creates the specified directory, returning true on success. */
179 static bool MakeDir(const CPath& file);
181 /** Returns true if the path exists, and is a file. */
182 static bool FileExists(const wxString& file);
183 /** Returns true if the path exists, and is a directory. */
184 static bool DirExists(const wxString& path);
186 /** Returns the size of the specified file, or wxInvalidOffset on failure. */
187 static sint64 GetFileSize(const wxString& file);
188 /** Returns the modification time the specified file, or (time_t)-1 on failure. */
189 static time_t GetModificationTime(const CPath& file);
190 /** Returns the free diskspace at the specified path, or wxInvalidOffset on failure. */
191 static sint64 GetFreeSpaceAt(const CPath& path);
193 private:
194 //! Contains the printable filename, for use in the UI.
195 wxString m_printable;
196 //! Contains the "raw" filename, for use in system-calls,
197 //! as well as in wxWidgets file functions.
198 wxString m_filesystem;
200 // Is made a friend to avoid needless copying of strings.
201 friend int CmpAny(const CPath& ArgA, const CPath& ArgB);
206 * Overloaded version of CmpAny for use with CPaths. As this is
207 * typically used in the UI, it uses the printable filename in
208 * order to get visually correct results.
210 inline int CmpAny(const CPath& ArgA, const CPath& ArgB)
212 return ArgA.m_printable.CmpNoCase(ArgB.m_printable);
217 #endif