tbx  0.7.5
path.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010-2014 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include <string>
26 #include <vector>
27 #include "kernel.h"
28 
29 #ifndef TBX_PATH_H
30 #define TBX_PATH_H
31 
32 namespace tbx
33 {
37  const int FILE_TYPE_DIRECTORY = 0x1000;
42  const int FILE_TYPE_APPLICATION = 0x2000;
43 
51  class UTCTime
52  {
53  public:
54  UTCTime();
55  UTCTime(long long csecs);
56  UTCTime(unsigned int loadAddress, unsigned int execAddress);
57  UTCTime(const UTCTime &other);
58 
59  UTCTime &operator=(const UTCTime &other);
60 
61  static UTCTime now();
62 
63  std::string text() const;
64  std::string text(const std::string &format) const;
65 
71  unsigned int low_word() const {return (unsigned int)(_centiseconds & 0xFFFFFFFF);}
77  unsigned char high_byte() const {return (unsigned char)((_centiseconds >> 32) & 0xFF);}
78 
84  long long centiseconds() const {return _centiseconds;}
85 
90  unsigned char *buffer() {return (unsigned char *)&_centiseconds;}
91 
96  unsigned char *buffer() const {return (unsigned char *)&_centiseconds;}
97 
98  protected:
102  long long _centiseconds;
103  };
104 
105  class Path;
106 
107 
111  class PathInfo
112  {
113  public:
114  PathInfo();
115  PathInfo(const PathInfo &other);
116 
117  PathInfo &operator=(const PathInfo &other);
118  bool operator==(const PathInfo &other);
119  bool operator!=(const PathInfo &other);
120 
121  bool read(const Path &path);
122  bool read_raw(const Path &path, bool calc_file_type);
123 
124 
127  {
131  IMAGE_FILE
132  };
133 
136  {
137  OWNER_READ = 0x1,
138  OWNER_WRITE = 0x2,
139  OWNER_LOCKED = 0x8,
140  OTHER_READ = 0x10,
141  OTHER_WRITE = 0x20,
142  OTHER_LOCKED = 0x80
143  };
144 
150  const std::string &name() const {return _name;}
151 
152  // Object type
153  ObjectType object_type() const;
155  bool exists() const {return (_object_type != NOT_FOUND);}
157  bool file() const {return (_object_type == FILE);}
159  bool directory() const {return (_object_type == DIRECTORY);}
161  bool image_file() const {return (_object_type == IMAGE_FILE);}
162 
163  // File type format
164  bool has_file_type() const;
165  int file_type() const;
166  int raw_file_type() const;
167  UTCTime modified_time() const;
168 
169  // Load/Executable format
170  bool has_load_address() const;
171  unsigned int load_address() const;
172  unsigned int exec_address() const;
173 
174  // All formats
175  int length() const;
176  int attributes() const;
177 
184  class Iterator
185  {
186  protected:
187  Iterator(const std::string &dirName, const char *wildCard);
188  friend class PathInfo;
189 
190  public:
191  Iterator();
192  ~Iterator();
193 
194  Iterator(const Iterator &other);
195  Iterator &operator=(const Iterator &other);
196  bool operator==(const Iterator &other);
197  bool operator!=(const Iterator &other);
198 
199  Iterator &operator++();
200  Iterator operator++(int);
201 
202  PathInfo &operator*();
203  PathInfo *operator->();
204 
205  void next();
206 
207  // Variables
208  protected:
214  class IterBlock
215  {
216  public:
217  IterBlock(const std::string &dirName, const char *wildCard);
218  ~IterBlock() {delete _dirName; delete _wildCard;}
219 
220  bool next();
224  const char *next_record() const {return _nextRecord;}
225  bool info(PathInfo &info);
226 
230  void add_ref() {_ref++;}
236  void release() {if (--_ref == 0) delete this;}
237 
238  // Variables
239  int _ref;
240  _kernel_swi_regs _regs;
241  char *_dirName;
242  char *_wildCard;
243  enum {_readSize = 2048};
244  char _readData[_readSize];
245  int _toRead;
246  char *_nextRecord;
248  } *_iterBlock;
249  };
250 
251  friend class Iterator::IterBlock;
252 
253  static PathInfo::Iterator begin(const Path &path, const std::string &wildCard);
254  static PathInfo::Iterator begin(const Path &path);
255  static PathInfo::Iterator end();
256 
257  protected:
258  std::string _name;
260  unsigned int _load_address;
261  unsigned int _exec_address;
262  int _length;
265  };
266 
270  class Path
271  {
272  public:
273  Path();
274  Path(const std::string &name);
275  Path(const char *name);
276  Path(const Path &other);
277  Path(const Path &other, const std::string &child);
278  Path(const Path &other, const char *name);
279 
280  virtual ~Path();
281 
282  // Assignment
283  Path &operator=(const Path &other);
284  Path &operator=(const std::string &name);
285  Path &operator=(const char *name);
286  Path &set(const Path &other, const std::string &child);
287 
288  // Attributes
294  const std::string &name() const {return _name;}
295 
296  operator const std::string&() const;
297  operator const char *() const;
298 
299  Path child(const std::string &child) const;
300  Path parent() const;
301 
302  Path &down(const std::string &child);
303  Path &up();
304  void leaf_name(const std::string &child);
305  std::string leaf_name() const;
306 
307  PathInfo::ObjectType object_type() const;
308  bool path_info(PathInfo &info) const;
309  bool raw_path_info(PathInfo &info, bool calc_file_type) const;
310 
311  bool exists() const;
312  bool file() const;
313  bool directory() const;
314  bool image_file() const;
315 
316  // File information
317  int file_type() const;
318  bool file_type(int type);
319  static int file_type(const std::string &file_name);
320  static bool file_type(const std::string &file_name, int type);
321  int raw_file_type() const;
322 
323  int attributes() const;
324  bool attributes(int new_attributes);
325 
326 
327  UTCTime modified_time() const;
328 //TODO: bool modified_time(const UTCTime &utcTime);
329 
330  // Creation
331  void create_file(int type) const;
332  void create_directory() const;
333 
334  // Deletion
335  void remove() const;
336 
337  // Simple renaming
338  void rename(const std::string &new_name);
339 
345  enum CopyOption {
346  COPY_RECURSE=1, // Recursively copy
347  COPY_FORCE=2, // Overwrite destination if it already exists
348  COPY_ALLOW_PRINT = 0x100u, // Allow copy to printer
349  COPY_NO_ATTRIBUTES = 0x200u, // Don't copy attributes
350  COPY_STAMP = 0x400u, // Reset date stamp
351  COPY_STRUCTURE = 0x800u, // Copy directory structure but not files
352  COPY_NEWER = 0x1000u, // Copy if newer then destination only
353  COPY_LOOK = 0x4000u // Check destination first
354  };
355 
356  void copy(const std::string &copyto, unsigned int options = 0);
357  void copy(const std::string &copyto, unsigned int options, void *buffer, unsigned int size);
358  void move(const std::string &copyto, unsigned int options = 0);
359  void move(const std::string &copyto, unsigned int options, void *buffer, unsigned int size);
360 
361  // Whole file loading/saving
362  char *load_file(int *length = 0) const;
363  void save_file(const char *data, int length, int file_type) const;
364 
365  bool set_current_directory() const;
366 
367  void canonicalise();
368  static std::string canonicalise(const std::string &path);
369  bool canonical_equals(const tbx::Path &compare_to) const;
370  bool canonical_equals(const std::string &compare_to) const;
371 
372  static Path temporary(const char *prefix = 0);
373  //Operators
374 
381  class Iterator
382  {
383  protected:
384  Iterator(const std::string &dirName, const char *wildCard);
385  friend class Path;
386 
387  public:
388  Iterator();
389  ~Iterator() {if (_iterBlock) _iterBlock->release();}
390 
391  Iterator(const Iterator &other);
392  Iterator &operator=(const Iterator &other);
393  bool operator==(const Iterator &other);
394  bool operator!=(const Iterator &other);
395 
396  Iterator &operator++();
397  Iterator operator++(int);
398 
404  std::string &operator*() {return _name;};
410  std::string *operator->() {return &_name;};
411 
412  void next();
413 
414  // Variables
418  std::string _name;
419 
423  class IterBlock
424  {
425  public:
426  IterBlock(const std::string &dirName, const char *wildCard);
427  ~IterBlock() {delete _dirName; delete _wildCard;}
428 
429  bool next();
430 
436  const char *next_name() const {return _nextName;}
437 
441  void add_ref() {_ref++;}
447  void release() {if (--_ref == 0) delete this;}
448 
449  // Variables
450  int _ref;
451  _kernel_swi_regs _regs;
452  char *_dirName;
453  char *_wildCard;
454  enum {_readSize = 2048};
455  char _readData[_readSize];
456  int _toRead;
457  char *_nextName;
458  } *_iterBlock;
459  };
460 
461  Path::Iterator begin(const std::string &wildCard);
462  Path::Iterator begin();
463  Path::Iterator end();
464 
465  protected:
469  std::string _name;
470  };
471 
472 };
473 
474 #endif // TBX_PATH_H
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
unsigned char * buffer()
Pointer to start of time in memory.
Definition: path.h:90
const std::string & name() const
Get file name of path.
Definition: path.h:294
UTCTime()
Construct UTC time for Midnight, 1st Jan 1990.
Definition: path.cc:1078
PathInfo * _info
Definition: path.h:209
bool directory() const
Definition: path.h:159
Iterator used to iterate through a directory.
Definition: path.h:184
int _attributes
Definition: path.h:263
bool exists() const
Definition: path.h:155
void release()
Decrease reference count on this block.
Definition: path.h:236
Class to manipulate RISC OS file and directory path names.
Definition: path.h:270
UTCTime & operator=(const UTCTime &other)
Assign to value fo another UTCTime.
Definition: path.cc:1121
Definition: path.h:128
int _length
Definition: path.h:262
Iterator to step through files in a folder.
Definition: path.h:381
Definition: path.h:130
int _ref
Definition: path.h:450
Low level class to deal with the file iteration kernel calls.
Definition: path.h:214
CopyOption
Enumeration to options for copy method.
Definition: path.h:345
ObjectType
Definition: path.h:126
const char * next_name() const
Get next name.
Definition: path.h:436
int _toRead
Definition: path.h:456
const std::string & name() const
Get the leaf name of the object the information if for.
Definition: path.h:150
char * _nextRecord
Definition: path.h:246
void release()
Decrease reference count on this block.
Definition: path.h:447
Attribute
Definition: path.h:135
std::string text() const
Get time/date as text in standard format.
Definition: path.cc:1155
Low level class to deal with the file iteration kernel calls.
Definition: path.h:423
char * _wildCard
Definition: path.h:242
void add_ref()
Increase reference count on this block.
Definition: path.h:441
char * _dirName
Definition: path.h:241
int _toRead
Definition: path.h:245
const int FILE_TYPE_APPLICATION
Special file type returned for an application directory.
Definition: path.h:42
unsigned int _load_address
Definition: path.h:260
int _file_type
Definition: path.h:264
char * _nextName
Definition: path.h:457
const char * next_record() const
Return next record from iteration block.
Definition: path.h:224
unsigned int _exec_address
Definition: path.h:261
char * _wildCard
Definition: path.h:453
bool file() const
Definition: path.h:157
const int FILE_TYPE_DIRECTORY
Special file type returned for a directory.
Definition: path.h:37
_kernel_swi_regs _regs
Definition: path.h:451
Class to handle the 5 byte times.
Definition: path.h:51
ObjectType _object_type
Definition: path.h:259
long long _centiseconds
Number of centiseconds since Midnight Jan 1st 1900.
Definition: path.h:102
Definition: path.h:129
void add_ref()
Increase reference count on this block.
Definition: path.h:230
std::string _name
Variable for current file name.
Definition: path.h:418
std::string _name
File name this path refers to.
Definition: path.h:469
unsigned char * buffer() const
Pointer to start of time in memory This is used for calls to the OS that pass a UTC.
Definition: path.h:96
long long centiseconds() const
Get the UTC time as centiseconds.
Definition: path.h:84
static UTCTime now()
Get a UTCTime representing the current time.
Definition: path.cc:1133
int _ref
Definition: path.h:239
char * _dirName
Definition: path.h:452
std::string _name
Definition: path.h:258
unsigned int low_word() const
Get the low 4 bytes of the UTC time.
Definition: path.h:71
std::string & operator*()
Get file name for current iterator.
Definition: path.h:404
unsigned char high_byte() const
Get the high byte of the UTC time.
Definition: path.h:77
std::string * operator->()
Get file name for current iterator.
Definition: path.h:410
bool image_file() const
Definition: path.h:161
_kernel_swi_regs _regs
Definition: path.h:240
Class to hold the catalogue information for a file.
Definition: path.h:111