home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Mac / MF.xs < prev    next >
Text File  |  1998-04-05  |  8KB  |  306 lines

  1. /* $Header: /home/neeri/MacCVS/MacPerl/perl/ext/Mac/MoreFiles/MF.xs,v 1.2 1997/11/18 00:52:46 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1996 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: MF.xs,v $
  9.  * Revision 1.2  1997/11/18 00:52:46  neeri
  10.  * MacPerl 5.1.5
  11.  *
  12.  * Revision 1.1  1997/04/07 20:50:06  neeri
  13.  * Synchronized with MacPerl 5.1.4a1
  14.  *
  15.  */
  16.  
  17. #define MAC_CONTEXT
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21. #include "XSUB.h"
  22. #include <Types.h>
  23. #include <Memory.h>
  24. #include <Files.h>
  25. #include <TFileSpec.h>
  26. #include "MoreFiles.h"
  27. #include "FileCopy.h"
  28. #include "IterateDirectory.h"
  29. #include "DirectoryCopy.h"
  30. #include "MoreDesktopMgr.h"
  31.  
  32. static SV * newMortalFSSpec(short vRefNum, long dirID, ConstStr255Param name)
  33. {
  34.     FSSpec  spec;
  35.     
  36.     spec.vRefNum    = vRefNum;
  37.     spec.parID      = dirID;
  38.     memcpy(spec.name, name, *name+1);
  39.     
  40.     return sv_2mortal(newSVpv(FSp2FullPath(&spec), 0));
  41. }
  42.  
  43. static SV * gMFProc;
  44.  
  45. static pascal Boolean MFErrHdlr(    
  46.         OSErr error,
  47.         short failedOperation,
  48.         short srcVRefNum,
  49.         long srcDirID,
  50.         ConstStr255Param srcName,
  51.         short dstVRefNum,
  52.         long dstDirID,
  53.         ConstStr255Param dstName)
  54. {
  55.     if (gMFProc) {
  56.         Boolean     res;
  57.         dSP;
  58.         ENTER;
  59.         SAVETMPS;
  60.         
  61.         PUSHMARK(sp);
  62.         XPUSHs(sv_2mortal(newSViv(error)));
  63.         XPUSHs(sv_2mortal(newSViv(failedOperation)));
  64.         XPUSHs(newMortalFSSpec(srcVRefNum, srcDirID, srcName));
  65.         XPUSHs(newMortalFSSpec(dstVRefNum, dstDirID, dstName));
  66.         PUTBACK;
  67.         
  68.         perl_call_sv(gMFProc, G_SCALAR);
  69.         
  70.         SPAGAIN;
  71.         
  72.         res = (Boolean) POPi;
  73.         
  74.         PUTBACK;
  75.         FREETMPS;
  76.         LEAVE;
  77.         
  78.         return res;
  79.     } else
  80.         return true;
  81. }
  82.  
  83. static pascal void MFIterateFilter(const CInfoPBRec * const cpbPtr,
  84.                                               Boolean *quitFlag,
  85.                                               SV *yourDataPtr)
  86. {
  87.     dSP;
  88.     ENTER;
  89.     SAVETMPS;
  90.     
  91.     PUSHMARK(sp);
  92.     XPUSHs(newMortalFSSpec(
  93.                 cpbPtr->hFileInfo.ioVRefNum, 
  94.                 cpbPtr->hFileInfo.ioFlParID, 
  95.                 cpbPtr->hFileInfo.ioNamePtr));
  96.     XPUSHs(yourDataPtr);
  97.     PUTBACK;
  98.     
  99.     perl_call_sv(gMFProc, G_SCALAR);
  100.     
  101.     SPAGAIN;
  102.     
  103.     *quitFlag = (Boolean) POPi;
  104.     
  105.     PUTBACK;
  106.     FREETMPS;
  107.     LEAVE;
  108. }
  109.  
  110. MODULE = Mac::MoreFiles PACKAGE = Mac::MoreFiles
  111.  
  112. =head2 Functions
  113.  
  114. =over 4
  115.  
  116. =item FSpCreateMinimum SPEC
  117.  
  118. Create a new file with no creator or file type.
  119.     The FSpCreateMinimum function creates a new file without attempting to set 
  120.     the the creator and file type of the new file.  This function is needed to
  121.     create a file in an AppleShare "dropbox" where the user can make
  122.     changes, but cannot see folder or files. The FSSpec in SPEC is used to create
  123.     the file.
  124.  
  125. =cut
  126. MacOSRet
  127. FSpCreateMinimum(spec)
  128.     FSSpec  &spec
  129.  
  130. =item FSpShare SPEC
  131.  
  132. Establish a local volume or directory as a share point.
  133.     The FSpShare function establishes a local volume or directory as a
  134.     share point. SPEC is an FSSpec record specifying the share point.
  135.  
  136. =cut
  137. MacOSRet
  138. FSpShare(spec)
  139.     FSSpec  &spec
  140.  
  141. =item FSpUnshare SPEC
  142.  
  143. The FSpUnshare function removes a share point in SPEC.
  144.  
  145. =cut
  146. MacOSRet
  147. FSpUnshare(spec)
  148.     FSSpec  &spec
  149.  
  150. =item FSpFileCopy SRCSPEC, DSTSPEC, COPYNAME, PREFLIGHT
  151.  
  152. The FSpFileCopy function duplicates a file and optionally renames it.
  153.     Since the PBHCopyFile() routine is only available on some
  154.     AFP server volumes under specific conditions, this routine
  155.     either uses PBHCopyFile(), or does all of the work PBHCopyFile()
  156.     does.  The SRCSPEC is used to
  157.     determine the location of the file to copy.  The DSTSPEC is
  158.     used to determine the location of the
  159.     destination directory.  If COPYNAME <> NIL, then it points
  160.     to the name of the new file.  
  161.  
  162. =cut
  163. MacOSRet
  164. FSpFileCopy(srcSpec, dstSpec, copyName, preflight)
  165.     FSSpec &srcSpec
  166.     FSSpec &dstSpec
  167.     Str255 copyName
  168.     Boolean preflight
  169.     CODE:
  170.     RETVAL = FSpFileCopy(&srcSpec, &dstSpec, copyName, nil, 0, preflight);
  171.     OUTPUT:
  172.     RETVAL
  173.  
  174. =item FSpDirectoryCopy SRCSPEC, DSTSPEC, PREFLIGHT, [COPYERRHANDLER]
  175.  
  176. Make a copy of a directory structure in a new location.
  177. The FSpDirectoryCopy function makes a copy of a directory structure in a
  178. new location. COPYERRHANDLER is the Perl routine name to handle an
  179. error, should one arise. It will be called as:
  180.  
  181.     $bailout = &$COPYERRHANDLER(ERRORCODE,OPERATION,SRCSPEC,DSTSPEC);
  182.  
  183. =cut
  184. MacOSRet
  185. FSpDirectoryCopy(srcSpec, dstSpec, preflight, copyErrHandler = NULL)
  186.     FSSpec  &srcSpec
  187.     FSSpec  &dstSpec
  188.     Boolean preflight
  189.     SV *        copyErrHandler
  190.     CODE:
  191.     gMFProc = copyErrHandler;
  192.     RETVAL = FSpDirectoryCopy(&srcSpec, &dstSpec, nil, 0, preflight, MFErrHdlr);
  193.     OUTPUT:
  194.     RETVAL
  195.  
  196. =item FSpIterateDirectory SPEC, MAXLEVELS, ITERATEFILTER, YOURDATAPTR
  197.  
  198. Iterate (scan) through a directory's content.
  199. The FSpIterateDirectory function performs a recursive iteration (scan)
  200. of the specified directory and calls your ITERATEFILTER function once
  201. for each file and directory found.
  202.  
  203. The MAXLEVELS parameter lets you control how deep the recursion goes.
  204. If MAXLEVELS is 1, FSpIterateDirectory only scans the specified directory;
  205. if MAXLEVELS is 2, FSpIterateDirectory scans the specified directory and
  206. one subdirectory below the specified directory; etc. Set MAXLEVELS to
  207. zero to scan all levels.
  208.  
  209. The YOURDATAPTR parameter can point to whatever data structure you might
  210. want to access from within the ITERATEFILTER. Your filter function will be
  211. called as:
  212.  
  213.     $quit = &$filterFunction(YOURDATAPTR, SPEC);
  214.  
  215. =cut
  216. MacOSRet
  217. FSpIterateDirectory(spec, maxLevels, iterateFilter, yourDataPtr)
  218.     FSSpec          &spec
  219.     unsigned short  maxLevels
  220.     SV *                iterateFilter
  221.     SV *                yourDataPtr
  222.     CODE:
  223.     gMFProc = iterateFilter;
  224.     RETVAL = FSpIterateDirectory(&spec, maxLevels, (IterateFilterProcPtr)MFIterateFilter, yourDataPtr);
  225.     OUTPUT:
  226.     RETVAL
  227.  
  228. =item FSpDTGetAPPL VOLUME, CREATOR
  229.  
  230. The FSpDTGetAPPL function finds an application (file type 'APPL') with
  231. the specified CREATOR on the specified VOLUME. It first tries to get
  232. the application mapping from the desktop database. If that fails, then
  233. it tries to find an application with the specified creator using
  234. the File Manager's CatSearch() routine. If that fails, then it tries to
  235. find an application in the Desktop file.
  236. Returns FSSpec or C<undef> on failure.
  237.  
  238. =cut
  239. FSSpec  
  240. FSpDTGetAPPL(volume, creator)
  241.     SV *            volume
  242.     OSType      creator
  243.     PREINIT:
  244.     StringPtr   vName = nil;
  245.     Str63           volName;
  246.     short           vRefNum;
  247.     STRLEN      len;
  248.     char *      vol;
  249.     CODE:
  250.     vol = SvPV(volume, len);
  251.     if (len && vol[len-1] == ':')
  252.         CopyC2PStr(vol, (vName = volName));
  253.     else
  254.         vRefNum = SvIV(volume);
  255.     if (gLastMacOSErr = FSpDTGetAPPL(vName, vRefNum, creator, &RETVAL)) {
  256.         XSRETURN_UNDEF;
  257.     }
  258.     OUTPUT:
  259.     RETVAL
  260.  
  261. =item FSpDTSetComment SPEC, COMMENT
  262.  
  263. The FSpDTSetComment function sets a file or directory's Finder comment
  264. field. The volume must support the Desktop Manager because you only
  265. have read access to the Desktop file.
  266.  
  267. =cut
  268. MacOSRet
  269. FSpDTSetComment(spec, comment)
  270.     FSSpec  &spec
  271.     Str255  comment
  272.  
  273. =item FSpDTGetComment SPEC
  274.  
  275. The FSpDTGetComment function gets a file or directory's Finder comment
  276. field (if any) from the Desktop Manager or if the Desktop Manager is
  277. not available, from the Finder's Desktop file.
  278. Returns Str255, or C<undef> on failure.
  279.  
  280. =cut
  281. Str255
  282. FSpDTGetComment(spec)
  283.     FSSpec  &spec
  284.     CODE:
  285.     if (gLastMacOSErr = FSpDTGetComment(&spec, RETVAL)) {
  286.         XSRETURN_UNDEF;
  287.     }
  288.     OUTPUT:
  289.     RETVAL
  290.  
  291. =item FSpDTCopyComment SRCSPEC, DSTSPEC
  292.  
  293. The FSpDTCopyComment function copies the desktop database comment from
  294. the source to the destination object.  Both the source and the
  295. destination volumes must support the Desktop Manager.
  296.  
  297. =cut
  298. MacOSRet
  299. FSpDTCopyComment(srcSpec, dstSpec)
  300.     FSSpec  &srcSpec
  301.     FSSpec  &dstSpec
  302.  
  303. =back
  304.  
  305. =cut
  306.