home *** CD-ROM | disk | FTP | other *** search
/ Launch & Play / spustahrej2.iso / Egoboo / code / mac-file.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-03  |  6.1 KB  |  229 lines

  1. // mac-file.c
  2.  
  3. // Egoboo, Copyright (C) 2000 Aaron Bishop
  4.  
  5. #include "egoboo.h"
  6. #include <Files.h>        /* MacOS specific file stuff */
  7. #include <TextUtils.h>    /* C to Pascal string conversion stuff */
  8.  
  9.  
  10.  
  11. /**> GLOBAL VARIABLES <**/
  12. FSSpec            gFileSpec;
  13. char            gFileType[32];
  14. HFileInfo        gFileInfo;
  15. short            gIndex;
  16. char            gFileName[256];
  17.  
  18.  
  19. //---------------------------------------------------------------------------------------------
  20. //File Routines-------------------------------------------------------------------------------
  21. //---------------------------------------------------------------------------------------------
  22. void make_directory(char *dirname)
  23. {
  24.     // ZZ> This function makes a new directory
  25.     
  26.     FSSpec    fileSpec;
  27.     long    dirID;
  28.     
  29.     // Convert the directory name from a c string to a pascal string for use with the FSSpec
  30.     c2pstr( dirname );
  31.     
  32.     // Create the file spec
  33.     FSMakeFSSpec( 0, 0, ( unsigned char * )dirname, &fileSpec );
  34.     
  35.     // Convert the filename back to a pascal string
  36.     p2cstr( ( unsigned char * )dirname );
  37.     
  38.     FSpDirCreate( &fileSpec, /*ScriptCode scriptTag*/0, &dirID );
  39.     
  40. }
  41.  
  42. //---------------------------------------------------------------------------------------------
  43. void remove_directory(char *dirname)
  44. {
  45.     // ZZ> This function removes a directory
  46.     
  47.     FSSpec    fileSpec;
  48.     
  49.     // Convert the directory name from a c string to a pascal string for use with the FSSpec
  50.     c2pstr( dirname );
  51.     
  52.     // Create the file spec
  53.     FSMakeFSSpec( 0, 0, ( unsigned char * )dirname, &fileSpec );
  54.     
  55.     // Convert the directory name back to a pascal string
  56.     p2cstr( ( unsigned char * )dirname );
  57.     
  58.     // Delete the file
  59.     FSpDelete( &fileSpec );
  60.     
  61. }
  62.  
  63. //---------------------------------------------------------------------------------------------
  64. void delete_file(char *filename)
  65. {
  66.     // ZZ> This function deletes a file
  67.     
  68.     FSSpec    fileSpec;
  69.     
  70.     // Convert the filename from a c string to a pascal string for use with the FSSpec
  71.     c2pstr( filename );
  72.     
  73.     // Create the file spec
  74.     FSMakeFSSpec( 0, 0, ( unsigned char * )filename, &fileSpec );
  75.     
  76.     // Convert the filename back to a pascal string
  77.     p2cstr( ( unsigned char * )filename );
  78.     
  79.     // Delete the file
  80.     FSpDelete( &fileSpec );
  81.     
  82. }
  83.  
  84. //---------------------------------------------------------------------------------------------
  85. void copy_file(char *source, char *dest)
  86. {
  87.     // ZZ> This function copies a file on the local machine
  88.     
  89.     
  90.     
  91. }
  92.  
  93. //---------------------------------------------------------------------------------------------
  94. void delete_directory(char *dirname)
  95. {
  96.     // ZZ> This function deletes all files in a directory,
  97.     //     and the directory itself
  98.     
  99.     FSSpec    fileSpec;
  100.     
  101.     // Convert the directory name from a c string to a pascal string for use with the FSSpec
  102.     c2pstr( dirname );
  103.     
  104.     // Create the file spec
  105.     FSMakeFSSpec( 0, 0, ( unsigned char * )dirname, &fileSpec );
  106.     
  107.     // Convert the directory name back to a pascal string
  108.     p2cstr( ( unsigned char * )dirname );
  109.     
  110.     // Delete the file
  111.     FSpDelete( &fileSpec );
  112.     
  113. }
  114.  
  115. //---------------------------------------------------------------------------------------------
  116. void copy_directory(char *dirname, char *todirname)
  117. {
  118.     // ZZ> This function copies all files in a directory
  119.     
  120.     
  121.     
  122. }
  123.  
  124. //---------------------------------------------------------------------------------------------
  125. void empty_import_directory(void)
  126. {
  127.     // ZZ> This function deletes all the TEMP????.OBJ subdirectories in the IMPORT directory
  128.     
  129.     
  130.     
  131. }
  132.  
  133. //---------------------------------------------------------------------------------------------
  134. //Directory Functions--------------------------------------------------------------------------
  135. //---------------------------------------------------------------------------------------------
  136.  
  137. //---------------------------------------------------------------------------------------------
  138. // Read the first directory entry
  139. char *DirGetFirst(char *search)
  140. {
  141.     
  142.     char    tempStr[256];
  143.     char    *beginTypePos;
  144.     
  145.     /* Set the index */
  146.     gIndex = 1;
  147.     
  148.     /* Set up the HFileInfo */
  149.     gFileInfo.ioNamePtr = ( unsigned char * )gFileName;
  150.     gFileInfo.ioVRefNum = 0;
  151.     
  152.     /* get the file type (file extension) */
  153.     beginTypePos = strstr( search, "*" );
  154.     beginTypePos += 1;
  155.     strncpy( gFileType, beginTypePos, strlen( beginTypePos ) );
  156.     
  157.     /* copy the directory name into a new string for some slight modifications */
  158.     strcpy( tempStr, search );
  159.     //strcat( tempStr, "qz" );    /* we need a real or fake file in the directory for this to work */
  160.     
  161.     /* Convert directory from a c string to a pascal string */
  162.     c2pstr( tempStr );
  163.     
  164.     /* Make a filespec to get the directory ID */
  165.     FSMakeFSSpec( 0, 0, ( unsigned char * )tempStr, &gFileSpec );
  166.     
  167.     /* Convert the directory from a pascal string to a c string */
  168.     p2cstr( ( unsigned char * )tempStr );
  169.     
  170.     /* do the search */
  171.     return DirGetNext();
  172.   
  173. }
  174.  
  175. //---------------------------------------------------------------------------------------------
  176. // Read the next directory entry (NULL if done)
  177. char *DirGetNext(void)
  178. {
  179.     
  180.     OSErr    error = noErr;
  181.     
  182.     /* set up more HFileInfo stuff */
  183.     gFileInfo.ioFDirIndex = gIndex;
  184.     gFileInfo.ioDirID = gFileSpec.parID;
  185.     
  186.     /* do the actual search */
  187.     error = PBGetCatInfo( ( union CInfoPBRec * )&gFileInfo, false );
  188.     if ( error == noErr )
  189.     {
  190.         p2cstr( ( unsigned char * )gFileName );
  191.         gIndex++;
  192.         
  193.         if ( strlen( gFileName ) > 0 )
  194.         {
  195.             if ( strstr( gFileName, gFileType ) != NULL )
  196.                 return gFileName;
  197.             else
  198.                 return DirGetNext();
  199.         }
  200.     }
  201.     else
  202.         return NULL;
  203.     
  204.     //return gFileName;
  205.     
  206. }
  207.  
  208. //---------------------------------------------------------------------------------------------
  209. // Close anything left open
  210. void DirClose()
  211. {
  212.     
  213.     /* Not sure if anything is needed here */
  214.     
  215. }
  216.  
  217. int ClockGetTick()
  218. {
  219.   return(clock());
  220. }
  221.  
  222. int DirGetAttrib(char *fromdir)
  223. {
  224.     
  225.     /* Not sure on this one yet */
  226.     
  227.     return(0);
  228. }
  229.