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

  1. /*==================================================================
  2.     File:        MacZStringOverrideTool.cpp
  3.     
  4.     Contains:    Tool for creating override libraries for
  5.                 ZStrings on the Mac.
  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 <StandardFile.h>
  37. #include <Resources.h>
  38.  
  39.  
  40. static Boolean
  41. ReadBinaryImage(
  42.     FILE *            inFile,
  43.     void * &        outMemoryImage,
  44.     UInt32 &        outLength);
  45.  
  46.  
  47. /*------------------------------------------------------------------
  48.     main
  49.     
  50.     Application main entry point.
  51. ------------------------------------------------------------------*/
  52.  
  53. int
  54. main()
  55. {
  56.     StandardFileReply         sfReply;
  57.     FILE *                    overrideFile = NULL;
  58.     char *                    overrideFileImage;
  59.     UInt32                    overrideFileSize;
  60.     OSType                    typeList[4];
  61.     
  62.     // Initialize the Mac toolbox by printing to the SIOUX log.
  63.     printf("Welcome to the ZString tool\n");
  64.     
  65.     // Ask for the new file name.
  66.     StandardGetFile(NULL, 0, typeList, &sfReply);
  67.     if (!sfReply.sfGood)
  68.     {
  69.         printf("Operation cancelled\n");
  70.         return 1;
  71.     }
  72.  
  73.     overrideFile = FSp_fopen(&sfReply.sfFile, "rb");
  74.  
  75.     // Ask for the name of the output file.
  76.     StandardPutFile("\pName the output file:", "\pOverrideDict.rsrc", &sfReply);
  77.     if (!sfReply.sfGood)
  78.     {
  79.         printf("Operation cancelled\n");
  80.         return 1;
  81.     }
  82.     
  83.     // Read in the binary.
  84.     if (!ReadBinaryImage(overrideFile, overrideFileImage, overrideFileSize))
  85.     {
  86.         printf("Operation cancelled\n");
  87.         return 1;
  88.     }
  89.  
  90.     fclose(overrideFile);
  91.     
  92.     // Process the data.
  93.     ZStringTool        stringTool;
  94.     ZToolOptions    options;
  95.     
  96.     stringTool.ProcessBinaries(overrideFileImage, overrideFileSize, NULL, 0, options);
  97.     
  98.     char *        dictionary;
  99.     UInt32        dictLength;
  100.     SInt16        fileRef;
  101.     
  102.     stringTool.CreateOverrideDictionary(dictionary, dictLength);
  103.     
  104.     Handle        newResHandle;
  105.     newResHandle = ::NewHandle(dictLength);
  106.     if (newResHandle == NULL)
  107.     {
  108.         printf("Out of memory. Couldn't allocate new resource.\n");
  109.         printf("Operation cancelled\n");
  110.         return 1;
  111.     }
  112.  
  113.     memcpy(*newResHandle, dictionary, dictLength);
  114.  
  115.     // Delete any existing file.
  116.     ::FSpDelete(&sfReply.sfFile);
  117.     ::FSpCreateResFile(&sfReply.sfFile, 'RSED', 'rsrc', 0);
  118.     
  119.     fileRef = ::FSpOpenResFile(&sfReply.sfFile, fsRdWrPerm);
  120.     if (fileRef == -1)
  121.     {
  122.         ::FSpDelete(&sfReply.sfFile);
  123.         printf("Output file couldn't be opened.\n");
  124.         printf("Operation cancelled\n");
  125.         return 1;
  126.     }
  127.     
  128.     AddResource(newResHandle, kZStringOverrideDictionaryResType, 128, "\p");
  129.     WriteResource(newResHandle);
  130.     ::CloseResFile(fileRef);
  131.  
  132.     printf("Operation completed successfully\n");
  133.     
  134.     delete [] overrideFileImage;
  135.     delete [] dictionary;
  136. }
  137.  
  138.  
  139. /*------------------------------------------------------------------
  140.     ReadBinaryImage
  141. ------------------------------------------------------------------*/
  142.  
  143. Boolean
  144. ReadBinaryImage(
  145.     FILE *            inFile,
  146.     void * &        outMemoryImage,
  147.     UInt32 &        outLength)
  148. {
  149.     // Now, attempt to read in the two binaries.
  150.     fseek(inFile, 0, SEEK_END);
  151.     outLength = ftell(inFile);
  152.     
  153.     outMemoryImage = malloc(outLength);
  154.     if (outMemoryImage == NULL)
  155.     {
  156.         fprintf(stderr, "Not enough memory to load file.");
  157.         return false;
  158.     } 
  159.     
  160.     fseek(inFile, 0, SEEK_SET);
  161.     if (fread(outMemoryImage, 1, outLength, inFile) != outLength)
  162.     {
  163.         fprintf(stderr, "File error encountered when reading file.");
  164.         return false;
  165.     }
  166.     
  167.     return true;
  168. }
  169.  
  170.  
  171.