OOFILE  1.9
ooffiles.h
Go to the documentation of this file.
1 #ifndef H_OOFFILES
2 #define H_OOFFILES
3 
4 // COPYRIGHT 1996-1998 A.D. Software, All rights reserved
5 
6 // see misc.txt for code conventions
7 #ifndef H_OOF1
8  #include "oof1.h"
9 #endif
10 #ifndef H_OOFEXCEP
11  #include "oofexcep.h"
12 #endif
13 #ifdef _Macintosh
14  #if defined _Macintosh_Carbon
15  #ifndef __CARBON__
16  #ifdef _Macintosh_Mach
17  #define __NOEXTENSIONS__ // see Metrowerks help - keeps pure Mach
18  #include <Carbon/Carbon.h>
19  #else
20  # include <Carbon.h>
21  #endif
22  #endif
23  #else
24  #include <Types.h>
25  #include <Files.h>
26  #endif
27 #else
28  #ifdef _Windows
29  #ifndef __SHELLAPI_H
30  #include <shellapi.h>
31  #endif
32  #if !OOF_USE_ANSI_STREAMS
33  // need headers to define tm
34  #endif
35  #endif
36 #endif
37 
38 #ifndef _INC_TIME
39  #include <time.h>
40 #endif
41 
42 
43 class oofSharedBLOB; // forward
44 
67 public:
68  enum isDirT { eDir, eFile, eNotSet };
69  enum { kMaxOSXPathBufferLen=2048 };
70 
71  oofFileRef(const oofString&, isDirT isDir=eNotSet);
72  oofFileRef(const char* path=0, isDirT isDir=eNotSet) ;
73  oofFileRef(const oofDirectory& theDir, const oofString& name, isDirT isDir=eNotSet);
74 #ifdef _Macintosh
75  oofFileRef(const FSSpec&, isDirT isDir=eNotSet);
76  oofFileRef(short vRefNum, long dirID, const oofString& name, isDirT isDir=eNotSet);
77  operator FSSpec&() { return mSpec; };
78  const FSSpec& spec() const { return mSpec; };
79  const oofFileRef& operator=(const FSSpec&);
80  bool scanVolsUntilFound();
81 #endif
82  // use default copy ctor, op=, dtor
83  const oofFileRef& operator=(const oofString&);
84  void clear(); // useful to point the ref elsewhere!
85 
86  bool isValid() const;
87 
88 // feature methods
89  oofFileRef makeRefToContainedFile(const oofString& name) const;
90  void extract(std::ostream&) const;
91  FILE* fopen(const char* mode="rb") const; // make it easy to use C io calls
92 
94 
95  void gotoParentDirectory() const; // make the dir containing file current dir
96  oofString readTextFile() const; // opens and closes file
97  void readBinary(oofSharedBLOB*) const ; // opens and closes file
98  void writeTextFile(oofString& theStr) const;
99  void deleteFile(bool ignoreMissingFiles=true) const; // like a const pointer - the ref remains untouched but file zapped
100  void renameFile(const oofString& newName);
101  void copyToDirectory(const oofDirectory&, bool overwriteDest=false) const;
102  void moveToDirectory(const oofDirectory&, bool overwriteDest=false);
104 
106 
107  oofString path() const;
108  oofString filename() const;
109  oofDirectory directory() const;
110  bool isDirectory() const;
111  tm created() const;
112  tm lastModified() const;
113  unsigned long fileSize(bool wantPhysical=true) const;
114  bool dirIsParent(const oofDirectory&) const;
116 
117 #ifdef _Macintosh
118  OSErr readCInfo(CInfoPBRec& cibr) const; // utility useful to get file info, fnfErr if invalid
119  void setFileType(OSType typeCode, OSType creatorCode=0);
120  void flushVolume() const;
121 #endif
122 
123 private:
124 
125 // data storage
126 #ifdef _Macintosh
127  FSSpec mSpec;
128 #else
129  oofString mPath;
130 #endif
131  isDirT mIsDir;
132 };
133 OOFILE_EXPORT std::ostream& operator<<(std::ostream&, const oofFileRef&);
134 
135 
142 public:
143  oofDirectoryVisitor(bool ignoreBadAliases=false);
144 
145  virtual ~oofDirectoryVisitor() {}; // so subclasses get dtor call
146  virtual void visit(const oofFileRef&, oofDirectory* caller=0)=0;
147  virtual bool enterRoot(oofDirectory* caller=0);
148  virtual bool enterDir(const oofFileRef&, oofDirectory* caller=0);
149  virtual void leaveDir(const oofFileRef&, oofDirectory* caller=0);
150  virtual bool handleLinkUnresolved(const oofFileRef&);
151 
152 protected:
153  short mDirDepth;
155 };
156 
157 
163 public:
164  oofDirectoryList(std::ostream& os, bool listFiles=true, const char* indentString=" ");
165  virtual void visit(const oofFileRef&, oofDirectory* caller);
166  virtual bool enterDir(const oofFileRef&, oofDirectory* caller);
167  virtual void leaveDir(const oofFileRef&, oofDirectory* caller);
168  virtual bool handleLinkUnresolved(const oofFileRef&);
169 
170 protected:
171  virtual void WriteIndentedName(const oofFileRef&) const;
172  virtual void WriteIndent() const;
173 // data storage
174  std::ostream& mOS;
178 };
179 
180 
186 public:
187  oofDelDirTreeVisitor(bool delSubdirs);
188  virtual void visit(const oofFileRef&, oofDirectory* caller);
189  virtual void leaveDir(const oofFileRef&, oofDirectory* caller); // del dir itself on exit
190 
191 protected:
192 // data storage
193  bool mDelSubdirs;
194 };
195 
196 
197 class OOF_ExpandableLongArray; // forward
198 
205 {
206 public:
207  oofFileList();
208  oofFileList(const oofFileList&);
209  ~oofFileList();
210 
211  void append(const oofFileRef&);
212  oofFileRef ref(unsigned short fileIndex) const;
213  unsigned short count() const;
214 
215 private:
216  OOF_ExpandableLongArray* mRefs; // owned
217 };
218 
219 
236 public:
237  oofDirectory();
238 #ifdef _Macintosh
239  oofDirectory(short vRefNum, long dirID, bool searchForDirAllVols=false);
240  oofDirectory(short vRefNum, long dirID, const char* fallBackPath, bool searchForDirAllVols=false);
241  oofDirectory(const FSSpec&);
242  #ifdef _Macintosh_Carbon
243  oofDirectory(const FSRef&);
244  const oofDirectory& operator=(const FSRef&);
245  void setFSRef(const FSRef&);
246  #endif
247 #endif
248  oofDirectory(const char* path, char* outFileName=0, bool createSubdirsIfMissing=false, bool makeAbsolute=true);
249  // a non-null outFileName indicates the caller expects the path to
250  // terminate in a filename
251 // use default copy ctor
252  virtual ~oofDirectory();
253 
254  void setDirFromPath(const char* path, char* outFileName=0, bool createSubdirsIfMissing=false, bool makeAbsolute=true);
255  void gotoDirectory() const;
256 
258 
259  bool validDir() const;
260  bool containsFile(const char* justFileName) const;
261  const oofString& path() const;
262  oofString filename() const;
263  bool isEmpty() const;
264  bool createdDiskDir() const; // ie: created it when this object was constructed
266 
268 
269  oofFileList* makeFileList() const;
270  virtual void visitFiles(oofDirectoryVisitor&, unsigned long remainingVisitDepth=1024, bool firstVisit=true /* private param, don't set */); // visits tree within dir, not including dir!
271  void extract(std::ostream&); // directory listing
273 
274 #ifdef _Macintosh
275  bool openFile(const char* justFileName) const;
277 #elif defined _Windows
278  bool openFile(const char* justFileName, HWND hwnd=NULL) const;
279 #endif
280 
281  void delDirectory(bool delDirAsWellAsContents=true, bool delTree=true, bool delSubdirs=true);
282 
283 #ifdef _Macintosh
284  void fillInFSSpec(const char* nameCanBeNull , FSSpec&) const;
285 #elif defined _Windows
286  static bool convertMac2WinPath(const char* macPath, char* winPath);
287 #endif
288 
289 
290 protected:
291  void GetCurrentLocation();
292 
293 #ifdef _Macintosh
294  void PathToDirID(const unsigned char* pascalPath);
295  void createDirID(const unsigned char* pascalPartialPath=0, bool searchForDirAllVols=false);
296  void CreateHardDirID(const unsigned char* pascalPartialPath, bool searchForDirAllVols);
297  void CreateWorkingDirID(const unsigned char* pascalPartialPath=0, bool searchForDirAllVols=false);
298  void CreateSubDirs(const unsigned char* pascalPartialPath);
299  void CreateSubdirsAndWorkingDirID(const unsigned char* pascalPartialPath);
300  oofString CurrentPath() const;
301 
302 // data storage
303  #ifdef _Macintosh_Carbon
304  FSRef mFSRef;
305  #else
307  #endif
308  long mDirID;
309  short mVRefNum;
310 #endif // _Macintosh
311 
312 #ifdef _Windows
313  #if(WINVER >= 0x0400)
314  void FillInExecInfo(const char* filename, SHELLEXECUTEINFO& sec, HWND hwnd) const;
315  #endif /* WINVER >= 0x0400 */
316  static bool CreateSubdirectories(char* dir);
317  static bool TransformMacDir(const char* macDir, char* winDir);
318  static bool FindWinDrive(char* winDrive, const char* macVol, const char* winDir, const char* fname);
319  static bool SplitMacPath(const char* macPath, char* vol, char* dir, char* file, char* ext);
320 
321  enum {
322  MAX_MAC_VOL = 50,
323  MAX_MAC_DIR = 256,
324  MAX_MAC_FNAME = 256,
325  MAX_MAC_EXT = 10
326  };
327 #endif
328 
330  bool mValidDir;
332 
333 public:
334  static oofDirectory createDirectory(const char* path);
335  static bool fileExists(const char* fullPath);
336  static void gotoDirectory(const oofString&);
337 #ifdef _Macintosh
338  static oofString FSSpecToPath(const FSSpec&);
339  static void gotoDirectory(short vRefNum, long dirID);
340  #ifdef _Macintosh_Carbon
341  static void gotoDirectory(const FSRef&);
342  #else
343  void closeWDDirectory();
344  #endif
345 #endif
346 
347 // utilities for creating and parsing paths
348  static const char* parentDirPath();
349  static const char* relativeRootPath();
350  static char directoryTerminator();
351 };
352 
353 OOFILE_EXPORT std::ostream& operator<<(std::ostream& os, oofDirectory&);
354 
355 
363 public:
364  stSaveDirectory();
365  ~stSaveDirectory();
366 
367 private:
368  oofDirectory mSavedDir;
369 };
370 
371 
372 // define these exceptions here to avoid oofexcep.h having to know about ooffile.h
373 #ifdef _Macintosh
374 
378  class OOFILE_EXPORT oofE_File : public oofE_MacOSErr { // _Macintosh
379  public:
380  oofE_File(const oofFileRef& f, const OSErr e, const char* extraMsg=0);
381  const oofFileRef& file() const;
382 
383  virtual void extract(std::ostream& os) const;
384 
385  private:
386  oofFileRef mFile;
387  };
388 
389 #else
390 
394  class OOFILE_EXPORT oofE_File : public oofE_Base { // _Windows or _Unix
395  public:
396  oofE_File(const oofFileRef& f, const char* extraMsg=0);
397  const oofFileRef& file() const;
398 
399  virtual void extract(std::ostream& os) const;
400 
401  private:
402  oofFileRef mFile;
403  oofString mErrorString;
404  };
405 #endif
406 
407 
414 public:
415  oofE_FileNotFound(const oofFileRef&, const char* extraMsg=0);
416 };
417 
418 
425 public:
426  oofE_InvalidDir(const oofDirectory& d, const char* extraMsg=0) :
427  mDir(d),
428  mErrorString(extraMsg)
429  {};
430  const oofDirectory& dir() const;
431 
432  virtual void extract(std::ostream& os) const;
433 
434 private:
435  oofDirectory mDir;
436  oofString mErrorString;
437 };
438 
439 
445 public:
446  oofE_AliasNotResolved(const oofFileRef& inRef) : mRef(inRef) {};
447 #ifdef _Macintosh
448  const FSSpec& spec() const;
449 #endif
450  const oofFileRef& fileRef() const;
451 
452  virtual void extract(std::ostream& os) const;
453 
454 private:
455  oofFileRef mRef;
456 };
457 
458 
482 public:
483 // limited subset of ctors
484  oofPathString(const char* str=0) : oofString(str) {}; // default
485  oofPathString(const char* inChars, unsigned long numChars) :
486  oofString(inChars, numChars) {};
487  oofPathString(char* adoptedChars, unsigned long numChars, bool adoptThem) :
488  oofString(adoptedChars, numChars, adoptThem) {};
489 
490  enum pathTypeT {
491  eMacRelPath, eUnixRelPath, eDOSRelPath,
492  eMacAbsPath, eUnixAbsPath, eDOSAbsPath,
493  eMacParentRelPath, eUnixParentRelPath, eDOSParentRelPath,
494  eJustFile
495  };
496 
497  pathTypeT pathType() const;
498 
499  bool isRelativePath() const;
500  static bool isRelativePath(const char*);
501  static bool isRelativePath(pathTypeT);
502 
503  static bool isMacPath(pathTypeT);
504  static bool isDOSPath(pathTypeT);
505  static bool isUnixPath(pathTypeT);
506 
507 
509 
510  bool relativePathPlatform(bool appendSeparator = true);
511  bool relativePathMac(bool appendSeparator = true);
512  bool relativePathUnix(bool appendSeparator = true);
513  bool relativePathDOS(bool appendSeparator = true);
515 
522  static char* relativePathDOS(char*, const unsigned long& ioLen, bool appendSeparator= true);
523  static char* relativePathMac(char*, const unsigned long& ioLen, bool appendSeparator= true);
524  static char* relativePathUnix(char*, const unsigned long& ioLen, bool appendSeparator= true);
526 
528  static pathTypeT pathType(const char*);
529 };
530 OOFILE_EXPORT std::ostream& operator<<(std::ostream&, oofPathString::pathTypeT);
531 
532 
533 // -------------------------------------------------------
534 // o o f D i r e c t o r y
535 // -------------------------------------------------------
536 
537 inline bool
539 {
540  return mValidDir;
541 }
542 
543 
544 inline bool
546 {
547  return mCreatedDiskDir;
548 }
549 
550 // Mac returns valid path even if rename dirs because recalculates when called, which is more expensive
551 inline const oofString&
553 {
554 #ifdef _Macintosh
555  return CurrentPath();
556 #else
557  return mPath;
558 #endif
559 }
560 
561 inline const char*
563 {
564 #ifdef _Macintosh
565  return "::";
566 #elif defined _Windows
567  return "..\\";
568 #else // _Unix
569  return "../";
570 #endif
571 }
572 
573 
574 inline const char*
576 {
577 #ifdef _Macintosh
578  return ":";
579 #elif defined _Windows
580  return ".\\";
581 #else // _Unix,
582  return "./";
583 #endif
584 }
585 
586 
587 inline char
589 {
590 #ifdef _Macintosh
591  return ':';
592 #elif defined _Windows
593  return '\\';
594 #else // _Unix
595  return '/';
596 #endif
597 }
598 
599 
600 #ifdef _Macintosh_Carbon
601 inline const oofDirectory&
602 oofDirectory::operator=(const FSRef& inRef)
603 {
604  setFSRef(inRef);
605  return *this;
606 }
607 #endif
608 
609 
610 // -------------------------------------------------------
611 // o o f F i l e R e f
612 // -------------------------------------------------------
613 #ifdef _Macintosh
614 inline oofDirectory
616 {
617  return oofDirectory(mSpec.vRefNum, mSpec.parID);
618 }
619 
620 
621 inline void
623 {
624 // throws oofE_InvalidDir if fails
625  oofDirectory::gotoDirectory(mSpec.vRefNum, mSpec.parID);
626 }
627 
628 
629 #else
630 inline oofString
631 oofFileRef::path() const
632 {
633  return mPath;
634 }
635 
636 #endif
637 
638 
639 // -------------------------------------------------------
640 // o o f E _ A l i a s N o t R e s o l v e d
641 // -------------------------------------------------------
642 #ifdef _Macintosh
643 inline const FSSpec&
645 {
646  return mRef.spec();
647 }
648 #endif
649 
650 
651 inline const oofFileRef&
653 {
654  return mRef;
655 }
656 
657 
658 // -------------------------------------------------------
659 // o o f E _ I n v a l i d D i r
660 // -------------------------------------------------------
661 inline const oofDirectory&
663 {
664  return mDir;
665 }
666 
667 
668 // -------------------------------------------------------
669 // o o f E _ F i l e
670 // -------------------------------------------------------
671 inline const oofFileRef&
673 {
674  return mFile;
675 }
676 
677 
678 // -------------------------------------------------------
679 // o o f P a t h S t r i n g
680 // -------------------------------------------------------
681 inline bool
683 {
684 #if defined(_Macintosh)
685  return relativePathMac(appendSeparator);
686 #elif defined(_MSDOS) // implies _Windows
687  return relativePathDOS(appendSeparator);
688 #elif defined(_Unix)
689  return relativePathUnix(appendSeparator);
690 #else
691  ERROR "must have _Macintosh _Windows or _Unix defined"
692 #endif
693 }
694 
695 
696 inline bool
698 {
699  const bool ret = (
700  (thePath == eMacRelPath) || (thePath == eUnixRelPath) ||
701  (thePath == eDOSRelPath) || (thePath == eMacParentRelPath ) ||
702  (thePath == eDOSParentRelPath) || (thePath == eUnixParentRelPath)
703  );
704  return ret;
705 }
706 
707 
708 inline bool
710 {
711  const bool ret = (
712  (thePath == eMacRelPath) || (thePath == eMacAbsPath) ||
713  (thePath == eMacParentRelPath)
714  );
715  return ret;
716 }
717 
718 
719 inline bool
721 {
722  const bool ret = (
723  (thePath == eDOSRelPath) || (thePath == eDOSAbsPath) ||
724  (thePath == eDOSParentRelPath)
725  );
726  return ret;
727 }
728 
729 
730 inline bool
732 {
733  const bool ret = (
734  (thePath == eUnixRelPath) || (thePath == eUnixAbsPath) ||
735  (thePath == eUnixParentRelPath)
736  );
737  return ret;
738 }
739 
740 
741 inline bool
743 {
744  return isRelativePath(mBody);
745 }
746 
747 
750 {
751  return pathType(mBody);
752 }
753 
754 #endif
755 
756 
static const char * relativeRootPath()
Definition: ooffiles.h:575
const oofString & path() const
Definition: ooffiles.h:552
Exception for file not found error, thrown only if you needed the file.
Definition: ooffiles.h:413
short mVRefNum
volume number of a mounted volume, not meaningful over reboots
Definition: ooffiles.h:309
void gotoParentDirectory() const
Definition: ooffiles.h:622
long mFilesVisited
Definition: ooffiles.h:175
static const char * parentDirPath()
Definition: ooffiles.h:562
oofString mPath
Definition: ooffiles.h:329
virtual void leaveDir(const oofFileRef &, oofDirectory *caller=0)
Definition: ooffiles.cpp:1625
Abstract base class used to visit directory tree.
Definition: ooffiles.h:141
List of oofFileRef objects typically used in a directory listing.
Definition: ooffiles.h:204
static bool isUnixPath(pathTypeT)
Definition: ooffiles.h:731
Exception for failure to resolve an alias (Mac) or link (Windows/Unix).
Definition: ooffiles.h:444
oofDirectory directory() const
Definition: ooffiles.h:615
pathTypeT pathType() const
Definition: ooffiles.h:749
bool mDelSubdirs
flag to indicate if leave empty dir structure behind
Definition: ooffiles.h:193
const oofDirectory & dir() const
Definition: ooffiles.h:662
bool relativePathUnix(bool appendSeparator=true)
Definition: ooffiles.cpp:419
oofString CurrentPath() const
calculates current path so valid if renaming has occurred.
bool relativePathDOS(bool appendSeparator=true)
Definition: ooffiles.cpp:170
oofE_AliasNotResolved(const oofFileRef &inRef)
Definition: ooffiles.h:446
oofE_InvalidDir(const oofDirectory &d, const char *extraMsg=0)
Definition: ooffiles.h:426
static char directoryTerminator()
Definition: ooffiles.h:588
long mDirID
directory ID on specified volume, preserved unless dir recreated.
Definition: ooffiles.h:308
const oofFileRef & fileRef() const
Definition: ooffiles.h:652
bool mCreatedDiskDir
Definition: ooffiles.h:331
Subclass of oofString to isolate path manipulation functions.
Definition: ooffiles.h:481
bool relativePathPlatform(bool appendSeparator=true)
Definition: ooffiles.h:682
char * mBody
Definition: oofstr.h:284
virtual void visit(const oofFileRef &, oofDirectory *caller=0)=0
virtual void extract(std::ostream &) const =0
const FSSpec & spec() const
Definition: ooffiles.h:644
Base class for exceptions returning MacOS error numbers.
Definition: oofexcep.h:497
oofFileRef ref(unsigned short fileIndex) const
Definition: ooffiles.cpp:738
oofString mIndentString
Definition: ooffiles.h:176
bool mValidDir
Definition: ooffiles.h:330
Base class for exception hierarchy.
Definition: oofexcep.h:45
bool relativePathMac(bool appendSeparator=true)
Definition: ooffiles.cpp:296
virtual bool handleLinkUnresolved(const oofFileRef &)
default implementation returns false
Definition: ooffiles.cpp:1587
Portable way to refer to files.
Definition: ooffiles.h:66
std::ostream & mOS
Definition: ooffiles.h:174
utility visitor used to output directory.
Definition: ooffiles.h:162
Array of longs which expands automatically as you write to cells.
Definition: oofarray.h:21
static bool isMacPath(pathTypeT)
Definition: ooffiles.h:709
oofPathString(char *adoptedChars, unsigned long numChars, bool adoptThem)
Definition: ooffiles.h:487
Portable highly capable string class.
Definition: oofstr.h:101
void append(const oofFileRef &)
Definition: ooffiles.cpp:723
bool isRelativePath() const
Definition: ooffiles.h:742
Save the current directory and restore when we leave scope.
Definition: ooffiles.h:362
oofPathString(const char *inChars, unsigned long numChars)
Definition: ooffiles.h:485
Store a shared chunk of binary info with reference count to avoid copying.
Definition: oofStruct.h:29
Provides cross-platform directory specification and iteration.
Definition: ooffiles.h:235
utility visitor used to delete directory tree.
Definition: ooffiles.h:185
virtual ~oofDirectoryVisitor()
Definition: ooffiles.h:145
oofString path() const
std::ostream & operator<<(std::ostream &, const oofE_Base &)
bool validDir() const
Definition: ooffiles.h:538
oofPathString(const char *str=0)
Definition: ooffiles.h:484
virtual void extract(std::ostream &os) const
Definition: oofexcep.cpp:792
Exception for directory not valid error, thrown only if you needed the dir.
Definition: ooffiles.h:424
bool createdDiskDir() const
Definition: ooffiles.h:545
#define OOFILE_EXPORT
Prefix for OOFILE database & general classes to enable optional DLL linking.
Definition: oof0.h:187
virtual bool enterDir(const oofFileRef &, oofDirectory *caller=0)
Called before iterating an oofDirectory's contents.
Definition: ooffiles.cpp:1617
const FSSpec & spec() const
Definition: ooffiles.h:78
unsigned short count() const
Definition: ooffiles.cpp:732
Exception for file operation on the Mac, able to include a file system error number.
Definition: ooffiles.h:378
const oofFileRef & file() const
Definition: ooffiles.h:672
static bool isDOSPath(pathTypeT)
Definition: ooffiles.h:720
short mWorkingDirID
old Mac Classic Working Directory number, combines mVRefNum & mDirID
Definition: ooffiles.h:306
void gotoDirectory() const
Change current dir to this oofDirectory.