home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / disks / iso9660 / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.7 KB  |  427 lines

  1. /*
  2.     File:        support.c
  3.     
  4.     Description:
  5.  
  6.     Author:        
  7.  
  8.     Copyright:     Copyright: © 1999 by Apple Computer, Inc.
  9.                 all rights reserved.
  10.     
  11.     Disclaimer:    You may incorporate this sample code into your applications without
  12.                 restriction, though the sample code has been provided "AS IS" and the
  13.                 responsibility for its operation is 100% yours.  However, what you are
  14.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  15.                 after having made changes. If you're going to re-distribute the source,
  16.                 we require that you make it clear in the source that the code was
  17.                 descended from Apple Sample Code, but that you've made changes.
  18.     
  19.     Change History (most recent first):
  20.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  21.  
  22. */
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26.  
  27. #include <Types.h>
  28. #include <QuickDraw.h>
  29. #include <Packages.h>
  30. #include <StandardFile.h>
  31.  
  32. #include "support.h"
  33.  
  34. typedef    unsigned char    byte;
  35.  
  36.  
  37. /************************************************************************
  38.  *
  39.  *  Function:        pStrCopy
  40.  *
  41.  *  Purpose:        copy a pascal string from p1 to p2
  42.  *
  43.  *  Returns:        nothing
  44.  *
  45.  *  Side Effects:    p2 gets filled with contents of p1
  46.  *
  47.  *  Description:    simple loop, copying from p1 to p2 for length
  48.  *                    determined by first byte of p1.
  49.  *
  50.  ************************************************************************/
  51. void
  52. pStrCopy(StringPtr p1, StringPtr p2)
  53. {
  54.     register short len;
  55.     
  56.     len = *p2++ = *p1++;
  57.     while (--len>=0)
  58.         *p2++=*p1++;
  59. }
  60.  
  61.  
  62. /************************************************************************
  63.  *
  64.  *  Function:        pStrLen
  65.  *
  66.  *  Purpose:        return length of pascal string
  67.  *
  68.  *  Returns:        short
  69.  *
  70.  *  Side Effects:    none
  71.  *
  72.  *  Description:    The first byte of a pascal string contains the
  73.  *                    length of the string.  Return that value.
  74.  *
  75.  ************************************************************************/
  76. short
  77. pStrLen(StringPtr p)
  78. {
  79.     return (p[0]);
  80. }
  81.  
  82.  
  83. /************************************************************************
  84.  *
  85.  *  Function:        CreateISOName
  86.  *
  87.  *  Purpose:        Create ISO name from pstring.
  88.  *
  89.  *  Returns:        nothing
  90.  *
  91.  *  Side Effects:    dest is filled.
  92.  *
  93.  *  Description:    An ISO file name is 31 characters plus '.;1', all
  94.  *                    in uppercase.  Copy and convert file name, with
  95.  *                    any bogus characters converted to underscore '_'.
  96.  *                    If there was no period in the file name,
  97.  *                    add one.  Add ";1" to the end of the file name.
  98.  *
  99.  ************************************************************************/
  100. short
  101. CreateISOName(char *dest, StringPtr src)
  102. {
  103.     short    i;
  104.     short    limit;
  105.     short    nameSize;
  106.     Boolean    madeDot;
  107.         
  108.     limit = pStrLen(src);
  109.     
  110.     dest[0] = toupper(src[1]);
  111.     
  112.     if (dest[0] == '\000' || dest[0] == '\001')
  113.         return 1;    /* don't add version stuff to myself or parent */
  114.  
  115.     for (i = 1; i < limit; i++)
  116.         dest[i] = (isalnum(src[i+1])) ? toupper(src[i+1]) : '_';
  117.     
  118.  
  119.     madeDot = false;
  120.     for (i = limit; i > 0 && madeDot == false; i--)
  121.         if (dest[i] == '_')
  122.         {
  123.             dest[i] = '.';
  124.             madeDot = true;
  125.         }
  126.  
  127.     nameSize = limit;
  128.     
  129.     if (madeDot == false)        /* we don't have a period. Add one */
  130.         dest[nameSize++] = '.';
  131.     dest[nameSize++] = ';';
  132.     dest[nameSize++] = '1';        /* version number */
  133.  
  134.     return nameSize;
  135. }
  136.  
  137. /************************************************************************
  138.  *
  139.  *  Function:        HFSFile
  140.  *
  141.  *  Purpose:        get the name and vRefNum of an HFS File
  142.  *
  143.  *  Returns:        Boolean
  144.  *                        true    a file was selected
  145.  *                        false    no file. Please stop
  146.  *
  147.  *  Side Effects:    *fn and *vRefNum get filled in
  148.  *
  149.  *  Description:    Call SFGetFile to get the file to be operated upon.
  150.  *
  151.  ************************************************************************/
  152. Boolean
  153. HFSFile(StringPtr fn, short *vRefNum)
  154. {
  155.     Point    SFGwhere;
  156.     SFReply    reply;
  157.     
  158.     SFGwhere.v = 90;
  159.     SFGwhere.h = 82;
  160.     SFGetFile(SFGwhere, (StringPtr)"\pChoose File to Add to ISO disk", 0L, -1, 0L, 0L, &reply );
  161.     if (reply.good) {
  162.         pStrCopy( reply.fName, fn );
  163.         *vRefNum = reply.vRefNum;
  164.         return(true);
  165.     }
  166.     else return(false);
  167. }
  168.  
  169.  
  170.  
  171. /************************************************************************
  172.  *
  173.  *  Function:        ClearOut
  174.  *
  175.  *  Purpose:        set memory to zeros
  176.  *
  177.  *  Returns:        nothing
  178.  *
  179.  *  Side Effects:    *buffer is zeroed out
  180.  *
  181.  *  Description:    zero out buffer for count bytes.
  182.  *
  183.  ************************************************************************/
  184. void
  185. ClearOut(Ptr buffer, short count)
  186. {
  187.     short    i;
  188.     
  189.     for (i = 0; i < count; i++)
  190.         buffer[i] = 0;
  191. }
  192.  
  193.  
  194. /************************************************************************
  195.  *
  196.  *  Function:        SpaceOut
  197.  *
  198.  *  Purpose:        set memory to spaces
  199.  *
  200.  *  Returns:        nothing
  201.  *
  202.  *  Side Effects:    *buffer is spaced out
  203.  *
  204.  *  Description:    Put spaces into buffer for count bytes.
  205.  *
  206.  ************************************************************************/
  207. void
  208. SpaceOut(Ptr buffer, short count)
  209. {
  210.     short    i;
  211.     
  212.     for (i = 0; i < count; i++)
  213.         buffer[i] = ' ';
  214. }
  215.  
  216.  
  217. /************************************************************************
  218.  *
  219.  *  Function:        CharCopy
  220.  *
  221.  *  Purpose:        copy and fill with blanks
  222.  *
  223.  *  Returns:        nothing
  224.  *
  225.  *  Side Effects:    *dest is filled with contents of src & blanks
  226.  *
  227.  *  Description:    copy from *src to *dest for the length specified.
  228.  *                    src is assumed to point to a C null-delimited string.
  229.  *                    If src is smaller than length in size, dest is filled
  230.  *                    with blanks.
  231.  *
  232.  ************************************************************************/
  233. void
  234. CharCopy(char *dest, char *src, short length)
  235. {
  236.     short     i;
  237.     short    j;
  238.     
  239.     i = 0;
  240.     while (*dest++ = *src++)
  241.         i++;
  242.     
  243.     *dest--;
  244.     i--;    /* so that no null terminator is left over */
  245.     
  246.     if (i < length)
  247.         for (j = i; j < length; j++)
  248.             *dest++ = ' ';
  249. }
  250.  
  251.  
  252.  
  253.  
  254. /************************************************************************
  255.  *
  256.  *  Function:    NormalizeLong
  257.  *
  258.  *  Purpose:    normalize a long number that's in lsb format
  259.  *
  260.  *  Returns:    long
  261.  *
  262.  *  Side Effects:    none
  263.  *
  264.  *  Description:
  265.  *                takes a long in lsb format order and converts it
  266.  *                to msb format order.  For example, the long value
  267.  *                0x12345678 becomes 0x78563412
  268.  *                0x78563412 becomes 0x12345678
  269.  *
  270.  *
  271.  ************************************************************************/
  272. long
  273. NormalizeLong(long incoming)
  274. {
  275.     byte    *byteArray;
  276.     long    result;
  277.     
  278.     byteArray = (byte *) &incoming;
  279.     result = (long) byteArray[0] | 
  280.              (long) byteArray[1] << 8 | 
  281.              (long) byteArray[2] << 16 |
  282.              (long) byteArray[3] << 24;
  283.     
  284.     return result;
  285. }
  286.  
  287. /************************************************************************
  288.  *
  289.  *  Function:    NormalizeWord
  290.  *
  291.  *  Purpose:    normalize a word number that's in lsb format
  292.  *
  293.  *  Returns:    word
  294.  *
  295.  *  Side Effects:    none
  296.  *
  297.  *  Description:
  298.  *                takes a word in lsb format order and converts it
  299.  *                to msb format order.  For example, the word value
  300.  *                0x1234 becomes 0x3412
  301.  *                0x3412 becomes 0x1234
  302.  *
  303.  *
  304.  ************************************************************************/
  305. short
  306. NormalizeWord(short incoming)
  307. {
  308.     byte    *byteArray;
  309.     short    result;
  310.     
  311.     byteArray = (byte *) &incoming;
  312.     result = (short) byteArray[0] | 
  313.              (short) byteArray[1] << 8;
  314.     
  315.     return result;
  316. }
  317.  
  318.  
  319. /************************************************************************
  320.  *
  321.  *  Function:    NormalizeVolumeName
  322.  *
  323.  *  Purpose:    convert a string to conform to ISO 9660 naming standards
  324.  *
  325.  *  Returns:    none
  326.  *
  327.  *  Side Effects:    changes the string "someString"
  328.  *
  329.  *  Description:
  330.  *                ISO 9660 forces volume names to be only alphanumeric
  331.  *                characters plus underscore.  This routine converts a
  332.  *                string of arbitrary length to such a volume name string,
  333.  *                converting all illegal characters to underscore.
  334.  *
  335.  *
  336.  ************************************************************************/
  337. void
  338. NormalizeVolumeName(char *someString)
  339. {
  340.     short    i;
  341.     
  342.     for (i = 0; i < strlen(someString); i++)
  343.         someString[i] = (isalnum(someString[i])) ? toupper(someString[i]) : '_';
  344. }
  345.  
  346.  
  347. /************************************************************************
  348.  *
  349.  *  Function:        GetFileInfo
  350.  *
  351.  *  Purpose:        get lengths of file rsrc and data forks
  352.  *
  353.  *  Returns:        OSErr
  354.  *                        noErr, unless PBHGetVInfo has an error.
  355.  *
  356.  *  Side Effects:    dataLength & rsrcLength are changed
  357.  *
  358.  *  Description:     call PBGetFInfo() and return its results.
  359.  *
  360.  ************************************************************************/
  361. OSErr
  362. GetFileInfo(StringPtr name, short vRefNum, long *rsrcLength, long *dataLength, OSType *fType, OSType *fCreator, short *flags)
  363. {
  364.     HParamBlockRec    io;
  365.     OSErr            result;
  366.     
  367.     io.fileParam.ioCompletion = 0L;
  368.     io.fileParam.ioNamePtr = name;
  369.     io.fileParam.ioVRefNum = vRefNum;
  370.     io.fileParam.ioFVersNum = 0;
  371.     io.fileParam.ioFDirIndex = 0;
  372.     result = PBGetFInfo((ParmBlkPtr)&io, false);
  373.     if (result == noErr)
  374.     {
  375.         *rsrcLength = io.fileParam.ioFlRLgLen;
  376.         *dataLength = io.fileParam.ioFlLgLen;
  377.         *fType = io.fileParam.ioFlFndrInfo.fdType;
  378.         *fCreator = io.fileParam.ioFlFndrInfo.fdCreator;
  379.         *flags = io.fileParam.ioFlFndrInfo.fdFlags;
  380.     }
  381.     else
  382.     {
  383.         *rsrcLength = 0L;
  384.         *dataLength = 0L;
  385.         *fType = 0L;
  386.         *fCreator = 0L;
  387.         *flags = 0;
  388.     }
  389.     return result;
  390. }
  391.  
  392.  
  393. /************************************************************************
  394.  *
  395.  *  Function:        GetFinderFlags
  396.  *
  397.  *  Purpose:        get finder flags for a file
  398.  *
  399.  *  Returns:        OSErr
  400.  *                        noErr, unless PBGetFInfo has an error.
  401.  *
  402.  *  Side Effects:    flags value is changed
  403.  *
  404.  *  Description:     call PBGetFInfo() and return its results.
  405.  *
  406.  ************************************************************************/
  407. OSErr
  408. GetFinderFlags(StringPtr name, short vRefNum, short *flags)
  409. {
  410.     HParamBlockRec    io;
  411.     OSErr            result;
  412.     
  413.     io.fileParam.ioCompletion = 0L;
  414.     io.fileParam.ioNamePtr = name;
  415.     io.fileParam.ioVRefNum = vRefNum;
  416.     io.fileParam.ioFVersNum = 0;
  417.     io.fileParam.ioFDirIndex = 0;
  418.     result = PBGetFInfo((ParmBlkPtr)&io, false);
  419.     if (result == noErr)
  420.         *flags = io.fileParam.ioFlFndrInfo.fdFlags;
  421.     else
  422.         *flags = 0;
  423.     return result;
  424. }
  425.  
  426.  
  427.