home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / src / asdtomac.c < prev    next >
Text File  |  1993-11-18  |  14KB  |  616 lines

  1. /*
  2. ** This source code was written by Tim Endres
  3. ** Email: time@ice.com.
  4. ** USMail: 8840 Main Street, Whitmore Lake, MI  48189
  5. **
  6. ** Some portions of this application utilize sources
  7. ** that are copyrighted by ICE Engineering, Inc., and
  8. ** ICE Engineering retains all rights to those sources.
  9. **
  10. ** Neither ICE Engineering, Inc., nor Tim Endres, 
  11. ** warrants this source code for any reason, and neither
  12. ** party assumes any responsbility for the use of these
  13. ** sources, libraries, or applications. The user of these
  14. ** sources and binaries assumes all responsbilities for
  15. ** any resulting consequences.
  16. */
  17.  
  18. #ifdef MPW3
  19. #    pragma segment ASD
  20. #endif
  21.  
  22. #ifndef THINK_C_PRECOMPILED
  23.  
  24. #include <types.h>
  25. #include <resources.h>
  26. #include <toolutils.h>
  27. #include <standardfile.h>
  28. #include <errors.h>
  29.  
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32.  
  33. #include "defines.h"
  34.  
  35. #endif /*THINK_C_PRECOMPILED*/
  36.  
  37. #include "asd.h"
  38. #include "mb.h"
  39. #include <errno.h>
  40.  
  41. #ifndef TRUE
  42. #    define TRUE        1
  43. #endif
  44. #ifndef FALSE
  45. #    define FALSE    0
  46. #endif
  47.  
  48. #undef BLK_SIZE
  49. #define BLK_SIZE 1024
  50. #define iobuffer kBuffer
  51. extern char kBuffer[];
  52. /* static char                iobuffer[BLK_SIZE]; */
  53.  
  54. #ifdef TCLAPPL
  55.  
  56. SF_asd_to_Mac()
  57. {
  58. Point        mypoint;
  59. SFReply        asdreply;
  60. SFReply        macreply;
  61. SFTypeList    mytypes;
  62. FILE        *asdfile;
  63. char        name[64];
  64.  
  65.     mypoint.h = mypoint.v = 75;
  66.     mytypes[0] = 'TEXT';
  67.     MyGetFile(mypoint, "\p", NULL, (CheckOption()?-1:1), mytypes, NULL, &asdreply);
  68.     if (asdreply.good) {
  69.         p2cstr(asdreply.fName);
  70.         Feedback("Apple Single/Double to Macintosh - '%s'", asdreply.fName);
  71.         SetVol(NULL, asdreply.vRefNum);
  72.         asdfile = fopen(asdreply.fName, "r");
  73.         if (asdfile == NULL)
  74.             {
  75.             Feedback("Error #%d opening Apple Single/Double file '%s'",
  76.                         errno, asdreply.fName);
  77.             return FAILURE;
  78.             }
  79.         
  80.         sprintf(name, "%s.mac", asdreply.fName);
  81.         asd_seek_a_name(asdfile, name);
  82.         c2pstr(name);
  83.         MyPutFile(mypoint, "\pMacintosh File:", name, NULL, ¯eply);
  84.  
  85.         yield_menus(YIELD_ON);
  86.  
  87.         do_asd_to_mac(asdreply.fName, asdfile, macreply.fName, macreply.vRefNum, TRUE);
  88.         
  89.         WatchCursorOn();
  90.         
  91.         fclose(asdfile);
  92.         
  93.         yield_menus(YIELD_OFF);
  94.         UInitCursor();
  95.         }
  96.     }
  97.  
  98. #endif
  99.  
  100. do_asd_to_mac(inname, infile, mac_fName, mac_vRefNum, interactive)
  101.     char    *inname;        /* C */
  102.     FILE    *infile;
  103.     char    *mac_fName;        /* Pascal */
  104.     int        mac_vRefNum;
  105.     int        interactive;
  106.     {
  107.     int    result = noErr;
  108.     long    bytes;
  109.  
  110.     bytes = fread(&asd_header, 1, sizeof(asd_hdr), infile);
  111.     if (bytes != sizeof(asd_hdr))
  112.         {
  113.         Feedback("Error #%d reading Apple Single/Double header bytes.", errno);
  114.         result = FAILURE;
  115.         }
  116.     else switch (asd_header.magic)
  117.         {
  118.         case MAGIC_APPLE_SINGLE: /* AppleSingle */
  119.             if (interactive)
  120.                 Feedback("Apple Single -> Macintosh.");
  121.             result = apple_single_to_mac(inname, infile,
  122.                                     mac_fName, mac_vRefNum, &asd_header, interactive);
  123.             break;
  124.         case MAGIC_APPLE_DOUBLE: /* AppleDouble */
  125.             if (interactive)
  126.                 Feedback("Apple Double -> Macintosh.");
  127.             result = apple_double_to_mac(inname, infile,
  128.                                     mac_fName, mac_vRefNum, &asd_header, interactive);
  129.             break;
  130.         default:
  131.             if (interactive)
  132.                 Feedback("Unknown magic number 0x%08lx. Not processed.",
  133.                             asd_header.magic);
  134.             result = FAILURE;
  135.             break;
  136.         }
  137.  
  138.     return result;
  139.     }
  140.  
  141. apple_single_to_mac(inname, infile, mac_fName, mac_vRefNum, header, interactive)
  142.     char    *inname;        /* C */
  143.     FILE    *infile;
  144.     char    *mac_fName;        /* Pascal */
  145.     int        mac_vRefNum;
  146.     asd_hdr    *header;
  147.     int        interactive;
  148.     {
  149.     int        i, myerr;
  150.     int        got_data, got_rsrc, got_fndr;
  151.     int        i_data, i_rsrc, i_fndr;
  152.     ParamBlockRec    pb;
  153.     FInfo            finfo;
  154.  
  155. #pragma unused (inname, outname)
  156.  
  157.     got_data = got_rsrc = got_fndr = 0;
  158.  
  159. #ifdef TCLAPPL
  160.     print_asd_hdr(header);
  161. #endif
  162.  
  163.     RotateCursor(32);
  164.     DoYield();
  165.  
  166.     for (i=0; i < header->entries; i++)
  167.         {
  168.         fread(&the_entries[i], 1, sizeof(asd_entry), infile);
  169. #ifdef TCLAPPL
  170.         print_asd_entry(&the_entries[i]);
  171. #endif
  172.         switch (the_entries[i].id)
  173.             {
  174.             case ASDID_DATA_FORK: got_data = 1; i_data = i; break;
  175.             case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
  176.             case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
  177.             }
  178.         }
  179.  
  180.     RotateCursor(32);
  181.     DoYield();
  182.  
  183.     if (! got_fndr)
  184.         {
  185.         if (interactive)
  186.             Feedback("NOTE: No Finder Information. Defaulting.");
  187.         finfo.fdType = (OSType)'????';
  188.         finfo.fdCreator = (OSType)'????';
  189.         finfo.fdFlags = 0;
  190.         finfo.fdFldr = 0;
  191.         finfo.fdLocation.h = 16;
  192.         finfo.fdLocation.v = 16;
  193.         }
  194.     else {
  195.         get_asd_fndr_data(infile, &the_entries[i_fndr], &g_asd_finder);
  196. #ifdef TCLAPPL
  197.         print_asd_fndr(&g_asd_finder);
  198. #endif
  199.         finfo.fdType = g_asd_finder.fdType;
  200.         finfo.fdCreator = g_asd_finder.fdCreator;
  201.         finfo.fdFlags = g_asd_finder.fdFlags;
  202.         memcpy(&finfo.fdLocation, g_asd_finder.fdLocation, sizeof(finfo.fdLocation));
  203.         }
  204.         
  205.     if (interactive)
  206.         {
  207.         Feedback("DATA Fork contains %ld bytes.", the_entries[i_data].length);
  208.         Feedback("RSRC Fork contains %ld bytes.", the_entries[i_rsrc].length);
  209.         }
  210.     
  211.     RotateCursor(32);
  212.     DoYield();
  213.  
  214.     myerr = Create(mac_fName, (short)mac_vRefNum, finfo.fdCreator, finfo.fdType);
  215.     if (myerr != noErr && myerr != dupFNErr && interactive)
  216.         Feedback("Error #%d creating '%.*s'.", myerr, mac_fName[0], &mac_fName[1]);
  217.     
  218.     pb.ioParam.ioNamePtr = mac_fName;
  219.     pb.ioParam.ioVRefNum = mac_vRefNum;
  220.  
  221.     if (got_data && the_entries[i_data].length > 0)
  222.         {
  223.         pb.ioParam.ioCompletion = 0;
  224.         pb.ioParam.ioVersNum = 0;
  225.         pb.ioParam.ioPermssn = fsWrPerm;
  226.         pb.ioParam.ioMisc = 0;
  227.         myerr = PBOpen(&pb, FALSE);
  228.         if (myerr != noErr)
  229.             {
  230.             if (interactive)
  231.                 Feedback("Error #%d opening DATA fork '%.*s' for writing.",
  232.                             myerr, mac_fName[0], &mac_fName[1]);
  233.             }
  234.         else {
  235.             if (interactive)
  236.                 Feedback("Writing DATA fork...");
  237.             do_mac_copy( infile, the_entries[i_data].offset,
  238.                          pb.ioParam.ioRefNum, the_entries[i_data].length );
  239.             pb.ioParam.ioCompletion = 0;
  240.             myerr = PBClose(&pb, FALSE);
  241.             }
  242.         }
  243.     
  244.     RotateCursor(32);
  245.     DoYield();
  246.  
  247.     if (got_rsrc && the_entries[i_rsrc].length)
  248.         {
  249.         SetVol(NULL, (short)mac_vRefNum);
  250.         CreateResFile(mac_fName);
  251.         pb.ioParam.ioCompletion = 0;
  252.         pb.ioParam.ioVersNum = 0;
  253.         pb.ioParam.ioPermssn = fsWrPerm;
  254.         pb.ioParam.ioMisc = 0;
  255.         myerr = PBOpenRF(&pb, FALSE);
  256.         if (myerr != noErr)
  257.             {
  258.             if (interactive)
  259.                 Feedback("Error #%d opening RSRC fork '%.*s' for writing.",
  260.                             myerr, mac_fName[0], &mac_fName[1]);
  261.             }
  262.         else {
  263.             if (interactive)
  264.                 Feedback("Writing RSRC fork...");
  265.             do_mac_copy( infile, the_entries[i_rsrc].offset,
  266.                             pb.ioParam.ioRefNum, the_entries[i_rsrc].length );
  267.             pb.ioParam.ioCompletion = 0;
  268.             myerr = PBClose(&pb, FALSE);
  269.             }
  270.         }
  271.     
  272.     RotateCursor(32);
  273.     DoYield();
  274.  
  275.     pb.fileParam.ioCompletion = 0;
  276.     pb.fileParam.ioFVersNum = 0;
  277.     pb.fileParam.ioFlFndrInfo = finfo;
  278.     pb.fileParam.ioFlCrDat = 0;
  279.     pb.fileParam.ioFlMdDat = 0;
  280.     myerr = PBSetFInfo(&pb, FALSE);
  281.     if (myerr != noErr && interactive)
  282.         {
  283.         Feedback("Warning #%d setting '%.*s' Finder info.",
  284.                     myerr, mac_fName[0], &mac_fName[1]);
  285.         }
  286.     
  287.     return noErr;
  288.     }
  289.  
  290. apple_double_to_mac(rname, rfile, mac_fName, mac_vRefNum, header, interactive)
  291.     char    *rname;            /* C */
  292.     FILE    *rfile;
  293.     char    *mac_fName;        /* Pascal */
  294.     int        mac_vRefNum;
  295.     asd_hdr    *header;
  296.     int        interactive;
  297.     {
  298.     FILE    *dfile = NULL;
  299.     SFReply    myreply;
  300.     Point    mypoint;
  301.     SFTypeList    mytypes;
  302.     ParamBlockRec    pb;
  303.     FInfo            finfo;
  304.     int        i, myerr;
  305.     int        got_data, got_rsrc, got_fndr, got_path;
  306.     int        i_data = 0, i_rsrc, i_fndr, i_path;
  307.     long    bytes;
  308.     char    dname[256];
  309.  
  310. #pragma unused (outname)
  311.  
  312.     got_data = got_rsrc = got_fndr = 0;
  313.  
  314. #ifdef TCLAPPL
  315.     print_asd_hdr(header);
  316. #endif
  317.  
  318.     the_entries[i_data].id = ASDID_DATA_FORK;
  319.     the_entries[i_data].offset = 0;
  320.     the_entries[i_data].length = 0;
  321.     
  322.     RotateCursor(32);
  323.     DoYield();
  324.  
  325.     for (i=1; i <= header->entries; i++)
  326.         {
  327.         fread(&the_entries[i], 1, sizeof(asd_entry), rfile);
  328. #ifdef TCLAPPL
  329.         print_asd_entry(&the_entries[i]);
  330. #endif
  331.         switch (the_entries[i].id) {
  332.             case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
  333.             case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
  334.             case ASDID_DATA_PATH: got_path = 1; i_path = i; break;
  335.             }
  336.         }
  337.  
  338.     RotateCursor(32);
  339.     DoYield();
  340.  
  341.     if (got_path)
  342.         {
  343.         if (the_entries[i_path].length >= sizeof(dname)-1)
  344.             {
  345.             if (interactive)
  346.                 Feedback("Real Name Too Long: %d > %d. Truncating.",
  347.                             the_entries[i_path].length, sizeof(dname)-1);
  348.             the_entries[i_path].length = sizeof(dname) - 1;
  349.             }
  350.         
  351.         bytes = fread(dname, 1, the_entries[i_path].length, rfile);
  352.         if (bytes > the_entries[i_path].length)
  353.             {
  354.             if (interactive)
  355.                 Feedback("Real Name Too Long: %d > %d. Truncating to '%.*s'.",
  356.                             bytes, the_entries[i_path].length,
  357.                             the_entries[i_path].length, dname);
  358.             dname[the_entries[i_path].length] = '\0';
  359.             }
  360.         else if (bytes < the_entries[i_path].length)
  361.             {
  362.             if (interactive)
  363.                 Feedback("Couldn't Read All Of Real Name: %d < %d. Using '%.*s'.",
  364.                             bytes, sizeof(dname)-1, bytes, dname);
  365.             dname[bytes] = '\0';
  366.             }
  367.         else {
  368.             dname[bytes] = '\0';
  369.             }
  370.         
  371.         if (interactive)
  372.             Feedback("Opening Real Name: '%s'.", dname);
  373.         
  374.         dfile = fopen(dname, "r");
  375.         if (dfile != NULL)
  376.             got_data = 1;
  377.         }
  378.     
  379.     RotateCursor(32);
  380.     DoYield();
  381.  
  382.     if (! got_data)
  383.         {
  384.         if (rname[0] == '%')
  385.             {
  386.             strcpy(dname, &rname[1]);
  387.             }
  388.         else
  389.             {
  390.             sprintf(dname, "%%%s", rname);
  391.             }
  392.         if (interactive)
  393.             Feedback("Trying Data Name: '%s'.", dname);
  394.         dfile = fopen(dname, "r");
  395.         if (dfile == NULL)
  396.             {
  397.             if (! interactive)
  398.                 {
  399.                 got_data = 0;
  400.                 }
  401.             else
  402.                 {
  403. #ifdef TCLAPPL
  404.                 mypoint.h = mypoint.v = 75;
  405.                 MyGetFile(mypoint, "\pWhere is data fork?", NULL, -1, mytypes, NULL, &myreply);
  406.                 if (myreply.good)
  407.                     {
  408.                     sprintf(dname, "%.*s", myreply.fName[0], &myreply.fName[1]);
  409.                     SetVol(NULL, myreply.vRefNum);
  410.                     dfile = fopen(dname, "r");
  411.                     if (dfile != NULL)
  412.                         got_data = 1;
  413.                     else
  414.                         return FAILURE;
  415.                     }
  416.                 else {
  417.                     if (! AskYesNo("Do you just want an empty data fork?", 1))
  418.                         return CANCEL;
  419.                     }
  420. #else
  421.                         return FAILURE;
  422. #endif
  423.                 }
  424.             }
  425.         else
  426.             got_data = 1;
  427.         }
  428.     
  429.     RotateCursor(32);
  430.     DoYield();
  431.  
  432.     if (dfile != NULL)
  433.         {
  434.         the_entries[i_data].offset = 0;
  435.         fseek(dfile, 0, SEEK_END);
  436.         the_entries[i_data].length = ftell(dfile);
  437.         fseek(dfile, 0, SEEK_SET);
  438.         }
  439.     else {
  440.         the_entries[i_data].offset = 0;
  441.         the_entries[i_data].length = 0;
  442.         }
  443.  
  444.     if (! got_fndr)
  445.         {
  446.         if (interactive)
  447.             Feedback("NOTE: No Finder Information. Defaulting.");
  448.         finfo.fdType = (OSType)'????';
  449.         finfo.fdCreator = (OSType)'????';
  450.         finfo.fdFlags = 0;
  451.         finfo.fdFldr = 0;
  452.         finfo.fdLocation.h = 16;
  453.         finfo.fdLocation.v = 16;
  454.         }
  455.     else
  456.         {
  457.         get_asd_fndr_data(rfile, &the_entries[i_fndr], &g_asd_finder);
  458. #ifdef TCLAPPL
  459.         print_asd_fndr(&g_asd_finder);
  460. #endif
  461.         finfo.fdType = g_asd_finder.fdType;
  462.         finfo.fdCreator = g_asd_finder.fdCreator;
  463.         finfo.fdFlags = g_asd_finder.fdFlags;
  464.         memcpy(&finfo.fdLocation, g_asd_finder.fdLocation, sizeof(finfo.fdLocation));
  465.         }
  466.         
  467.     RotateCursor(32);
  468.     DoYield();
  469.  
  470.     if (interactive)
  471.         {
  472.         Feedback("DATA Fork contains %ld bytes.", the_entries[i_data].length);
  473.         Feedback("RSRC Fork contains %ld bytes.", the_entries[i_rsrc].length);
  474.         }
  475.  
  476.     pb.ioParam.ioNamePtr = mac_fName;
  477.     pb.ioParam.ioVRefNum = mac_vRefNum;
  478.  
  479.     myerr = Create(mac_fName, (short)mac_vRefNum, finfo.fdCreator, finfo.fdType);
  480.     if (myerr != noErr && myerr != dupFNErr && interactive)
  481.         Feedback("Error #%d creating '%.*s'.", myerr, mac_fName[0], &mac_fName[1]);
  482.     
  483.     if (the_entries[i_data].length > 0)
  484.         {
  485.         pb.ioParam.ioCompletion = 0;
  486.         pb.ioParam.ioVersNum = 0;
  487.         pb.ioParam.ioPermssn = fsWrPerm;
  488.         pb.ioParam.ioMisc = 0;
  489.         myerr = PBOpen(&pb, FALSE);
  490.         if (myerr != noErr)
  491.             {
  492.             if (interactive)
  493.                 Feedback("Error #%d opening DATA fork '%.*s' for writing.",
  494.                             myerr, mac_fName[0], &mac_fName[1]);
  495.             }
  496.         else {
  497.             if (interactive)
  498.                 Feedback("Writing DATA fork...");
  499.             do_mac_copy( dfile, the_entries[i_data].offset,
  500.                             pb.ioParam.ioRefNum, the_entries[i_data].length );
  501.             pb.ioParam.ioCompletion = 0;
  502.             myerr = PBClose(&pb, FALSE);
  503.             }
  504.         }
  505.     
  506.     RotateCursor(32);
  507.     DoYield();
  508.  
  509.     if (got_rsrc && the_entries[i_rsrc].length)
  510.         {
  511.         SetVol(NULL, (short)mac_vRefNum);
  512.         CreateResFile(mac_fName);
  513.         pb.ioParam.ioCompletion = 0;
  514.         pb.ioParam.ioVersNum = 0;
  515.         pb.ioParam.ioPermssn = fsWrPerm;
  516.         pb.ioParam.ioMisc = 0;
  517.         myerr = PBOpenRF(&pb, FALSE);
  518.         if (myerr != noErr)
  519.             {
  520.             if (interactive)
  521.                 Feedback("Error #%d opening RSRC fork '%.*s' for writing.",
  522.                             myerr, mac_fName[0], &mac_fName[1]);
  523.             }
  524.         else {
  525.             if (interactive)
  526.                 Feedback("Writing RSRC fork...");
  527.             do_mac_copy( rfile, the_entries[i_rsrc].offset,
  528.                             pb.ioParam.ioRefNum, the_entries[i_rsrc].length );
  529.             pb.ioParam.ioCompletion = 0;
  530.             myerr = PBClose(&pb, FALSE);
  531.             }
  532.         }
  533.     
  534.     pb.fileParam.ioCompletion = 0;
  535.     pb.fileParam.ioFVersNum = 0;
  536.     pb.fileParam.ioFlFndrInfo = finfo;
  537.     pb.fileParam.ioFlCrDat = 0;
  538.     pb.fileParam.ioFlMdDat = 0;
  539.     myerr = PBSetFInfo(&pb, FALSE);
  540.     if (myerr != noErr)
  541.         {
  542.         if (interactive)
  543.             Feedback("Warning #%d setting '%.*s' Finder info.",
  544.                         myerr, mac_fName[0], &mac_fName[1]);
  545.         }
  546.         
  547.     if (dfile)
  548.         fclose(dfile);
  549.     }
  550.  
  551. do_mac_copy(infile, inoffset, outref, length)
  552.     FILE        *infile;
  553.     long        inoffset;
  554.     int            outref;
  555.     long        length;
  556.     {
  557.     int                blocks, residue, myerr;
  558.     ParamBlockRec    pb;
  559.  
  560.     //Feedback("do_mac_copy: in_offset = %ld outref = %d ", inoffset, outref);
  561.     
  562.     if (inoffset != -1)
  563.         fseek(infile, inoffset, SEEK_SET);
  564.  
  565.     //Feedback("do_mac_copy: length = %ld BLKSZ %d ", length, BLK_SIZE);
  566.     blocks = length / BLK_SIZE;
  567.     residue = length % BLK_SIZE;
  568.  
  569.     //Feedback("do_mac_copy: blks = %ld res %ld", blocks, residue);
  570.     for ( ; blocks ; blocks--)
  571.         {
  572.         RotateCursor(32);
  573.         DoYield();
  574.         if (pause_op)
  575.             while (pause_op)
  576.                 pausing();
  577.         
  578.         if (cancel_current_op)
  579.             return CANCEL;
  580.  
  581.         fread(iobuffer, 1, BLK_SIZE, infile);
  582.         
  583.         pb.ioParam.ioCompletion = 0;
  584.         pb.ioParam.ioRefNum = outref;
  585.         pb.ioParam.ioBuffer = iobuffer;
  586.         pb.ioParam.ioReqCount = BLK_SIZE;
  587.         pb.ioParam.ioPosMode = fsAtMark;
  588.         pb.ioParam.ioPosOffset = 0;
  589.         myerr = PBWrite(&pb, FALSE);
  590.         if (myerr != noErr)
  591.             {
  592.             //Feedback("do_mac_copy: Error #%d on block #%d write.", myerr, blocks);
  593.             break;
  594.             }
  595.         }
  596.  
  597.     if (residue)
  598.         {
  599.         RotateCursor(32);
  600.         DoYield();
  601.  
  602.         memset(iobuffer, 0, BLK_SIZE);
  603.         fread(iobuffer, 1, residue, infile);
  604.         
  605.         pb.ioParam.ioCompletion = 0;
  606.         pb.ioParam.ioRefNum = outref;
  607.         pb.ioParam.ioBuffer = iobuffer;
  608.         pb.ioParam.ioReqCount = residue;
  609.         pb.ioParam.ioPosMode = fsAtMark;
  610.         pb.ioParam.ioPosOffset = 0;
  611.         myerr = PBWrite(&pb, FALSE);
  612.         //if (myerr != noErr)
  613.             //Feedback("do_mac_copy: Error #%d on residue write.", myerr);
  614.         }
  615.     }
  616.