home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: MacZStringOverrideTool.cpp
-
- Contains: Tool for creating override libraries for
- ZStrings on the Mac.
-
- Written by: Eric Traut
-
- Copyright: 2000-2001 Connectix Corporation
-
- This source has been placed into the public domain by
- Connectix Corporation. You have the right to modify,
- distribute or use this code without any legal limitations
- or finanicial/licensing requirements. Connectix is not
- liable for any problems that result from the use of this
- code.
-
- If you have comments, feedback, questions, or would like
- to submit bug fixes or updates to this code, please email
- opensource@connectix.com.
- ==================================================================*/
-
- #include "ZStringTool.h"
- #include "MacZString.h"
-
- #include <cstring>
- #include <cstddef>
- #include <cstdlib>
-
- extern "C" {
- #include <FSp_fopen.h>
- }
-
- #include <Files.h>
- #include <Devices.h>
- #include <StandardFile.h>
- #include <Resources.h>
-
-
- static Boolean
- ReadBinaryImage(
- FILE * inFile,
- void * & outMemoryImage,
- UInt32 & outLength);
-
-
- /*------------------------------------------------------------------
- main
-
- Application main entry point.
- ------------------------------------------------------------------*/
-
- int
- main()
- {
- StandardFileReply sfReply;
- FILE * overrideFile = NULL;
- char * overrideFileImage;
- UInt32 overrideFileSize;
- OSType typeList[4];
-
- // Initialize the Mac toolbox by printing to the SIOUX log.
- printf("Welcome to the ZString tool\n");
-
- // Ask for the new file name.
- StandardGetFile(NULL, 0, typeList, &sfReply);
- if (!sfReply.sfGood)
- {
- printf("Operation cancelled\n");
- return 1;
- }
-
- overrideFile = FSp_fopen(&sfReply.sfFile, "rb");
-
- // Ask for the name of the output file.
- StandardPutFile("\pName the output file:", "\pOverrideDict.rsrc", &sfReply);
- if (!sfReply.sfGood)
- {
- printf("Operation cancelled\n");
- return 1;
- }
-
- // Read in the binary.
- if (!ReadBinaryImage(overrideFile, overrideFileImage, overrideFileSize))
- {
- printf("Operation cancelled\n");
- return 1;
- }
-
- fclose(overrideFile);
-
- // Process the data.
- ZStringTool stringTool;
- ZToolOptions options;
-
- stringTool.ProcessBinaries(overrideFileImage, overrideFileSize, NULL, 0, options);
-
- char * dictionary;
- UInt32 dictLength;
- SInt16 fileRef;
-
- stringTool.CreateOverrideDictionary(dictionary, dictLength);
-
- Handle newResHandle;
- newResHandle = ::NewHandle(dictLength);
- if (newResHandle == NULL)
- {
- printf("Out of memory. Couldn't allocate new resource.\n");
- printf("Operation cancelled\n");
- return 1;
- }
-
- memcpy(*newResHandle, dictionary, dictLength);
-
- // Delete any existing file.
- ::FSpDelete(&sfReply.sfFile);
- ::FSpCreateResFile(&sfReply.sfFile, 'RSED', 'rsrc', 0);
-
- fileRef = ::FSpOpenResFile(&sfReply.sfFile, fsRdWrPerm);
- if (fileRef == -1)
- {
- ::FSpDelete(&sfReply.sfFile);
- printf("Output file couldn't be opened.\n");
- printf("Operation cancelled\n");
- return 1;
- }
-
- AddResource(newResHandle, kZStringOverrideDictionaryResType, 128, "\p");
- WriteResource(newResHandle);
- ::CloseResFile(fileRef);
-
- printf("Operation completed successfully\n");
-
- delete [] overrideFileImage;
- delete [] dictionary;
- }
-
-
- /*------------------------------------------------------------------
- ReadBinaryImage
- ------------------------------------------------------------------*/
-
- Boolean
- ReadBinaryImage(
- FILE * inFile,
- void * & outMemoryImage,
- UInt32 & outLength)
- {
- // Now, attempt to read in the two binaries.
- fseek(inFile, 0, SEEK_END);
- outLength = ftell(inFile);
-
- outMemoryImage = malloc(outLength);
- if (outMemoryImage == NULL)
- {
- fprintf(stderr, "Not enough memory to load file.");
- return false;
- }
-
- fseek(inFile, 0, SEEK_SET);
- if (fread(outMemoryImage, 1, outLength, inFile) != outLength)
- {
- fprintf(stderr, "File error encountered when reading file.");
- return false;
- }
-
- return true;
- }
-
-
-