c1710c13e9328f66623abb911dfac745858468e6
[feisty_meow.git] / nucleus / library / filesystem / filename.h
1 #ifndef FILENAME_CLASS
2 #define FILENAME_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : filename                                                          *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include <basis/astring.h>
19 #include <basis/byte_array.h>
20 #include <basis/contracts.h>
21 #include <structures/string_array.h>
22
23 // forward declarations.
24 class status_info;
25
26 //hmmm: this doesn't really belong here, does it...
27 // define useful constant for filesystem path length.
28 #ifndef MAX_ABS_PATH 
29   #ifdef __WIN32__
30     #include <windows.h>
31     #define MAX_ABS_PATH MAX_PATH
32   #else
33     #ifdef __APPLE__
34       #include <sys/syslimits.h>
35     #else
36       #include <limits.h>
37     #endif
38     #define MAX_ABS_PATH PATH_MAX
39   #endif
40 #endif
41
42
43 namespace filesystem {
44
45 //! Provides operations commonly needed on file names.
46
47 class filename : public basis::astring, public virtual basis::packable
48 {
49 public:
50   filename();  //!< blank constructor.
51   filename(const basis::astring &name);
52     //!< creates a filename from any part of a full pathname, if possible.
53     /*!< if the name contains quotes, they are stripped out. */
54   filename(const basis::astring &directory, const basis::astring &name_of_file);
55     //!< constructs a filename from a "directory" and the "name_of_file".
56     /*!< the "name_of_file" can itself be a directory. */
57   filename(const filename &to_copy);  //!< copy constructor.
58
59   DEFINE_CLASS_NAME("filename");
60
61   virtual ~filename();
62
63   bool good() const;
64     //!< returns true if the filename seems to be valid.
65     /*!< this means that not only was the pathname parsed and found valid,
66     but the file actually exists. */
67
68   void reset(const basis::astring &name);
69     //!< changes the file name held by the object.
70
71   const basis::astring &raw() const;
72     //!< returns the astring that we're holding onto for the path.
73   basis::astring &raw();
74     //!< accesses the astring that we're holding onto for the path.
75     /*!< important note: if you change the string with this non-const raw()
76     method, you MUST call canonicalize() on it again afterwards. */
77
78   filename &operator = (const filename &to_copy);
79     //!< provides assignment for this object, plus a simple string.
80   filename &operator = (const basis::astring &to_copy);
81     //!< provides assignment for this object, plus a simple string.
82     /*!< the latter version invokes canonicalize to clean the string up. */
83
84   void canonicalize();
85     //!< cleans up the filename as needed for the current operating system.
86     /*!< reforms the name by replacing any alternate directory separators with
87     the operating system's preferred character. */
88
89   bool exists() const;
90     //!< returns true if the file exists.
91
92   bool unlink() const;
93     //!< actually removes the file, if possible.
94     /*!< if the file was successfully deleted, then true is returned. */
95
96   filename parent() const;
97     //!< returns the parent filename for this one.
98
99   basis::astring pop();
100     //!< removes the deepest component of the pathname.
101     /*!< the component might be a file or directory name, but popping beyond
102     the top-level directory will not succeed.  the returned string contains
103     the component that was removed.  it will be a blank string if nothing
104     could be popped. */
105
106   void push(const basis::astring &to_push);
107     //!< pushes a new filename onto the current pathname.
108     /*!< this only makes sense as a real pathname if this is currently a
109     directory name and the component "to_push" is a child of that directory
110     (or one intends to create that component as a child).  this is the
111     opposite of pop. */
112
113   filename basename() const;
114     //!< returns the base of the filename; no directory.
115   filename dirname() const;
116     //!< returns the directory for the filename.
117     /*!< if no directory name can be found in the filename, then "." is
118     returned. */
119   basis::astring dirname(bool add_slash) const;
120     //!< returns the directory for the filename and optionally adds a slash.
121     /*!< if "add_slash" is true, then the default directory separator will be
122     present on the end of the string. */
123   bool had_directory() const { return _had_directory; }
124     //!< returns true if the name that we were given had a non-empty directory.
125     /*!< this allows one to distinguish between a file with the current
126     directory (.) attached and a file with no directory specified. */
127
128   char drive(bool interact_with_fs = false) const;
129     //!< returns the drive letter for the file, without the colon.
130     /*!< this only makes sense for a fully qualified MS-DOS style name.  if no
131     drive letter is found, then '\0' is returned.  if "interact_with_fs" is
132     true, then the file system will be checked for the actual drive if no
133     drive letter was found in the contents. */
134
135   basis::astring extension() const;
136     //!< returns the extension for the file, if one is present.
137
138   basis::astring rootname() const;
139     //!< returns the root part of the basename without an extension.
140
141   // status functions return true if the characteristic embodied in
142   // the name is also true.
143
144   bool is_directory() const;
145   bool is_writable() const;
146   bool is_readable() const;
147   bool is_executable() const;
148
149   // is_normal makes sure that the file or directory is not a named pipe or other
150   // special type of file.  symbolic links are considered normal.
151   bool is_normal() const;
152
153   enum write_modes {
154     ALLOW_NEITHER = 0x0,
155     ALLOW_READ = 0x1, ALLOW_WRITE = 0x2,
156     ALLOW_BOTH = ALLOW_READ | ALLOW_WRITE
157   };
158
159   enum ownership_modes {
160     NO_RIGHTS = 0x0,
161     USER_RIGHTS = 0x1, GROUP_RIGHTS = 0x2, OTHER_RIGHTS = 0x4,
162     ALL_RIGHTS = USER_RIGHTS | GROUP_RIGHTS | OTHER_RIGHTS
163   };
164
165   bool chmod(int write_mode, int owner_mode) const;
166     //!< changes the access rights on the file.
167
168   //! the default separator for directories per operating system.
169   /*! the PC uses the backward slash to separate file and directory names from
170   each other, while Unix uses the forward slash. */
171   enum directory_separator { pc_separator = '\\', unix_separator = '/' };
172
173   static bool separator(char is_it);
174     //!< returns true if the character "is_it" in question is a separator.
175
176   static basis::astring default_separator();
177     //!< returns the default separator character for this OS.
178
179   static bool legal_character(char to_check);
180     //!< returns true if "to_check" is a valid character in a filename.
181     /*!< this does not consider separator characters; it only looks at the
182     the name components.  also, it is appropriate for the union of the
183     operating systems we support. */
184
185   static void detooth_filename(basis::astring &to_clean, char replacement = '_');
186     //!< takes any known illegal file system characters out of "to_clean".
187     /*!< this prepares "to_clean" for use as a component in a larger filename
188     by ensuring that the file system will not reject the name (as long as a
189     suitable directory path is prepended to the name and permissions allow
190     the file to be created or accessed).  the "replacement" is used as the
191     character that is substituted instead of illegal characters. */
192
193   void separate(bool &rooted, structures::string_array &pieces) const;
194     //!< breaks the filename into its component parts.
195     /*!< this returns an array containing the component names for the path in
196     this filename object.  if the "rooted" flag is set to true, then the path
197     was absolute (i.e. started at '/' in unix.  this notion is not needed for
198     dos/windoze, as the first component will be something like 'a:'). */
199
200   void join(bool rooted, const structures::string_array &pieces);
201     //!< undoes a separate() operation to get the filename back.
202     /*!< "this" is set to a filename made from each of the "pieces".  if there
203     are any directory separators inside the pieces themselves, then they will
204     be removed by canonicalize().  if separate() said the path was rooted,
205     then join needs to be told that. */
206
207   // these implement the packing functionality.
208   virtual void pack(basis::byte_array &packed_form) const;
209   virtual bool unpack(basis::byte_array &packed_form);
210   virtual int packed_size() const;
211
212   bool compare_prefix(const filename &to_compare, basis::astring &sequel);
213     //!< examines "this" filename to see if it's a prefix of "to_compare".
214     /*!< this returns true if all of "this" is the same as the first portion
215     of "to_compare".  that is, if "this" is a prefix of "to_compare", then
216     true is returned.  this will always fail if there are fewer components in
217     "to_compare".  it will always succeed if the two filenames are identical.
218     on success, the "sequel" is set to the portion of "to_compare" that's
219     not included in this filename. */
220
221   bool compare_prefix(const filename &to_compare);
222     //!< this simpler form doesn't bother with computing the sequel.
223
224   bool compare_suffix(const filename &to_compare, basis::astring &prequel);
225     //!< compares the back end of a filename to this.
226     /*!< this is similar to compare_prefix() but it checks to see if the
227     back end of "this" filename is the same as "to_compare".  if "this" is
228     longer than "to_compare", then failure occurs.  only if all of the bits
229     in "this" are seen in the back of "to_compare" is true returned. */
230
231   bool compare_suffix(const filename &to_compare);
232
233   static basis::astring null_device();
234     //!< returns the name for the black hole device that consumes all input, i.e. /dev/null.
235
236 private:
237   bool _had_directory;  //!< true if _some_ directory was specified on init.
238 ///  basis::astring *_contents;  //!< the full path is held here.
239
240   int find_last_separator(const basis::astring &look_at) const;
241     //!< locates the last separator character in the filename.
242
243   bool get_info(status_info *to_fill) const;
244     //!< returns information for the filename.
245
246   // helper functions do the real work for comparing.
247   bool base_compare_prefix(const filename &to_compare, structures::string_array &first,
248           structures::string_array &second);
249   bool base_compare_suffix(const filename &to_compare, structures::string_array &first,
250           structures::string_array &second);
251 };
252
253 } //namespace.
254
255 #endif
256