home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: MacZStringByteTableTool.cpp
-
- Contains: Tool for extracting ZString byte table from
- Mac INTL 5 resources.
-
- 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 <Resources.h>
- #include <StandardFile.h>
- #include <Script.h>
-
-
- typedef struct Intl5Record
- {
- UInt32 fVersion;
- UInt16 fTableCount;
- UInt16 fReserved1[3];
- UInt32 fTable1Type; // Should be 'btyp'
- UInt32 fReserved2;
- UInt32 fTable1Offset; // Should be 30
- UInt32 fTable1Size; // Should be 256
- // ...
- } Intl5Record;
-
- /*------------------------------------------------------------------
- main
-
- Application main entry point.
- ------------------------------------------------------------------*/
-
- int
- main()
- {
- StandardFileReply sfReply;
- SInt16 fileRef;
- Handle intl5Handle;
- Handle byteCodeTableHandle;
- Intl5Record * intl5Record;
-
- // Initialize the Mac toolbox by printing to the SIOUX log.
- std::printf("Welcome to the ZString byte table tool\n");
-
- // Ask for the name of the output file.
- StandardPutFile("\pName the output file:", "\pByteTable.rsrc", &sfReply);
- if (!sfReply.sfGood)
- {
- std::printf("Operation cancelled\n");
- return 1;
- }
-
- // Delete any existing file.
- ::FSpDelete(&sfReply.sfFile);
- ::FSpCreateResFile(&sfReply.sfFile, 'RSED', 'rsrc', 0);
-
- fileRef = ::FSpOpenResFile(&sfReply.sfFile, fsRdWrPerm);
- if (fileRef == -1)
- {
- ::FSpDelete(&sfReply.sfFile);
- std::printf("Output file couldn't be opened.\n");
- std::printf("Operation cancelled\n");
- return 1;
- }
-
- intl5Handle = ::GetIntlResource(5);
- if (intl5Handle == NULL)
- {
- std::printf("Couldn't find intl 5 resource.\n");
- return 1;
- }
-
- byteCodeTableHandle = ::NewHandle(256);
- if (byteCodeTableHandle == NULL)
- {
- std::printf("Couldn't allocate memory.\n");
- return 1;
- }
-
- intl5Record = reinterpret_cast<Intl5Record *>(*intl5Handle);
- if (intl5Record->fTable1Type != 'btyp' ||
- intl5Record->fTable1Size != 256)
- {
- std::printf("Couldn't find byte table in intl 5 resource.\n");
- return 1;
- }
-
- if (intl5Record->fTable1Offset + intl5Record->fTable1Size >
- ::GetHandleSize(intl5Handle))
- {
- std::printf("Byte table goes beyond limit of intl 5 resource.\n");
- return 1;
- }
-
- // Copy the data over
- ::BlockMoveData(
- *intl5Handle + intl5Record->fTable1Offset,
- *byteCodeTableHandle,
- 256);
-
- // Now, munge the data so it fits our purpose. Namely,
- // any -1 value should be set to zero.
- for (UInt32 byteIndex = 0; byteIndex < 256; byteIndex++)
- {
- SInt8 charValue = (*byteCodeTableHandle)[byteIndex];
-
- if (charValue == -1)
- {
- (*byteCodeTableHandle)[byteIndex] = 0;
- }
- else if (charValue != 1 && charValue != 0)
- {
- std::printf("Found unexpected value in byte table in intl 5 resource (%d).\n", charValue);
- return 1;
- }
- }
-
- // Assume japanese. If the user wants a different ID,
- // he can renumber it in ResEdit.
- ::AddResource(byteCodeTableHandle, kZStringTwoByteTableResType, kJapaneseOverrideID, "\p");
- ::WriteResource(byteCodeTableHandle);
- ::CloseResFile(fileRef);
-
- std::printf("Operation completed successfully\n");
- }
-
-
-