Advertisement
Guest User

Untitled

a guest
Jul 24th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /* For use with CodeWarrior Standard Console */
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <Files.h>
  6. #include <StandardFile.h>
  7. #include <Types.h>
  8.  
  9. enum {
  10.     kBufferSize     = 0x8000,
  11.     kMaxLoops       = 10
  12. };
  13.  
  14. int main(void) {
  15.     StandardFileReply   stdReply;
  16.     FSRef               fsRef;
  17.     FSForkIOParam       fsForkIOParam;
  18.     HFSUniStr255        dataForkName;
  19.     SInt64              forkSize;
  20.     FSForkIOParamPtr    fsForkPtr[2];
  21.     FSForkIOParamPtr    currFSForkPtr;
  22.     OSErr               err;
  23.     short               i, j;
  24.     char                filename[256];
  25.     short               currFSForkIndex = 0;
  26.    
  27.     printf("Here we go...\n");
  28.  
  29.     StandardGetFile(NULL, -1, NULL, &stdReply);
  30.  
  31.     if (stdReply.sfGood) {
  32.         FSpMakeFSRef(&stdReply.sfFile, &fsRef);
  33.         FSGetDataForkName(&dataForkName);
  34.        
  35.         fsForkIOParam.ref               = &fsRef;
  36.         fsForkIOParam.forkNameLength    = dataForkName.length;
  37.         fsForkIOParam.forkName          = dataForkName.unicode;
  38.         fsForkIOParam.permissions       = fsRdPerm;
  39.        
  40.         err = PBOpenForkSync(&fsForkIOParam);
  41.        
  42.         if (err) {
  43.             printf("Error opening file (%d)", err);
  44.         } else {
  45.             /* Print the filename */
  46.             BlockMove(stdReply.sfFile.name + 1, filename, stdReply.sfFile.name[0]);
  47.             filename[stdReply.sfFile.name[0]] = 0;
  48.             printf("File: %s\n", filename);
  49.            
  50.             FSGetForkSize(fsForkIOParam.forkRefNum, &forkSize);
  51.             printf("Data Fork Size: %lld bytes\n", forkSize);
  52.            
  53.             /* Setup the two FSForkIOParam blocks */
  54.             for (i = 0 ; i < 2 ; ++i) {
  55.                 fsForkPtr[i]                    = (FSForkIOParamPtr)NewPtrClear(sizeof(FSForkIOParam));            
  56.                 fsForkPtr[i]->ioCompletion      = 0;
  57.                 fsForkPtr[i]->forkRefNum        = fsForkIOParam.forkRefNum;
  58.                 fsForkPtr[i]->positionMode      = fsAtMark;
  59.                 fsForkPtr[i]->positionOffset    = 0LL;
  60.                 fsForkPtr[i]->requestCount      = kBufferSize;
  61.                 fsForkPtr[i]->buffer            = NewPtrClear(kBufferSize);
  62.                
  63.                 /* Add read to the queue */
  64.                 PBReadForkAsync(fsForkPtr[i]);
  65.             }
  66.            
  67.             currFSForkPtr = fsForkPtr[0];
  68.             i = 0;
  69.            
  70.             while (true) {
  71.                 /* Wait for data */
  72.                 j = 0;
  73.                
  74.                 while (currFSForkPtr->ioResult > 0) {
  75.                     printf("[Thread %d]\t\tWaiting for data (%d)\n", currFSForkIndex + 1,currFSForkPtr->ioResult);
  76.                     if (++j == kMaxLoops) {
  77.                         printf("[Thread %d]\t\tWaited too long!\n");
  78.                         break;
  79.                     }
  80.                 };
  81.                
  82.                 printf("[Thread %d][%d]\t%d bytes read. Mark now @%08llx. ",
  83.                     currFSForkIndex + 1,
  84.                     i++,
  85.                     currFSForkPtr->actualCount,
  86.                     currFSForkPtr->positionOffset);
  87.                    
  88.                 printf("Data: %llx\n", *((long long*)currFSForkPtr->buffer));
  89.                
  90.                 /* eofErr is -39 */
  91.                 if (currFSForkPtr->ioResult < 0) {
  92.                     break;
  93.                 }
  94.                
  95.                 /* Add next read to the queue */
  96.                 PBReadForkAsync(currFSForkPtr);
  97.                
  98.                 /* Switch to the other buffer */
  99.                 currFSForkIndex = 1 - currFSForkIndex;
  100.                 currFSForkPtr = fsForkPtr[currFSForkIndex];
  101.             }
  102.            
  103.             printf("[Thread %d]\t\tioResult was %d\n", currFSForkIndex + 1, currFSForkPtr->ioResult);
  104.            
  105.             /* Close the file */
  106.             err = PBCloseForkSync(&fsForkIOParam);
  107.            
  108.             if (err) {
  109.                 printf("Error closing file (%d)", err);
  110.             }
  111.         }
  112.        
  113.     } else {
  114.         printf("No file selected");  
  115.     }
  116.    
  117.     /* Cleanup */
  118.     for (i = 0 ; i < 2 ; ++i) {
  119.         DisposePtr(fsForkPtr[i]->buffer);
  120.         DisposePtr((Ptr)fsForkPtr);
  121.     }
  122.    
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement