home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Files / PedFSRef.cc < prev   
Encoding:
C/C++ Source or Header  |  2000-06-24  |  5.6 KB  |  361 lines

  1. /* PedFSRef.cc */
  2.  
  3. #include <Errors.h>
  4. #include <Files.h>
  5. #include <Resources.h>
  6. #include <Script.h>
  7.  
  8. #include <string.h>
  9.  
  10. #include "DGDebugging.h"
  11.  
  12. #include "pstring.h"
  13. #include "SiesString.hh"
  14. #include "NGLList.hh"
  15. #include "NGLIterator.hh"
  16. #include "MoreFilesExtras.h"
  17.  
  18. #include "PedFSRef.hh"
  19.  
  20.  
  21. void
  22. Split(const SiesString &inString, char inChar, NGLList<SiesString *> &ioList);
  23. void
  24. Split(const SiesString &inString, char inChar, NGLList<SiesString *> &ioList)
  25. {
  26.     long len = inString.Length();
  27.     if (len == 0) {
  28.         return;
  29.     }
  30.     char *data = new char [len];
  31.     
  32.     inString.GetData(data, len);
  33.     
  34.     char *p, *q, *end;
  35.     
  36.     p = data;
  37.     end = p + len;
  38.     while (1) {
  39.         q = p;
  40.         while (p < end && *p++ != inChar) ;
  41.         SiesString *str = new SiesString(q, p - q);
  42.         ioList.Append(str);
  43.         if (p >= end) {
  44.             break;
  45.         }
  46.         p++;
  47.     }
  48.     delete [] data;
  49. }
  50.  
  51.  
  52. PedFSRef::PedFSRef()
  53. {
  54.     mFSS.vRefNum = 0;
  55.     mFSS.parID = 0;
  56.     mFSS.name[0] = 0;
  57. }
  58.  
  59. PedFSRef::PedFSRef(const FSSpec &inFSS)
  60. : mFSS(inFSS)
  61. {
  62. }
  63.  
  64. PedFSRef::PedFSRef(short inVRefNum, long inDir, const char *inName)
  65. {
  66.     mFSS.vRefNum = inVRefNum;
  67.     mFSS.parID = inDir;
  68.     Pstrcpy(mFSS.name, inName);
  69. }
  70.  
  71. PedFSRef::PedFSRef(short inVRefNum, long inDir, const unsigned char *inName)
  72. {
  73.     mFSS.vRefNum = inVRefNum;
  74.     mFSS.parID = inDir;
  75.     PstrPcpy(mFSS.name, inName);
  76. }
  77.  
  78. PedFSRef::PedFSRef(const char *inPathname)
  79. {
  80.     if (inPathname[0] == ':') {
  81.         throw;
  82.     }
  83.     SiesString str(inPathname);
  84.     NGLList<SiesString *> list;
  85.     Split(str, ':', list);
  86.     SiesString *volName = list.Behead();
  87.     // for each subdir, ...
  88.     NGLIterator<SiesString *> iter(list);
  89.     SiesString *subdir;
  90.     while (iter.GetNext(subdir)) {
  91.         
  92.     }
  93. }
  94.  
  95. PedFSRef::PedFSRef(const unsigned char *inPathname)
  96. {
  97.     OSErr err;
  98.     
  99.     err = ::FSMakeFSSpec(0, 0, inPathname, &mFSS);
  100.     ThrowIfOSErr_(err);
  101. }
  102.  
  103. PedFSRef::~PedFSRef()
  104. {
  105. }
  106.  
  107. const FSSpec &
  108. PedFSRef::FSS() const
  109. {
  110.     return mFSS;
  111. }
  112.  
  113. void
  114. PedFSRef::SetFSSpec(const FSSpec &inFSS)
  115. {
  116.     mFSS = inFSS;
  117. }
  118.  
  119. void
  120. PedFSRef::SetVolDefault(short inVRefNum)
  121. {
  122.     OSErr err;
  123.     
  124.     err = ::FSMakeFSSpec(inVRefNum, 0, NULL, &mFSS);
  125.     ThrowIfOSErr_(err);
  126. }
  127.  
  128. void
  129. PedFSRef::SetVolRoot(short inVRefNum)
  130. {
  131.     OSErr err;
  132.     
  133.     err = ::FSMakeFSSpec(inVRefNum, 2, NULL, &mFSS);
  134.     ThrowIfOSErr_(err);
  135. }
  136.  
  137. void
  138. PedFSRef::SetVolDir(short inVRefNum, long inDir)
  139. {
  140.     OSErr err;
  141.     
  142.     err = ::FSMakeFSSpec(inVRefNum, inDir, NULL, &mFSS);
  143.     ThrowIfOSErr_(err);
  144. }
  145.  
  146. void
  147. PedFSRef::SetVolDirName(short inVRefNum, long inDir, const char *inName)
  148. {
  149.     Str255 name;
  150.     Pstrcpy(name, inName);
  151.     SetVolDirName(inVRefNum, inDir, name);
  152. }
  153.  
  154. void
  155. PedFSRef::SetVolDirName(short inVRefNum, long inDir, const unsigned char *inName)
  156. {
  157.     OSErr err;
  158.     
  159.     err = ::FSMakeFSSpec(inVRefNum, inDir, inName, &mFSS);
  160.     ThrowIfOSErr_(err);
  161. }
  162.  
  163. void
  164. PedFSRef::SetPathname(const char *inPathname)
  165. {
  166.     
  167. }
  168.  
  169. void
  170. PedFSRef::SetPathname(const unsigned char *inPathname)
  171. {
  172.     OSErr err;
  173.     
  174.     err = ::FSMakeFSSpec(0, 0, inPathname, &mFSS);
  175.     ThrowIfOSErr_(err);
  176. }
  177.  
  178.  
  179. bool
  180. PedFSRef::Exists()
  181. {
  182.     short realVRefNum;
  183.     long realDirID;
  184.     Str255 realName;
  185.     Boolean isDir;
  186.     OSErr err;
  187.     
  188.     err = ::GetObjectLocation(mFSS.vRefNum, mFSS.parID, mFSS.name, 
  189.         &realVRefNum, &realDirID, realName, &isDir);
  190.     return (err == noErr);
  191. }
  192.  
  193. bool
  194. PedFSRef::IsFile()
  195. {
  196.     short realVRefNum;
  197.     long realDirID;
  198.     Str255 realName;
  199.     Boolean isDir;
  200.     OSErr err;
  201.     
  202.     err = ::GetObjectLocation(mFSS.vRefNum, mFSS.parID, mFSS.name, 
  203.         &realVRefNum, &realDirID, realName, &isDir);
  204.     return (err == noErr) && !isDir;
  205. }
  206.  
  207. bool
  208. PedFSRef::IsDirectory()
  209. {
  210.     short realVRefNum;
  211.     long realDirID;
  212.     Str255 realName;
  213.     Boolean isDir;
  214.     OSErr err;
  215.     
  216.     err = ::GetObjectLocation(mFSS.vRefNum, mFSS.parID, mFSS.name, 
  217.         &realVRefNum, &realDirID, realName, &isDir);
  218.     return (err == noErr) && isDir;
  219. }
  220.  
  221. void
  222. PedFSRef::CreateFile(OSType inCreator, OSType inType)
  223. {
  224.     OSErr err;
  225.     
  226.     err = ::FSpCreate(&mFSS, inCreator, inType, smRoman);
  227.     ThrowIfOSErr_(err);
  228. }
  229.  
  230. void
  231. PedFSRef::CreateDirectory()
  232. {
  233.     OSErr err;
  234.     long dirID;
  235.     
  236.     err = ::FSpDirCreate(&mFSS, smRoman, &dirID);
  237.     ThrowIfOSErr_(err);
  238. }
  239.  
  240. AccessPath
  241. PedFSRef::OpenData(SInt8 inPerm) const
  242. {
  243.     OSErr err;
  244.     AccessPath refNum;
  245.     FSSpec spec;
  246.     
  247.     spec = FSS();
  248.     
  249.     err = ::FSpOpenDF(&spec, inPerm, &refNum);
  250.     ThrowIfOSErr_(err);
  251.     return refNum;
  252. }
  253.  
  254. AccessPath
  255. PedFSRef::OpenRF(SInt8 inPerm) const
  256. {
  257.     OSErr err;
  258.     AccessPath refNum;
  259.     FSSpec spec;
  260.     
  261.     spec = FSS();
  262.     
  263.     err = ::FSpOpenRF(&spec, inPerm, &refNum);
  264.     ThrowIfOSErr_(err);
  265.     return refNum;
  266. }
  267.  
  268. AccessPath
  269. PedFSRef::OpenResFile(SInt8 inPerm) const
  270. {
  271.     OSErr err;
  272.     AccessPath refNum;
  273.     FSSpec spec;
  274.     
  275.     spec = FSS();
  276.     
  277.     refNum = ::FSpOpenResFile(&spec, inPerm);
  278.     err = ::ResError();
  279.     ThrowIfOSErr_(err);
  280.     return refNum;
  281. }
  282.  
  283. PedFSRef &
  284. PedFSRef::operator-(short inLevels)
  285. {
  286.     return *this;
  287. }
  288.  
  289. PedFSRef &
  290. PedFSRef::operator-=(short inLevels)
  291. {
  292.     long parID;
  293.     OSErr err;
  294.     
  295.     err = ::GetParentID(mFSS.vRefNum, mFSS.parID, mFSS.name, &parID);
  296.     if (err == fnfErr) {
  297.         parID = mFSS.parID;
  298.     } else {
  299.         ThrowIfOSErr_(err);
  300.     }
  301.     err = ::FSMakeFSSpec(mFSS.vRefNum, parID, NULL, &mFSS);
  302.     ThrowIfOSErr_(err);
  303.     return *this;
  304. }
  305.  
  306. PedFSRef &
  307. PedFSRef::operator--()
  308. {
  309.     return ((*this) -= 1);
  310. }
  311.  
  312.  
  313. PedFSRef &
  314. PedFSRef::operator+(const char *inName)
  315. {
  316.     return *this;
  317. }
  318.  
  319. PedFSRef &
  320. PedFSRef::operator+(const unsigned char *inName)
  321. {
  322.     return *this;
  323. }
  324.  
  325. PedFSRef &
  326. PedFSRef::operator+=(const char *inName)
  327. {
  328.     if (inName == NULL) {
  329.         return *this;
  330.     }
  331.     
  332.     Str255 name;
  333.     
  334.     if (strlen(inName) > 255) {
  335.         throw;
  336.     }
  337.     Pstrcpy(name, inName);
  338.     return (*this += name);
  339. }
  340.  
  341. PedFSRef &
  342. PedFSRef::operator+=(const unsigned char *inName)
  343. {
  344.     long dirID;
  345.     Boolean isDir;
  346.     OSErr err;
  347.     
  348.     err = ::FSpGetDirectoryID(&mFSS, &dirID, &isDir);
  349.     ThrowIfOSErr_(err);
  350.     if (!isDir) {
  351.         throw;
  352.     }
  353.     err = ::FSMakeFSSpec(mFSS.vRefNum, dirID, inName, &mFSS);
  354.     // fnfErr indicates a valid spec of a location with no item in it.
  355.     if (err != noErr && err != fnfErr) {
  356.         ThrowOSErr_(err);
  357.     }
  358.     return *this;
  359. }
  360.  
  361.