home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / MacOS / CommandLineTool / MacZStringByteTableTool.cpp next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  3.8 KB  |  154 lines

  1. /*==================================================================
  2.     File:        MacZStringByteTableTool.cpp
  3.     
  4.     Contains:    Tool for extracting ZString byte table from
  5.                 Mac INTL 5 resources.
  6.  
  7.     Written by:    Eric Traut
  8.     
  9.     Copyright:    2000-2001 Connectix Corporation
  10.     
  11.     This source has been placed into the public domain by
  12.     Connectix Corporation. You have the right to modify, 
  13.     distribute or use this code without any legal limitations
  14.     or finanicial/licensing requirements. Connectix is not 
  15.     liable for any problems that result from the use of this 
  16.     code.
  17.     
  18.     If you have comments, feedback, questions, or would like
  19.     to submit bug fixes or updates to this code, please email
  20.     opensource@connectix.com.
  21. ==================================================================*/
  22.  
  23. #include "ZStringTool.h"
  24. #include "MacZString.h"
  25.  
  26. #include <cstring>
  27. #include <cstddef>
  28. #include <cstdlib>
  29.  
  30. extern "C" {
  31. #include <FSp_fopen.h>
  32. }
  33.  
  34. #include <Files.h>
  35. #include <Devices.h>
  36. #include <Resources.h>
  37. #include <StandardFile.h>
  38. #include <Script.h>
  39.  
  40.  
  41. typedef struct Intl5Record
  42. {
  43.     UInt32            fVersion;
  44.     UInt16            fTableCount;
  45.     UInt16            fReserved1[3];
  46.     UInt32            fTable1Type;        // Should be 'btyp'
  47.     UInt32            fReserved2;
  48.     UInt32            fTable1Offset;        // Should be 30
  49.     UInt32            fTable1Size;        // Should be 256
  50.     // ...
  51. } Intl5Record;
  52.  
  53. /*------------------------------------------------------------------
  54.     main
  55.     
  56.     Application main entry point.
  57. ------------------------------------------------------------------*/
  58.  
  59. int
  60. main()
  61. {
  62.     StandardFileReply         sfReply;
  63.     SInt16                    fileRef;
  64.     Handle                    intl5Handle;
  65.     Handle                    byteCodeTableHandle;
  66.     Intl5Record *            intl5Record;
  67.     
  68.     // Initialize the Mac toolbox by printing to the SIOUX log.
  69.     std::printf("Welcome to the ZString byte table tool\n");
  70.     
  71.     // Ask for the name of the output file.
  72.     StandardPutFile("\pName the output file:", "\pByteTable.rsrc", &sfReply);
  73.     if (!sfReply.sfGood)
  74.     {
  75.         std::printf("Operation cancelled\n");
  76.         return 1;
  77.     }
  78.     
  79.     // Delete any existing file.
  80.     ::FSpDelete(&sfReply.sfFile);
  81.     ::FSpCreateResFile(&sfReply.sfFile, 'RSED', 'rsrc', 0);
  82.     
  83.     fileRef = ::FSpOpenResFile(&sfReply.sfFile, fsRdWrPerm);
  84.     if (fileRef == -1)
  85.     {
  86.         ::FSpDelete(&sfReply.sfFile);
  87.         std::printf("Output file couldn't be opened.\n");
  88.         std::printf("Operation cancelled\n");
  89.         return 1;
  90.     }
  91.     
  92.     intl5Handle = ::GetIntlResource(5);
  93.     if (intl5Handle == NULL)
  94.     {
  95.         std::printf("Couldn't find intl 5 resource.\n");
  96.         return 1;
  97.     }
  98.     
  99.     byteCodeTableHandle = ::NewHandle(256);
  100.     if (byteCodeTableHandle == NULL)
  101.     {
  102.         std::printf("Couldn't allocate memory.\n");
  103.         return 1;
  104.     }
  105.  
  106.     intl5Record = reinterpret_cast<Intl5Record *>(*intl5Handle);
  107.     if (intl5Record->fTable1Type != 'btyp' ||
  108.         intl5Record->fTable1Size != 256)
  109.     {
  110.         std::printf("Couldn't find byte table in intl 5 resource.\n");
  111.         return 1;
  112.     }
  113.  
  114.     if (intl5Record->fTable1Offset + intl5Record->fTable1Size >
  115.         ::GetHandleSize(intl5Handle))
  116.     {
  117.         std::printf("Byte table goes beyond limit of intl 5 resource.\n");
  118.         return 1;
  119.     }
  120.  
  121.     // Copy the data over
  122.     ::BlockMoveData(
  123.         *intl5Handle + intl5Record->fTable1Offset,
  124.         *byteCodeTableHandle,
  125.         256);
  126.  
  127.     // Now, munge the data so it fits our purpose. Namely,
  128.     // any -1 value should be set to zero.
  129.     for (UInt32 byteIndex = 0; byteIndex < 256; byteIndex++)
  130.     {
  131.         SInt8        charValue = (*byteCodeTableHandle)[byteIndex];
  132.         
  133.         if (charValue == -1)
  134.         {
  135.             (*byteCodeTableHandle)[byteIndex] = 0;
  136.         }
  137.         else if (charValue != 1 && charValue != 0)
  138.         {
  139.             std::printf("Found unexpected value in byte table in intl 5 resource (%d).\n", charValue);
  140.             return 1;
  141.         }
  142.     }
  143.  
  144.     // Assume japanese. If the user wants a different ID,
  145.     // he can renumber it in ResEdit.
  146.     ::AddResource(byteCodeTableHandle, kZStringTwoByteTableResType, kJapaneseOverrideID, "\p");
  147.     ::WriteResource(byteCodeTableHandle);
  148.     ::CloseResFile(fileRef);
  149.  
  150.     std::printf("Operation completed successfully\n");
  151. }
  152.  
  153.  
  154.