feisty meow concerns codebase  2.140
filename.h
Go to the documentation of this file.
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>
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  #define MAX_ABS_PATH MAX_PATH
31  #else
32  #ifdef __APPLE__
33  #include <sys/syslimits.h>
34  #else
35  #include <limits.h>
36  #endif
37  #define MAX_ABS_PATH PATH_MAX
38  #endif
39 /*
40  #ifdef __WIN32__
41  // winsock support...
42 // #undef FD_SETSIZE
43 // #define FD_SETSIZE 1000
44  // if you don't set this, you can only select on a default of 64 sockets.
45  #include <winsock2.h>
46  #include <windows.h>
47  #define MAX_ABS_PATH MAX_PATH
48  #else
49  #ifdef __APPLE__
50  #include <sys/syslimits.h>
51  #else
52  #include <limits.h>
53  #endif
54  #define MAX_ABS_PATH PATH_MAX
55  #endif
56 */
57 #endif
58 
59 namespace filesystem {
60 
62 
63 class filename : public basis::astring, public virtual basis::packable
64 {
65 public:
66  filename();
67  filename(const basis::astring &name);
69 
70  filename(const basis::astring &directory, const basis::astring &name_of_file);
72 
73  filename(const filename &to_copy);
74 
75  DEFINE_CLASS_NAME("filename");
76 
77  virtual ~filename();
78 
79  bool good() const;
81 
84  void reset(const basis::astring &name);
86 
87  const basis::astring &raw() const;
91 
94  filename &operator = (const filename &to_copy);
96  filename &operator = (const basis::astring &to_copy);
98 
100  void canonicalize();
102 
105  bool exists() const;
107 
108  bool unlink() const;
110 
112  filename parent() const;
114 
117 
122  void push(const basis::astring &to_push);
124 
129  filename basename() const;
131  filename dirname() const;
133 
135  basis::astring dirname(bool add_slash) const;
137 
139  bool had_directory() const { return _had_directory; }
141 
144  char drive(bool interact_with_fs = false) const;
146 
151  basis::astring extension() const;
153 
154  basis::astring rootname() const;
156 
157  // status functions return true if the characteristic embodied in
158  // the name is also true.
159 
160  bool is_directory() const;
161  bool is_writable() const;
162  bool is_readable() const;
163  bool is_executable() const;
164 
165  // is_normal makes sure that the file or directory is not a named pipe or other
166  // special type of file. symbolic links are considered normal.
167  bool is_normal() const;
168 
169  enum write_modes {
171  ALLOW_READ = 0x1, ALLOW_WRITE = 0x2,
173  };
174 
176  NO_RIGHTS = 0x0,
177  USER_RIGHTS = 0x1, GROUP_RIGHTS = 0x2, OTHER_RIGHTS = 0x4,
179  };
180 
181  bool chmod(int write_mode, int owner_mode) const;
183 
185 
187  enum directory_separator { pc_separator = '\\', unix_separator = '/' };
188 
189  static bool separator(char is_it);
191 
194 
195  static bool legal_character(char to_check);
197 
201  static void detooth_filename(basis::astring &to_clean, char replacement = '_');
203 
209  void separate(bool &rooted, structures::string_array &pieces) const;
211 
216  void join(bool rooted, const structures::string_array &pieces);
218 
223  // these implement the packing functionality.
224  virtual void pack(basis::byte_array &packed_form) const;
225  virtual bool unpack(basis::byte_array &packed_form);
226  virtual int packed_size() const;
227 
228  bool compare_prefix(const filename &to_compare, basis::astring &sequel);
230 
237  bool compare_prefix(const filename &to_compare);
239 
240  bool compare_suffix(const filename &to_compare, basis::astring &prequel);
242 
247  bool compare_suffix(const filename &to_compare);
248 
249  static basis::astring null_device();
251 
252 private:
253  bool _had_directory;
255 
256  int find_last_separator(const basis::astring &look_at) const;
258 
259  bool get_info(status_info *to_fill) const;
261 
262  // helper functions do the real work for comparing.
263  bool base_compare_prefix(const filename &to_compare, structures::string_array &first,
264  structures::string_array &second);
265  bool base_compare_suffix(const filename &to_compare, structures::string_array &first,
266  structures::string_array &second);
267 };
268 
269 } //namespace.
270 
271 #endif
272 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
void reset()
clears out the contents string.
Definition: astring.h:202
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
Implements a scanner that finds all filenames in the directory specified.
Definition: directory.h:27
Provides operations commonly needed on file names.
Definition: filename.h:64
bool exists() const
returns true if the file exists.
Definition: filename.cpp:426
static bool separator(char is_it)
returns true if the character "is_it" in question is a separator.
Definition: filename.cpp:118
bool is_readable() const
Definition: filename.cpp:341
void canonicalize()
cleans up the filename as needed for the current operating system.
Definition: filename.cpp:157
virtual ~filename()
Definition: filename.cpp:91
void join(bool rooted, const structures::string_array &pieces)
undoes a separate() operation to get the filename back.
Definition: filename.cpp:503
bool good() const
returns true if the filename seems to be valid.
Definition: filename.cpp:99
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: filename.cpp:471
bool is_normal() const
Definition: filename.cpp:357
void separate(bool &rooted, structures::string_array &pieces) const
breaks the filename into its component parts.
Definition: filename.cpp:482
bool compare_prefix(const filename &to_compare, basis::astring &sequel)
examines "this" filename to see if it's a prefix of "to_compare".
Definition: filename.cpp:545
static basis::astring default_separator()
returns the default separator character for this OS.
Definition: filename.cpp:93
basis::astring extension() const
returns the extension for the file, if one is present.
Definition: filename.cpp:299
filename parent() const
returns the parent filename for this one.
Definition: filename.cpp:150
directory_separator
the default separator for directories per operating system.
Definition: filename.h:186
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
Definition: filename.cpp:465
char drive(bool interact_with_fs=false) const
returns the drive letter for the file, without the colon.
Definition: filename.cpp:282
basis::astring rootname() const
returns the root part of the basename without an extension.
Definition: filename.cpp:308
bool compare_suffix(const filename &to_compare, basis::astring &prequel)
compares the back end of a filename to this.
Definition: filename.cpp:600
bool had_directory() const
returns true if the name that we were given had a non-empty directory.
Definition: filename.h:139
static basis::astring null_device()
returns the name for the black hole device that consumes all input, i.e. /dev/null.
Definition: filename.cpp:109
bool chmod(int write_mode, int owner_mode) const
changes the access rights on the file.
Definition: filename.cpp:624
bool is_directory() const
Definition: filename.cpp:325
void push(const basis::astring &to_push)
pushes a new filename onto the current pathname.
Definition: filename.cpp:152
bool is_writable() const
Definition: filename.cpp:333
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
Definition: filename.cpp:97
filename dirname() const
returns the directory for the filename.
Definition: filename.cpp:393
bool is_executable() const
Definition: filename.cpp:349
bool unlink() const
actually removes the file, if possible.
Definition: filename.cpp:101
static void detooth_filename(basis::astring &to_clean, char replacement='_')
takes any known illegal file system characters out of "to_clean".
Definition: filename.cpp:452
DEFINE_CLASS_NAME("filename")
filename basename() const
returns the base of the filename; no directory.
Definition: filename.cpp:385
basis::astring pop()
removes the deepest component of the pathname.
Definition: filename.cpp:138
filename & operator=(const filename &to_copy)
provides assignment for this object, plus a simple string.
Definition: filename.cpp:121
static bool legal_character(char to_check)
returns true if "to_check" is a valid character in a filename.
Definition: filename.cpp:436
virtual int packed_size() const
Estimates the space needed for the packed structure.
Definition: filename.cpp:460
filename()
blank constructor.
Definition: filename.cpp:57
An array of strings with some additional helpful methods.
Definition: string_array.h:32
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37