1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
********************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1998-2003, International Business Machines
|
1999-12-13 22:28:37 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
********************************************************************************
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* makeconv.c:
|
|
|
|
* tool creating a binary (compressed) representation of the conversion mapping
|
|
|
|
* table (IBM NLTC ucmap format).
|
2000-05-12 19:59:03 +00:00
|
|
|
*
|
|
|
|
* 05/04/2000 helena Added fallback mapping into the picture...
|
2000-06-27 20:47:56 +00:00
|
|
|
* 06/29/2000 helena Major rewrite of the callback APIs.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2001-03-21 23:22:16 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <stdio.h>
|
2001-01-03 00:18:57 +00:00
|
|
|
#include "unicode/putil.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ucnv_io.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
2000-06-27 20:47:56 +00:00
|
|
|
#include "ucnv_bld.h"
|
1999-10-18 22:48:32 +00:00
|
|
|
#include "ucnv_imp.h"
|
2000-04-19 23:05:27 +00:00
|
|
|
#include "ucnv_cnv.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "filestrm.h"
|
1999-12-14 01:30:27 +00:00
|
|
|
#include "toolutil.h"
|
2000-04-18 23:51:15 +00:00
|
|
|
#include "uoptions.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/udata.h"
|
1999-12-04 02:31:40 +00:00
|
|
|
#include "unewdata.h"
|
|
|
|
#include "ucmpwrit.h"
|
2000-11-15 00:56:35 +00:00
|
|
|
#include "makeconv.h"
|
|
|
|
#include "genmbcs.h"
|
2003-08-14 21:34:54 +00:00
|
|
|
#include "unicode/uclean.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-07-13 00:26:14 +00:00
|
|
|
#define DEBUG 0
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-04-20 19:43:32 +00:00
|
|
|
/*
|
|
|
|
* from ucnvstat.c - static prototypes of data-based converters
|
|
|
|
*/
|
|
|
|
extern const UConverterStaticData * ucnv_converterStaticData[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES];
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
/*
|
|
|
|
* Global - verbosity
|
2000-02-05 00:19:15 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool VERBOSE = FALSE;
|
2002-07-17 03:56:50 +00:00
|
|
|
UBool TOUCHFILE = FALSE;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*Reads the header of the table file and fills in basic knowledge about the converter
|
|
|
|
*in "converter"
|
|
|
|
*/
|
2000-07-13 00:26:14 +00:00
|
|
|
static void readHeaderFromFile(UConverterSharedData* myConverter, FileStream* convFile, const char* converterName, UErrorCode* err);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
/*Reads the rest of the file, and fills up the shared objects if necessary
|
|
|
|
Returns the UConverterTable. */
|
2000-11-15 00:56:35 +00:00
|
|
|
static void loadTableFromFile(FileStream* convFile, UConverterSharedData* sharedData, UErrorCode* err);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
/* creates a UConverterSharedData from a mapping file.
|
|
|
|
* Fills in: *staticData, *table. Converter is NOT otherwise useful.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-04-19 23:05:27 +00:00
|
|
|
static UConverterSharedData* createConverterFromTableFile(const char* realName, UErrorCode* err);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
/*
|
|
|
|
* Set up the UNewData and write the converter..
|
|
|
|
*/
|
|
|
|
void writeConverterData(UConverterSharedData *mySharedData, const char *cnvName, const char *cnvDir, UErrorCode *status);
|
1999-12-04 02:31:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
static const char NLTC_SEPARATORS[9] = { '\r', '\n', '\t', ' ', '<', '>' ,'"' , 'U', '\0' };
|
2000-05-12 19:59:03 +00:00
|
|
|
static const char FALLBACK_SEPARATOR = '|';
|
1999-08-16 21:50:52 +00:00
|
|
|
static const char CODEPOINT_SEPARATORS[8] = { '\r', '>', '\\', 'x', '\n', ' ', '\t', '\0' };
|
|
|
|
static const char UNICODE_CODEPOINT_SEPARATORS[6] = { '<', '>', 'U', ' ', '\t', '\0' };
|
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
static const char *
|
|
|
|
skipWhitespace(const char *s) {
|
|
|
|
while(*s==' ' || *s=='\t') {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
parseCodepageBytes(const char *s, uint32_t *pBytes, const char **pEnd) {
|
|
|
|
char *end;
|
|
|
|
int32_t length=0;
|
|
|
|
uint32_t bytes=0, value;
|
|
|
|
|
|
|
|
while(s[0]=='\\' && s[1]=='x') {
|
|
|
|
if(length==4) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
value=uprv_strtoul(s+2, &end, 16);
|
|
|
|
s+=4;
|
|
|
|
if(end!=s) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bytes=(bytes<<8)|value;
|
|
|
|
++length;
|
|
|
|
}
|
|
|
|
if(length==0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(pEnd!=NULL) {
|
|
|
|
*pEnd=s;
|
|
|
|
}
|
|
|
|
*pBytes=bytes;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2002-02-14 20:04:35 +00:00
|
|
|
/* Remove all characters followed by '#'. There is an exception if there
|
|
|
|
* is a fallback sign '|' after the comment and the comment does not
|
|
|
|
* start in column 0. In this case, we just blank from '#' to just
|
|
|
|
* before the '|' in order to support the fact that IBM official .ucm
|
|
|
|
* files have the fallback information in comments!
|
1999-11-23 23:04:40 +00:00
|
|
|
*/
|
2001-02-06 22:11:41 +00:00
|
|
|
static char *
|
1999-11-23 23:04:40 +00:00
|
|
|
removeComments (char *line)
|
|
|
|
{
|
2002-02-14 20:04:35 +00:00
|
|
|
char *pound;
|
|
|
|
|
2002-02-16 00:44:52 +00:00
|
|
|
line = (char*)skipWhitespace(line);
|
2002-02-14 20:04:35 +00:00
|
|
|
pound = uprv_strchr (line, '#');
|
1999-11-23 23:04:40 +00:00
|
|
|
if (pound != NULL)
|
2000-05-12 19:59:03 +00:00
|
|
|
{
|
2002-02-14 20:04:35 +00:00
|
|
|
char *fallback = pound == line ? 0 : uprv_strchr(pound + 1, '|');
|
2000-05-12 19:59:03 +00:00
|
|
|
if (fallback != NULL)
|
|
|
|
{
|
|
|
|
uprv_memset(pound, ' ', fallback-pound);
|
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
else
|
2000-05-12 19:59:03 +00:00
|
|
|
{
|
|
|
|
*pound = '\0';
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 23:04:40 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true in c is a in set 'setOfChars', false otherwise
|
|
|
|
*/
|
2001-03-21 23:22:16 +00:00
|
|
|
static UBool
|
1999-11-23 23:04:40 +00:00
|
|
|
isInSet (char c, const char *setOfChars)
|
|
|
|
{
|
|
|
|
uint8_t i = 0;
|
|
|
|
|
|
|
|
while (setOfChars[i] != '\0')
|
|
|
|
{
|
|
|
|
if (c == setOfChars[i++])
|
2000-02-09 01:29:21 +00:00
|
|
|
return TRUE;
|
1999-11-23 23:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns pointer to the next non-whitespace (or non-separator)
|
|
|
|
*/
|
2001-03-21 23:22:16 +00:00
|
|
|
static int32_t
|
1999-11-23 23:04:40 +00:00
|
|
|
nextTokenOffset (const char *line, const char *separators)
|
|
|
|
{
|
2002-03-15 23:41:40 +00:00
|
|
|
int32_t i = 0;
|
1999-11-23 23:04:40 +00:00
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
while (line[i] && isInSet(line[i], separators))
|
|
|
|
i++;
|
1999-11-23 23:04:40 +00:00
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
return i;
|
1999-11-23 23:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns pointer to the next token based on the set of separators
|
|
|
|
*/
|
2001-02-06 22:11:41 +00:00
|
|
|
static char *
|
1999-11-23 23:04:40 +00:00
|
|
|
getToken (char *token, char *line, const char *separators)
|
|
|
|
{
|
2002-03-15 23:41:40 +00:00
|
|
|
int32_t i = nextTokenOffset (line, separators);
|
|
|
|
int8_t j = 0;
|
1999-11-23 23:04:40 +00:00
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
while (line[i] && (!isInSet(line[i], separators)))
|
|
|
|
token[j++] = line[i++];
|
|
|
|
token[j] = '\0';
|
1999-11-23 23:04:40 +00:00
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
return line + i;
|
1999-11-23 23:04:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool haveCopyright=TRUE;
|
2000-04-18 23:51:15 +00:00
|
|
|
|
2000-07-13 00:26:14 +00:00
|
|
|
static UDataInfo dataInfo={
|
1999-12-04 02:31:40 +00:00
|
|
|
sizeof(UDataInfo),
|
|
|
|
0,
|
|
|
|
|
|
|
|
U_IS_BIG_ENDIAN,
|
|
|
|
U_CHARSET_FAMILY,
|
|
|
|
sizeof(UChar),
|
|
|
|
0,
|
|
|
|
|
2001-02-06 22:11:41 +00:00
|
|
|
{0x63, 0x6e, 0x76, 0x74}, /* dataFormat="cnvt" */
|
2001-02-26 19:44:41 +00:00
|
|
|
{6, 2, 0, 0}, /* formatVersion */
|
2001-02-06 22:11:41 +00:00
|
|
|
{0, 0, 0, 0} /* dataVersion (calculated at runtime) */
|
1999-12-04 02:31:40 +00:00
|
|
|
};
|
|
|
|
|
2001-03-21 23:22:16 +00:00
|
|
|
void writeConverterData(UConverterSharedData *mySharedData,
|
|
|
|
const char *cnvName,
|
|
|
|
const char *cnvDir,
|
2000-05-12 19:59:03 +00:00
|
|
|
UErrorCode *status)
|
1999-12-04 02:31:40 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
UNewDataMemory *mem = NULL;
|
|
|
|
uint32_t sz2;
|
|
|
|
uint32_t size = 0;
|
|
|
|
|
|
|
|
if(U_FAILURE(*status))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem = udata_create(cnvDir, "cnv", cnvName, &dataInfo, haveCopyright ? U_COPYRIGHT_STRING : NULL, status);
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Couldn't create the udata %s.%s: %s\n",
|
|
|
|
cnvName,
|
|
|
|
"cnv",
|
|
|
|
u_errorName(*status));
|
|
|
|
return;
|
|
|
|
}
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
if(VERBOSE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "- Opened udata %s.%s\n", cnvName, "cnv");
|
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
/* all read only, clean, platform independent data. Mmmm. :) */
|
|
|
|
udata_writeBlock(mem, mySharedData->staticData, sizeof(UConverterStaticData));
|
|
|
|
size += sizeof(UConverterStaticData); /* Is 4-aligned - by size */
|
|
|
|
/* Now, write the table */
|
|
|
|
size += ((NewConverter *)mySharedData->table)->write((NewConverter *)mySharedData->table, mySharedData->staticData, mem);
|
|
|
|
|
|
|
|
sz2 = udata_finish(mem, status);
|
|
|
|
if(size != sz2)
|
2000-04-19 23:05:27 +00:00
|
|
|
{
|
2001-05-21 23:30:57 +00:00
|
|
|
fprintf(stderr, "error: wrote %d bytes to the .cnv file but counted %d bytes\n", sz2, size);
|
2000-11-15 00:56:35 +00:00
|
|
|
*status=U_INTERNAL_PROGRAM_ERROR;
|
2000-04-19 23:05:27 +00:00
|
|
|
}
|
2000-11-15 00:56:35 +00:00
|
|
|
if(VERBOSE)
|
2000-04-19 23:05:27 +00:00
|
|
|
{
|
2001-03-21 23:22:16 +00:00
|
|
|
fprintf(stderr, "- Wrote %d bytes to the udata.\n", sz2);
|
2000-04-19 23:05:27 +00:00
|
|
|
}
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
|
|
|
|
2000-04-18 23:51:15 +00:00
|
|
|
static UOption options[]={
|
2001-03-21 23:22:16 +00:00
|
|
|
UOPTION_HELP_H, /* 0 Numbers for those who*/
|
2000-04-19 23:05:27 +00:00
|
|
|
UOPTION_HELP_QUESTION_MARK, /* 1 can't count. */
|
|
|
|
UOPTION_COPYRIGHT, /* 2 */
|
|
|
|
UOPTION_VERSION, /* 3 */
|
|
|
|
UOPTION_DESTDIR, /* 4 */
|
2002-07-17 03:56:50 +00:00
|
|
|
UOPTION_VERBOSE, /* 5 */
|
|
|
|
UOPTION_PACKAGE_NAME, /* 6 */
|
|
|
|
UOPTION_DEF( "touchfile", 't', UOPT_NO_ARG) /* 7 */
|
2000-04-18 23:51:15 +00:00
|
|
|
};
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-09-21 21:49:32 +00:00
|
|
|
int main(int argc, char* argv[])
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-03-21 23:22:16 +00:00
|
|
|
UConverterSharedData* mySharedData = NULL;
|
2003-07-03 18:02:55 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR, localError;
|
2000-08-28 23:15:05 +00:00
|
|
|
char outFileName[UCNV_MAX_FULL_FILE_NAME_LENGTH];
|
2002-07-17 03:56:50 +00:00
|
|
|
char touchFileName[UCNV_MAX_FULL_FILE_NAME_LENGTH];
|
2000-08-28 23:15:05 +00:00
|
|
|
const char* destdir, *arg;
|
2002-07-17 03:56:50 +00:00
|
|
|
const char *pkgName = NULL;
|
2000-08-28 23:15:05 +00:00
|
|
|
size_t destdirlen;
|
|
|
|
char* dot = NULL, *outBasename;
|
|
|
|
char cnvName[UCNV_MAX_FULL_FILE_NAME_LENGTH];
|
2002-07-17 03:56:50 +00:00
|
|
|
char cnvNameWithPkg[UCNV_MAX_FULL_FILE_NAME_LENGTH];
|
2000-10-03 20:18:22 +00:00
|
|
|
UVersionInfo icuVersion;
|
2000-08-28 21:36:13 +00:00
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
/* Initialize ICU */
|
|
|
|
u_init(&err);
|
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
fprintf(stderr, "%s: can not initialize ICU. err = %s\n",
|
|
|
|
argv[0], u_errorName(err));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
U_MAIN_INIT_ARGS(argc, argv);
|
2000-07-13 00:26:14 +00:00
|
|
|
|
2000-10-03 20:18:22 +00:00
|
|
|
/* Set up the ICU version number */
|
|
|
|
u_getVersion(icuVersion);
|
|
|
|
uprv_memcpy(&dataInfo.dataVersion, &icuVersion, sizeof(UVersionInfo));
|
|
|
|
|
2000-04-18 23:51:15 +00:00
|
|
|
/* preset then read command line options */
|
|
|
|
options[4].value=u_getDataDirectory();
|
|
|
|
argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
|
|
|
|
|
|
|
|
/* error handling, printing usage message */
|
|
|
|
if(argc<0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error in command line argument \"%s\"\n",
|
|
|
|
argv[-argc]);
|
|
|
|
} else if(argc<2) {
|
|
|
|
argc=-1;
|
|
|
|
}
|
|
|
|
if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-options] files...\n"
|
|
|
|
"\tread .ucm codepage mapping files and write .cnv files\n"
|
2002-03-15 23:41:40 +00:00
|
|
|
"options:\n"
|
|
|
|
"\t-h or -? or --help this usage text\n"
|
|
|
|
"\t-V or --version show a version message\n"
|
|
|
|
"\t-c or --copyright include a copyright notice\n"
|
|
|
|
"\t-d or --destdir destination directory, followed by the path\n"
|
|
|
|
"\t-v or --verbose Turn on verbose output\n",
|
2000-04-18 23:51:15 +00:00
|
|
|
argv[0]);
|
2002-07-24 21:21:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"\t-p or --pkgname sets the 'package' name for output files.\n"
|
|
|
|
"\t If name is ICUDATA, then the default icu package\n"
|
|
|
|
"\t name will be used.\n"
|
|
|
|
"\t-t or --touchfile Generate additional small file without packagename, for nmake\n");
|
2000-04-18 23:51:15 +00:00
|
|
|
return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2000-04-18 23:51:15 +00:00
|
|
|
if(options[3].doesOccur) {
|
2000-04-19 23:05:27 +00:00
|
|
|
fprintf(stderr,"makeconv version %hu.%hu, ICU tool to read .ucm codepage mapping files and write .cnv files\n",
|
2000-04-18 23:51:15 +00:00
|
|
|
dataInfo.formatVersion[0], dataInfo.formatVersion[1]);
|
2000-04-19 23:05:27 +00:00
|
|
|
fprintf(stderr, "Copyright (C) 1998-2000, International Business Machines\n");
|
|
|
|
fprintf(stderr,"Corporation and others. All Rights Reserved.\n");
|
2000-04-18 23:51:15 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2000-02-29 18:42:28 +00:00
|
|
|
|
2002-07-17 03:56:50 +00:00
|
|
|
TOUCHFILE = options[7].doesOccur;
|
|
|
|
|
|
|
|
if(!options[6].doesOccur)
|
|
|
|
{
|
2002-07-24 21:21:05 +00:00
|
|
|
fprintf(stderr, "%s : option -p (package name) is required.\n",
|
2002-07-17 03:56:50 +00:00
|
|
|
argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pkgName =options[6].value;
|
|
|
|
if(!strcmp(pkgName, "ICUDATA"))
|
|
|
|
{
|
|
|
|
pkgName = U_ICUDATA_NAME;
|
|
|
|
}
|
|
|
|
if(pkgName[0] == 0)
|
|
|
|
{
|
|
|
|
pkgName = NULL;
|
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
if(TOUCHFILE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Don't use touchfile option with an empty packagename.\n",
|
|
|
|
argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-07-17 03:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-18 23:51:15 +00:00
|
|
|
/* get the options values */
|
|
|
|
haveCopyright = options[2].doesOccur;
|
|
|
|
destdir = options[4].value;
|
2000-04-19 23:05:27 +00:00
|
|
|
VERBOSE = options[5].doesOccur;
|
2000-02-29 18:42:28 +00:00
|
|
|
|
2000-04-18 23:51:15 +00:00
|
|
|
if (destdir != NULL && *destdir != 0) {
|
2000-02-09 01:29:21 +00:00
|
|
|
uprv_strcpy(outFileName, destdir);
|
|
|
|
destdirlen = uprv_strlen(destdir);
|
|
|
|
outBasename = outFileName + destdirlen;
|
|
|
|
if (*(outBasename - 1) != U_FILE_SEP_CHAR) {
|
|
|
|
*outBasename++ = U_FILE_SEP_CHAR;
|
|
|
|
++destdirlen;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destdirlen = 0;
|
|
|
|
outBasename = outFileName;
|
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2000-08-09 17:24:54 +00:00
|
|
|
#if DEBUG
|
|
|
|
{
|
|
|
|
int i;
|
2000-07-13 00:26:14 +00:00
|
|
|
printf("makeconv: processing %d files...\n", argc - 1);
|
|
|
|
for(i=1; i<argc; ++i) {
|
|
|
|
printf("%s ", argv[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2000-08-09 17:24:54 +00:00
|
|
|
#endif
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2003-07-03 18:02:55 +00:00
|
|
|
err = U_ZERO_ERROR;
|
2000-04-19 23:05:27 +00:00
|
|
|
for (++argv; --argc; ++argv)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-02-29 18:42:28 +00:00
|
|
|
arg = getLongPathname(*argv);
|
1999-12-14 01:30:27 +00:00
|
|
|
|
2000-02-07 20:54:50 +00:00
|
|
|
/*produces the right destination path for display*/
|
2000-02-09 01:29:21 +00:00
|
|
|
if (destdirlen != 0)
|
|
|
|
{
|
2000-04-18 23:51:15 +00:00
|
|
|
const char *basename;
|
2000-02-07 20:54:50 +00:00
|
|
|
|
|
|
|
/* find the last file sepator */
|
2000-02-09 01:29:21 +00:00
|
|
|
basename = uprv_strrchr(arg, U_FILE_SEP_CHAR);
|
|
|
|
if (basename == NULL) {
|
2000-02-07 20:54:50 +00:00
|
|
|
basename = arg;
|
|
|
|
} else {
|
|
|
|
++basename;
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
|
|
|
|
uprv_strcpy(outBasename, basename);
|
2000-02-07 20:54:50 +00:00
|
|
|
}
|
|
|
|
else
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
|
|
|
uprv_strcpy(outFileName, arg);
|
2000-02-07 20:54:50 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*removes the extension if any is found*/
|
2000-09-21 21:49:32 +00:00
|
|
|
dot = uprv_strrchr(outBasename, '.');
|
2001-03-21 23:22:16 +00:00
|
|
|
if (dot)
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
|
|
|
*dot = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the basename without extension is the converter name */
|
|
|
|
uprv_strcpy(cnvName, outBasename);
|
|
|
|
|
2002-07-17 03:56:50 +00:00
|
|
|
if(TOUCHFILE)
|
2002-07-24 21:21:05 +00:00
|
|
|
{
|
|
|
|
uprv_strcpy(touchFileName, outBasename);
|
|
|
|
uprv_strcat(touchFileName, ".cnv");
|
|
|
|
}
|
2002-07-17 03:56:50 +00:00
|
|
|
|
|
|
|
if(pkgName != NULL)
|
|
|
|
{
|
|
|
|
/* changes both baename and filename */
|
|
|
|
uprv_strcpy(outBasename, pkgName);
|
|
|
|
uprv_strcat(outBasename, "_");
|
|
|
|
uprv_strcat(outBasename, cnvName);
|
|
|
|
}
|
2002-07-24 21:21:05 +00:00
|
|
|
|
2002-07-17 03:56:50 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*Adds the target extension*/
|
2000-02-09 01:29:21 +00:00
|
|
|
uprv_strcat(outBasename, CONVERTER_FILE_EXTENSION);
|
|
|
|
|
2000-08-09 17:24:54 +00:00
|
|
|
#if DEBUG
|
2000-07-13 00:26:14 +00:00
|
|
|
printf("makeconv: processing %s ...\n", arg);
|
|
|
|
fflush(stdout);
|
2000-08-09 17:24:54 +00:00
|
|
|
#endif
|
2003-07-03 18:02:55 +00:00
|
|
|
localError = U_ZERO_ERROR;
|
|
|
|
mySharedData = createConverterFromTableFile(arg, &localError);
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2003-07-03 18:02:55 +00:00
|
|
|
if (U_FAILURE(localError) || (mySharedData == NULL))
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
|
|
|
/* if an error is found, print out an error msg and keep going */
|
2003-07-03 18:02:55 +00:00
|
|
|
fprintf(stderr, "Error creating converter for \"%s\" file for \"%s\" (%s)\n", outFileName, arg,
|
|
|
|
u_errorName(localError));
|
|
|
|
if(U_SUCCESS(err)) {
|
|
|
|
err = localError;
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
else
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
2000-08-14 23:11:00 +00:00
|
|
|
/* Make the static data name equal to the file name */
|
|
|
|
if( /*VERBOSE && */ uprv_stricmp(cnvName,mySharedData->staticData->name))
|
|
|
|
{
|
2001-03-21 23:22:16 +00:00
|
|
|
fprintf(stderr, "Warning: %s%s claims to be '%s'\n",
|
2000-08-14 23:11:00 +00:00
|
|
|
cnvName,
|
|
|
|
CONVERTER_FILE_EXTENSION,
|
|
|
|
mySharedData->staticData->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_strcpy((char*)mySharedData->staticData->name, cnvName);
|
|
|
|
|
2003-07-03 18:02:55 +00:00
|
|
|
if(!uprv_isInvariantString((char*)mySharedData->staticData->name, -1)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error: A converter name must contain only invariant characters.\n"
|
|
|
|
"%s is not a valid converter name.\n",
|
|
|
|
mySharedData->staticData->name);
|
|
|
|
if(U_SUCCESS(err)) {
|
|
|
|
err = U_INVALID_TABLE_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-17 03:56:50 +00:00
|
|
|
if(pkgName == NULL)
|
|
|
|
{
|
|
|
|
uprv_strcpy(cnvNameWithPkg, cnvName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uprv_strcpy(cnvNameWithPkg, pkgName);
|
|
|
|
uprv_strcat(cnvNameWithPkg, "_");
|
|
|
|
uprv_strcat(cnvNameWithPkg, cnvName);
|
|
|
|
}
|
|
|
|
|
2003-07-03 18:02:55 +00:00
|
|
|
localError = U_ZERO_ERROR;
|
|
|
|
writeConverterData(mySharedData, cnvNameWithPkg, destdir, &localError);
|
2000-11-15 00:56:35 +00:00
|
|
|
((NewConverter *)mySharedData->table)->close((NewConverter *)mySharedData->table);
|
2002-07-24 21:21:05 +00:00
|
|
|
if(TOUCHFILE)
|
|
|
|
{
|
|
|
|
FileStream *q;
|
|
|
|
char msg[1024];
|
|
|
|
|
|
|
|
sprintf(msg, "This empty file tells nmake that %s in package %s has been updated.\n",
|
|
|
|
cnvName, pkgName);
|
|
|
|
|
|
|
|
q = T_FileStream_open(touchFileName, "w");
|
|
|
|
if(q == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error writing touchfile \"%s\"\n", touchFileName);
|
2003-07-03 18:02:55 +00:00
|
|
|
localError = U_FILE_ACCESS_ERROR;
|
2002-07-24 21:21:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T_FileStream_write(q, msg, uprv_strlen(msg));
|
|
|
|
T_FileStream_close(q);
|
|
|
|
}
|
|
|
|
}
|
2002-07-17 03:56:50 +00:00
|
|
|
|
|
|
|
/* write the information data */
|
2000-11-15 00:56:35 +00:00
|
|
|
uprv_free((UConverterStaticData *)mySharedData->staticData);
|
|
|
|
uprv_free(mySharedData);
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2003-07-03 18:02:55 +00:00
|
|
|
if(U_FAILURE(localError))
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
2000-07-13 00:26:14 +00:00
|
|
|
/* if an error is found, print out an error msg and keep going*/
|
2003-07-03 18:02:55 +00:00
|
|
|
fprintf(stderr, "Error writing \"%s\" file for \"%s\" (%s)\n", outFileName, arg,
|
|
|
|
u_errorName(localError));
|
|
|
|
if(U_SUCCESS(err)) {
|
|
|
|
err = localError;
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-04-19 23:05:27 +00:00
|
|
|
puts(outFileName);
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-05 02:14:53 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2001-03-17 00:34:04 +00:00
|
|
|
static void
|
|
|
|
getPlatformAndCCSIDFromName(const char *name, int8_t *pPlatform, int32_t *pCCSID) {
|
|
|
|
if( (name[0]=='i' || name[0]=='I') &&
|
|
|
|
(name[1]=='b' || name[1]=='B') &&
|
|
|
|
(name[2]=='m' || name[2]=='M')
|
|
|
|
) {
|
|
|
|
name+=3;
|
|
|
|
if(*name=='-') {
|
|
|
|
++name;
|
|
|
|
}
|
|
|
|
*pPlatform=UCNV_IBM;
|
|
|
|
*pCCSID=(int32_t)uprv_strtoul(name, NULL, 10);
|
|
|
|
} else {
|
|
|
|
*pPlatform=UCNV_UNKNOWN;
|
|
|
|
*pCCSID=0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Reads the header of the table file and fills in basic knowledge about the converter in "converter"*/
|
2000-07-13 00:26:14 +00:00
|
|
|
void readHeaderFromFile(UConverterSharedData* mySharedData,
|
2000-02-09 01:29:21 +00:00
|
|
|
FileStream* convFile,
|
2000-04-18 21:57:47 +00:00
|
|
|
const char* converterName,
|
2001-02-26 19:44:41 +00:00
|
|
|
UErrorCode *pErrorCode)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-02-26 19:44:41 +00:00
|
|
|
char line[200];
|
|
|
|
char *s, *end, *key, *value;
|
|
|
|
UConverterStaticData *staticData;
|
|
|
|
char c;
|
2000-07-13 00:26:14 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
2000-12-20 18:41:36 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
staticData=(UConverterStaticData *)mySharedData->staticData;
|
|
|
|
staticData->conversionType=UCNV_UNSUPPORTED_CONVERTER;
|
|
|
|
staticData->platform=UCNV_IBM;
|
|
|
|
staticData->subCharLen=0;
|
|
|
|
|
|
|
|
while(T_FileStream_readLine(convFile, line, sizeof(line))) {
|
|
|
|
/* remove comments and trailing CR and LF and remove whitespace from the end */
|
|
|
|
for(end=line; (c=*end)!=0; ++end) {
|
|
|
|
if(c=='#' || c=='\r' || c=='\n') {
|
|
|
|
break;
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
}
|
|
|
|
while(end>line && (*(end-1)==' ' || *(end-1)=='\t')) {
|
|
|
|
--end;
|
|
|
|
}
|
|
|
|
*end=0;
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
/* skip leading white space and ignore empty lines */
|
|
|
|
s=(char *)skipWhitespace(line);
|
|
|
|
if(*s==0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop at the beginning of the mapping section */
|
|
|
|
if(uprv_memcmp(s, "CHARMAP", 7)==0) {
|
|
|
|
break;
|
|
|
|
}
|
2000-08-28 23:15:05 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
/* get the key name, bracketed in <> */
|
|
|
|
if(*s!='<') {
|
|
|
|
fprintf(stderr, "error: no header field <key> in line \"%s\"\n", line);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
key=++s;
|
|
|
|
while(*s!='>') {
|
|
|
|
if(*s==0) {
|
|
|
|
fprintf(stderr, "error: incomplete header field <key> in line \"%s\"\n", line);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
*s=0;
|
|
|
|
|
|
|
|
/* get the value string, possibly quoted */
|
|
|
|
s=(char *)skipWhitespace(s+1);
|
|
|
|
if(*s!='"') {
|
|
|
|
value=s;
|
|
|
|
} else {
|
|
|
|
/* remove the quotes */
|
|
|
|
value=s+1;
|
|
|
|
if(end>value && *(end-1)=='"') {
|
|
|
|
*--end=0;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
/* collect the information from the header field, ignore unknown keys */
|
|
|
|
if(uprv_strcmp(key, "code_set_name")==0) {
|
|
|
|
if(*value!=0) {
|
|
|
|
uprv_strcpy((char*)staticData->name, value);
|
2001-03-17 00:34:04 +00:00
|
|
|
getPlatformAndCCSIDFromName(value, &staticData->platform, &staticData->codepage);
|
2001-02-26 19:44:41 +00:00
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "uconv_class")==0) {
|
|
|
|
const UConverterStaticData *prototype;
|
|
|
|
|
|
|
|
if(uprv_strcmp(value, "DBCS")==0) {
|
|
|
|
staticData->conversionType=UCNV_DBCS;
|
|
|
|
} else if(uprv_strcmp(value, "SBCS")==0) {
|
|
|
|
staticData->conversionType = UCNV_SBCS;
|
|
|
|
} else if(uprv_strcmp(value, "MBCS")==0) {
|
|
|
|
staticData->conversionType = UCNV_MBCS;
|
|
|
|
} else if(uprv_strcmp(value, "EBCDIC_STATEFUL")==0) {
|
|
|
|
staticData->conversionType = UCNV_EBCDIC_STATEFUL;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: unknown <uconv_class> %s\n", value);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
/* Now that we know the type, copy any 'default' values from the table. */
|
|
|
|
prototype=ucnv_converterStaticData[staticData->conversionType];
|
|
|
|
if(prototype!=NULL) {
|
|
|
|
if(staticData->name[0]==0) {
|
|
|
|
uprv_strcpy((char*)staticData->name, prototype->name);
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
|
|
|
|
if(staticData->codepage==0) {
|
|
|
|
staticData->codepage = prototype->codepage;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
|
|
|
|
if(staticData->platform==0) {
|
|
|
|
staticData->platform = prototype->platform;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
|
|
|
|
if(staticData->minBytesPerChar==0) {
|
|
|
|
staticData->minBytesPerChar = prototype->minBytesPerChar;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
|
|
|
|
if(staticData->maxBytesPerChar==0) {
|
|
|
|
staticData->maxBytesPerChar = prototype->maxBytesPerChar;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
if(staticData->subCharLen==0) {
|
|
|
|
staticData->subCharLen=prototype->subCharLen;
|
2000-12-20 20:34:23 +00:00
|
|
|
if(prototype->subCharLen>0) {
|
2001-02-26 19:44:41 +00:00
|
|
|
uprv_memcpy(staticData->subChar, prototype->subChar, prototype->subCharLen);
|
2000-12-20 20:34:23 +00:00
|
|
|
}
|
2000-04-20 19:43:32 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "mb_cur_max")==0) {
|
|
|
|
if('1'<=*value && *value<='4' && value[1]==0) {
|
|
|
|
staticData->maxBytesPerChar=(int8_t)(*value-'0');
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: illegal <mb_cur_max> %s\n", value);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "mb_cur_min")==0) {
|
|
|
|
if('1'<=*value && *value<='4' && value[1]==0) {
|
|
|
|
staticData->minBytesPerChar=(int8_t)(*value-'0');
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: illegal <mb_cur_min> %s\n", value);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "subchar")==0) {
|
|
|
|
uint32_t bytes;
|
|
|
|
int32_t length;
|
|
|
|
|
|
|
|
length=parseCodepageBytes(value, &bytes, (const char **)&end);
|
|
|
|
if(length>0 && *end==0) {
|
|
|
|
staticData->subCharLen=(int8_t)length;
|
|
|
|
do {
|
|
|
|
staticData->subChar[--length]=(uint8_t)bytes;
|
|
|
|
bytes>>=8;
|
|
|
|
} while(length>0);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: illegal <subchar> %s\n", value);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "subchar1")==0) {
|
|
|
|
uint32_t bytes;
|
|
|
|
|
|
|
|
if(1==parseCodepageBytes(value, &bytes, (const char **)&end) && *end==0) {
|
|
|
|
staticData->subChar1=(uint8_t)bytes;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: illegal <subchar1> %s\n", value);
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if(uprv_strcmp(key, "icu:state")==0) {
|
|
|
|
/* if an SBCS/DBCS/EBCDIC_STATEFUL converter has icu:state, then turn it into MBCS */
|
|
|
|
switch(staticData->conversionType) {
|
|
|
|
case UCNV_SBCS:
|
|
|
|
case UCNV_DBCS:
|
|
|
|
case UCNV_EBCDIC_STATEFUL:
|
|
|
|
staticData->conversionType = UCNV_MBCS;
|
|
|
|
break;
|
|
|
|
case UCNV_MBCS:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error: <icu:state> entry for non-MBCS table or before the <uconv_class> line\n");
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
2000-04-20 19:43:32 +00:00
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
if(staticData->maxBytesPerChar==0) {
|
|
|
|
fprintf(stderr, "error: <icu:state> before the <mb_cur_max> line\n");
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
if(mySharedData->table==NULL) {
|
|
|
|
mySharedData->table=(UConverterTable *)MBCSOpen(staticData->maxBytesPerChar);
|
|
|
|
if(mySharedData->table==NULL) {
|
|
|
|
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
2001-02-26 19:44:41 +00:00
|
|
|
if(!MBCSAddState((NewConverter *)mySharedData->table, value)) {
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 19:44:41 +00:00
|
|
|
if(staticData->conversionType==UCNV_UNSUPPORTED_CONVERTER) {
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
|
|
|
} else if(staticData->conversionType==UCNV_MBCS && mySharedData->table==NULL) {
|
|
|
|
fprintf(stderr, "error: missing state table information (<icu:state>) for MBCS\n");
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
2003-04-11 21:05:50 +00:00
|
|
|
} else if(staticData->subChar1!=0 &&
|
|
|
|
!staticData->conversionType==UCNV_MBCS &&
|
|
|
|
!staticData->conversionType==UCNV_EBCDIC_STATEFUL
|
|
|
|
) {
|
|
|
|
fprintf(stderr, "error: <subchar1> defined for a type other than MBCS or EBCDIC_STATEFUL\n");
|
|
|
|
*pErrorCode=U_INVALID_TABLE_FORMAT;
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
void loadTableFromFile(FileStream* convFile, UConverterSharedData* sharedData, UErrorCode* err)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
char storageLine[200];
|
|
|
|
char* line = NULL;
|
|
|
|
UConverterStaticData *staticData=(UConverterStaticData *)sharedData->staticData;
|
|
|
|
NewConverter *cnvData = (NewConverter *)sharedData->table;
|
|
|
|
UChar32 unicodeValue, codepageValue;
|
|
|
|
uint8_t mbcsBytes[8];
|
|
|
|
int32_t mbcsLength;
|
|
|
|
char codepointBytes[20];
|
|
|
|
UBool isOK = TRUE;
|
2000-12-19 23:07:50 +00:00
|
|
|
uint8_t precisionMask = 0, unicodeMask = 0;
|
2000-11-15 00:56:35 +00:00
|
|
|
char endOfLine;
|
|
|
|
|
|
|
|
if(cnvData->startMappings!=NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-01-02 22:55:14 +00:00
|
|
|
if(!cnvData->startMappings(cnvData)) {
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-01-03 00:30:31 +00:00
|
|
|
if(cnvData->isValid!=NULL)
|
|
|
|
{
|
|
|
|
const uint8_t *p = staticData->subChar;
|
|
|
|
codepageValue = 0;
|
|
|
|
switch(staticData->subCharLen) {
|
|
|
|
case 4: codepageValue = (codepageValue << 8) | *p++;
|
|
|
|
case 3: codepageValue = (codepageValue << 8) | *p++;
|
|
|
|
case 2: codepageValue = (codepageValue << 8) | *p++;
|
|
|
|
case 1: codepageValue = (codepageValue << 8) | *p;
|
|
|
|
default: break; /* must never occur */
|
|
|
|
}
|
|
|
|
if(!cnvData->isValid(cnvData, staticData->subChar, staticData->subCharLen, codepageValue)) {
|
|
|
|
fprintf(stderr, " the substitution character byte sequence is illegal in this codepage structure!\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
isOK = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
staticData->hasFromUnicodeFallback = staticData->hasToUnicodeFallback = FALSE;
|
2000-05-12 19:59:03 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
while (T_FileStream_readLine(convFile, storageLine, sizeof(storageLine)))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
removeComments(storageLine);
|
|
|
|
line = storageLine;
|
|
|
|
if (line[nextTokenOffset(line, NLTC_SEPARATORS)] != '\0')
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
/* get the Unicode code point */
|
|
|
|
line = getToken(codepointBytes, line, UNICODE_CODEPOINT_SEPARATORS);
|
|
|
|
if (uprv_strcmp(codepointBytes, "END") == 0)
|
2000-07-13 00:26:14 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
break;
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
2000-11-15 00:56:35 +00:00
|
|
|
unicodeValue = (UChar32)T_CString_stringToInteger(codepointBytes, 16);
|
2000-07-05 23:04:48 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
/* get the codepage bytes */
|
|
|
|
codepageValue = 0;
|
|
|
|
mbcsLength = 0;
|
|
|
|
do
|
2000-02-09 01:29:21 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
line = getToken(codepointBytes, line, CODEPOINT_SEPARATORS);
|
|
|
|
mbcsBytes[mbcsLength] = (uint8_t)T_CString_stringToInteger(codepointBytes, 16);
|
|
|
|
codepageValue = codepageValue << 8 | mbcsBytes[mbcsLength++];
|
2000-07-13 00:26:14 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
/* End of line could be \0 or | (if fallback) */
|
|
|
|
endOfLine= line[nextTokenOffset(line, CODEPOINT_SEPARATORS)];
|
2000-07-13 00:26:14 +00:00
|
|
|
} while((endOfLine != '\0') && (endOfLine != FALLBACK_SEPARATOR));
|
2000-12-19 23:07:50 +00:00
|
|
|
|
|
|
|
if(unicodeValue>=0x10000) {
|
|
|
|
unicodeMask|=UCNV_HAS_SUPPLEMENTARY; /* there are supplementary code points */
|
|
|
|
} else if(UTF_IS_SURROGATE(unicodeValue)) {
|
|
|
|
unicodeMask|=UCNV_HAS_SURROGATES; /* there are single surrogates */
|
|
|
|
}
|
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
if((uint32_t)unicodeValue > 0x10ffff)
|
2000-07-13 00:26:14 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
fprintf(stderr, "error: Unicode code point > U+10ffff in '%s'\n", storageLine);
|
|
|
|
isOK = FALSE;
|
|
|
|
}
|
|
|
|
else if(endOfLine == FALLBACK_SEPARATOR)
|
|
|
|
{
|
|
|
|
/* we know that there is a fallback separator */
|
|
|
|
precisionMask |= 1;
|
|
|
|
line = uprv_strchr(line, FALLBACK_SEPARATOR) + 1;
|
|
|
|
switch(*line)
|
2000-07-13 00:26:14 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
case '0':
|
|
|
|
/* set roundtrip mappings */
|
|
|
|
isOK &= cnvData->addToUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, 0) &&
|
|
|
|
cnvData->addFromUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, 0);
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
/* set only a fallback mapping from Unicode to codepage */
|
|
|
|
staticData->hasFromUnicodeFallback = TRUE;
|
|
|
|
isOK &= cnvData->addFromUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, 1);
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
/* skip subchar mappings */
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
/* set only a fallback mapping from codepage to Unicode */
|
|
|
|
staticData->hasToUnicodeFallback = TRUE;
|
|
|
|
isOK &= cnvData->addToUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error: illegal fallback indicator '%s' in '%s'\n", line - 1, storageLine);
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
break;
|
2000-07-13 00:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2000-11-15 00:56:35 +00:00
|
|
|
else
|
2000-07-13 00:26:14 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
precisionMask |= 2;
|
|
|
|
/* set the mappings */
|
|
|
|
isOK &= cnvData->addToUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, -1) &&
|
|
|
|
cnvData->addFromUnicode(cnvData, mbcsBytes, mbcsLength, unicodeValue, codepageValue, -1);
|
2000-02-09 01:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-12-19 23:07:50 +00:00
|
|
|
if(unicodeMask == 3)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "warning: contains mappings to both supplementary code points and single surrogates\n");
|
|
|
|
}
|
|
|
|
staticData->unicodeMask = unicodeMask;
|
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
if(cnvData->finishMappings!=NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
cnvData->finishMappings(cnvData, staticData);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
if(!isOK)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-11-15 00:56:35 +00:00
|
|
|
else if(precisionMask == 3)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
fprintf(stderr, "error: some entries have the mapping precision (with '|'), some do not\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
/*creates a UConverterStaticData, fills in necessary links to it the appropriate function pointers*/
|
|
|
|
UConverterSharedData* createConverterFromTableFile(const char* converterName, UErrorCode* err)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-07-24 21:21:05 +00:00
|
|
|
FileStream* convFile = NULL;
|
|
|
|
UConverterSharedData* mySharedData = NULL;
|
|
|
|
UConverterStaticData* myStaticData = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
if (U_FAILURE(*err)) return NULL;
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
convFile = T_FileStream_open(converterName, "r");
|
|
|
|
if (convFile == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-07-24 21:21:05 +00:00
|
|
|
*err = U_FILE_ACCESS_ERROR;
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
|
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
mySharedData = (UConverterSharedData*) uprv_malloc(sizeof(UConverterSharedData));
|
|
|
|
if (mySharedData == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-07-24 21:21:05 +00:00
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
T_FileStream_close(convFile);
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
uprv_memset(mySharedData, 0, sizeof(UConverterSharedData));
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
mySharedData->structSize = sizeof(UConverterSharedData);
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
myStaticData = (UConverterStaticData*) uprv_malloc(sizeof(UConverterStaticData));
|
|
|
|
if (myStaticData == NULL)
|
2000-04-19 23:05:27 +00:00
|
|
|
{
|
2002-07-24 21:21:05 +00:00
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
T_FileStream_close(convFile);
|
|
|
|
return NULL;
|
2001-03-21 23:22:16 +00:00
|
|
|
}
|
2002-07-24 21:21:05 +00:00
|
|
|
uprv_memset(myStaticData, 0, sizeof(UConverterStaticData));
|
|
|
|
mySharedData->staticData = myStaticData;
|
|
|
|
myStaticData->structSize = sizeof(UConverterStaticData);
|
|
|
|
/* mySharedData->staticDataOwned = FALSE; */ /* not owned if in udata */
|
|
|
|
mySharedData->sharedDataCached = FALSE;
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
mySharedData->dataMemory = NULL; /* for init */
|
1999-12-04 02:31:40 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
readHeaderFromFile(mySharedData, convFile, converterName, err);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
if (U_FAILURE(*err)) return NULL;
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
switch (myStaticData->conversionType)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-03-21 23:22:16 +00:00
|
|
|
case UCNV_SBCS:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-12-20 20:34:23 +00:00
|
|
|
/* SBCS: use MBCS data structure with a default state table */
|
2001-01-02 22:55:14 +00:00
|
|
|
if(mySharedData->staticData->maxBytesPerChar!=1) {
|
|
|
|
fprintf(stderr, "error: SBCS codepage with max bytes/char!=1\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-20 20:34:23 +00:00
|
|
|
myStaticData->conversionType = UCNV_MBCS;
|
2001-01-12 21:13:57 +00:00
|
|
|
if(mySharedData->table == NULL) {
|
2001-05-22 23:31:54 +00:00
|
|
|
NewConverter *sharedDataTable = MBCSOpen(1);
|
|
|
|
if(sharedDataTable != NULL) {
|
|
|
|
if(!MBCSAddState(sharedDataTable, "0-ff")) {
|
2001-01-12 21:13:57 +00:00
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
2001-05-22 23:31:54 +00:00
|
|
|
sharedDataTable->close(sharedDataTable);
|
|
|
|
} else {
|
|
|
|
mySharedData->table = (UConverterTable *)sharedDataTable;
|
2001-01-12 21:13:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
2000-12-20 20:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
case UCNV_MBCS:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-12-20 20:34:23 +00:00
|
|
|
/* MBCSOpen() was called by readHeaderFromFile() */
|
2000-02-09 01:29:21 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
case UCNV_EBCDIC_STATEFUL:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-01-02 22:55:14 +00:00
|
|
|
/* EBCDIC_STATEFUL: use MBCS data structure with a default state table */
|
|
|
|
if(mySharedData->staticData->maxBytesPerChar!=2) {
|
|
|
|
fprintf(stderr, "error: DBCS codepage with max bytes/char!=2\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
myStaticData->conversionType = UCNV_MBCS;
|
2001-01-12 21:13:57 +00:00
|
|
|
if(mySharedData->table == NULL) {
|
2001-05-22 23:31:54 +00:00
|
|
|
NewConverter *sharedDataTable = MBCSOpen(2);
|
|
|
|
if(sharedDataTable != NULL) {
|
|
|
|
if( !MBCSAddState(sharedDataTable, "0-ff, e:1.s, f:0.s") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "initial, 0-3f:4, e:1.s, f:0.s, 40:3, 41-fe:2, ff:4") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "0-40:1.i, 41-fe:1., ff:1.i") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "0-ff:1.i, 40:1.") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "0-ff:1.i")
|
2001-01-12 21:13:57 +00:00
|
|
|
) {
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
2001-05-22 23:31:54 +00:00
|
|
|
sharedDataTable->close(sharedDataTable);
|
|
|
|
} else {
|
|
|
|
mySharedData->table = (UConverterTable *)sharedDataTable;
|
2001-01-12 21:13:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
2001-01-02 22:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-21 23:22:16 +00:00
|
|
|
case UCNV_DBCS:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-01-02 22:55:14 +00:00
|
|
|
/* DBCS: use MBCS data structure with a default state table */
|
|
|
|
if(mySharedData->staticData->maxBytesPerChar!=2) {
|
|
|
|
fprintf(stderr, "error: DBCS codepage with max bytes/char!=2\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
myStaticData->conversionType = UCNV_MBCS;
|
2001-01-12 21:13:57 +00:00
|
|
|
if(mySharedData->table == NULL) {
|
2001-05-22 23:31:54 +00:00
|
|
|
NewConverter *sharedDataTable = MBCSOpen(2);
|
|
|
|
if(sharedDataTable != NULL) {
|
|
|
|
if( !MBCSAddState(sharedDataTable, "0-3f:3, 40:2, 41-fe:1, ff:3") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "41-fe") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "40") ||
|
|
|
|
!MBCSAddState(sharedDataTable, "")
|
2001-01-12 21:13:57 +00:00
|
|
|
) {
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
2001-05-22 23:31:54 +00:00
|
|
|
sharedDataTable->close(sharedDataTable);
|
|
|
|
} else {
|
|
|
|
mySharedData->table = (UConverterTable *)sharedDataTable;
|
2001-01-12 21:13:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
2001-01-02 22:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
2000-02-09 01:29:21 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-11-15 00:56:35 +00:00
|
|
|
default :
|
2002-07-24 21:21:05 +00:00
|
|
|
fprintf(stderr, "error: <uconv_class> omitted\n");
|
|
|
|
*err = U_INVALID_TABLE_FORMAT;
|
|
|
|
mySharedData->table = NULL;
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
2001-01-02 22:55:14 +00:00
|
|
|
if(U_SUCCESS(*err) && mySharedData->table != NULL)
|
1999-12-15 05:14:03 +00:00
|
|
|
{
|
2000-11-15 00:56:35 +00:00
|
|
|
loadTableFromFile(convFile, mySharedData, err);
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
2000-11-15 00:56:35 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
T_FileStream_close(convFile);
|
2001-03-21 23:22:16 +00:00
|
|
|
|
2002-07-24 21:21:05 +00:00
|
|
|
return mySharedData;
|
1999-12-04 02:31:40 +00:00
|
|
|
}
|
|
|
|
|
2000-02-29 18:42:28 +00:00
|
|
|
/*
|
|
|
|
* Hey, Emacs, please set the following:
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
*/
|