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

  1. /* $Header: /home/neeri/MacCVS/MacPerl/perl/ext/Mac/Files/Files.xs,v 1.4 1997/11/18 00:52:22 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: Files.xs,v $
  9.  * Revision 1.4  1997/11/18 00:52:22  neeri
  10.  * MacPerl 5.1.5
  11.  *
  12.  * Revision 1.3  1997/09/02 23:06:38  neeri
  13.  * Added Structs, other minor fixes
  14.  *
  15.  * Revision 1.2  1997/06/04 22:55:46  neeri
  16.  * Compiles fine.
  17.  *
  18.  * Revision 1.1  1997/04/07 20:49:37  neeri
  19.  * Synchronized with MacPerl 5.1.4a1
  20.  *
  21.  */
  22.  
  23. #define MAC_CONTEXT
  24.  
  25. #include "EXTERN.h"
  26. #include "perl.h"
  27. #include "XSUB.h"
  28. #include <Folders.h>
  29. #include <Files.h>
  30. #include <TFileSpec.h>
  31. #include <Script.h>
  32. #include <Errors.h>
  33. #include <Aliases.h>
  34.  
  35. typedef FSSpec          RealFSSpec;
  36. typedef CInfoPBPtr  CatInfo;
  37.  
  38. static CatInfo NewCatInfo()
  39. {
  40.     CatInfo ci;
  41.     ci = (CatInfo) malloc(sizeof(CInfoPBRec)+sizeof(Str63));
  42.     ci->hFileInfo.ioNamePtr = (StringPtr) ((char *)ci+sizeof(CInfoPBRec));
  43.     
  44.     return ci;
  45. }
  46.  
  47. MODULE = Mac::Files PACKAGE = Mac::Files
  48.  
  49. =head2 Types
  50.  
  51. =over 4
  52.  
  53. =item FInfo
  54.  
  55. Information for a file, including:
  56.  
  57.     OSType          fdType          the type of the file
  58.     OSType          fdCreator       file's creator
  59.     U16             fdFlags         flags ex. hasbundle,invisible,locked, etc.
  60.     Point           fdLocation      file's location in folder
  61.  
  62. =cut
  63. STRUCT FInfo
  64.     OSType          fdType;                     /*the type of the file*/
  65.     OSType          fdCreator;                  /*file's creator*/
  66.     U16             fdFlags;                    /*flags ex. hasbundle,invisible,locked, etc.*/
  67.     Point           fdLocation;                 /*file's location in folder*/
  68.  
  69. =item FXInfo
  70.  
  71. Further information for a file, including:
  72.  
  73.     short           fdIconID        Icon ID
  74.     I8              fdScript        Script flag and number
  75.     I8              fdXFlags        More flag bits
  76.     short           fdComment       Comment ID
  77.     long            fdPutAway       Home Dir ID
  78.  
  79. =cut
  80. STRUCT FXInfo
  81.     short           fdIconID;                   /*Icon ID*/
  82.     I8              fdScript;                   /*Script flag and number*/
  83.     I8              fdXFlags;                   /*More flag bits*/
  84.     short           fdComment;                  /*Comment ID*/
  85.     long            fdPutAway;                  /*Home Dir ID*/
  86.  
  87. =item DInfo
  88.  
  89. Information for a directory, including:
  90.  
  91.     Rect            frRect          folder rect
  92.     U16             frFlags         Flags
  93.     Point           frLocation      folder location
  94.     short           frView          folder view
  95.  
  96. =cut
  97. STRUCT DInfo
  98.     Rect            frRect;                     /*folder rect*/
  99.     U16             frFlags;                    /*Flags*/
  100.     Point           frLocation;                 /*folder location*/
  101.     short           frView;                     /*folder view*/
  102.  
  103. =item DXInfo
  104.  
  105. Further information for a directory, including:
  106.  
  107.     Point           frScroll        scroll position
  108.     long            frOpenChain     DirID chain of open folders
  109.     I8              frScript        Script flag and number
  110.     I8              frXFlags;       More flag bits
  111.     short           frComment       comment
  112.     long            frPutAway;      DirID
  113.  
  114. =cut
  115. STRUCT DXInfo
  116.     Point           frScroll;                   /*scroll position*/
  117.     long            frOpenChain;                /*DirID chain of open folders*/
  118.     I8              frScript;                   /*Script flag and number*/
  119.     I8              frXFlags;                   /*More flag bits*/
  120.     short           frComment;                  /*comment*/
  121.     long            frPutAway;                  /*DirID*/
  122.  
  123. =back
  124.  
  125. =cut
  126.  
  127. MODULE = Mac::Files PACKAGE = CatInfo
  128.  
  129. STRUCT * CatInfo
  130.     HFileInfo *     STRUCT;
  131.         INPUT:
  132.         XS_INPUT(CatInfo, *(CatInfo *)&STRUCT, $arg);
  133.         OUTPUT:
  134.         XS_PUSH(CatInfo, STRUCT);
  135.     Str255          ioNamePtr;
  136.     short           ioVRefNum;
  137.     short           ioFRefNum;
  138.     I8              ioFVersNum;
  139.     short           ioFDirIndex;
  140.     I8              ioFlAttrib;
  141.     I8              ioACUser;
  142.     FInfo           ioFlFndrInfo;
  143.     long            ioDirID;
  144.     U16             ioFlStBlk;
  145.     long            ioFlLgLen;
  146.     long            ioFlPyLen;
  147.     U16             ioFlRStBlk;
  148.     long            ioFlRLgLen;
  149.     long            ioFlRPyLen;
  150.     time_t          ioFlCrDat;
  151.     time_t          ioFlMdDat;
  152.     time_t          ioFlBkDat;
  153.     FXInfo          ioFlXFndrInfo;
  154.     long            ioFlParID;
  155.     long            ioFlClpSiz;
  156.     DInfo           ioDrUsrWds;
  157.         INPUT:
  158.         XS_INPUT(DInfo, ((DirInfo *)STRUCT)->ioDrUsrWds, $arg);
  159.         OUTPUT:
  160.         XS_OUTPUT(DInfo, ((DirInfo *)STRUCT)->ioDrUsrWds, $arg);
  161.     long            ioDrDirID;
  162.         INPUT:
  163.         XS_INPUT(long, ((DirInfo *)STRUCT)->ioDrDirID, $arg);
  164.         OUTPUT:
  165.         XS_OUTPUT(long, ((DirInfo *)STRUCT)->ioDrDirID, $arg);
  166.     U16             ioDrNmFls;
  167.         INPUT:
  168.         XS_INPUT(U16, ((DirInfo *)STRUCT)->ioDrNmFls, $arg);
  169.         OUTPUT:
  170.         XS_OUTPUT(U16, ((DirInfo *)STRUCT)->ioDrNmFls, $arg);
  171.     time_t          ioDrCrDat;
  172.         INPUT:
  173.         XS_INPUT(time_t, ((DirInfo *)STRUCT)->ioDrCrDat, $arg);
  174.         OUTPUT:
  175.         XS_OUTPUT(time_t, ((DirInfo *)STRUCT)->ioDrCrDat, $arg);
  176.     time_t          ioDrMdDat;
  177.         INPUT:
  178.         XS_INPUT(time_t, ((DirInfo *)STRUCT)->ioDrMdDat, $arg);
  179.         OUTPUT:
  180.         XS_OUTPUT(time_t, ((DirInfo *)STRUCT)->ioDrMdDat, $arg);
  181.     time_t          ioDrBkDat;
  182.         INPUT:
  183.         XS_INPUT(time_t, ((DirInfo *)STRUCT)->ioDrBkDat, $arg);
  184.         OUTPUT:
  185.         XS_OUTPUT(time_t, ((DirInfo *)STRUCT)->ioDrBkDat, $arg);
  186.     DXInfo          ioDrFndrInfo;
  187.         INPUT:
  188.         XS_INPUT(DXInfo, ((DirInfo *)STRUCT)->ioDrFndrInfo, $arg);
  189.         OUTPUT:
  190.         XS_OUTPUT(DXInfo, ((DirInfo *)STRUCT)->ioDrFndrInfo, $arg);
  191.     long            ioDrParID;
  192.         INPUT:
  193.         XS_INPUT(long, ((DirInfo *)STRUCT)->ioDrParID, $arg);
  194.         OUTPUT:
  195.         XS_OUTPUT(long, ((DirInfo *)STRUCT)->ioDrParID, $arg);
  196.  
  197. void
  198. DESTROY(cat)
  199.     CatInfo cat
  200.     CODE:
  201.     free(cat);
  202.  
  203.  
  204. MODULE = Mac::Files PACKAGE = Mac::Files
  205.  
  206. =head2 Functions
  207.  
  208. =over 4
  209.  
  210. =item FSpGetCatInfo FILE [, INDEX ]
  211.  
  212. If INDEX is omitted or 0, returns information about the specified 
  213. file or folder. If INDEX is nonzero, returns information obout the 
  214. nth item in the specified folder.
  215.  
  216. =cut
  217. CatInfo
  218. FSpGetCatInfo(file, index=0)
  219.     FSSpec  file
  220.     short       index
  221.     CODE:
  222.     if ((index && FSpUp(&file)) || !(RETVAL = NewCatInfo())) {
  223.         XSRETURN_UNDEF;
  224.     }
  225.     RETVAL->hFileInfo.ioVRefNum     = file.vRefNum;
  226.     RETVAL->hFileInfo.ioDirID   = file.parID;
  227.     RETVAL->hFileInfo.ioFDirIndex = index;
  228.     if (!index)
  229.         memcpy(RETVAL->hFileInfo.ioNamePtr, file.name, *file.name+1);
  230.     if (gLastMacOSErr = PBGetCatInfoSync(RETVAL)) {
  231.         free(RETVAL);
  232.         XSRETURN_UNDEF;
  233.     }
  234.     OUTPUT:
  235.     RETVAL
  236.  
  237. =item FSpSetCatInfo FILE, INFO
  238.  
  239. Change information about the specified file.
  240.  
  241. =cut
  242. MacOSRet
  243. FSpSetCatInfo(file, info)
  244.     FSSpec  file
  245.     CatInfo info;
  246.     CODE:
  247.     info->hFileInfo.ioVRefNum   = file.vRefNum;
  248.     info->hFileInfo.ioDirID     = file.parID;
  249.     memcpy(info->hFileInfo.ioNamePtr, file.name, *file.name+1);
  250.     RETVAL = PBSetCatInfoSync(info);
  251.     OUTPUT:
  252.     RETVAL
  253.  
  254. =item FSMakeFSSpec VREF, DIRID, NAME
  255.  
  256. Creates a file system specification record from a volume number, 
  257. directory ID, and name. This call never returns a path name.
  258.  
  259. =cut
  260. RealFSSpec
  261. FSMakeFSSpec(vRefNum, dirID, fileName)
  262.     short       vRefNum
  263.     long        dirID
  264.     Str255  fileName
  265.     CODE:
  266.     if (gLastMacOSErr = FSMakeFSSpec(vRefNum, dirID, fileName, &RETVAL)) {
  267.         XSRETURN_UNDEF;
  268.     }
  269.     OUTPUT:
  270.     RETVAL
  271.  
  272. =item FSpCreate FILE, CREATOR, TYPE [, SCRIPTTAG]
  273.  
  274. Creates a file with the specified file creator and type. You don't
  275. want to know what a script tag is.
  276.  
  277. =cut
  278. MacOSRet
  279. FSpCreate(spec, creator, type, scriptTag=smSystemScript)
  280.     FSSpec  &spec
  281.     OSType  creator
  282.     OSType  type
  283.     char        scriptTag
  284.     
  285. =item FSpDirCreate FILE [, SCRIPTTAG]
  286.  
  287. Creates a directory and returns its ID.
  288.  
  289. =cut
  290. long
  291. FSpDirCreate(spec, scriptTag=smSystemScript)
  292.     FSSpec  &spec
  293.     char        scriptTag
  294.     CODE:
  295.     if (gLastMacOSErr = FSpDirCreate(&spec, scriptTag, &RETVAL))
  296.         RETVAL = 0;
  297.     OUTPUT:
  298.     RETVAL
  299.  
  300. =item FSpDelete FILE
  301.  
  302. End the sad existence of a file or (empty) folder.
  303.  
  304. =cut
  305. MacOSRet
  306. FSpDelete(spec)
  307.     FSSpec  &spec
  308.  
  309. =item FSpGetFInfo FILE
  310.  
  311. Returns finder info about a specified file.
  312.  
  313. =cut
  314. FInfo
  315. FSpGetFInfo(spec)
  316.     FSSpec  &spec
  317.     CODE:
  318.     if (gLastMacOSErr = FSpGetFInfo(&spec, &RETVAL)) {
  319.         XSRETURN_UNDEF;
  320.     }
  321.     OUTPUT:
  322.     RETVAL
  323.  
  324. =item FSpSetFInfo FILE, INFO
  325.  
  326. Changes the finder info about a specified file.
  327.  
  328. =cut
  329. MacOSRet
  330. FSpSetFInfo(spec, info)
  331.     FSSpec  &spec
  332.     FInfo   &info;
  333.  
  334. =item FSpSetFLock FILE
  335.  
  336. Software lock a file.
  337.  
  338. =cut
  339. MacOSRet
  340. FSpSetFLock(spec)
  341.     FSSpec  &spec
  342.  
  343. =item FSpRstFLock FILE
  344.  
  345. Unlock a file.
  346.  
  347. =cut
  348. MacOSRet
  349. FSpRstFLock(spec)
  350.     FSSpec  &spec
  351.  
  352. =item FSpRename FILE, NAME
  353.  
  354. Rename a file (only the name component).
  355.  
  356. =cut
  357. MacOSRet
  358. FSpRename(spec, newName)
  359.     FSSpec  &spec
  360.     Str255  newName
  361.  
  362. =item FSpCatMove FILE, FOLDER
  363.  
  364. Move a file into a different folder.
  365.  
  366. =cut
  367. MacOSRet
  368. FSpCatMove(source, dest)
  369.     FSSpec  &source
  370.     FSSpec  &dest
  371.  
  372. =item FSpExchangeFiles FILE1, FILE2
  373.  
  374. Swap the contents of two files, e.g. if you saved to a temp file
  375. and finally swap it with the original.
  376.  
  377. =cut
  378. MacOSRet
  379. FSpExchangeFiles(source, dest)
  380.     FSSpec  &source
  381.     FSSpec  &dest
  382.  
  383. =item NewAlias FILE
  384.  
  385. Returns an AliasHandle for the file.
  386.  
  387. =cut
  388. Handle
  389. NewAlias(target)
  390.     FSSpec  &target
  391.     CODE:
  392.     gLastMacOSErr = NewAlias(nil, &target, (AliasHandle *)&RETVAL);
  393.     OUTPUT:
  394.     RETVAL
  395.  
  396. =item NewAliasRelative FROM, FILE
  397.  
  398. Returns a AliasHandle relative to FROM for the file.
  399.  
  400. =cut
  401. Handle
  402. NewAliasRelative(from, target)
  403.     FSSpec  &from
  404.     FSSpec  &target
  405.     CODE:
  406.     gLastMacOSErr = NewAlias(&from, &target, (AliasHandle *)&RETVAL);
  407.     OUTPUT:
  408.     RETVAL
  409.  
  410. =item NewAliasMinimal FILE
  411.  
  412. Returns an AliasHandle containing minimal information for the file.
  413. This type of alias is best suited for short lived aliases, e.g. in
  414. AppleEvents.
  415.  
  416. =cut
  417. Handle
  418. NewAliasMinimal(target)
  419.     FSSpec  &target
  420.     CODE:
  421.     gLastMacOSErr = NewAliasMinimal(&target, (AliasHandle *)&RETVAL);
  422.     OUTPUT:
  423.     RETVAL
  424.  
  425. =item NewAliasMinimalFromFullPath NAME [, ZONE [, SERVER]]
  426.  
  427. Create a new alias containing only the path name.
  428.  
  429. =cut
  430. Handle
  431. NewAliasMinimalFromFullPath(name, zone=, server=)
  432.     char *  name
  433.     Str255  zone
  434.     Str255  server
  435.     CODE:
  436.     if (items < 3)
  437.         server[0] = 0;
  438.     if (items < 2)
  439.         zone[0] = 0;
  440.     gLastMacOSErr = 
  441.         NewAliasMinimalFromFullPath(strlen(name), name, zone, server, (AliasHandle *)&RETVAL);
  442.     OUTPUT:
  443.     RETVAL
  444.  
  445. =item UpdateAlias TARGET, ALIAS
  446.  
  447. Updates an alias to point to the target file. Returns whether the alias was changed..
  448.  
  449. =cut
  450. Boolean
  451. UpdateAlias(target, alias)
  452.     FSSpec  target
  453.     Handle  alias
  454.     CODE:
  455.     if (gLastMacOSErr = UpdateAlias(nil, &target, (AliasHandle) alias, &RETVAL)) {
  456.         XSRETURN_UNDEF;
  457.     }
  458.     OUTPUT:
  459.     RETVAL
  460.  
  461. =item UpdateAliasRelative FROM, TARGET, ALIAS
  462.  
  463. Updates an alias to point to the target file relative to FROM. Returns whether 
  464. the alias was changed..
  465.  
  466. =cut
  467. Boolean
  468. UpdateAliasRelative(from, target, alias)
  469.     FSSpec  &from
  470.     FSSpec  &target
  471.     Handle  alias
  472.     CODE:
  473.     if (gLastMacOSErr = UpdateAlias(&from, &target, (AliasHandle) alias, &RETVAL)) {
  474.         XSRETURN_UNDEF;
  475.     }
  476.     OUTPUT:
  477.     RETVAL
  478.  
  479. =item ResolveAlias ALIAS
  480.  
  481. Resolves an alias. In scalar context, returns the path to the resolved file.
  482. In list context, also returns whether the alias was changed when resolving.
  483.  
  484. =cut
  485. void
  486. ResolveAlias(alias)
  487.     Handle  alias
  488.     PPCODE:
  489.     {
  490.         FSSpec  target;
  491.         Boolean changed;
  492.         
  493.         gLastMacOSErr = ResolveAlias(nil, (AliasHandle) alias, &target, &changed);
  494.         
  495.         if (gLastMacOSErr)  {
  496.             XSRETURN_EMPTY;
  497.         } 
  498.         XS_XPUSH(FSSpec, target);
  499.         if (GIMME == G_ARRAY) {
  500.             XS_XPUSH(Boolean, changed);
  501.         }
  502.     }
  503.  
  504. =item ResolveAliasRelative FROM, ALIAS
  505.  
  506. Resolves an alias relative to a file. In scalar context, returns the path to the 
  507. resolved file. In list context, also returns whether the alias was changed when 
  508. resolving.
  509.  
  510. =cut
  511. void
  512. ResolveAliasRelative(from, alias)
  513.     FSSpec  &from
  514.     Handle  alias
  515.     PPCODE:
  516.     {
  517.         FSSpec  target;
  518.         Boolean changed;
  519.         
  520.         gLastMacOSErr = ResolveAlias(&from, (AliasHandle) alias, &target, &changed);
  521.         
  522.         if (gLastMacOSErr)  {
  523.             XSRETURN_EMPTY;
  524.         } 
  525.         XS_XPUSH(FSSpec, target);
  526.         if (GIMME == G_ARRAY) {
  527.             XS_XPUSH(Boolean, changed);
  528.         }
  529.     }
  530.  
  531. =item GetAliasInfo ALIAS, INDEX
  532.  
  533. Return a component of the alias information.
  534.  
  535. =cut
  536. Str255
  537. GetAliasInfo(alias, index)
  538.     Handle  alias
  539.     short   index
  540.     CODE:
  541.     if (gLastMacOSErr = GetAliasInfo((AliasHandle) alias, index, RETVAL)) {
  542.         XSRETURN_UNDEF;
  543.     }
  544.     OUTPUT:
  545.     RETVAL
  546.  
  547. =item UnmountVol (VOLUMENAME | VOLUMENR)
  548.  
  549. Unmounts a volume.
  550.  
  551. =cut
  552. MacOSRet
  553. _UnmountVol(volName, vRefNum)
  554.     Str255  volName
  555.     short   vRefNum
  556.     CODE:
  557.     RETVAL = UnmountVol(volName, vRefNum);
  558.     OUTPUT:
  559.     RETVAL
  560.  
  561. =item Eject (VOLUMENAME | VOLUMENR)
  562.  
  563. Ejects a volume, placing it offline.
  564.  
  565. =cut
  566. MacOSRet
  567. _Eject(volName, vRefNum)
  568.     Str255  volName
  569.     short   vRefNum
  570.     CODE:
  571.     RETVAL = Eject(volName, vRefNum);
  572.     OUTPUT:
  573.     RETVAL
  574.  
  575. =item FlushVol (VOLUMENAME | VOLUMENR)
  576.  
  577. Flush pending write operations on a volume.
  578.  
  579. =cut
  580. MacOSRet
  581. _FlushVol(volName, vRefNum)
  582.     Str255  volName
  583.     short   vRefNum
  584.     CODE:
  585.     RETVAL = FlushVol(volName, vRefNum);
  586.     OUTPUT:
  587.     RETVAL
  588.  
  589. =item FindFolder VREF, FOLDERTYPE [, CREATE]
  590.  
  591. Returns a path to a special folder on the given volume (specify 
  592. C<kOnSystemDisk> for the boot volume). For FOLDERTYPE, you can specify
  593. any of the C<kXXXFolderType> constants listed above.
  594.  
  595. =cut
  596. FSSpec
  597. FindFolder(vRefNum, folderType, createFolder=0)
  598.     short   vRefNum
  599.     OSType  folderType
  600.     Boolean     createFolder
  601.     CODE:
  602.     if (gLastMacOSErr = FindFolder(vRefNum, folderType, createFolder, &RETVAL.vRefNum, &RETVAL.parID)) {
  603.         XSRETURN_UNDEF;
  604.     }
  605.     FSpUp(&RETVAL);
  606.     OUTPUT:
  607.     RETVAL
  608.  
  609. =back
  610.  
  611. =cut
  612.