diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /intl/icu/source/tools/genrb | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'intl/icu/source/tools/genrb')
28 files changed, 10712 insertions, 0 deletions
diff --git a/intl/icu/source/tools/genrb/Makefile.in b/intl/icu/source/tools/genrb/Makefile.in new file mode 100644 index 000000000..63cd52bb9 --- /dev/null +++ b/intl/icu/source/tools/genrb/Makefile.in @@ -0,0 +1,113 @@ +################################################################################# +## Makefile.in for ICU - tools/genrb # +## Copyright (C) 2016 and later: Unicode, Inc. and others. # +## License & terms of use: http://www.unicode.org/copyright.html # +## Copyright (c) 1999-2014, International Business Machines Corporation and # +## others. All Rights Reserved. # +################################################################################# + +## Source directory information +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ + +top_builddir = ../.. + +include $(top_builddir)/icudefs.mk + +## Build directory information +subdir = tools/genrb + +TARGET_STUB_NAME = genrb +DERB_STUB_NAME = derb + +SECTION = 1 + +MAN_FILES = $(TARGET_STUB_NAME).$(SECTION) +@ICUIO_TRUE@MAN_FILES += $(DERB_STUB_NAME).$(SECTION) + +## Extra files to remove for 'make clean' +CLEANFILES = *~ $(MAN_FILES) $(DEPS) $(DERB_DEPS) + +## Target information +TARGET = $(BINDIR)/$(TARGET_STUB_NAME)$(EXEEXT) +# derb depends on icuio +@ICUIO_TRUE@DERB = $(BINDIR)/$(DERB_STUB_NAME)$(EXEEXT) + +CPPFLAGS += -I$(top_srcdir)/common -I$(top_srcdir)/i18n -I$(srcdir)/../toolutil -I$(top_srcdir)/io +CPPFLAGS += -DUNISTR_FROM_CHAR_EXPLICIT=explicit -DUNISTR_FROM_STRING_EXPLICIT=explicit +LIBS = $(LIBICUTOOLUTIL) $(LIBICUI18N) $(LIBICUUC) $(DEFAULT_LIBS) $(LIB_M) + +OBJECTS = errmsg.o genrb.o parse.o read.o reslist.o ustr.o rbutil.o \ +wrtjava.o rle.o wrtxml.o prscmnts.o +DERB_OBJ = derb.o + +DEPS = $(OBJECTS:.o=.d) +DERB_DEPS = $(DERB_OBJ:.o=.d) + +-include Makefile.local + +## List of phony targets +.PHONY : all all-local install install-local clean clean-local \ +distclean distclean-local dist dist-local check check-local install-man + +## Clear suffix list +.SUFFIXES : + +## List of standard targets +all: all-local +install: install-local +clean: clean-local +distclean : distclean-local +dist: dist-local +check: all check-local + +all-local: $(TARGET) $(DERB) $(MAN_FILES) + +install-local: all-local install-man + $(MKINSTALLDIRS) $(DESTDIR)$(bindir) + $(INSTALL) $(TARGET) $(DESTDIR)$(bindir) +@ICUIO_TRUE@ $(INSTALL) $(DERB) $(DESTDIR)$(bindir) + +install-man: $(MAN_FILES) + $(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man$(SECTION) + $(INSTALL_DATA) $? $(DESTDIR)$(mandir)/man$(SECTION) + +dist-local: + +clean-local: + test -z "$(CLEANFILES)" || $(RMV) $(CLEANFILES) + $(RMV) $(TARGET) $(DERB) $(OBJECTS) $(DERB_OBJ) + +distclean-local: clean-local + $(RMV) Makefile + +check-local: all-local + +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + +$(TARGET) : $(OBJECTS) + $(LINK.cc) $(OUTOPT)$@ $^ $(LIBS) + $(POST_BUILD_STEP) + +$(DERB) : $(DERB_OBJ) + $(LINK.cc) $(OUTOPT)$@ $^ $(LIBICUIO) $(LIBS) + $(POST_BUILD_STEP) + +# This line is needed to serialize builds when the gmake -j option is used. +$(TARGET_STUB_NAME).$(SECTION): $(DERB_STUB_NAME).$(SECTION) + +%.$(SECTION): $(srcdir)/%.$(SECTION).in + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + + +ifeq (,$(MAKECMDGOALS)) +-include $(DEPS) +-include $(DERB_DEPS) +else +ifneq ($(patsubst %clean,,$(MAKECMDGOALS)),) +-include $(DEPS) +endif +endif diff --git a/intl/icu/source/tools/genrb/derb.1.in b/intl/icu/source/tools/genrb/derb.1.in new file mode 100644 index 000000000..725b571ce --- /dev/null +++ b/intl/icu/source/tools/genrb/derb.1.in @@ -0,0 +1,198 @@ +.\" Hey, Emacs! This is -*-nroff-*- you know... +.\" +.\" derb.1: manual page for the derb utility +.\" +.\" Copyright (C) 2016 and later: Unicode, Inc. and others. +.\" License & terms of use: http://www.unicode.org/copyright.html +.\" Copyright (C) 2000-2014 IBM, Inc. and others. +.\" +.TH DERB 1 "7 Mar 2014" "ICU MANPAGE" "ICU @VERSION@ Manual" +.SH NAME +.B derb +\- disassemble a resource bundle +.SH SYNOPSIS +.B derb +[ +.BR "\-h\fP, \fB\-?\fP, \fB\-\-help" +] +[ +.BR "\-V\fP, \fB\-\-version" +] +[ +.BR "\-v\fP, \fB\-\-verbose" +] +[ +.BI "\-e\fP, \fB\-\-encoding" " encoding" +] +[ +.BI "\-\-bom" +] +[ +.BI "\-t\fP, \fB\-\-truncate" " \fR[ \fPsize\fR ]\fP" +] +[ +.BI "\-s\fP, \fB\-\-sourcedir" " source" +] +[ +.BI "\-d\fP, \fB\-\-destdir" " destination" +] +[ +.BI "\-i\fP, \fB\-\-icudatadir" " directory" +] +[ +.BI "\-c\fP, \fB\-\-to\-stdout" +] +.IR bundle " \.\.\." +.SH DESCRIPTION +.B derb +reads the compiled resource +.I bundle +files passed on the command line and write them back in text form. +The resulting text files have a +.B .txt +extension while compiled resource bundle source files typically have a +.B .res +extension. +.PP +It is customary to name the resource bundles by their locale name, +i.e. to use a local identifier for the +.I bundle +filename, e.g. +.B ja_JP.res +for Japanese (Japan) data, or +.B root.res +for the root bundle. +This is especially important for +.B derb +since the locale name is not accessible directly from the compiled +resource bundle, and to know which locale to ask for when opening +the bundle. +.B derb +will produce a file whose base name is the base name of the compiled resource file itself. +If the +.BI "\-\-to\-stdout\fP, \fB\-c\fP" +option is used, however, the text will be written on the standard output. +.SH OPTIONS +.TP +.BR "\-h\fP, \fB\-?\fP, \fB\-\-help" +Print help about usage and exit. +.TP +.BR "\-V\fP, \fB\-\-version" +Print the version of +.B derb +and exit. +.TP +.BR "\-v\fP, \fB\-\-verbose" +Display extra informative messages during execution. +.TP +.BR "\-A\fP, \fB\-\-suppressAliases" +Don't follow aliases when producing output. +.TP +.BI "\-e\fP, \fB\-\-encoding" " encoding" +Set the encoding used to write output files to +.IR encoding . +The default encoding is the invariant (subset of ASCII or EBCDIC) +codepage for the system (see section +.BR "INVARIANT CHARACTERS" ). +The choice of the encoding does not affect the data, just their +representation. Characters that cannot be represented in the +.I encoding +will be represented using +.BI \eu "hhhh" +escape sequences. +.TP +.BI "\-\-bom" +Write a byte order mark (BOM) at the beginning of the file. +.TP +.BI "\-l\fP, \fB\-\-locale" " locale" +Set the +.I locale +for the resource bundle, which is used both in the generated text and +as the base name of the output file. +.TP +.BI "\-t\fP, \fB\-\-truncate" " \fR[ \fPsize\fR ]\fP" +Truncate individual resources (strings or binary data) to +.I size +bytes. The default if +.I size +is not specified is +.B 80 +bytes. +.TP +.BI "\-s\fP, \fB\-\-sourcedir" " source" +Set the source directory to +.IR source . +The default source directory is the current directory. +If +.B - +is passed for +.IR source , +then the +.I bundle +will be looked for in its default location, specified by +the +.B ICU_DATA +environment variable (or defaulting to +the location set when ICU was built if +.B ICU_DATA +is not set). +.TP +.BI "\-d\fP, \fB\-\-destdir" " destination" +Set the destination directory to +.IR destination . +The default destination directory is specified by the environment variable +.BR ICU_DATA +or is the location set when ICU was built if +.B ICU_DATA +is not set. +.TP +.BI "\-i\fP, \fB\-\-icudatadir" " directory" +Look for any necessary ICU data files in +.IR directory . +For example, when processing collation overrides, the file +.B ucadata.dat +must be located. +The default ICU data directory is specified by the environment variable +.BR ICU_DATA . +.TP +.BI "\-c\fP, \fB\-\-to\-stdout" +Write the disassembled +.I bundle +on standard output instead of into a file. +.SH CAVEATS +When the option +.BI \-\-bom +is used, the character +.B U+FEFF +is written in the destination +.I encoding +regardless of whether it is a Unicode transformation format (UTF) or not. +This option should only be used with an UTF encoding, as byte order marks +are not meaningful for other encodings. +.SH INVARIANT CHARACTERS +The +.B invariant character set +consists of the following set of characters, expressed as a standard POSIX +regular expression: +.BR "[a-z]|[A-Z]|[0-9]|_| |+|-|*|/" . +This is the set which is guaranteed to be available regardless of code page. +.SH ENVIRONMENT +.TP 10 +.B ICU_DATA +Specifies the directory containing ICU data. Defaults to +.BR @thepkgicudatadir@/@PACKAGE@/@VERSION@/ . +Some tools in ICU depend on the presence of the trailing slash. It is thus +important to make sure that it is present if +.B ICU_DATA +is set. +.SH AUTHORS +Vladimir Weinstein +.br +Yves Arrouye +.SH VERSION +1.0 +.SH COPYRIGHT +Copyright (C) 2002 IBM, Inc. and others. +.SH SEE ALSO +.BR genrb (1) + diff --git a/intl/icu/source/tools/genrb/derb.cpp b/intl/icu/source/tools/genrb/derb.cpp new file mode 100644 index 000000000..22e275ef3 --- /dev/null +++ b/intl/icu/source/tools/genrb/derb.cpp @@ -0,0 +1,657 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1999-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* file name: derb.cpp +* encoding: US-ASCII +* tab size: 8 (not used) +* indentation:4 +* +* created on: 2000sep6 +* created by: Vladimir Weinstein as an ICU workshop example +* maintained by: Yves Arrouye <yves@realnames.com> +*/ + +#include "unicode/stringpiece.h" +#include "unicode/ucnv.h" +#include "unicode/unistr.h" +#include "unicode/ustring.h" +#include "unicode/putil.h" +#include "unicode/ustdio.h" + +#include "charstr.h" +#include "uresimp.h" +#include "cmemory.h" +#include "cstring.h" +#include "uoptions.h" +#include "toolutil.h" +#include "ustrfmt.h" + +#if !UCONFIG_NO_FORMATTING + +#define DERB_VERSION "1.1" + +#define DERB_DEFAULT_TRUNC 80 + +static const int32_t indentsize = 4; +static int32_t truncsize = DERB_DEFAULT_TRUNC; +static UBool opt_truncate = FALSE; + +static const char *getEncodingName(const char *encoding); +static void reportError(const char *pname, UErrorCode *status, const char *when); +static UChar *quotedString(const UChar *string); +static void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status); +static void printString(UFILE *out, const UChar *str, int32_t len); +static void printCString(UFILE *out, const char *str, int32_t len); +static void printIndent(UFILE *out, int32_t indent); +static void printHex(UFILE *out, uint8_t what); + +static UOption options[]={ + UOPTION_HELP_H, + UOPTION_HELP_QUESTION_MARK, +/* 2 */ UOPTION_ENCODING, +/* 3 */ { "to-stdout", NULL, NULL, NULL, 'c', UOPT_NO_ARG, 0 } , +/* 4 */ { "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 }, +/* 5 */ UOPTION_VERBOSE, +/* 6 */ UOPTION_DESTDIR, +/* 7 */ UOPTION_SOURCEDIR, +/* 8 */ { "bom", NULL, NULL, NULL, 0, UOPT_NO_ARG, 0 }, +/* 9 */ UOPTION_ICUDATADIR, +/* 10 */ UOPTION_VERSION, +/* 11 */ { "suppressAliases", NULL, NULL, NULL, 'A', UOPT_NO_ARG, 0 }, +}; + +static UBool verbose = FALSE; +static UBool suppressAliases = FALSE; +static UFILE *ustderr = NULL; + +extern int +main(int argc, char* argv[]) { + const char *encoding = NULL; + const char *outputDir = NULL; /* NULL = no output directory, use current */ + const char *inputDir = "."; + int tostdout = 0; + int prbom = 0; + + const char *pname; + + UResourceBundle *bundle = NULL; + int32_t i = 0; + + const char* arg; + + /* Get the name of tool. */ + pname = uprv_strrchr(*argv, U_FILE_SEP_CHAR); +#if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR + if (!pname) { + pname = uprv_strrchr(*argv, U_FILE_ALT_SEP_CHAR); + } +#endif + if (!pname) { + pname = *argv; + } else { + ++pname; + } + + /* error handling, printing usage message */ + argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options); + + /* error handling, printing usage message */ + if(argc<0) { + fprintf(stderr, + "%s: error in command line argument \"%s\"\n", pname, + argv[-argc]); + } + if(argc<0 || options[0].doesOccur || options[1].doesOccur) { + fprintf(argc < 0 ? stderr : stdout, + "%csage: %s [ -h, -?, --help ] [ -V, --version ]\n" + " [ -v, --verbose ] [ -e, --encoding encoding ] [ --bom ]\n" + " [ -t, --truncate [ size ] ]\n" + " [ -s, --sourcedir source ] [ -d, --destdir destination ]\n" + " [ -i, --icudatadir directory ] [ -c, --to-stdout ]\n" + " [ -A, --suppressAliases]\n" + " bundle ...\n", argc < 0 ? 'u' : 'U', + pname); + return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; + } + + if(options[10].doesOccur) { + fprintf(stderr, + "%s version %s (ICU version %s).\n" + "%s\n", + pname, DERB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); + return U_ZERO_ERROR; + } + if(options[2].doesOccur) { + encoding = options[2].value; + } + + if (options[3].doesOccur) { + if(options[2].doesOccur) { + fprintf(stderr, "%s: Error: don't specify an encoding (-e) when writing to stdout (-c).\n", pname); + return 3; + } + tostdout = 1; + } + + if(options[4].doesOccur) { + opt_truncate = TRUE; + if(options[4].value != NULL) { + truncsize = atoi(options[4].value); /* user defined printable size */ + } else { + truncsize = DERB_DEFAULT_TRUNC; /* we'll use default omitting size */ + } + } else { + opt_truncate = FALSE; + } + + if(options[5].doesOccur) { + verbose = TRUE; + } + + if (options[6].doesOccur) { + outputDir = options[6].value; + } + + if(options[7].doesOccur) { + inputDir = options[7].value; /* we'll use users resources */ + } + + if (options[8].doesOccur) { + prbom = 1; + } + + if (options[9].doesOccur) { + u_setDataDirectory(options[9].value); + } + + if (options[11].doesOccur) { + suppressAliases = TRUE; + } + + fflush(stderr); // use ustderr now. + ustderr = u_finit(stderr, NULL, NULL); + + for (i = 1; i < argc; ++i) { + static const UChar sp[] = { 0x0020 }; /* " " */ + + arg = getLongPathname(argv[i]); + + if (verbose) { + u_fprintf(ustderr, "processing bundle \"%s\"\n", argv[i]); + } + + icu::CharString locale; + UErrorCode status = U_ZERO_ERROR; + { + const char *p = findBasename(arg); + const char *q = uprv_strrchr(p, '.'); + if (q == NULL) { + locale.append(p, status); + } else { + locale.append(p, (int32_t)(q - p), status); + } + } + if (U_FAILURE(status)) { + return status; + } + + icu::CharString infile; + const char *thename = NULL; + UBool fromICUData = !uprv_strcmp(inputDir, "-"); + if (!fromICUData) { + UBool absfilename = *arg == U_FILE_SEP_CHAR; +#if U_PLATFORM_HAS_WIN32_API + if (!absfilename) { + absfilename = (uprv_strlen(arg) > 2 && isalpha(arg[0]) + && arg[1] == ':' && arg[2] == U_FILE_SEP_CHAR); + } +#endif + if (absfilename) { + thename = arg; + } else { + const char *q = uprv_strrchr(arg, U_FILE_SEP_CHAR); +#if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR + if (q == NULL) { + q = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); + } +#endif + infile.append(inputDir, status); + if(q != NULL) { + infile.appendPathPart(icu::StringPiece(arg, (int32_t)(q - arg)), status); + } + if (U_FAILURE(status)) { + return status; + } + thename = infile.data(); + } + } + if (thename) { + bundle = ures_openDirect(thename, locale.data(), &status); + } else { + bundle = ures_open(fromICUData ? 0 : inputDir, locale.data(), &status); + } + if (U_SUCCESS(status)) { + UFILE *out = NULL; + + const char *filename = 0; + const char *ext = 0; + + if (locale.isEmpty() || !tostdout) { + filename = findBasename(arg); + ext = uprv_strrchr(filename, '.'); + if (!ext) { + ext = uprv_strchr(filename, 0); + } + } + + if (tostdout) { + out = u_get_stdout(); + } else { + icu::CharString thefile; + if (outputDir) { + thefile.append(outputDir, status); + } + thefile.appendPathPart(filename, status); + if (*ext) { + thefile.truncate(thefile.length() - (int32_t)uprv_strlen(ext)); + } + thefile.append(".txt", status); + if (U_FAILURE(status)) { + return status; + } + + out = u_fopen(thefile.data(), "w", NULL, encoding); + if (!out) { + u_fprintf(ustderr, "%s: couldn't create %s\n", pname, thefile.data()); + u_fclose(ustderr); + return 4; + } + } + + // now, set the callback. + ucnv_setFromUCallBack(u_fgetConverter(out), UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, 0, 0, &status); + if (U_FAILURE(status)) { + u_fprintf(ustderr, "%s: couldn't configure converter for encoding\n", pname); + u_fclose(ustderr); + if(!tostdout) { + u_fclose(out); + } + return 3; + } + + if (prbom) { /* XXX: Should be done only for UTFs */ + u_fputc(0xFEFF, out); + } + u_fprintf(out, "// -*- Coding: %s; -*-\n//\n", encoding ? encoding : getEncodingName(ucnv_getDefaultName())); + u_fprintf(out, "// This file was dumped by derb(8) from "); + if (thename) { + u_fprintf(out, "%s", thename); + } else if (fromICUData) { + u_fprintf(out, "the ICU internal %s locale", locale.data()); + } + + u_fprintf(out, "\n// derb(8) by Vladimir Weinstein and Yves Arrouye\n\n"); + + if (!locale.isEmpty()) { + u_fprintf(out, "%s", locale.data()); + } else { + u_fprintf(out, "%.*s%.*S", (int32_t)(ext - filename), filename, UPRV_LENGTHOF(sp), sp); + } + printOutBundle(out, bundle, 0, pname, &status); + + if (!tostdout) { + u_fclose(out); + } + } + else { + reportError(pname, &status, "opening resource file"); + } + + ures_close(bundle); + } + + return 0; +} + +static UChar *quotedString(const UChar *string) { + int len = u_strlen(string); + int alen = len; + const UChar *sp; + UChar *newstr, *np; + + for (sp = string; *sp; ++sp) { + switch (*sp) { + case '\n': + case 0x0022: + ++alen; + break; + } + } + + newstr = (UChar *) uprv_malloc((1 + alen) * U_SIZEOF_UCHAR); + for (sp = string, np = newstr; *sp; ++sp) { + switch (*sp) { + case '\n': + *np++ = 0x005C; + *np++ = 0x006E; + break; + + case 0x0022: + *np++ = 0x005C; + U_FALLTHROUGH; + default: + *np++ = *sp; + break; + } + } + *np = 0; + + return newstr; +} + + +static void printString(UFILE *out, const UChar *str, int32_t len) { + u_file_write(str, len, out); +} + +static void printCString(UFILE *out, const char *str, int32_t len) { + if(len==-1) { + u_fprintf(out, "%s", str); + } else { + u_fprintf(out, "%.*s", len, str); + } +} + +static void printIndent(UFILE *out, int32_t indent) { + icu::UnicodeString inchar(indent, 0x20, indent); + printString(out, inchar.getBuffer(), indent); +} + +static void printHex(UFILE *out, uint8_t what) { + static const char map[] = "0123456789ABCDEF"; + UChar hex[2]; + + hex[0] = map[what >> 4]; + hex[1] = map[what & 0xf]; + + printString(out, hex, 2); +} + +static void printOutAlias(UFILE *out, UResourceBundle *parent, Resource r, const char *key, int32_t indent, const char *pname, UErrorCode *status) { + static const UChar cr[] = { 0xA }; // LF + int32_t len = 0; + const UChar* thestr = res_getAlias(&(parent->fResData), r, &len); + UChar *string = quotedString(thestr); + if(opt_truncate && len > truncsize) { + char msg[128]; + printIndent(out, indent); + sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", + (long)len, (long)truncsize/2); + printCString(out, msg, -1); + len = truncsize; + } + if(U_SUCCESS(*status)) { + static const UChar openStr[] = { 0x003A, 0x0061, 0x006C, 0x0069, 0x0061, 0x0073, 0x0020, 0x007B, 0x0020, 0x0022 }; /* ":alias { \"" */ + static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D, 0x0020 }; /* "\" } " */ + printIndent(out, indent); + if(key != NULL) { + printCString(out, key, -1); + } + printString(out, openStr, UPRV_LENGTHOF(openStr)); + printString(out, string, len); + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + if(verbose) { + printCString(out, " // ALIAS", -1); + } + printString(out, cr, UPRV_LENGTHOF(cr)); + } else { + reportError(pname, status, "getting binary value"); + } + uprv_free(string); +} + +static void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status) +{ + static const UChar cr[] = { 0xA }; // LF + +/* int32_t noOfElements = ures_getSize(resource);*/ + int32_t i = 0; + const char *key = ures_getKey(resource); + + switch(ures_getType(resource)) { + case URES_STRING : + { + int32_t len=0; + const UChar* thestr = ures_getString(resource, &len, status); + UChar *string = quotedString(thestr); + + /* TODO: String truncation */ + if(opt_truncate && len > truncsize) { + char msg[128]; + printIndent(out, indent); + sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", + (long)len, (long)(truncsize/2)); + printCString(out, msg, -1); + len = truncsize/2; + } + printIndent(out, indent); + if(key != NULL) { + static const UChar openStr[] = { 0x0020, 0x007B, 0x0020, 0x0022 }; /* " { \"" */ + static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D }; /* "\" }" */ + printCString(out, key, (int32_t)uprv_strlen(key)); + printString(out, openStr, UPRV_LENGTHOF(openStr)); + printString(out, string, len); + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + } else { + static const UChar openStr[] = { 0x0022 }; /* "\"" */ + static const UChar closeStr[] = { 0x0022, 0x002C }; /* "\"," */ + + printString(out, openStr, UPRV_LENGTHOF(openStr)); + printString(out, string, (int32_t)(u_strlen(string))); + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + } + + if(verbose) { + printCString(out, "// STRING", -1); + } + printString(out, cr, UPRV_LENGTHOF(cr)); + + uprv_free(string); + } + break; + + case URES_INT : + { + static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0020, 0x007B, 0x0020 }; /* ":int { " */ + static const UChar closeStr[] = { 0x0020, 0x007D }; /* " }" */ + UChar num[20]; + + printIndent(out, indent); + if(key != NULL) { + printCString(out, key, -1); + } + printString(out, openStr, UPRV_LENGTHOF(openStr)); + uprv_itou(num, 20, ures_getInt(resource, status), 10, 0); + printString(out, num, u_strlen(num)); + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + + if(verbose) { + printCString(out, "// INT", -1); + } + printString(out, cr, UPRV_LENGTHOF(cr)); + break; + } + case URES_BINARY : + { + int32_t len = 0; + const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status); + if(opt_truncate && len > truncsize) { + char msg[128]; + printIndent(out, indent); + sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", + (long)len, (long)(truncsize/2)); + printCString(out, msg, -1); + len = truncsize; + } + if(U_SUCCESS(*status)) { + static const UChar openStr[] = { 0x003A, 0x0062, 0x0069, 0x006E, 0x0061, 0x0072, 0x0079, 0x0020, 0x007B, 0x0020 }; /* ":binary { " */ + static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ + printIndent(out, indent); + if(key != NULL) { + printCString(out, key, -1); + } + printString(out, openStr, UPRV_LENGTHOF(openStr)); + for(i = 0; i<len; i++) { + printHex(out, *data++); + } + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + if(verbose) { + printCString(out, " // BINARY", -1); + } + printString(out, cr, UPRV_LENGTHOF(cr)); + } else { + reportError(pname, status, "getting binary value"); + } + } + break; + case URES_INT_VECTOR : + { + int32_t len = 0; + const int32_t *data = ures_getIntVector(resource, &len, status); + if(U_SUCCESS(*status)) { + static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0076, 0x0065, 0x0063, 0x0074, 0x006F, 0x0072, 0x0020, 0x007B, 0x0020 }; /* ":intvector { " */ + static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ + UChar num[20]; + + printIndent(out, indent); + if(key != NULL) { + printCString(out, key, -1); + } + printString(out, openStr, UPRV_LENGTHOF(openStr)); + for(i = 0; i < len - 1; i++) { + int32_t numLen = uprv_itou(num, 20, data[i], 10, 0); + num[numLen++] = 0x002C; /* ',' */ + num[numLen++] = 0x0020; /* ' ' */ + num[numLen] = 0; + printString(out, num, u_strlen(num)); + } + if(len > 0) { + uprv_itou(num, 20, data[len - 1], 10, 0); + printString(out, num, u_strlen(num)); + } + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + if(verbose) { + printCString(out, "// INTVECTOR", -1); + } + printString(out, cr, UPRV_LENGTHOF(cr)); + } else { + reportError(pname, status, "getting int vector"); + } + } + break; + case URES_TABLE : + case URES_ARRAY : + { + static const UChar openStr[] = { 0x007B }; /* "{" */ + static const UChar closeStr[] = { 0x007D, '\n' }; /* "}\n" */ + + UResourceBundle *t = NULL; + ures_resetIterator(resource); + printIndent(out, indent); + if(key != NULL) { + printCString(out, key, -1); + } + printString(out, openStr, UPRV_LENGTHOF(openStr)); + if(verbose) { + if(ures_getType(resource) == URES_TABLE) { + printCString(out, "// TABLE", -1); + } else { + printCString(out, "// ARRAY", -1); + } + } + printString(out, cr, UPRV_LENGTHOF(cr)); + + if(suppressAliases == FALSE) { + while(U_SUCCESS(*status) && ures_hasNext(resource)) { + t = ures_getNextResource(resource, t, status); + if(U_SUCCESS(*status)) { + printOutBundle(out, t, indent+indentsize, pname, status); + } else { + reportError(pname, status, "While processing table"); + *status = U_ZERO_ERROR; + } + } + } else { /* we have to use low level access to do this */ + Resource r; + int32_t resSize = ures_getSize(resource); + UBool isTable = (UBool)(ures_getType(resource) == URES_TABLE); + for(i = 0; i < resSize; i++) { + /* need to know if it's an alias */ + if(isTable) { + r = res_getTableItemByIndex(&resource->fResData, resource->fRes, i, &key); + } else { + r = res_getArrayItem(&resource->fResData, resource->fRes, i); + } + if(U_SUCCESS(*status)) { + if(res_getPublicType(r) == URES_ALIAS) { + printOutAlias(out, resource, r, key, indent+indentsize, pname, status); + } else { + t = ures_getByIndex(resource, i, t, status); + printOutBundle(out, t, indent+indentsize, pname, status); + } + } else { + reportError(pname, status, "While processing table"); + *status = U_ZERO_ERROR; + } + } + } + + printIndent(out, indent); + printString(out, closeStr, UPRV_LENGTHOF(closeStr)); + ures_close(t); + } + break; + default: + break; + } + +} + +static const char *getEncodingName(const char *encoding) { + UErrorCode err; + const char *enc; + + err = U_ZERO_ERROR; + if (!(enc = ucnv_getStandardName(encoding, "MIME", &err))) { + err = U_ZERO_ERROR; + if (!(enc = ucnv_getStandardName(encoding, "IANA", &err))) { + ; + } + } + + return enc; +} + +static void reportError(const char *pname, UErrorCode *status, const char *when) { + u_fprintf(ustderr, "%s: error %d while %s: %s\n", pname, *status, when, u_errorName(*status)); +} + +#else +extern int +main(int argc, char* argv[]) { + /* Changing stdio.h ustdio.h requires that formatting not be disabled. */ + return 3; +} +#endif /* !UCONFIG_NO_FORMATTING */ + +/* + * Local Variables: + * indent-tabs-mode: nil + * End: + */ diff --git a/intl/icu/source/tools/genrb/derb.vcxproj b/intl/icu/source/tools/genrb/derb.vcxproj new file mode 100644 index 000000000..2777eec91 --- /dev/null +++ b/intl/icu/source/tools/genrb/derb.vcxproj @@ -0,0 +1,285 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{D3065ADB-8820-4CC7-9B6C-9510833961A3}</ProjectGuid>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseOfMfc>false</UseOfMfc>
+ <CharacterSet>MultiByte</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseOfMfc>false</UseOfMfc>
+ <CharacterSet>MultiByte</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseOfMfc>false</UseOfMfc>
+ <CharacterSet>MultiByte</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseOfMfc>false</UseOfMfc>
+ <CharacterSet>MultiByte</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />
+ </ImportGroup>
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup>
+ <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\x86\Release_derb\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\x86\Release_derb\</IntDir>
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\x86\Debug_derb\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\x86\Debug_derb\</IntDir>
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release_derb\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release_derb\</IntDir>
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug_derb\</OutDir>
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug_derb\</IntDir>
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <CustomBuildStep>
+ <Command>copy "$(TargetPath)" ..\..\..\bin +</Command>
+ <Outputs>..\..\..\bin\$(TargetFileName);%(Outputs)</Outputs>
+ </CustomBuildStep>
+ <Midl>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <MkTypLibCompatible>true</MkTypLibCompatible>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <TargetEnvironment>Win32</TargetEnvironment>
+ <TypeLibraryName>.\x86\Release_derb/derb.tlb</TypeLibraryName>
+ </Midl>
+ <ClCompile>
+ <AdditionalIncludeDirectories>..\..\i18n;..\..\common;..\toolutil;..\..\io;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <StringPooling>true</StringPooling>
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <DisableLanguageExtensions>true</DisableLanguageExtensions>
+ <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
+ <PrecompiledHeaderOutputFile>.\x86\Release_derb/derb.pch</PrecompiledHeaderOutputFile>
+ <AssemblerListingLocation>.\x86\Release_derb/</AssemblerListingLocation>
+ <ObjectFileName>.\x86\Release_derb/</ObjectFileName>
+ <ProgramDataBaseFileName>.\x86\Release_derb/</ProgramDataBaseFileName>
+ <WarningLevel>Level3</WarningLevel>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <CompileAs>Default</CompileAs>
+ </ClCompile>
+ <ResourceCompile>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <Culture>0x0409</Culture>
+ </ResourceCompile>
+ <Link>
+ <OutputFile>.\x86\Release_derb/derb.exe</OutputFile>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <ProgramDatabaseFile>.\x86\Release_derb/derb.pdb</ProgramDatabaseFile>
+ <SubSystem>Console</SubSystem>
+ <RandomizedBaseAddress>false</RandomizedBaseAddress>
+ <DataExecutionPrevention>
+ </DataExecutionPrevention>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <CustomBuildStep>
+ <Command>copy "$(TargetPath)" ..\..\..\bin +</Command>
+ <Outputs>..\..\..\bin\$(TargetFileName);%(Outputs)</Outputs>
+ </CustomBuildStep>
+ <Midl>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <MkTypLibCompatible>true</MkTypLibCompatible>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <TargetEnvironment>Win32</TargetEnvironment>
+ <TypeLibraryName>.\x86\Debug_derb/derb.tlb</TypeLibraryName>
+ </Midl>
+ <ClCompile>
+ <Optimization>Disabled</Optimization>
+ <AdditionalIncludeDirectories>..\..\i18n;..\..\common;..\toolutil;..\..\io;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
+ <BufferSecurityCheck>true</BufferSecurityCheck>
+ <DisableLanguageExtensions>true</DisableLanguageExtensions>
+ <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
+ <PrecompiledHeaderOutputFile>.\x86\Debug_derb/derb.pch</PrecompiledHeaderOutputFile>
+ <AssemblerListingLocation>.\x86\Debug_derb/</AssemblerListingLocation>
+ <ObjectFileName>.\x86\Debug_derb/</ObjectFileName>
+ <ProgramDataBaseFileName>.\x86\Debug_derb/</ProgramDataBaseFileName>
+ <WarningLevel>Level3</WarningLevel>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
+ <CompileAs>Default</CompileAs>
+ </ClCompile>
+ <ResourceCompile>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <Culture>0x0409</Culture>
+ </ResourceCompile>
+ <Link>
+ <OutputFile>.\x86\Debug_derb/derb.exe</OutputFile>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <ProgramDatabaseFile>.\x86\Debug_derb/derb.pdb</ProgramDatabaseFile>
+ <SubSystem>Console</SubSystem>
+ <RandomizedBaseAddress>false</RandomizedBaseAddress>
+ <DataExecutionPrevention>
+ </DataExecutionPrevention>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <CustomBuildStep>
+ <Command>copy "$(TargetPath)" ..\..\..\bin64 +</Command>
+ <Outputs>..\..\..\bin64\$(TargetFileName);%(Outputs)</Outputs>
+ </CustomBuildStep>
+ <Midl>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <MkTypLibCompatible>true</MkTypLibCompatible>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <TargetEnvironment>X64</TargetEnvironment>
+ <TypeLibraryName>.\x64\Release_derb/derb.tlb</TypeLibraryName>
+ </Midl>
+ <ClCompile>
+ <AdditionalIncludeDirectories>..\..\i18n;..\..\common;..\toolutil;..\..\io;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <PreprocessorDefinitions>WIN64;WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <StringPooling>true</StringPooling>
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <DisableLanguageExtensions>true</DisableLanguageExtensions>
+ <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
+ <PrecompiledHeaderOutputFile>.\x64\Release_derb/derb.pch</PrecompiledHeaderOutputFile>
+ <AssemblerListingLocation>.\x64\Release_derb/</AssemblerListingLocation>
+ <ObjectFileName>.\x64\Release_derb/</ObjectFileName>
+ <ProgramDataBaseFileName>.\x64\Release_derb/</ProgramDataBaseFileName>
+ <WarningLevel>Level3</WarningLevel>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <CompileAs>Default</CompileAs>
+ </ClCompile>
+ <ResourceCompile>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <Culture>0x0409</Culture>
+ </ResourceCompile>
+ <Link>
+ <OutputFile>.\x64\Release_derb/derb.exe</OutputFile>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <ProgramDatabaseFile>.\x64\Release_derb/derb.pdb</ProgramDatabaseFile>
+ <SubSystem>Console</SubSystem>
+ <TargetMachine>MachineX64</TargetMachine>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <CustomBuildStep>
+ <Command>copy "$(TargetPath)" ..\..\..\bin64 +</Command>
+ <Outputs>..\..\..\bin64\$(TargetFileName);%(Outputs)</Outputs>
+ </CustomBuildStep>
+ <Midl>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <MkTypLibCompatible>true</MkTypLibCompatible>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <TargetEnvironment>X64</TargetEnvironment>
+ <TypeLibraryName>.\x64\Debug_derb/derb.tlb</TypeLibraryName>
+ </Midl>
+ <ClCompile>
+ <Optimization>Disabled</Optimization>
+ <AdditionalIncludeDirectories>..\..\i18n;..\..\common;..\toolutil;..\..\io;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <PreprocessorDefinitions>WIN64;WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
+ <BufferSecurityCheck>true</BufferSecurityCheck>
+ <DisableLanguageExtensions>true</DisableLanguageExtensions>
+ <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
+ <PrecompiledHeaderOutputFile>.\x64\Debug_derb/derb.pch</PrecompiledHeaderOutputFile>
+ <AssemblerListingLocation>.\x64\Debug_derb/</AssemblerListingLocation>
+ <ObjectFileName>.\x64\Debug_derb/</ObjectFileName>
+ <ProgramDataBaseFileName>.\x64\Debug_derb/</ProgramDataBaseFileName>
+ <WarningLevel>Level3</WarningLevel>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
+ <CompileAs>Default</CompileAs>
+ </ClCompile>
+ <ResourceCompile>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <Culture>0x0409</Culture>
+ </ResourceCompile>
+ <Link>
+ <OutputFile>.\x64\Debug_derb/derb.exe</OutputFile>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <ProgramDatabaseFile>.\x64\Debug_derb/derb.pdb</ProgramDatabaseFile>
+ <SubSystem>Console</SubSystem>
+ <TargetMachine>MachineX64</TargetMachine>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="derb.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\common\common.vcxproj">
+ <Project>{73c0a65b-d1f2-4de1-b3a6-15dad2c23f3d}</Project>
+ <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
+ </ProjectReference>
+ <ProjectReference Include="..\..\i18n\i18n.vcxproj">
+ <Project>{0178b127-6269-407d-b112-93877bb62776}</Project>
+ </ProjectReference>
+ <ProjectReference Include="..\..\io\io.vcxproj">
+ <Project>{c2b04507-2521-4801-bf0d-5fd79d6d518c}</Project>
+ <Private>true</Private>
+ <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
+ <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
+ <LinkLibraryDependencies>true</LinkLibraryDependencies>
+ <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
+ </ProjectReference>
+ <ProjectReference Include="..\toolutil\toolutil.vcxproj">
+ <Project>{6b231032-3cb5-4eed-9210-810d666a23a0}</Project>
+ <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
\ No newline at end of file diff --git a/intl/icu/source/tools/genrb/derb.vcxproj.filters b/intl/icu/source/tools/genrb/derb.vcxproj.filters new file mode 100644 index 000000000..c62d61288 --- /dev/null +++ b/intl/icu/source/tools/genrb/derb.vcxproj.filters @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{b10d3c34-0b4c-43e9-9c28-e17fdabee575}</UniqueIdentifier> + <Extensions>cpp;c;cxx;rc;def;r;odl;idl;hpj;bat</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{0f0a70a2-7e7e-4e7a-88ab-b3bf739fabed}</UniqueIdentifier> + <Extensions>h;hpp;hxx;hm;inl</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{ac6d5215-57af-486d-81ed-badc17745780}</UniqueIdentifier> + <Extensions>ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="derb.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/intl/icu/source/tools/genrb/errmsg.c b/intl/icu/source/tools/genrb/errmsg.c new file mode 100644 index 000000000..7340f01af --- /dev/null +++ b/intl/icu/source/tools/genrb/errmsg.c @@ -0,0 +1,73 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2011, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File error.c +* +* Modification History: +* +* Date Name Description +* 05/28/99 stephen Creation. +******************************************************************************* +*/ + +#include <stdarg.h> +#include <stdio.h> +#include "cstring.h" +#include "errmsg.h" + +U_CFUNC void error(uint32_t linenumber, const char *msg, ...) +{ + va_list va; + + va_start(va, msg); + fprintf(stderr, "%s:%u: ", gCurrentFileName, (int)linenumber); + vfprintf(stderr, msg, va); + fprintf(stderr, "\n"); + va_end(va); +} + +static UBool gShowWarning = TRUE; + +U_CFUNC void setShowWarning(UBool val) +{ + gShowWarning = val; +} + +U_CFUNC UBool getShowWarning(){ + return gShowWarning; +} + +static UBool gStrict =FALSE; +U_CFUNC UBool isStrict(){ + return gStrict; +} +U_CFUNC void setStrict(UBool val){ + gStrict = val; +} +static UBool gVerbose =FALSE; +U_CFUNC UBool isVerbose(){ + return gVerbose; +} +U_CFUNC void setVerbose(UBool val){ + gVerbose = val; +} +U_CFUNC void warning(uint32_t linenumber, const char *msg, ...) +{ + if (gShowWarning) + { + va_list va; + + va_start(va, msg); + fprintf(stderr, "%s:%u: warning: ", gCurrentFileName, (int)linenumber); + vfprintf(stderr, msg, va); + fprintf(stderr, "\n"); + va_end(va); + } +} diff --git a/intl/icu/source/tools/genrb/errmsg.h b/intl/icu/source/tools/genrb/errmsg.h new file mode 100644 index 000000000..5026ecf7b --- /dev/null +++ b/intl/icu/source/tools/genrb/errmsg.h @@ -0,0 +1,46 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File error.h +* +* Modification History: +* +* Date Name Description +* 05/28/99 stephen Creation. +******************************************************************************* +*/ + +#ifndef ERROR_H +#define ERROR_H 1 + +#include "unicode/utypes.h" + +U_CDECL_BEGIN + +extern const char *gCurrentFileName; + +U_CFUNC void error(uint32_t linenumber, const char *msg, ...); +U_CFUNC void warning(uint32_t linenumber, const char *msg, ...); + +/* Show warnings? */ +U_CFUNC void setShowWarning(UBool val); +U_CFUNC UBool getShowWarning(void); + +/* strict */ +U_CFUNC void setStrict(UBool val); +U_CFUNC UBool isStrict(void); + +/* verbosity */ +U_CFUNC void setVerbose(UBool val); +U_CFUNC UBool isVerbose(void); + +U_CDECL_END + +#endif diff --git a/intl/icu/source/tools/genrb/genrb.1.in b/intl/icu/source/tools/genrb/genrb.1.in new file mode 100644 index 000000000..a45771923 --- /dev/null +++ b/intl/icu/source/tools/genrb/genrb.1.in @@ -0,0 +1,148 @@ +.\" Hey, Emacs! This is -*-nroff-*- you know... +.\" +.\" genrb.1: manual page for the genrb utility +.\" +.\" Copyright (C) 2016 and later: Unicode, Inc. and others. +.\" License & terms of use: http://www.unicode.org/copyright.html +.\" Copyright (C) 2000-2002 IBM, Inc. and others. +.\" +.\" Manual page by Yves Arrouye <yves@realnames.com>. +.\" +.TH GENRB 1 "16 April 2002" "ICU MANPAGE" "ICU @VERSION@ Manual" +.SH NAME +.B genrb +\- compile a resource bundle +.SH SYNOPSIS +.B genrb +[ +.BR "\-h\fP, \fB\-?\fP, \fB\-\-help" +] +[ +.BR "\-V\fP, \fB\-\-version" +] +[ +.BR "\-v\fP, \fB\-\-verbose" +] +[ +.BI "\-e\fP, \fB\-\-encoding" " encoding" +] +[ +.BI "\-j\fP, \fB\-\-write\-java" " \fR[ \fPencoding\fR ]\fP" +] +[ +.BI "\-s\fP, \fB\-\-sourcedir" " source" +] +[ +.BI "\-d\fP, \fB\-\-destdir" " destination" +] +[ +.BI "\-i\fP, \fB\-\-icudatadir" " directory" +] +.IR bundle " \.\.\." +.SH DESCRIPTION +.B genrb +converts the resource +.I bundle +source files passed on the command line to their binary form or to +a Java source file for use with ICU4J. +The resulting binary files have a +.B .res +extension while resource bundle source files typically have a +.B .txt +extension. Java source files have a +.B java +extension and follow the ICU4J naming conventions. +.PP +It is customary to name the resource bundles by their locale name, +i.e. to use a local identifier for the +.I bundle +filename, e.g. +.B ja_JP.txt +for Japanese (Japan) data, or +.B root.txt +for the root bundle. +In any case, +.B genrb +will produce a file whose base name is the name of the locale found +in the resource file, not the base name of the resource file itself. +.PP +The binary files can be read directly by ICU, or used by +.BR pkgdata (1) +for incorporation into a larger archive or library. +.SH OPTIONS +.TP +.BR "\-h\fP, \fB\-?\fP, \fB\-\-help" +Print help about usage and exit. +.TP +.BR "\-V\fP, \fB\-\-version" +Print the version of +.B genrb +and exit. +.TP +.BR "\-v\fP, \fB\-\-verbose" +Display extra informative messages during execution. +.TP +.BI "\-e\fP, \fB\-\-encoding" " encoding" +Set the encoding used to read input files to +.IR encoding . +The default encoding is the invariant (subset of ASCII or EBCDIC) +codepage for the system (see section +.BR "INVARIANT CHARACTERS" ). +The encodings UTF-8, UTF-16BE, and UTF-16LE are automatically detected +if a byte order mark (BOM) is present. +.TP +.BI "\-j\fP, \fB\-\-write\-java" " \fR[ \fPencoding\fR ]\fP" +Generate a Java source code for use with ICU4J. An optional +.I encoding +for the Java file can be given. +.TP +.BI "\-s\fP, \fB\-\-sourcedir" " source" +Set the source directory to +.IR source . +The default source directory is specified by the environment variable +.BR ICU_DATA , +or the location set when ICU was built if +.B ICU_DATA +is not set. +.TP +.BI "\-d\fP, \fB\-\-destdir" " destination" +Set the destination directory to +.IR destination . +The default destination directory is specified by the environment variable +.BR ICU_DATA +or is the location set when ICU was built if +.B ICU_DATA +is not set. +.TP +.BI "\-i\fP, \fB\-\-icudatadir" " directory" +Look for any necessary ICU data files in +.IR directory . +For example, when processing collation overrides, the file +.B ucadata.dat +must be located. +The default ICU data directory is specified by the environment variable +.BR ICU_DATA . +.SH INVARIANT CHARACTERS +The +.B invariant character set +consists of the following set of characters, expressed as a standard POSIX +regular expression: +.BR "[a-z]|[A-Z]|[0-9]|_| |+|-|*|/" . +This is the set which is guaranteed to be available regardless of code page. +.SH ENVIRONMENT +.TP 10 +.B ICU_DATA +Specifies the directory containing ICU data. Defaults to +.BR @thepkgicudatadir@/@PACKAGE@/@VERSION@/ . +Some tools in ICU depend on the presence of the trailing slash. It is thus +important to make sure that it is present if +.B ICU_DATA +is set. +.SH VERSION +@VERSION@ +.SH COPYRIGHT +Copyright (C) 2000-2002 IBM, Inc. and others. +.SH SEE ALSO +.BR derb (1) +.br +.BR pkgdata (1) diff --git a/intl/icu/source/tools/genrb/genrb.cpp b/intl/icu/source/tools/genrb/genrb.cpp new file mode 100644 index 000000000..685fb5884 --- /dev/null +++ b/intl/icu/source/tools/genrb/genrb.cpp @@ -0,0 +1,837 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File genrb.cpp +* +* Modification History: +* +* Date Name Description +* 05/25/99 stephen Creation. +* 5/10/01 Ram removed ustdio dependency +******************************************************************************* +*/ + +#include <assert.h> +#include "genrb.h" +#include "unicode/localpointer.h" +#include "unicode/uclean.h" +#include "unicode/utf16.h" +#include "charstr.h" +#include "cmemory.h" +#include "reslist.h" +#include "ucmndata.h" /* TODO: for reading the pool bundle */ + +U_NAMESPACE_USE + +/* Protos */ +void processFile(const char *filename, const char* cp, const char *inputDir, const char *outputDir, + const char *packageName, + SRBRoot *newPoolBundle, UBool omitBinaryCollation, UErrorCode &status); +static char *make_res_filename(const char *filename, const char *outputDir, + const char *packageName, UErrorCode &status); + +/* File suffixes */ +#define RES_SUFFIX ".res" +#define COL_SUFFIX ".col" + +const char *gCurrentFileName = NULL; +#ifdef XP_MAC_CONSOLE +#include <console.h> +#endif + +void ResFile::close() { + delete[] fBytes; + fBytes = NULL; + delete fStrings; + fStrings = NULL; +} + +enum +{ + HELP1, + HELP2, + VERBOSE, + QUIET, + VERSION, + SOURCEDIR, + DESTDIR, + ENCODING, + ICUDATADIR, + WRITE_JAVA, + COPYRIGHT, + JAVA_PACKAGE, + BUNDLE_NAME, + WRITE_XLIFF, + STRICT, + NO_BINARY_COLLATION, + LANGUAGE, + NO_COLLATION_RULES, + FORMAT_VERSION, + WRITE_POOL_BUNDLE, + USE_POOL_BUNDLE, + INCLUDE_UNIHAN_COLL +}; + +UOption options[]={ + UOPTION_HELP_H, + UOPTION_HELP_QUESTION_MARK, + UOPTION_VERBOSE, + UOPTION_QUIET, + UOPTION_VERSION, + UOPTION_SOURCEDIR, + UOPTION_DESTDIR, + UOPTION_ENCODING, + UOPTION_ICUDATADIR, + UOPTION_WRITE_JAVA, + UOPTION_COPYRIGHT, + UOPTION_DEF("java-package", '\x01', UOPT_REQUIRES_ARG), + UOPTION_BUNDLE_NAME, + UOPTION_DEF("write-xliff", 'x', UOPT_OPTIONAL_ARG), + UOPTION_DEF("strict", 'k', UOPT_NO_ARG), /* 14 */ + UOPTION_DEF("noBinaryCollation", 'C', UOPT_NO_ARG),/* 15 */ + UOPTION_DEF("language", 'l', UOPT_REQUIRES_ARG), /* 16 */ + UOPTION_DEF("omitCollationRules", 'R', UOPT_NO_ARG),/* 17 */ + UOPTION_DEF("formatVersion", '\x01', UOPT_REQUIRES_ARG),/* 18 */ + UOPTION_DEF("writePoolBundle", '\x01', UOPT_NO_ARG),/* 19 */ + UOPTION_DEF("usePoolBundle", '\x01', UOPT_OPTIONAL_ARG),/* 20 */ + UOPTION_DEF("includeUnihanColl", '\x01', UOPT_NO_ARG),/* 21 */ /* temporary, don't display in usage info */ + }; + +static UBool write_java = FALSE; +static UBool write_xliff = FALSE; +static const char* outputEnc =""; + +static ResFile poolBundle; + +/*added by Jing*/ +static const char* language = NULL; +static const char* xliffOutputFileName = NULL; +int +main(int argc, + char* argv[]) +{ + UErrorCode status = U_ZERO_ERROR; + const char *arg = NULL; + const char *outputDir = NULL; /* NULL = no output directory, use current */ + const char *inputDir = NULL; + const char *encoding = ""; + int i; + UBool illegalArg = FALSE; + + U_MAIN_INIT_ARGS(argc, argv); + + options[JAVA_PACKAGE].value = "com.ibm.icu.impl.data"; + options[BUNDLE_NAME].value = "LocaleElements"; + argc = u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options); + + /* error handling, printing usage message */ + if(argc<0) { + fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]); + illegalArg = TRUE; + } else if(argc<2) { + illegalArg = TRUE; + } + if(options[WRITE_POOL_BUNDLE].doesOccur && options[USE_POOL_BUNDLE].doesOccur) { + fprintf(stderr, "%s: cannot combine --writePoolBundle and --usePoolBundle\n", argv[0]); + illegalArg = TRUE; + } + if(options[FORMAT_VERSION].doesOccur) { + const char *s = options[FORMAT_VERSION].value; + if(uprv_strlen(s) != 1 || (s[0] < '1' && '3' < s[0])) { + fprintf(stderr, "%s: unsupported --formatVersion %s\n", argv[0], s); + illegalArg = TRUE; + } else if(s[0] == '1' && + (options[WRITE_POOL_BUNDLE].doesOccur || options[USE_POOL_BUNDLE].doesOccur) + ) { + fprintf(stderr, "%s: cannot combine --formatVersion 1 with --writePoolBundle or --usePoolBundle\n", argv[0]); + illegalArg = TRUE; + } else { + setFormatVersion(s[0] - '0'); + } + } + + if((options[JAVA_PACKAGE].doesOccur || options[BUNDLE_NAME].doesOccur) && + !options[WRITE_JAVA].doesOccur) { + fprintf(stderr, + "%s error: command line argument --java-package or --bundle-name " + "without --write-java\n", + argv[0]); + illegalArg = TRUE; + } + + if(options[VERSION].doesOccur) { + fprintf(stderr, + "%s version %s (ICU version %s).\n" + "%s\n", + argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); + if(!illegalArg) { + return U_ZERO_ERROR; + } + } + + if(illegalArg || options[HELP1].doesOccur || options[HELP2].doesOccur) { + /* + * Broken into chunks because the C89 standard says the minimum + * required supported string length is 509 bytes. + */ + fprintf(stderr, + "Usage: %s [OPTIONS] [FILES]\n" + "\tReads the list of resource bundle source files and creates\n" + "\tbinary version of resource bundles (.res files)\n", + argv[0]); + fprintf(stderr, + "Options:\n" + "\t-h or -? or --help this usage text\n" + "\t-q or --quiet do not display warnings\n" + "\t-v or --verbose print extra information when processing files\n" + "\t-V or --version prints out version number and exits\n" + "\t-c or --copyright include copyright notice\n"); + fprintf(stderr, + "\t-e or --encoding encoding of source files\n" + "\t-d of --destdir destination directory, followed by the path, defaults to %s\n" + "\t-s or --sourcedir source directory for files followed by path, defaults to %s\n" + "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" + "\t followed by path, defaults to %s\n", + u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory()); + fprintf(stderr, + "\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n" + "\t defaults to ASCII and \\uXXXX format.\n" + "\t --java-package For --write-java: package name for writing the ListResourceBundle,\n" + "\t defaults to com.ibm.icu.impl.data\n"); + fprintf(stderr, + "\t-b or --bundle-name For --write-java: root resource bundle name for writing the ListResourceBundle,\n" + "\t defaults to LocaleElements\n" + "\t-x or --write-xliff write an XLIFF file for the resource bundle. Followed by\n" + "\t an optional output file name.\n" + "\t-k or --strict use pedantic parsing of syntax\n" + /*added by Jing*/ + "\t-l or --language for XLIFF: language code compliant with BCP 47.\n"); + fprintf(stderr, + "\t-C or --noBinaryCollation do not generate binary collation image;\n" + "\t makes .res file smaller but collator instantiation much slower;\n" + "\t maintains ability to get tailoring rules\n" + "\t-R or --omitCollationRules do not include collation (tailoring) rules;\n" + "\t makes .res file smaller and maintains collator instantiation speed\n" + "\t but tailoring rules will not be available (they are rarely used)\n"); + fprintf(stderr, + "\t --formatVersion write a .res file compatible with the requested formatVersion (single digit);\n" + "\t for example, --formatVersion 1\n"); + fprintf(stderr, + "\t --writePoolBundle write a pool.res file with all of the keys of all input bundles\n" + "\t --usePoolBundle [path-to-pool.res] point to keys from the pool.res keys pool bundle if they are available there;\n" + "\t makes .res files smaller but dependent on the pool bundle\n" + "\t (--writePoolBundle and --usePoolBundle cannot be combined)\n"); + + return illegalArg ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; + } + + if(options[VERBOSE].doesOccur) { + setVerbose(TRUE); + } + + if(options[QUIET].doesOccur) { + setShowWarning(FALSE); + } + if(options[STRICT].doesOccur) { + setStrict(TRUE); + } + if(options[COPYRIGHT].doesOccur){ + setIncludeCopyright(TRUE); + } + + if(options[SOURCEDIR].doesOccur) { + inputDir = options[SOURCEDIR].value; + } + + if(options[DESTDIR].doesOccur) { + outputDir = options[DESTDIR].value; + } + + if(options[ENCODING].doesOccur) { + encoding = options[ENCODING].value; + } + + if(options[ICUDATADIR].doesOccur) { + u_setDataDirectory(options[ICUDATADIR].value); + } + /* Initialize ICU */ + u_init(&status); + if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) { + /* Note: u_init() will try to open ICU property data. + * failures here are expected when building ICU from scratch. + * ignore them. + */ + fprintf(stderr, "%s: can not initialize ICU. status = %s\n", + argv[0], u_errorName(status)); + exit(1); + } + status = U_ZERO_ERROR; + if(options[WRITE_JAVA].doesOccur) { + write_java = TRUE; + outputEnc = options[WRITE_JAVA].value; + } + + if(options[WRITE_XLIFF].doesOccur) { + write_xliff = TRUE; + if(options[WRITE_XLIFF].value != NULL){ + xliffOutputFileName = options[WRITE_XLIFF].value; + } + } + + initParser(); + + /*added by Jing*/ + if(options[LANGUAGE].doesOccur) { + language = options[LANGUAGE].value; + } + + LocalPointer<SRBRoot> newPoolBundle; + if(options[WRITE_POOL_BUNDLE].doesOccur) { + newPoolBundle.adoptInsteadAndCheckErrorCode(new SRBRoot(NULL, TRUE, status), status); + if(U_FAILURE(status)) { + fprintf(stderr, "unable to create an empty bundle for the pool keys: %s\n", u_errorName(status)); + return status; + } else { + const char *poolResName = "pool.res"; + char *nameWithoutSuffix = static_cast<char *>(uprv_malloc(uprv_strlen(poolResName) + 1)); + if (nameWithoutSuffix == NULL) { + fprintf(stderr, "out of memory error\n"); + return U_MEMORY_ALLOCATION_ERROR; + } + uprv_strcpy(nameWithoutSuffix, poolResName); + *uprv_strrchr(nameWithoutSuffix, '.') = 0; + newPoolBundle->fLocale = nameWithoutSuffix; + } + } + + if(options[USE_POOL_BUNDLE].doesOccur) { + const char *poolResName = "pool.res"; + FileStream *poolFile; + int32_t poolFileSize; + int32_t indexLength; + /* + * TODO: Consolidate inputDir/filename handling from main() and processFile() + * into a common function, and use it here as well. + * Try to create toolutil functions for dealing with dir/filenames and + * loading ICU data files without udata_open(). + * Share code with icupkg? + * Also, make_res_filename() seems to be unused. Review and remove. + */ + CharString poolFileName; + if (options[USE_POOL_BUNDLE].value!=NULL) { + poolFileName.append(options[USE_POOL_BUNDLE].value, status); + } else if (inputDir) { + poolFileName.append(inputDir, status); + } + poolFileName.appendPathPart(poolResName, status); + if (U_FAILURE(status)) { + return status; + } + poolFile = T_FileStream_open(poolFileName.data(), "rb"); + if (poolFile == NULL) { + fprintf(stderr, "unable to open pool bundle file %s\n", poolFileName.data()); + return 1; + } + poolFileSize = T_FileStream_size(poolFile); + if (poolFileSize < 32) { + fprintf(stderr, "the pool bundle file %s is too small\n", poolFileName.data()); + return 1; + } + poolBundle.fBytes = new uint8_t[(poolFileSize + 15) & ~15]; + if (poolFileSize > 0 && poolBundle.fBytes == NULL) { + fprintf(stderr, "unable to allocate memory for the pool bundle file %s\n", poolFileName.data()); + return U_MEMORY_ALLOCATION_ERROR; + } + + UDataSwapper *ds; + const DataHeader *header; + int32_t bytesRead = T_FileStream_read(poolFile, poolBundle.fBytes, poolFileSize); + if (bytesRead != poolFileSize) { + fprintf(stderr, "unable to read the pool bundle file %s\n", poolFileName.data()); + return 1; + } + /* + * Swap the pool bundle so that a single checked-in file can be used. + * The swapper functions also test that the data looks like + * a well-formed .res file. + */ + ds = udata_openSwapperForInputData(poolBundle.fBytes, bytesRead, + U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &status); + if (U_FAILURE(status)) { + fprintf(stderr, "udata_openSwapperForInputData(pool bundle %s) failed: %s\n", + poolFileName.data(), u_errorName(status)); + return status; + } + ures_swap(ds, poolBundle.fBytes, bytesRead, poolBundle.fBytes, &status); + udata_closeSwapper(ds); + if (U_FAILURE(status)) { + fprintf(stderr, "ures_swap(pool bundle %s) failed: %s\n", + poolFileName.data(), u_errorName(status)); + return status; + } + header = (const DataHeader *)poolBundle.fBytes; + if (header->info.formatVersion[0] < 2) { + fprintf(stderr, "invalid format of pool bundle file %s\n", poolFileName.data()); + return U_INVALID_FORMAT_ERROR; + } + const int32_t *pRoot = (const int32_t *)( + (const char *)header + header->dataHeader.headerSize); + poolBundle.fIndexes = pRoot + 1; + indexLength = poolBundle.fIndexes[URES_INDEX_LENGTH] & 0xff; + if (indexLength <= URES_INDEX_POOL_CHECKSUM) { + fprintf(stderr, "insufficient indexes[] in pool bundle file %s\n", poolFileName.data()); + return U_INVALID_FORMAT_ERROR; + } + int32_t keysBottom = 1 + indexLength; + int32_t keysTop = poolBundle.fIndexes[URES_INDEX_KEYS_TOP]; + poolBundle.fKeys = (const char *)(pRoot + keysBottom); + poolBundle.fKeysLength = (keysTop - keysBottom) * 4; + poolBundle.fChecksum = poolBundle.fIndexes[URES_INDEX_POOL_CHECKSUM]; + + for (i = 0; i < poolBundle.fKeysLength; ++i) { + if (poolBundle.fKeys[i] == 0) { + ++poolBundle.fKeysCount; + } + } + + // 16BitUnits[] begins with strings-v2. + // The strings-v2 may optionally be terminated by what looks like + // an explicit string length that exceeds the number of remaining 16-bit units. + int32_t stringUnitsLength = (poolBundle.fIndexes[URES_INDEX_16BIT_TOP] - keysTop) * 2; + if (stringUnitsLength >= 2 && getFormatVersion() >= 3) { + poolBundle.fStrings = new PseudoListResource(NULL, status); + if (poolBundle.fStrings == NULL) { + fprintf(stderr, "unable to allocate memory for the pool bundle strings %s\n", + poolFileName.data()); + return U_MEMORY_ALLOCATION_ERROR; + } + // The PseudoListResource constructor call did not allocate further memory. + assert(U_SUCCESS(status)); + const UChar *p = (const UChar *)(pRoot + keysTop); + int32_t remaining = stringUnitsLength; + do { + int32_t first = *p; + int8_t numCharsForLength; + int32_t length; + if (!U16_IS_TRAIL(first)) { + // NUL-terminated + numCharsForLength = 0; + for (length = 0; + length < remaining && p[length] != 0; + ++length) {} + } else if (first < 0xdfef) { + numCharsForLength = 1; + length = first & 0x3ff; + } else if (first < 0xdfff && remaining >= 2) { + numCharsForLength = 2; + length = ((first - 0xdfef) << 16) | p[1]; + } else if (first == 0xdfff && remaining >= 3) { + numCharsForLength = 3; + length = ((int32_t)p[1] << 16) | p[2]; + } else { + break; // overrun + } + // Check for overrun before changing remaining, + // so that it is always accurate after the loop body. + if ((numCharsForLength + length) >= remaining || + p[numCharsForLength + length] != 0) { + break; // overrun or explicitly terminated + } + int32_t poolStringIndex = stringUnitsLength - remaining; + // Maximum pool string index when suffix-sharing the last character. + int32_t maxStringIndex = poolStringIndex + numCharsForLength + length - 1; + if (maxStringIndex >= RES_MAX_OFFSET) { + // pool string index overrun + break; + } + p += numCharsForLength; + remaining -= numCharsForLength; + if (length != 0) { + StringResource *sr = + new StringResource(poolStringIndex, numCharsForLength, + p, length, status); + if (sr == NULL) { + fprintf(stderr, "unable to allocate memory for a pool bundle string %s\n", + poolFileName.data()); + return U_MEMORY_ALLOCATION_ERROR; + } + poolBundle.fStrings->add(sr); + poolBundle.fStringIndexLimit = maxStringIndex + 1; + // The StringResource constructor did not allocate further memory. + assert(U_SUCCESS(status)); + } + p += length + 1; + remaining -= length + 1; + } while (remaining > 0); + if (poolBundle.fStrings->fCount == 0) { + delete poolBundle.fStrings; + poolBundle.fStrings = NULL; + } + } + + T_FileStream_close(poolFile); + setUsePoolBundle(TRUE); + if (isVerbose() && poolBundle.fStrings != NULL) { + printf("number of shared strings: %d\n", (int)poolBundle.fStrings->fCount); + int32_t length = poolBundle.fStringIndexLimit + 1; // incl. last NUL + printf("16-bit units for strings: %6d = %6d bytes\n", + (int)length, (int)length * 2); + } + } + + if(!options[FORMAT_VERSION].doesOccur && getFormatVersion() == 3 && + poolBundle.fStrings == NULL && + !options[WRITE_POOL_BUNDLE].doesOccur) { + // If we just default to formatVersion 3 + // but there are no pool bundle strings to share + // and we do not write a pool bundle, + // then write formatVersion 2 which is just as good. + setFormatVersion(2); + } + + if(options[INCLUDE_UNIHAN_COLL].doesOccur) { + puts("genrb option --includeUnihanColl ignored: \n" + "CLDR 26/ICU 54 unihan data is small, except\n" + "the ucadata-unihan.icu version of the collation root data\n" + "is about 300kB larger than the ucadata-implicithan.icu version."); + } + + if((argc-1)!=1) { + printf("genrb number of files: %d\n", argc - 1); + } + /* generate the binary files */ + for(i = 1; i < argc; ++i) { + status = U_ZERO_ERROR; + arg = getLongPathname(argv[i]); + + CharString theCurrentFileName; + if (inputDir) { + theCurrentFileName.append(inputDir, status); + } + theCurrentFileName.appendPathPart(arg, status); + if (U_FAILURE(status)) { + break; + } + + gCurrentFileName = theCurrentFileName.data(); + if (isVerbose()) { + printf("Processing file \"%s\"\n", theCurrentFileName.data()); + } + processFile(arg, encoding, inputDir, outputDir, NULL, + newPoolBundle.getAlias(), + options[NO_BINARY_COLLATION].doesOccur, status); + } + + poolBundle.close(); + + if(U_SUCCESS(status) && options[WRITE_POOL_BUNDLE].doesOccur) { + char outputFileName[256]; + newPoolBundle->write(outputDir, NULL, outputFileName, sizeof(outputFileName), status); + if(U_FAILURE(status)) { + fprintf(stderr, "unable to write the pool bundle: %s\n", u_errorName(status)); + } + } + + u_cleanup(); + + /* Dont return warnings as a failure */ + if (U_SUCCESS(status)) { + return 0; + } + + return status; +} + +/* Process a file */ +void +processFile(const char *filename, const char *cp, + const char *inputDir, const char *outputDir, const char *packageName, + SRBRoot *newPoolBundle, + UBool omitBinaryCollation, UErrorCode &status) { + LocalPointer<SRBRoot> data; + UCHARBUF *ucbuf = NULL; + char *rbname = NULL; + char *openFileName = NULL; + char *inputDirBuf = NULL; + + char outputFileName[256]; + + int32_t dirlen = 0; + int32_t filelen = 0; + + if (U_FAILURE(status)) { + return; + } + if(filename==NULL){ + status=U_ILLEGAL_ARGUMENT_ERROR; + return; + }else{ + filelen = (int32_t)uprv_strlen(filename); + } + + if(inputDir == NULL) { + const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR); + openFileName = (char *) uprv_malloc(dirlen + filelen + 2); + openFileName[0] = '\0'; + if (filenameBegin != NULL) { + /* + * When a filename ../../../data/root.txt is specified, + * we presume that the input directory is ../../../data + * This is very important when the resource file includes + * another file, like UCARules.txt or thaidict.brk. + */ + int32_t filenameSize = (int32_t)(filenameBegin - filename + 1); + inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize); + + /* test for NULL */ + if(inputDirBuf == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + inputDirBuf[filenameSize - 1] = 0; + inputDir = inputDirBuf; + dirlen = (int32_t)uprv_strlen(inputDir); + } + }else{ + dirlen = (int32_t)uprv_strlen(inputDir); + + if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { + openFileName = (char *) uprv_malloc(dirlen + filelen + 2); + + /* test for NULL */ + if(openFileName == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + openFileName[0] = '\0'; + /* + * append the input dir to openFileName if the first char in + * filename is not file seperation char and the last char input directory is not '.'. + * This is to support : + * genrb -s. /home/icu/data + * genrb -s. icu/data + * The user cannot mix notations like + * genrb -s. /icu/data --- the absolute path specified. -s redundant + * user should use + * genrb -s. icu/data --- start from CWD and look in icu/data dir + */ + if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ + uprv_strcpy(openFileName, inputDir); + openFileName[dirlen] = U_FILE_SEP_CHAR; + } + openFileName[dirlen + 1] = '\0'; + } else { + openFileName = (char *) uprv_malloc(dirlen + filelen + 1); + + /* test for NULL */ + if(openFileName == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + uprv_strcpy(openFileName, inputDir); + + } + } + + uprv_strcat(openFileName, filename); + + ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, &status); + if(status == U_FILE_ACCESS_ERROR) { + + fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName); + goto finish; + } + if (ucbuf == NULL || U_FAILURE(status)) { + fprintf(stderr, "An error occured processing file %s. Error: %s\n", + openFileName == NULL ? filename : openFileName, u_errorName(status)); + goto finish; + } + /* auto detected popular encodings? */ + if (cp!=NULL && isVerbose()) { + printf("autodetected encoding %s\n", cp); + } + /* Parse the data into an SRBRoot */ + data.adoptInstead(parse(ucbuf, inputDir, outputDir, filename, + !omitBinaryCollation, options[NO_COLLATION_RULES].doesOccur, &status)); + + if (data.isNull() || U_FAILURE(status)) { + fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename, u_errorName(status)); + goto finish; + } + if(options[WRITE_POOL_BUNDLE].doesOccur) { + data->fWritePoolBundle = newPoolBundle; + data->compactKeys(status); + int32_t newKeysLength; + const char *newKeys = data->getKeyBytes(&newKeysLength); + newPoolBundle->addKeyBytes(newKeys, newKeysLength, status); + if(U_FAILURE(status)) { + fprintf(stderr, "bundle_compactKeys(%s) or bundle_getKeyBytes() failed: %s\n", + filename, u_errorName(status)); + goto finish; + } + /* count the number of just-added key strings */ + for(const char *newKeysLimit = newKeys + newKeysLength; newKeys < newKeysLimit; ++newKeys) { + if(*newKeys == 0) { + ++newPoolBundle->fKeysCount; + } + } + } + + if(options[USE_POOL_BUNDLE].doesOccur) { + data->fUsePoolBundle = &poolBundle; + } + + /* Determine the target rb filename */ + rbname = make_res_filename(filename, outputDir, packageName, status); + if(U_FAILURE(status)) { + fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n", + filename, u_errorName(status)); + goto finish; + } + if(write_java== TRUE){ + bundle_write_java(data.getAlias(), outputDir, outputEnc, + outputFileName, sizeof(outputFileName), + options[JAVA_PACKAGE].value, options[BUNDLE_NAME].value, &status); + }else if(write_xliff ==TRUE){ + bundle_write_xml(data.getAlias(), outputDir, outputEnc, + filename, outputFileName, sizeof(outputFileName), + language, xliffOutputFileName, &status); + }else{ + /* Write the data to the file */ + data->write(outputDir, packageName, outputFileName, sizeof(outputFileName), status); + } + if (U_FAILURE(status)) { + fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName, u_errorName(status)); + } + +finish: + + if (inputDirBuf != NULL) { + uprv_free(inputDirBuf); + } + + if (openFileName != NULL) { + uprv_free(openFileName); + } + + if(ucbuf) { + ucbuf_close(ucbuf); + } + + if (rbname) { + uprv_free(rbname); + } +} + +/* Generate the target .res file name from the input file name */ +static char* +make_res_filename(const char *filename, + const char *outputDir, + const char *packageName, + UErrorCode &status) { + char *basename; + char *dirname; + char *resName; + + int32_t pkgLen = 0; /* length of package prefix */ + + + if (U_FAILURE(status)) { + return 0; + } + + if(packageName != NULL) + { + pkgLen = (int32_t)(1 + uprv_strlen(packageName)); + } + + /* setup */ + basename = dirname = resName = 0; + + /* determine basename, and compiled file names */ + basename = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1)); + if(basename == 0) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + get_basename(basename, filename); + + dirname = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(filename) + 1)); + if(dirname == 0) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + get_dirname(dirname, filename); + + if (outputDir == NULL) { + /* output in same dir as .txt */ + resName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(dirname) + + pkgLen + + uprv_strlen(basename) + + uprv_strlen(RES_SUFFIX) + 8)); + if(resName == 0) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + uprv_strcpy(resName, dirname); + + if(packageName != NULL) + { + uprv_strcat(resName, packageName); + uprv_strcat(resName, "_"); + } + + uprv_strcat(resName, basename); + + } else { + int32_t dirlen = (int32_t)uprv_strlen(outputDir); + int32_t basenamelen = (int32_t)uprv_strlen(basename); + + resName = (char*) uprv_malloc(sizeof(char) * (dirlen + pkgLen + basenamelen + 8)); + + if (resName == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + goto finish; + } + + uprv_strcpy(resName, outputDir); + + if(outputDir[dirlen] != U_FILE_SEP_CHAR) { + resName[dirlen] = U_FILE_SEP_CHAR; + resName[dirlen + 1] = '\0'; + } + + if(packageName != NULL) + { + uprv_strcat(resName, packageName); + uprv_strcat(resName, "_"); + } + + uprv_strcat(resName, basename); + } + +finish: + uprv_free(basename); + uprv_free(dirname); + + return resName; +} + +/* + * Local Variables: + * indent-tabs-mode: nil + * End: + */ diff --git a/intl/icu/source/tools/genrb/genrb.h b/intl/icu/source/tools/genrb/genrb.h new file mode 100644 index 000000000..99c604f78 --- /dev/null +++ b/intl/icu/source/tools/genrb/genrb.h @@ -0,0 +1,52 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2002-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File genrb.h +*/ + +#ifndef GENRB_H +#define GENRB_H + +#include <stdio.h> +#include "unicode/utypes.h" +#include "unicode/putil.h" +#include "cmemory.h" +#include "cstring.h" +#include "filestrm.h" + + +#include "ucbuf.h" +#include "errmsg.h" +#include "parse.h" +#include "rbutil.h" + +#include "toolutil.h" +#include "uoptions.h" + +#include "unicode/ucol.h" +#include "unicode/uloc.h" + +/* The version of genrb */ +#define GENRB_VERSION "56" + +U_CDECL_BEGIN + +U_CAPI void processFile( + const char *filename, + const char* cp, + const char *inputDir, + const char *outputDir, + const char *packageName, + UBool omitBinaryCollation, + UErrorCode *status); + +U_CDECL_END + +#endif diff --git a/intl/icu/source/tools/genrb/genrb.vcxproj b/intl/icu/source/tools/genrb/genrb.vcxproj new file mode 100644 index 000000000..d79545016 --- /dev/null +++ b/intl/icu/source/tools/genrb/genrb.vcxproj @@ -0,0 +1,295 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{97521D06-EC47-45D4-8BD0-9E16B3F93B2A}</ProjectGuid> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup> + <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\x86\Release\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\x86\Release\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\x86\Debug\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\x86\Debug\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <CustomBuildStep> + <Command>copy "$(TargetPath)" ..\..\..\bin +</Command> + <Outputs>..\..\..\bin\$(TargetFileName);%(Outputs)</Outputs> + </CustomBuildStep> + <Midl> + <TypeLibraryName>.\x86\Release/genrb.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <AdditionalIncludeDirectories>..\..\..\include;..\..\common;..\toolutil;..\..\i18n;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <DisableLanguageExtensions>true</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x86\Release/genrb.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x86\Release/</AssemblerListingLocation> + <ObjectFileName>.\x86\Release/</ObjectFileName> + <ProgramDataBaseFileName>.\x86\Release/</ProgramDataBaseFileName> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + </ResourceCompile> + <Link> + <OutputFile>.\x86\Release/genrb.exe</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <ProgramDatabaseFile>.\x86\Release/genrb.pdb</ProgramDatabaseFile> + <SubSystem>Console</SubSystem> + <RandomizedBaseAddress>false</RandomizedBaseAddress> + <DataExecutionPrevention> + </DataExecutionPrevention> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <CustomBuildStep> + <Command>copy "$(TargetPath)" ..\..\..\bin +</Command> + <Outputs>..\..\..\bin\$(TargetFileName);%(Outputs)</Outputs> + </CustomBuildStep> + <Midl> + <TypeLibraryName>.\x86\Debug/genrb.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..\..\include;..\..\common;..\toolutil;..\..\i18n;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + <BufferSecurityCheck>true</BufferSecurityCheck> + <DisableLanguageExtensions>true</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x86\Debug/genrb.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x86\Debug/</AssemblerListingLocation> + <ObjectFileName>.\x86\Debug/</ObjectFileName> + <ProgramDataBaseFileName>.\x86\Debug/</ProgramDataBaseFileName> + <BrowseInformation>true</BrowseInformation> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <DebugInformationFormat>EditAndContinue</DebugInformationFormat> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + </ResourceCompile> + <Link> + <OutputFile>.\x86\Debug/genrb.exe</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ProgramDatabaseFile>.\x86\Debug/genrb.pdb</ProgramDatabaseFile> + <SubSystem>Console</SubSystem> + <RandomizedBaseAddress>false</RandomizedBaseAddress> + <DataExecutionPrevention> + </DataExecutionPrevention> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <CustomBuildStep> + <Command>copy "$(TargetPath)" ..\..\..\bin64 +</Command> + <Outputs>..\..\..\bin64\$(TargetFileName);%(Outputs)</Outputs> + </CustomBuildStep> + <Midl> + <TargetEnvironment>X64</TargetEnvironment> + <TypeLibraryName>.\x64\Release/genrb.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <AdditionalIncludeDirectories>..\..\..\include;..\..\common;..\toolutil;..\..\i18n;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN64;WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <DisableLanguageExtensions>true</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x64\Release/genrb.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x64\Release/</AssemblerListingLocation> + <ObjectFileName>.\x64\Release/</ObjectFileName> + <ProgramDataBaseFileName>.\x64\Release/</ProgramDataBaseFileName> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + </ResourceCompile> + <Link> + <OutputFile>.\x64\Release/genrb.exe</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <ProgramDatabaseFile>.\x64\Release/genrb.pdb</ProgramDatabaseFile> + <SubSystem>Console</SubSystem> + <TargetMachine>MachineX64</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <CustomBuildStep> + <Command>copy "$(TargetPath)" ..\..\..\bin64 +</Command> + <Outputs>..\..\..\bin64\$(TargetFileName);%(Outputs)</Outputs> + </CustomBuildStep> + <Midl> + <TargetEnvironment>X64</TargetEnvironment> + <TypeLibraryName>.\x64\Debug/genrb.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..\..\include;..\..\common;..\toolutil;..\..\i18n;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN64;WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + <BufferSecurityCheck>true</BufferSecurityCheck> + <DisableLanguageExtensions>true</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x64\Debug/genrb.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x64\Debug/</AssemblerListingLocation> + <ObjectFileName>.\x64\Debug/</ObjectFileName> + <ProgramDataBaseFileName>.\x64\Debug/</ProgramDataBaseFileName> + <BrowseInformation>true</BrowseInformation> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + </ResourceCompile> + <Link> + <OutputFile>.\x64\Debug/genrb.exe</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ProgramDatabaseFile>.\x64\Debug/genrb.pdb</ProgramDatabaseFile> + <SubSystem>Console</SubSystem> + <TargetMachine>MachineX64</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="errmsg.c" /> + <ClCompile Include="genrb.cpp" /> + <ClCompile Include="parse.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + <ClCompile Include="prscmnts.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + <ClCompile Include="rbutil.c" /> + <ClCompile Include="read.c" /> + <ClCompile Include="reslist.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + <ClCompile Include="rle.c" /> + <ClCompile Include="ustr.c" /> + <ClCompile Include="wrtjava.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + <ClCompile Include="wrtxml.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="errmsg.h" /> + <ClInclude Include="genrb.h" /> + <ClInclude Include="parse.h" /> + <ClInclude Include="prscmnts.h" /> + <ClInclude Include="rbutil.h" /> + <ClInclude Include="read.h" /> + <ClInclude Include="reslist.h" /> + <ClInclude Include="rle.h" /> + <ClInclude Include="ustr.h" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\common\common.vcxproj"> + <Project>{73c0a65b-d1f2-4de1-b3a6-15dad2c23f3d}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + <ProjectReference Include="..\..\i18n\i18n.vcxproj"> + <Project>{0178b127-6269-407d-b112-93877bb62776}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + <ProjectReference Include="..\toolutil\toolutil.vcxproj"> + <Project>{6b231032-3cb5-4eed-9210-810d666a23a0}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/intl/icu/source/tools/genrb/genrb.vcxproj.filters b/intl/icu/source/tools/genrb/genrb.vcxproj.filters new file mode 100644 index 000000000..73de73044 --- /dev/null +++ b/intl/icu/source/tools/genrb/genrb.vcxproj.filters @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{2dee2c2f-25a5-43f0-985f-de4ba26925b4}</UniqueIdentifier> + <Extensions>cpp;c;cxx;rc;def;r;odl;idl;hpj;bat</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{7156c811-7116-4eef-8bb1-0400c51f9fd3}</UniqueIdentifier> + <Extensions>h;hpp;hxx;hm;inl</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{df647868-56cc-475d-a3f6-1d1f50aa5e4f}</UniqueIdentifier> + <Extensions>ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="errmsg.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="genrb.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="parse.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="prscmnts.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="rbutil.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="read.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="reslist.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="rle.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ustr.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="wrtjava.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="wrtxml.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="errmsg.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="genrb.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="parse.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="prscmnts.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="rbutil.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="read.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="reslist.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="rle.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="ustr.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/intl/icu/source/tools/genrb/parse.cpp b/intl/icu/source/tools/genrb/parse.cpp new file mode 100644 index 000000000..81a3bf597 --- /dev/null +++ b/intl/icu/source/tools/genrb/parse.cpp @@ -0,0 +1,2079 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File parse.cpp +* +* Modification History: +* +* Date Name Description +* 05/26/99 stephen Creation. +* 02/25/00 weiv Overhaul to write udata +* 5/10/01 Ram removed ustdio dependency +* 06/10/2001 Dominic Ludlam <dom@recoil.org> Rewritten +******************************************************************************* +*/ + +// Safer use of UnicodeString. +#ifndef UNISTR_FROM_CHAR_EXPLICIT +# define UNISTR_FROM_CHAR_EXPLICIT explicit +#endif + +// Less important, but still a good idea. +#ifndef UNISTR_FROM_STRING_EXPLICIT +# define UNISTR_FROM_STRING_EXPLICIT explicit +#endif + +#include <assert.h> +#include "parse.h" +#include "errmsg.h" +#include "uhash.h" +#include "cmemory.h" +#include "cstring.h" +#include "uinvchar.h" +#include "read.h" +#include "ustr.h" +#include "reslist.h" +#include "rbt_pars.h" +#include "genrb.h" +#include "unicode/stringpiece.h" +#include "unicode/unistr.h" +#include "unicode/ustring.h" +#include "unicode/uscript.h" +#include "unicode/utf16.h" +#include "unicode/putil.h" +#include "charstr.h" +#include "collationbuilder.h" +#include "collationdata.h" +#include "collationdatareader.h" +#include "collationdatawriter.h" +#include "collationfastlatinbuilder.h" +#include "collationinfo.h" +#include "collationroot.h" +#include "collationruleparser.h" +#include "collationtailoring.h" +#include <stdio.h> + +/* Number of tokens to read ahead of the current stream position */ +#define MAX_LOOKAHEAD 3 + +#define CR 0x000D +#define LF 0x000A +#define SPACE 0x0020 +#define TAB 0x0009 +#define ESCAPE 0x005C +#define HASH 0x0023 +#define QUOTE 0x0027 +#define ZERO 0x0030 +#define STARTCOMMAND 0x005B +#define ENDCOMMAND 0x005D +#define OPENSQBRACKET 0x005B +#define CLOSESQBRACKET 0x005D + +using icu::CharString; +using icu::LocalMemory; +using icu::LocalPointer; +using icu::LocalUCHARBUFPointer; +using icu::StringPiece; +using icu::UnicodeString; + +struct Lookahead +{ + enum ETokenType type; + struct UString value; + struct UString comment; + uint32_t line; +}; + +/* keep in sync with token defines in read.h */ +const char *tokenNames[TOK_TOKEN_COUNT] = +{ + "string", /* A string token, such as "MonthNames" */ + "'{'", /* An opening brace character */ + "'}'", /* A closing brace character */ + "','", /* A comma */ + "':'", /* A colon */ + + "<end of file>", /* End of the file has been reached successfully */ + "<end of line>" +}; + +/* Just to store "TRUE" */ +//static const UChar trueValue[] = {0x0054, 0x0052, 0x0055, 0x0045, 0x0000}; + +typedef struct { + struct Lookahead lookahead[MAX_LOOKAHEAD + 1]; + uint32_t lookaheadPosition; + UCHARBUF *buffer; + struct SRBRoot *bundle; + const char *inputdir; + uint32_t inputdirLength; + const char *outputdir; + uint32_t outputdirLength; + const char *filename; + UBool makeBinaryCollation; + UBool omitCollationRules; +} ParseState; + +typedef struct SResource * +ParseResourceFunction(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status); + +static struct SResource *parseResource(ParseState* state, char *tag, const struct UString *comment, UErrorCode *status); + +/* The nature of the lookahead buffer: + There are MAX_LOOKAHEAD + 1 slots, used as a circular buffer. This provides + MAX_LOOKAHEAD lookahead tokens and a slot for the current token and value. + When getToken is called, the current pointer is moved to the next slot and the + old slot is filled with the next token from the reader by calling getNextToken. + The token values are stored in the slot, which means that token values don't + survive a call to getToken, ie. + + UString *value; + + getToken(&value, NULL, status); + getToken(NULL, NULL, status); bad - value is now a different string +*/ +static void +initLookahead(ParseState* state, UCHARBUF *buf, UErrorCode *status) +{ + static uint32_t initTypeStrings = 0; + uint32_t i; + + if (!initTypeStrings) + { + initTypeStrings = 1; + } + + state->lookaheadPosition = 0; + state->buffer = buf; + + resetLineNumber(); + + for (i = 0; i < MAX_LOOKAHEAD; i++) + { + state->lookahead[i].type = getNextToken(state->buffer, &state->lookahead[i].value, &state->lookahead[i].line, &state->lookahead[i].comment, status); + if (U_FAILURE(*status)) + { + return; + } + } + + *status = U_ZERO_ERROR; +} + +static void +cleanupLookahead(ParseState* state) +{ + uint32_t i; + for (i = 0; i <= MAX_LOOKAHEAD; i++) + { + ustr_deinit(&state->lookahead[i].value); + ustr_deinit(&state->lookahead[i].comment); + } + +} + +static enum ETokenType +getToken(ParseState* state, struct UString **tokenValue, struct UString* comment, uint32_t *linenumber, UErrorCode *status) +{ + enum ETokenType result; + uint32_t i; + + result = state->lookahead[state->lookaheadPosition].type; + + if (tokenValue != NULL) + { + *tokenValue = &state->lookahead[state->lookaheadPosition].value; + } + + if (linenumber != NULL) + { + *linenumber = state->lookahead[state->lookaheadPosition].line; + } + + if (comment != NULL) + { + ustr_cpy(comment, &(state->lookahead[state->lookaheadPosition].comment), status); + } + + i = (state->lookaheadPosition + MAX_LOOKAHEAD) % (MAX_LOOKAHEAD + 1); + state->lookaheadPosition = (state->lookaheadPosition + 1) % (MAX_LOOKAHEAD + 1); + ustr_setlen(&state->lookahead[i].comment, 0, status); + ustr_setlen(&state->lookahead[i].value, 0, status); + state->lookahead[i].type = getNextToken(state->buffer, &state->lookahead[i].value, &state->lookahead[i].line, &state->lookahead[i].comment, status); + + /* printf("getToken, returning %s\n", tokenNames[result]); */ + + return result; +} + +static enum ETokenType +peekToken(ParseState* state, uint32_t lookaheadCount, struct UString **tokenValue, uint32_t *linenumber, struct UString *comment, UErrorCode *status) +{ + uint32_t i = (state->lookaheadPosition + lookaheadCount) % (MAX_LOOKAHEAD + 1); + + if (U_FAILURE(*status)) + { + return TOK_ERROR; + } + + if (lookaheadCount >= MAX_LOOKAHEAD) + { + *status = U_INTERNAL_PROGRAM_ERROR; + return TOK_ERROR; + } + + if (tokenValue != NULL) + { + *tokenValue = &state->lookahead[i].value; + } + + if (linenumber != NULL) + { + *linenumber = state->lookahead[i].line; + } + + if(comment != NULL){ + ustr_cpy(comment, &(state->lookahead[state->lookaheadPosition].comment), status); + } + + return state->lookahead[i].type; +} + +static void +expect(ParseState* state, enum ETokenType expectedToken, struct UString **tokenValue, struct UString *comment, uint32_t *linenumber, UErrorCode *status) +{ + uint32_t line; + + enum ETokenType token = getToken(state, tokenValue, comment, &line, status); + + if (linenumber != NULL) + { + *linenumber = line; + } + + if (U_FAILURE(*status)) + { + return; + } + + if (token != expectedToken) + { + *status = U_INVALID_FORMAT_ERROR; + error(line, "expecting %s, got %s", tokenNames[expectedToken], tokenNames[token]); + } + else + { + *status = U_ZERO_ERROR; + } +} + +static char *getInvariantString(ParseState* state, uint32_t *line, struct UString *comment, UErrorCode *status) +{ + struct UString *tokenValue; + char *result; + uint32_t count; + + expect(state, TOK_STRING, &tokenValue, comment, line, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + + count = u_strlen(tokenValue->fChars); + if(!uprv_isInvariantUString(tokenValue->fChars, count)) { + *status = U_INVALID_FORMAT_ERROR; + error(*line, "invariant characters required for table keys, binary data, etc."); + return NULL; + } + + result = static_cast<char *>(uprv_malloc(count+1)); + + if (result == NULL) + { + *status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + + u_UCharsToChars(tokenValue->fChars, result, count+1); + return result; +} + +static struct SResource * +parseUCARules(ParseState* state, char *tag, uint32_t startline, const struct UString* /*comment*/, UErrorCode *status) +{ + struct SResource *result = NULL; + struct UString *tokenValue; + FileStream *file = NULL; + char filename[256] = { '\0' }; + char cs[128] = { '\0' }; + uint32_t line; + UBool quoted = FALSE; + UCHARBUF *ucbuf=NULL; + UChar32 c = 0; + const char* cp = NULL; + UChar *pTarget = NULL; + UChar *target = NULL; + UChar *targetLimit = NULL; + int32_t size = 0; + + expect(state, TOK_STRING, &tokenValue, NULL, &line, status); + + if(isVerbose()){ + printf(" %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + if (U_FAILURE(*status)) + { + return NULL; + } + /* make the filename including the directory */ + if (state->inputdir != NULL) + { + uprv_strcat(filename, state->inputdir); + + if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) + { + uprv_strcat(filename, U_FILE_SEP_STRING); + } + } + + u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + uprv_strcat(filename, cs); + + if(state->omitCollationRules) { + return res_none(); + } + + ucbuf = ucbuf_open(filename, &cp, getShowWarning(),FALSE, status); + + if (U_FAILURE(*status)) { + error(line, "An error occured while opening the input file %s\n", filename); + return NULL; + } + + /* We allocate more space than actually required + * since the actual size needed for storing UChars + * is not known in UTF-8 byte stream + */ + size = ucbuf_size(ucbuf) + 1; + pTarget = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * size); + uprv_memset(pTarget, 0, size*U_SIZEOF_UCHAR); + target = pTarget; + targetLimit = pTarget+size; + + /* read the rules into the buffer */ + while (target < targetLimit) + { + c = ucbuf_getc(ucbuf, status); + if(c == QUOTE) { + quoted = (UBool)!quoted; + } + /* weiv (06/26/2002): adding the following: + * - preserving spaces in commands [...] + * - # comments until the end of line + */ + if (c == STARTCOMMAND && !quoted) + { + /* preserve commands + * closing bracket will be handled by the + * append at the end of the loop + */ + while(c != ENDCOMMAND) { + U_APPEND_CHAR32_ONLY(c, target); + c = ucbuf_getc(ucbuf, status); + } + } + else if (c == HASH && !quoted) { + /* skip comments */ + while(c != CR && c != LF) { + c = ucbuf_getc(ucbuf, status); + } + continue; + } + else if (c == ESCAPE) + { + c = unescape(ucbuf, status); + + if (c == (UChar32)U_ERR) + { + uprv_free(pTarget); + T_FileStream_close(file); + return NULL; + } + } + else if (!quoted && (c == SPACE || c == TAB || c == CR || c == LF)) + { + /* ignore spaces carriage returns + * and line feed unless in the form \uXXXX + */ + continue; + } + + /* Append UChar * after dissembling if c > 0xffff*/ + if (c != (UChar32)U_EOF) + { + U_APPEND_CHAR32_ONLY(c, target); + } + else + { + break; + } + } + + /* terminate the string */ + if(target < targetLimit){ + *target = 0x0000; + } + + result = string_open(state->bundle, tag, pTarget, (int32_t)(target - pTarget), NULL, status); + + + ucbuf_close(ucbuf); + uprv_free(pTarget); + T_FileStream_close(file); + + return result; +} + +static struct SResource * +parseTransliterator(ParseState* state, char *tag, uint32_t startline, const struct UString* /*comment*/, UErrorCode *status) +{ + struct SResource *result = NULL; + struct UString *tokenValue; + FileStream *file = NULL; + char filename[256] = { '\0' }; + char cs[128] = { '\0' }; + uint32_t line; + UCHARBUF *ucbuf=NULL; + const char* cp = NULL; + UChar *pTarget = NULL; + const UChar *pSource = NULL; + int32_t size = 0; + + expect(state, TOK_STRING, &tokenValue, NULL, &line, status); + + if(isVerbose()){ + printf(" %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + if (U_FAILURE(*status)) + { + return NULL; + } + /* make the filename including the directory */ + if (state->inputdir != NULL) + { + uprv_strcat(filename, state->inputdir); + + if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) + { + uprv_strcat(filename, U_FILE_SEP_STRING); + } + } + + u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + uprv_strcat(filename, cs); + + + ucbuf = ucbuf_open(filename, &cp, getShowWarning(),FALSE, status); + + if (U_FAILURE(*status)) { + error(line, "An error occured while opening the input file %s\n", filename); + return NULL; + } + + /* We allocate more space than actually required + * since the actual size needed for storing UChars + * is not known in UTF-8 byte stream + */ + pSource = ucbuf_getBuffer(ucbuf, &size, status); + pTarget = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * (size + 1)); + uprv_memset(pTarget, 0, size*U_SIZEOF_UCHAR); + +#if !UCONFIG_NO_TRANSLITERATION + size = utrans_stripRules(pSource, size, pTarget, status); +#else + size = 0; + fprintf(stderr, " Warning: writing empty transliteration data ( UCONFIG_NO_TRANSLITERATION ) \n"); +#endif + result = string_open(state->bundle, tag, pTarget, size, NULL, status); + + ucbuf_close(ucbuf); + uprv_free(pTarget); + T_FileStream_close(file); + + return result; +} +static ArrayResource* dependencyArray = NULL; + +static struct SResource * +parseDependency(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) +{ + struct SResource *result = NULL; + struct SResource *elem = NULL; + struct UString *tokenValue; + uint32_t line; + char filename[256] = { '\0' }; + char cs[128] = { '\0' }; + + expect(state, TOK_STRING, &tokenValue, NULL, &line, status); + + if(isVerbose()){ + printf(" %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + if (U_FAILURE(*status)) + { + return NULL; + } + /* make the filename including the directory */ + if (state->outputdir != NULL) + { + uprv_strcat(filename, state->outputdir); + + if (state->outputdir[state->outputdirLength - 1] != U_FILE_SEP_CHAR) + { + uprv_strcat(filename, U_FILE_SEP_STRING); + } + } + + u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); + + if (U_FAILURE(*status)) + { + return NULL; + } + uprv_strcat(filename, cs); + if(!T_FileStream_file_exists(filename)){ + if(isStrict()){ + error(line, "The dependency file %s does not exist. Please make sure it exists.\n",filename); + }else{ + warning(line, "The dependency file %s does not exist. Please make sure it exists.\n",filename); + } + } + if(dependencyArray==NULL){ + dependencyArray = array_open(state->bundle, "%%DEPENDENCY", NULL, status); + } + if(tag!=NULL){ + result = string_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); + } + elem = string_open(state->bundle, NULL, tokenValue->fChars, tokenValue->fLength, comment, status); + + dependencyArray->add(elem); + + if (U_FAILURE(*status)) + { + return NULL; + } + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + return result; +} +static struct SResource * +parseString(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) +{ + struct UString *tokenValue; + struct SResource *result = NULL; + +/* if (tag != NULL && uprv_strcmp(tag, "%%UCARULES") == 0) + { + return parseUCARules(tag, startline, status); + }*/ + if(isVerbose()){ + printf(" string %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + expect(state, TOK_STRING, &tokenValue, NULL, NULL, status); + + if (U_SUCCESS(*status)) + { + /* create the string now - tokenValue doesn't survive a call to getToken (and therefore + doesn't survive expect either) */ + + result = string_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); + if(U_SUCCESS(*status) && result) { + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + } + } + + return result; +} + +static struct SResource * +parseAlias(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + struct UString *tokenValue; + struct SResource *result = NULL; + + expect(state, TOK_STRING, &tokenValue, NULL, NULL, status); + + if(isVerbose()){ + printf(" alias %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + if (U_SUCCESS(*status)) + { + /* create the string now - tokenValue doesn't survive a call to getToken (and therefore + doesn't survive expect either) */ + + result = alias_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + } + + return result; +} + +#if !UCONFIG_NO_COLLATION + +namespace { + +static struct SResource* resLookup(struct SResource* res, const char* key){ + if (res == res_none() || !res->isTable()) { + return NULL; + } + + TableResource *list = static_cast<TableResource *>(res); + SResource *current = list->fFirst; + while (current != NULL) { + if (uprv_strcmp(((list->fRoot->fKeys) + (current->fKey)), key) == 0) { + return current; + } + current = current->fNext; + } + return NULL; +} + +class GenrbImporter : public icu::CollationRuleParser::Importer { +public: + GenrbImporter(const char *in, const char *out) : inputDir(in), outputDir(out) {} + virtual ~GenrbImporter(); + virtual void getRules( + const char *localeID, const char *collationType, + UnicodeString &rules, + const char *&errorReason, UErrorCode &errorCode); + +private: + const char *inputDir; + const char *outputDir; +}; + +GenrbImporter::~GenrbImporter() {} + +void +GenrbImporter::getRules( + const char *localeID, const char *collationType, + UnicodeString &rules, + const char *& /*errorReason*/, UErrorCode &errorCode) { + CharString filename(localeID, errorCode); + for(int32_t i = 0; i < filename.length(); i++){ + if(filename[i] == '-'){ + filename.data()[i] = '_'; + } + } + filename.append(".txt", errorCode); + if (U_FAILURE(errorCode)) { + return; + } + CharString inputDirBuf; + CharString openFileName; + if(inputDir == NULL) { + const char *filenameBegin = uprv_strrchr(filename.data(), U_FILE_SEP_CHAR); + if (filenameBegin != NULL) { + /* + * When a filename ../../../data/root.txt is specified, + * we presume that the input directory is ../../../data + * This is very important when the resource file includes + * another file, like UCARules.txt or thaidict.brk. + */ + StringPiece dir = filename.toStringPiece(); + const char *filenameLimit = filename.data() + filename.length(); + dir.remove_suffix((int32_t)(filenameLimit - filenameBegin)); + inputDirBuf.append(dir, errorCode); + inputDir = inputDirBuf.data(); + } + }else{ + int32_t dirlen = (int32_t)uprv_strlen(inputDir); + + if((filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')) { + /* + * append the input dir to openFileName if the first char in + * filename is not file separator char and the last char input directory is not '.'. + * This is to support : + * genrb -s. /home/icu/data + * genrb -s. icu/data + * The user cannot mix notations like + * genrb -s. /icu/data --- the absolute path specified. -s redundant + * user should use + * genrb -s. icu/data --- start from CWD and look in icu/data dir + */ + openFileName.append(inputDir, dirlen, errorCode); + if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { + openFileName.append(U_FILE_SEP_CHAR, errorCode); + } + } + } + openFileName.append(filename, errorCode); + if(U_FAILURE(errorCode)) { + return; + } + // printf("GenrbImporter::getRules(%s, %s) reads %s\n", localeID, collationType, openFileName.data()); + const char* cp = ""; + LocalUCHARBUFPointer ucbuf( + ucbuf_open(openFileName.data(), &cp, getShowWarning(), TRUE, &errorCode)); + if(errorCode == U_FILE_ACCESS_ERROR) { + fprintf(stderr, "couldn't open file %s\n", openFileName.data()); + return; + } + if (ucbuf.isNull() || U_FAILURE(errorCode)) { + fprintf(stderr, "An error occured processing file %s. Error: %s\n", openFileName.data(), u_errorName(errorCode)); + return; + } + + /* Parse the data into an SRBRoot */ + struct SRBRoot *data = + parse(ucbuf.getAlias(), inputDir, outputDir, filename.data(), FALSE, FALSE, &errorCode); + if (U_FAILURE(errorCode)) { + return; + } + + struct SResource *root = data->fRoot; + struct SResource *collations = resLookup(root, "collations"); + if (collations != NULL) { + struct SResource *collation = resLookup(collations, collationType); + if (collation != NULL) { + struct SResource *sequence = resLookup(collation, "Sequence"); + if (sequence != NULL && sequence->isString()) { + // No string pointer aliasing so that we need not hold onto the resource bundle. + StringResource *sr = static_cast<StringResource *>(sequence); + rules = sr->fString; + } + } + } +} + +// Quick-and-dirty escaping function. +// Assumes that we are on an ASCII-based platform. +static void +escape(const UChar *s, char *buffer) { + int32_t length = u_strlen(s); + int32_t i = 0; + for (;;) { + UChar32 c; + U16_NEXT(s, i, length, c); + if (c == 0) { + *buffer = 0; + return; + } else if (0x20 <= c && c <= 0x7e) { + // printable ASCII + *buffer++ = (char)c; // assumes ASCII-based platform + } else { + buffer += sprintf(buffer, "\\u%04X", (int)c); + } + } +} + +} // namespace + +#endif // !UCONFIG_NO_COLLATION + +static TableResource * +addCollation(ParseState* state, TableResource *result, const char *collationType, + uint32_t startline, UErrorCode *status) +{ + // TODO: Use LocalPointer for result, or make caller close it when there is a failure. + struct SResource *member = NULL; + struct UString *tokenValue; + struct UString comment; + enum ETokenType token; + char subtag[1024]; + UnicodeString rules; + UBool haveRules = FALSE; + UVersionInfo version; + uint32_t line; + + /* '{' . (name resource)* '}' */ + version[0]=0; version[1]=0; version[2]=0; version[3]=0; + + for (;;) + { + ustr_init(&comment); + token = getToken(state, &tokenValue, &comment, &line, status); + + if (token == TOK_CLOSE_BRACE) + { + break; + } + + if (token != TOK_STRING) + { + res_close(result); + *status = U_INVALID_FORMAT_ERROR; + + if (token == TOK_EOF) + { + error(startline, "unterminated table"); + } + else + { + error(line, "Unexpected token %s", tokenNames[token]); + } + + return NULL; + } + + u_UCharsToChars(tokenValue->fChars, subtag, u_strlen(tokenValue->fChars) + 1); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + member = parseResource(state, subtag, NULL, status); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + if (result == NULL) + { + // Ignore the parsed resources, continue parsing. + } + else if (uprv_strcmp(subtag, "Version") == 0 && member->isString()) + { + StringResource *sr = static_cast<StringResource *>(member); + char ver[40]; + int32_t length = sr->length(); + + if (length >= UPRV_LENGTHOF(ver)) + { + length = UPRV_LENGTHOF(ver) - 1; + } + + sr->fString.extract(0, length, ver, UPRV_LENGTHOF(ver), US_INV); + u_versionFromString(version, ver); + + result->add(member, line, *status); + member = NULL; + } + else if(uprv_strcmp(subtag, "%%CollationBin")==0) + { + /* discard duplicate %%CollationBin if any*/ + } + else if (uprv_strcmp(subtag, "Sequence") == 0 && member->isString()) + { + StringResource *sr = static_cast<StringResource *>(member); + rules = sr->fString; + haveRules = TRUE; + // Defer building the collator until we have seen + // all sub-elements of the collation table, including the Version. + /* in order to achieve smaller data files, we can direct genrb */ + /* to omit collation rules */ + if(!state->omitCollationRules) { + result->add(member, line, *status); + member = NULL; + } + } + else // Just copy non-special items. + { + result->add(member, line, *status); + member = NULL; + } + res_close(member); // TODO: use LocalPointer + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + } + + if (!haveRules) { return result; } + +#if UCONFIG_NO_COLLATION || UCONFIG_NO_FILE_IO + warning(line, "Not building collation elements because of UCONFIG_NO_COLLATION and/or UCONFIG_NO_FILE_IO, see uconfig.h"); + (void)collationType; +#else + // CLDR ticket #3949, ICU ticket #8082: + // Do not build collation binary data for for-import-only "private" collation rule strings. + if (uprv_strncmp(collationType, "private-", 8) == 0) { + if(isVerbose()) { + printf("Not building %s~%s collation binary\n", state->filename, collationType); + } + return result; + } + + if(!state->makeBinaryCollation) { + if(isVerbose()) { + printf("Not building %s~%s collation binary\n", state->filename, collationType); + } + return result; + } + UErrorCode intStatus = U_ZERO_ERROR; + UParseError parseError; + uprv_memset(&parseError, 0, sizeof(parseError)); + GenrbImporter importer(state->inputdir, state->outputdir); + const icu::CollationTailoring *base = icu::CollationRoot::getRoot(intStatus); + if(U_FAILURE(intStatus)) { + error(line, "failed to load root collator (ucadata.icu) - %s", u_errorName(intStatus)); + res_close(result); + return NULL; // TODO: use LocalUResourceBundlePointer for result + } + icu::CollationBuilder builder(base, intStatus); + if(uprv_strncmp(collationType, "search", 6) == 0) { + builder.disableFastLatin(); // build fast-Latin table unless search collator + } + LocalPointer<icu::CollationTailoring> t( + builder.parseAndBuild(rules, version, &importer, &parseError, intStatus)); + if(U_FAILURE(intStatus)) { + const char *reason = builder.getErrorReason(); + if(reason == NULL) { reason = ""; } + error(line, "CollationBuilder failed at %s~%s/Sequence rule offset %ld: %s %s", + state->filename, collationType, + (long)parseError.offset, u_errorName(intStatus), reason); + if(parseError.preContext[0] != 0 || parseError.postContext[0] != 0) { + // Print pre- and post-context. + char preBuffer[100], postBuffer[100]; + escape(parseError.preContext, preBuffer); + escape(parseError.postContext, postBuffer); + error(line, " error context: \"...%s\" ! \"%s...\"", preBuffer, postBuffer); + } + if(isStrict() || t.isNull()) { + *status = intStatus; + res_close(result); + return NULL; + } + } + icu::LocalMemory<uint8_t> buffer; + int32_t capacity = 100000; + uint8_t *dest = buffer.allocateInsteadAndCopy(capacity); + if(dest == NULL) { + fprintf(stderr, "memory allocation (%ld bytes) for file contents failed\n", + (long)capacity); + *status = U_MEMORY_ALLOCATION_ERROR; + res_close(result); + return NULL; + } + int32_t indexes[icu::CollationDataReader::IX_TOTAL_SIZE + 1]; + int32_t totalSize = icu::CollationDataWriter::writeTailoring( + *t, *t->settings, indexes, dest, capacity, intStatus); + if(intStatus == U_BUFFER_OVERFLOW_ERROR) { + intStatus = U_ZERO_ERROR; + capacity = totalSize; + dest = buffer.allocateInsteadAndCopy(capacity); + if(dest == NULL) { + fprintf(stderr, "memory allocation (%ld bytes) for file contents failed\n", + (long)capacity); + *status = U_MEMORY_ALLOCATION_ERROR; + res_close(result); + return NULL; + } + totalSize = icu::CollationDataWriter::writeTailoring( + *t, *t->settings, indexes, dest, capacity, intStatus); + } + if(U_FAILURE(intStatus)) { + fprintf(stderr, "CollationDataWriter::writeTailoring() failed: %s\n", + u_errorName(intStatus)); + res_close(result); + return NULL; + } + if(isVerbose()) { + printf("%s~%s collation tailoring part sizes:\n", state->filename, collationType); + icu::CollationInfo::printSizes(totalSize, indexes); + if(t->settings->hasReordering()) { + printf("%s~%s collation reordering ranges:\n", state->filename, collationType); + icu::CollationInfo::printReorderRanges( + *t->data, t->settings->reorderCodes, t->settings->reorderCodesLength); + } + } + struct SResource *collationBin = bin_open(state->bundle, "%%CollationBin", totalSize, dest, NULL, NULL, status); + result->add(collationBin, line, *status); + if (U_FAILURE(*status)) { + res_close(result); + return NULL; + } +#endif + return result; +} + +static UBool +keepCollationType(const char * /*type*/) { + return TRUE; +} + +static struct SResource * +parseCollationElements(ParseState* state, char *tag, uint32_t startline, UBool newCollation, UErrorCode *status) +{ + TableResource *result = NULL; + struct SResource *member = NULL; + struct UString *tokenValue; + struct UString comment; + enum ETokenType token; + char subtag[1024], typeKeyword[1024]; + uint32_t line; + + result = table_open(state->bundle, tag, NULL, status); + + if (result == NULL || U_FAILURE(*status)) + { + return NULL; + } + if(isVerbose()){ + printf(" collation elements %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + if(!newCollation) { + return addCollation(state, result, "(no type)", startline, status); + } + else { + for(;;) { + ustr_init(&comment); + token = getToken(state, &tokenValue, &comment, &line, status); + + if (token == TOK_CLOSE_BRACE) + { + return result; + } + + if (token != TOK_STRING) + { + res_close(result); + *status = U_INVALID_FORMAT_ERROR; + + if (token == TOK_EOF) + { + error(startline, "unterminated table"); + } + else + { + error(line, "Unexpected token %s", tokenNames[token]); + } + + return NULL; + } + + u_UCharsToChars(tokenValue->fChars, subtag, u_strlen(tokenValue->fChars) + 1); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + if (uprv_strcmp(subtag, "default") == 0) + { + member = parseResource(state, subtag, NULL, status); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + result->add(member, line, *status); + } + else + { + token = peekToken(state, 0, &tokenValue, &line, &comment, status); + /* this probably needs to be refactored or recursively use the parser */ + /* first we assume that our collation table won't have the explicit type */ + /* then, we cannot handle aliases */ + if(token == TOK_OPEN_BRACE) { + token = getToken(state, &tokenValue, &comment, &line, status); + TableResource *collationRes; + if (keepCollationType(subtag)) { + collationRes = table_open(state->bundle, subtag, NULL, status); + } else { + collationRes = NULL; + } + // need to parse the collation data regardless + collationRes = addCollation(state, collationRes, subtag, startline, status); + if (collationRes != NULL) { + result->add(collationRes, startline, *status); + } + } else if(token == TOK_COLON) { /* right now, we'll just try to see if we have aliases */ + /* we could have a table too */ + token = peekToken(state, 1, &tokenValue, &line, &comment, status); + u_UCharsToChars(tokenValue->fChars, typeKeyword, u_strlen(tokenValue->fChars) + 1); + if(uprv_strcmp(typeKeyword, "alias") == 0) { + member = parseResource(state, subtag, NULL, status); + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + result->add(member, line, *status); + } else { + res_close(result); + *status = U_INVALID_FORMAT_ERROR; + return NULL; + } + } else { + res_close(result); + *status = U_INVALID_FORMAT_ERROR; + return NULL; + } + } + + /*member = string_open(bundle, subtag, tokenValue->fChars, tokenValue->fLength, status);*/ + + /*expect(TOK_CLOSE_BRACE, NULL, NULL, status);*/ + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + } + } +} + +/* Necessary, because CollationElements requires the bundle->fRoot member to be present which, + if this weren't special-cased, wouldn't be set until the entire file had been processed. */ +static struct SResource * +realParseTable(ParseState* state, TableResource *table, char *tag, uint32_t startline, UErrorCode *status) +{ + struct SResource *member = NULL; + struct UString *tokenValue=NULL; + struct UString comment; + enum ETokenType token; + char subtag[1024]; + uint32_t line; + UBool readToken = FALSE; + + /* '{' . (name resource)* '}' */ + + if(isVerbose()){ + printf(" parsing table %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + for (;;) + { + ustr_init(&comment); + token = getToken(state, &tokenValue, &comment, &line, status); + + if (token == TOK_CLOSE_BRACE) + { + if (!readToken) { + warning(startline, "Encountered empty table"); + } + return table; + } + + if (token != TOK_STRING) + { + *status = U_INVALID_FORMAT_ERROR; + + if (token == TOK_EOF) + { + error(startline, "unterminated table"); + } + else + { + error(line, "unexpected token %s", tokenNames[token]); + } + + return NULL; + } + + if(uprv_isInvariantUString(tokenValue->fChars, -1)) { + u_UCharsToChars(tokenValue->fChars, subtag, u_strlen(tokenValue->fChars) + 1); + } else { + *status = U_INVALID_FORMAT_ERROR; + error(line, "invariant characters required for table keys"); + return NULL; + } + + if (U_FAILURE(*status)) + { + error(line, "parse error. Stopped parsing tokens with %s", u_errorName(*status)); + return NULL; + } + + member = parseResource(state, subtag, &comment, status); + + if (member == NULL || U_FAILURE(*status)) + { + error(line, "parse error. Stopped parsing resource with %s", u_errorName(*status)); + return NULL; + } + + table->add(member, line, *status); + + if (U_FAILURE(*status)) + { + error(line, "parse error. Stopped parsing table with %s", u_errorName(*status)); + return NULL; + } + readToken = TRUE; + ustr_deinit(&comment); + } + + /* not reached */ + /* A compiler warning will appear if all paths don't contain a return statement. */ +/* *status = U_INTERNAL_PROGRAM_ERROR; + return NULL;*/ +} + +static struct SResource * +parseTable(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + if (tag != NULL && uprv_strcmp(tag, "CollationElements") == 0) + { + return parseCollationElements(state, tag, startline, FALSE, status); + } + if (tag != NULL && uprv_strcmp(tag, "collations") == 0) + { + return parseCollationElements(state, tag, startline, TRUE, status); + } + if(isVerbose()){ + printf(" table %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + TableResource *result = table_open(state->bundle, tag, comment, status); + + if (result == NULL || U_FAILURE(*status)) + { + return NULL; + } + return realParseTable(state, result, tag, startline, status); +} + +static struct SResource * +parseArray(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + struct SResource *member = NULL; + struct UString *tokenValue; + struct UString memberComments; + enum ETokenType token; + UBool readToken = FALSE; + + ArrayResource *result = array_open(state->bundle, tag, comment, status); + + if (result == NULL || U_FAILURE(*status)) + { + return NULL; + } + if(isVerbose()){ + printf(" array %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + ustr_init(&memberComments); + + /* '{' . resource [','] '}' */ + for (;;) + { + /* reset length */ + ustr_setlen(&memberComments, 0, status); + + /* check for end of array, but don't consume next token unless it really is the end */ + token = peekToken(state, 0, &tokenValue, NULL, &memberComments, status); + + + if (token == TOK_CLOSE_BRACE) + { + getToken(state, NULL, NULL, NULL, status); + if (!readToken) { + warning(startline, "Encountered empty array"); + } + break; + } + + if (token == TOK_EOF) + { + res_close(result); + *status = U_INVALID_FORMAT_ERROR; + error(startline, "unterminated array"); + return NULL; + } + + /* string arrays are a special case */ + if (token == TOK_STRING) + { + getToken(state, &tokenValue, &memberComments, NULL, status); + member = string_open(state->bundle, NULL, tokenValue->fChars, tokenValue->fLength, &memberComments, status); + } + else + { + member = parseResource(state, NULL, &memberComments, status); + } + + if (member == NULL || U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + result->add(member); + + /* eat optional comma if present */ + token = peekToken(state, 0, NULL, NULL, NULL, status); + + if (token == TOK_COMMA) + { + getToken(state, NULL, NULL, NULL, status); + } + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + readToken = TRUE; + } + + ustr_deinit(&memberComments); + return result; +} + +static struct SResource * +parseIntVector(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + enum ETokenType token; + char *string; + int32_t value; + UBool readToken = FALSE; + char *stopstring; + uint32_t len; + struct UString memberComments; + + IntVectorResource *result = intvector_open(state->bundle, tag, comment, status); + + if (result == NULL || U_FAILURE(*status)) + { + return NULL; + } + + if(isVerbose()){ + printf(" vector %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + ustr_init(&memberComments); + /* '{' . string [','] '}' */ + for (;;) + { + ustr_setlen(&memberComments, 0, status); + + /* check for end of array, but don't consume next token unless it really is the end */ + token = peekToken(state, 0, NULL, NULL,&memberComments, status); + + if (token == TOK_CLOSE_BRACE) + { + /* it's the end, consume the close brace */ + getToken(state, NULL, NULL, NULL, status); + if (!readToken) { + warning(startline, "Encountered empty int vector"); + } + ustr_deinit(&memberComments); + return result; + } + + string = getInvariantString(state, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + /* For handling illegal char in the Intvector */ + value = uprv_strtoul(string, &stopstring, 0);/* make intvector support decimal,hexdigit,octal digit ranging from -2^31-2^32-1*/ + len=(uint32_t)(stopstring-string); + + if(len==uprv_strlen(string)) + { + result->add(value, *status); + uprv_free(string); + token = peekToken(state, 0, NULL, NULL, NULL, status); + } + else + { + uprv_free(string); + *status=U_INVALID_CHAR_FOUND; + } + + if (U_FAILURE(*status)) + { + res_close(result); + return NULL; + } + + /* the comma is optional (even though it is required to prevent the reader from concatenating + consecutive entries) so that a missing comma on the last entry isn't an error */ + if (token == TOK_COMMA) + { + getToken(state, NULL, NULL, NULL, status); + } + readToken = TRUE; + } + + /* not reached */ + /* A compiler warning will appear if all paths don't contain a return statement. */ +/* intvector_close(result, status); + *status = U_INTERNAL_PROGRAM_ERROR; + return NULL;*/ +} + +static struct SResource * +parseBinary(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + uint32_t line; + LocalMemory<char> string(getInvariantString(state, &line, NULL, status)); + if (string.isNull() || U_FAILURE(*status)) + { + return NULL; + } + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + if (U_FAILURE(*status)) + { + return NULL; + } + + if(isVerbose()){ + printf(" binary %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + uint32_t count = (uint32_t)uprv_strlen(string.getAlias()); + if (count > 0){ + if((count % 2)==0){ + LocalMemory<uint8_t> value; + if (value.allocateInsteadAndCopy(count) == NULL) + { + *status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + + char toConv[3] = {'\0', '\0', '\0'}; + for (uint32_t i = 0; i < count; i += 2) + { + toConv[0] = string[i]; + toConv[1] = string[i + 1]; + + char *stopstring; + value[i >> 1] = (uint8_t) uprv_strtoul(toConv, &stopstring, 16); + uint32_t len=(uint32_t)(stopstring-toConv); + + if(len!=2) + { + *status=U_INVALID_CHAR_FOUND; + return NULL; + } + } + + return bin_open(state->bundle, tag, count >> 1, value.getAlias(), NULL, comment, status); + } + else + { + *status = U_INVALID_CHAR_FOUND; + error(line, "Encountered invalid binary value (length is odd)"); + return NULL; + } + } + else + { + warning(startline, "Encountered empty binary value"); + return bin_open(state->bundle, tag, 0, NULL, "", comment, status); + } +} + +static struct SResource * +parseInteger(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) +{ + struct SResource *result = NULL; + int32_t value; + char *string; + char *stopstring; + uint32_t len; + + string = getInvariantString(state, NULL, NULL, status); + + if (string == NULL || U_FAILURE(*status)) + { + return NULL; + } + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + uprv_free(string); + return NULL; + } + + if(isVerbose()){ + printf(" integer %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + if (uprv_strlen(string) <= 0) + { + warning(startline, "Encountered empty integer. Default value is 0."); + } + + /* Allow integer support for hexdecimal, octal digit and decimal*/ + /* and handle illegal char in the integer*/ + value = uprv_strtoul(string, &stopstring, 0); + len=(uint32_t)(stopstring-string); + if(len==uprv_strlen(string)) + { + result = int_open(state->bundle, tag, value, comment, status); + } + else + { + *status=U_INVALID_CHAR_FOUND; + } + uprv_free(string); + + return result; +} + +static struct SResource * +parseImport(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) +{ + uint32_t line; + LocalMemory<char> filename(getInvariantString(state, &line, NULL, status)); + if (U_FAILURE(*status)) + { + return NULL; + } + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + + if(isVerbose()){ + printf(" import %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + /* Open the input file for reading */ + CharString fullname; + if (state->inputdir != NULL) { + fullname.append(state->inputdir, *status); + } + fullname.appendPathPart(filename.getAlias(), *status); + if (U_FAILURE(*status)) { + return NULL; + } + + FileStream *file = T_FileStream_open(fullname.data(), "rb"); + if (file == NULL) + { + error(line, "couldn't open input file %s", filename.getAlias()); + *status = U_FILE_ACCESS_ERROR; + return NULL; + } + + int32_t len = T_FileStream_size(file); + LocalMemory<uint8_t> data; + if(data.allocateInsteadAndCopy(len) == NULL) + { + *status = U_MEMORY_ALLOCATION_ERROR; + T_FileStream_close (file); + return NULL; + } + + /* int32_t numRead = */ T_FileStream_read(file, data.getAlias(), len); + T_FileStream_close (file); + + return bin_open(state->bundle, tag, len, data.getAlias(), fullname.data(), comment, status); +} + +static struct SResource * +parseInclude(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) +{ + struct SResource *result; + int32_t len=0; + char *filename; + uint32_t line; + UChar *pTarget = NULL; + + UCHARBUF *ucbuf; + char *fullname = NULL; + int32_t count = 0; + const char* cp = NULL; + const UChar* uBuffer = NULL; + + filename = getInvariantString(state, &line, NULL, status); + count = (int32_t)uprv_strlen(filename); + + if (U_FAILURE(*status)) + { + return NULL; + } + + expect(state, TOK_CLOSE_BRACE, NULL, NULL, NULL, status); + + if (U_FAILURE(*status)) + { + uprv_free(filename); + return NULL; + } + + if(isVerbose()){ + printf(" include %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + fullname = (char *) uprv_malloc(state->inputdirLength + count + 2); + /* test for NULL */ + if(fullname == NULL) + { + *status = U_MEMORY_ALLOCATION_ERROR; + uprv_free(filename); + return NULL; + } + + if(state->inputdir!=NULL){ + if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) + { + + uprv_strcpy(fullname, state->inputdir); + + fullname[state->inputdirLength] = U_FILE_SEP_CHAR; + fullname[state->inputdirLength + 1] = '\0'; + + uprv_strcat(fullname, filename); + } + else + { + uprv_strcpy(fullname, state->inputdir); + uprv_strcat(fullname, filename); + } + }else{ + uprv_strcpy(fullname,filename); + } + + ucbuf = ucbuf_open(fullname, &cp,getShowWarning(),FALSE,status); + + if (U_FAILURE(*status)) { + error(line, "couldn't open input file %s\n", filename); + return NULL; + } + + uBuffer = ucbuf_getBuffer(ucbuf,&len,status); + result = string_open(state->bundle, tag, uBuffer, len, comment, status); + + ucbuf_close(ucbuf); + + uprv_free(pTarget); + + uprv_free(filename); + uprv_free(fullname); + + return result; +} + + + + + +U_STRING_DECL(k_type_string, "string", 6); +U_STRING_DECL(k_type_binary, "binary", 6); +U_STRING_DECL(k_type_bin, "bin", 3); +U_STRING_DECL(k_type_table, "table", 5); +U_STRING_DECL(k_type_table_no_fallback, "table(nofallback)", 17); +U_STRING_DECL(k_type_int, "int", 3); +U_STRING_DECL(k_type_integer, "integer", 7); +U_STRING_DECL(k_type_array, "array", 5); +U_STRING_DECL(k_type_alias, "alias", 5); +U_STRING_DECL(k_type_intvector, "intvector", 9); +U_STRING_DECL(k_type_import, "import", 6); +U_STRING_DECL(k_type_include, "include", 7); + +/* Various non-standard processing plugins that create one or more special resources. */ +U_STRING_DECL(k_type_plugin_uca_rules, "process(uca_rules)", 18); +U_STRING_DECL(k_type_plugin_collation, "process(collation)", 18); +U_STRING_DECL(k_type_plugin_transliterator, "process(transliterator)", 23); +U_STRING_DECL(k_type_plugin_dependency, "process(dependency)", 19); + +typedef enum EResourceType +{ + RESTYPE_UNKNOWN, + RESTYPE_STRING, + RESTYPE_BINARY, + RESTYPE_TABLE, + RESTYPE_TABLE_NO_FALLBACK, + RESTYPE_INTEGER, + RESTYPE_ARRAY, + RESTYPE_ALIAS, + RESTYPE_INTVECTOR, + RESTYPE_IMPORT, + RESTYPE_INCLUDE, + RESTYPE_PROCESS_UCA_RULES, + RESTYPE_PROCESS_COLLATION, + RESTYPE_PROCESS_TRANSLITERATOR, + RESTYPE_PROCESS_DEPENDENCY, + RESTYPE_RESERVED +} EResourceType; + +static struct { + const char *nameChars; /* only used for debugging */ + const UChar *nameUChars; + ParseResourceFunction *parseFunction; +} gResourceTypes[] = { + {"Unknown", NULL, NULL}, + {"string", k_type_string, parseString}, + {"binary", k_type_binary, parseBinary}, + {"table", k_type_table, parseTable}, + {"table(nofallback)", k_type_table_no_fallback, NULL}, /* parseFunction will never be called */ + {"integer", k_type_integer, parseInteger}, + {"array", k_type_array, parseArray}, + {"alias", k_type_alias, parseAlias}, + {"intvector", k_type_intvector, parseIntVector}, + {"import", k_type_import, parseImport}, + {"include", k_type_include, parseInclude}, + {"process(uca_rules)", k_type_plugin_uca_rules, parseUCARules}, + {"process(collation)", k_type_plugin_collation, NULL /* not implemented yet */}, + {"process(transliterator)", k_type_plugin_transliterator, parseTransliterator}, + {"process(dependency)", k_type_plugin_dependency, parseDependency}, + {"reserved", NULL, NULL} +}; + +void initParser() +{ + U_STRING_INIT(k_type_string, "string", 6); + U_STRING_INIT(k_type_binary, "binary", 6); + U_STRING_INIT(k_type_bin, "bin", 3); + U_STRING_INIT(k_type_table, "table", 5); + U_STRING_INIT(k_type_table_no_fallback, "table(nofallback)", 17); + U_STRING_INIT(k_type_int, "int", 3); + U_STRING_INIT(k_type_integer, "integer", 7); + U_STRING_INIT(k_type_array, "array", 5); + U_STRING_INIT(k_type_alias, "alias", 5); + U_STRING_INIT(k_type_intvector, "intvector", 9); + U_STRING_INIT(k_type_import, "import", 6); + U_STRING_INIT(k_type_include, "include", 7); + + U_STRING_INIT(k_type_plugin_uca_rules, "process(uca_rules)", 18); + U_STRING_INIT(k_type_plugin_collation, "process(collation)", 18); + U_STRING_INIT(k_type_plugin_transliterator, "process(transliterator)", 23); + U_STRING_INIT(k_type_plugin_dependency, "process(dependency)", 19); +} + +static inline UBool isTable(enum EResourceType type) { + return (UBool)(type==RESTYPE_TABLE || type==RESTYPE_TABLE_NO_FALLBACK); +} + +static enum EResourceType +parseResourceType(ParseState* state, UErrorCode *status) +{ + struct UString *tokenValue; + struct UString comment; + enum EResourceType result = RESTYPE_UNKNOWN; + uint32_t line=0; + ustr_init(&comment); + expect(state, TOK_STRING, &tokenValue, &comment, &line, status); + + if (U_FAILURE(*status)) + { + return RESTYPE_UNKNOWN; + } + + *status = U_ZERO_ERROR; + + /* Search for normal types */ + result=RESTYPE_UNKNOWN; + while ((result=(EResourceType)(result+1)) < RESTYPE_RESERVED) { + if (u_strcmp(tokenValue->fChars, gResourceTypes[result].nameUChars) == 0) { + break; + } + } + /* Now search for the aliases */ + if (u_strcmp(tokenValue->fChars, k_type_int) == 0) { + result = RESTYPE_INTEGER; + } + else if (u_strcmp(tokenValue->fChars, k_type_bin) == 0) { + result = RESTYPE_BINARY; + } + else if (result == RESTYPE_RESERVED) { + char tokenBuffer[1024]; + u_austrncpy(tokenBuffer, tokenValue->fChars, sizeof(tokenBuffer)); + tokenBuffer[sizeof(tokenBuffer) - 1] = 0; + *status = U_INVALID_FORMAT_ERROR; + error(line, "unknown resource type '%s'", tokenBuffer); + } + + return result; +} + +/* parse a non-top-level resource */ +static struct SResource * +parseResource(ParseState* state, char *tag, const struct UString *comment, UErrorCode *status) +{ + enum ETokenType token; + enum EResourceType resType = RESTYPE_UNKNOWN; + ParseResourceFunction *parseFunction = NULL; + struct UString *tokenValue; + uint32_t startline; + uint32_t line; + + + token = getToken(state, &tokenValue, NULL, &startline, status); + + if(isVerbose()){ + printf(" resource %s at line %i \n", (tag == NULL) ? "(null)" : tag, (int)startline); + } + + /* name . [ ':' type ] '{' resource '}' */ + /* This function parses from the colon onwards. If the colon is present, parse the + type then try to parse a resource of that type. If there is no explicit type, + work it out using the lookahead tokens. */ + switch (token) + { + case TOK_EOF: + *status = U_INVALID_FORMAT_ERROR; + error(startline, "Unexpected EOF encountered"); + return NULL; + + case TOK_ERROR: + *status = U_INVALID_FORMAT_ERROR; + return NULL; + + case TOK_COLON: + resType = parseResourceType(state, status); + expect(state, TOK_OPEN_BRACE, &tokenValue, NULL, &startline, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + + break; + + case TOK_OPEN_BRACE: + break; + + default: + *status = U_INVALID_FORMAT_ERROR; + error(startline, "syntax error while reading a resource, expected '{' or ':'"); + return NULL; + } + + + if (resType == RESTYPE_UNKNOWN) + { + /* No explicit type, so try to work it out. At this point, we've read the first '{'. + We could have any of the following: + { { => array (nested) + { :/} => array + { string , => string array + + { string { => table + + { string :/{ => table + { string } => string + */ + + token = peekToken(state, 0, NULL, &line, NULL,status); + + if (U_FAILURE(*status)) + { + return NULL; + } + + if (token == TOK_OPEN_BRACE || token == TOK_COLON ||token ==TOK_CLOSE_BRACE ) + { + resType = RESTYPE_ARRAY; + } + else if (token == TOK_STRING) + { + token = peekToken(state, 1, NULL, &line, NULL, status); + + if (U_FAILURE(*status)) + { + return NULL; + } + + switch (token) + { + case TOK_COMMA: resType = RESTYPE_ARRAY; break; + case TOK_OPEN_BRACE: resType = RESTYPE_TABLE; break; + case TOK_CLOSE_BRACE: resType = RESTYPE_STRING; break; + case TOK_COLON: resType = RESTYPE_TABLE; break; + default: + *status = U_INVALID_FORMAT_ERROR; + error(line, "Unexpected token after string, expected ',', '{' or '}'"); + return NULL; + } + } + else + { + *status = U_INVALID_FORMAT_ERROR; + error(line, "Unexpected token after '{'"); + return NULL; + } + + /* printf("Type guessed as %s\n", resourceNames[resType]); */ + } else if(resType == RESTYPE_TABLE_NO_FALLBACK) { + *status = U_INVALID_FORMAT_ERROR; + error(startline, "error: %s resource type not valid except on top bundle level", gResourceTypes[resType].nameChars); + return NULL; + } + + + /* We should now know what we need to parse next, so call the appropriate parser + function and return. */ + parseFunction = gResourceTypes[resType].parseFunction; + if (parseFunction != NULL) { + return parseFunction(state, tag, startline, comment, status); + } + else { + *status = U_INTERNAL_PROGRAM_ERROR; + error(startline, "internal error: %s resource type found and not handled", gResourceTypes[resType].nameChars); + } + + return NULL; +} + +/* parse the top-level resource */ +struct SRBRoot * +parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, const char *filename, + UBool makeBinaryCollation, UBool omitCollationRules, UErrorCode *status) +{ + struct UString *tokenValue; + struct UString comment; + uint32_t line; + enum EResourceType bundleType; + enum ETokenType token; + ParseState state; + uint32_t i; + + + for (i = 0; i < MAX_LOOKAHEAD + 1; i++) + { + ustr_init(&state.lookahead[i].value); + ustr_init(&state.lookahead[i].comment); + } + + initLookahead(&state, buf, status); + + state.inputdir = inputDir; + state.inputdirLength = (state.inputdir != NULL) ? (uint32_t)uprv_strlen(state.inputdir) : 0; + state.outputdir = outputDir; + state.outputdirLength = (state.outputdir != NULL) ? (uint32_t)uprv_strlen(state.outputdir) : 0; + state.filename = filename; + state.makeBinaryCollation = makeBinaryCollation; + state.omitCollationRules = omitCollationRules; + + ustr_init(&comment); + expect(&state, TOK_STRING, &tokenValue, &comment, NULL, status); + + state.bundle = new SRBRoot(&comment, FALSE, *status); + + if (state.bundle == NULL || U_FAILURE(*status)) + { + return NULL; + } + + + state.bundle->setLocale(tokenValue->fChars, *status); + + /* The following code is to make Empty bundle work no matter with :table specifer or not */ + token = getToken(&state, NULL, NULL, &line, status); + if(token==TOK_COLON) { + *status=U_ZERO_ERROR; + bundleType=parseResourceType(&state, status); + + if(isTable(bundleType)) + { + expect(&state, TOK_OPEN_BRACE, NULL, NULL, &line, status); + } + else + { + *status=U_PARSE_ERROR; + error(line, "parse error. Stopped parsing with %s", u_errorName(*status)); + } + } + else + { + /* not a colon */ + if(token==TOK_OPEN_BRACE) + { + *status=U_ZERO_ERROR; + bundleType=RESTYPE_TABLE; + } + else + { + /* neither colon nor open brace */ + *status=U_PARSE_ERROR; + bundleType=RESTYPE_UNKNOWN; + error(line, "parse error, did not find open-brace '{' or colon ':', stopped with %s", u_errorName(*status)); + } + } + + if (U_FAILURE(*status)) + { + delete state.bundle; + return NULL; + } + + if(bundleType==RESTYPE_TABLE_NO_FALLBACK) { + /* + * Parse a top-level table with the table(nofallback) declaration. + * This is the same as a regular table, but also sets the + * URES_ATT_NO_FALLBACK flag in indexes[URES_INDEX_ATTRIBUTES] . + */ + state.bundle->fNoFallback=TRUE; + } + /* top-level tables need not handle special table names like "collations" */ + assert(!state.bundle->fIsPoolBundle); + assert(state.bundle->fRoot->fType == URES_TABLE); + TableResource *rootTable = static_cast<TableResource *>(state.bundle->fRoot); + realParseTable(&state, rootTable, NULL, line, status); + if(dependencyArray!=NULL){ + rootTable->add(dependencyArray, 0, *status); + dependencyArray = NULL; + } + if (U_FAILURE(*status)) + { + delete state.bundle; + res_close(dependencyArray); + return NULL; + } + + if (getToken(&state, NULL, NULL, &line, status) != TOK_EOF) + { + warning(line, "extraneous text after resource bundle (perhaps unmatched braces)"); + if(isStrict()){ + *status = U_INVALID_FORMAT_ERROR; + return NULL; + } + } + + cleanupLookahead(&state); + ustr_deinit(&comment); + return state.bundle; +} diff --git a/intl/icu/source/tools/genrb/parse.h b/intl/icu/source/tools/genrb/parse.h new file mode 100644 index 000000000..d7341be6d --- /dev/null +++ b/intl/icu/source/tools/genrb/parse.h @@ -0,0 +1,38 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File parse.h +* +* Modification History: +* +* Date Name Description +* 05/26/99 stephen Creation. +******************************************************************************* +*/ + +#ifndef PARSE_H +#define PARSE_H 1 + +#include "unicode/utypes.h" +#include "filestrm.h" +#include "ucbuf.h" + +U_CDECL_BEGIN +/* One time parser initalisation */ +void initParser(); + +/* Parse a ResourceBundle text file */ +struct SRBRoot* parse(UCHARBUF *buf, const char* inputDir, const char* outputDir, + const char *filename, + UBool makeBinaryCollation, UBool omitCollationRules, UErrorCode *status); + +U_CDECL_END + +#endif diff --git a/intl/icu/source/tools/genrb/prscmnts.cpp b/intl/icu/source/tools/genrb/prscmnts.cpp new file mode 100644 index 000000000..03cad7f32 --- /dev/null +++ b/intl/icu/source/tools/genrb/prscmnts.cpp @@ -0,0 +1,248 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* + ******************************************************************************* + * Copyright (C) 2003-2014, International Business Machines + * Corporation and others. All Rights Reserved. + ******************************************************************************* + * + * File prscmnts.cpp + * + * Modification History: + * + * Date Name Description + * 08/22/2003 ram Creation. + ******************************************************************************* + */ + +// Safer use of UnicodeString. +#ifndef UNISTR_FROM_CHAR_EXPLICIT +# define UNISTR_FROM_CHAR_EXPLICIT explicit +#endif + +// Less important, but still a good idea. +#ifndef UNISTR_FROM_STRING_EXPLICIT +# define UNISTR_FROM_STRING_EXPLICIT explicit +#endif + +#include "unicode/regex.h" +#include "unicode/unistr.h" +#include "unicode/parseerr.h" +#include "prscmnts.h" +#include <stdio.h> +#include <stdlib.h> + +U_NAMESPACE_USE + +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when RegularExpressions not available */ + +#define MAX_SPLIT_STRINGS 20 + +const char *patternStrings[UPC_LIMIT]={ + "^translate\\s*(.*)", + "^note\\s*(.*)" +}; + +U_CFUNC int32_t +removeText(UChar *source, int32_t srcLen, + UnicodeString patString,uint32_t options, + UnicodeString replaceText, UErrorCode *status){ + + if(status == NULL || U_FAILURE(*status)){ + return 0; + } + + UnicodeString src(source, srcLen); + + RegexMatcher myMatcher(patString, src, options, *status); + if(U_FAILURE(*status)){ + return 0; + } + UnicodeString dest; + + + dest = myMatcher.replaceAll(replaceText,*status); + + + return dest.extract(source, srcLen, *status); + +} +U_CFUNC int32_t +trim(UChar *src, int32_t srcLen, UErrorCode *status){ + srcLen = removeText(src, srcLen, UnicodeString("^[ \\r\\n]+ "), 0, UnicodeString(), status); // remove leading new lines + srcLen = removeText(src, srcLen, UnicodeString("^\\s+"), 0, UnicodeString(), status); // remove leading spaces + srcLen = removeText(src, srcLen, UnicodeString("\\s+$"), 0, UnicodeString(), status); // remvoe trailing spcaes + return srcLen; +} + +U_CFUNC int32_t +removeCmtText(UChar* source, int32_t srcLen, UErrorCode* status){ + srcLen = trim(source, srcLen, status); + UnicodeString patString("^\\s*?\\*\\s*?"); // remove pattern like " * " at the begining of the line + srcLen = removeText(source, srcLen, patString, UREGEX_MULTILINE, UnicodeString(), status); + return removeText(source, srcLen, UnicodeString("[ \\r\\n]+"), 0, UnicodeString(" "), status);// remove new lines; +} + +U_CFUNC int32_t +getText(const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UnicodeString patternString, + UErrorCode* status){ + + if(status == NULL || U_FAILURE(*status)){ + return 0; + } + + UnicodeString stringArray[MAX_SPLIT_STRINGS]; + RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), 0, *status); + UnicodeString src (source,srcLen); + + if (U_FAILURE(*status)) { + return 0; + } + pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status); + + RegexMatcher matcher(patternString, UREGEX_DOTALL, *status); + if (U_FAILURE(*status)) { + return 0; + } + for(int32_t i=0; i<MAX_SPLIT_STRINGS; i++){ + matcher.reset(stringArray[i]); + if(matcher.lookingAt(*status)){ + UnicodeString out = matcher.group(1, *status); + + return out.extract(*dest, destCapacity,*status); + } + } + return 0; +} + + +#define AT_SIGN 0x0040 + +U_CFUNC int32_t +getDescription( const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status){ + if(status == NULL || U_FAILURE(*status)){ + return 0; + } + + UnicodeString stringArray[MAX_SPLIT_STRINGS]; + RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status); + UnicodeString src(source, srcLen); + + if (U_FAILURE(*status)) { + return 0; + } + pattern->split(src, stringArray,MAX_SPLIT_STRINGS , *status); + + if(stringArray[0].indexOf((UChar)AT_SIGN)==-1){ + int32_t destLen = stringArray[0].extract(*dest, destCapacity, *status); + return trim(*dest, destLen, status); + } + return 0; +} + +U_CFUNC int32_t +getCount(const UChar* source, int32_t srcLen, + UParseCommentsOption option, UErrorCode *status){ + + if(status == NULL || U_FAILURE(*status)){ + return 0; + } + + UnicodeString stringArray[MAX_SPLIT_STRINGS]; + RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status); + UnicodeString src (source, srcLen); + + + if (U_FAILURE(*status)) { + return 0; + } + int32_t retLen = pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status); + + UnicodeString patternString(patternStrings[option]); + RegexMatcher matcher(patternString, UREGEX_DOTALL, *status); + if (U_FAILURE(*status)) { + return 0; + } + int32_t count = 0; + for(int32_t i=0; i<retLen; i++){ + matcher.reset(stringArray[i]); + if(matcher.lookingAt(*status)){ + count++; + } + } + if(option == UPC_TRANSLATE && count > 1){ + fprintf(stderr, "Multiple @translate tags cannot be supported.\n"); + exit(U_UNSUPPORTED_ERROR); + } + return count; +} + +U_CFUNC int32_t +getAt(const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + int32_t index, + UParseCommentsOption option, + UErrorCode* status){ + + if(status == NULL || U_FAILURE(*status)){ + return 0; + } + + UnicodeString stringArray[MAX_SPLIT_STRINGS]; + RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status); + UnicodeString src (source, srcLen); + + + if (U_FAILURE(*status)) { + return 0; + } + int32_t retLen = pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status); + + UnicodeString patternString(patternStrings[option]); + RegexMatcher matcher(patternString, UREGEX_DOTALL, *status); + if (U_FAILURE(*status)) { + return 0; + } + int32_t count = 0; + for(int32_t i=0; i<retLen; i++){ + matcher.reset(stringArray[i]); + if(matcher.lookingAt(*status)){ + if(count == index){ + UnicodeString out = matcher.group(1, *status); + return out.extract(*dest, destCapacity,*status); + } + count++; + + } + } + return 0; + +} + +U_CFUNC int32_t +getTranslate( const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status){ + UnicodeString notePatternString("^translate\\s*?(.*)"); + + int32_t destLen = getText(source, srcLen, dest, destCapacity, notePatternString, status); + return trim(*dest, destLen, status); +} + +U_CFUNC int32_t +getNote(const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status){ + + UnicodeString notePatternString("^note\\s*?(.*)"); + int32_t destLen = getText(source, srcLen, dest, destCapacity, notePatternString, status); + return trim(*dest, destLen, status); + +} + +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ + diff --git a/intl/icu/source/tools/genrb/prscmnts.h b/intl/icu/source/tools/genrb/prscmnts.h new file mode 100644 index 000000000..7ceaa6c53 --- /dev/null +++ b/intl/icu/source/tools/genrb/prscmnts.h @@ -0,0 +1,66 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File read.h +* +* Modification History: +* +* Date Name Description +* 05/26/99 stephen Creation. +* 5/10/01 Ram removed ustdio dependency +******************************************************************************* +*/ + +#ifndef PRSCMNTS_H +#define PRSCMNTS_H 1 + +#include "unicode/utypes.h" + +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */ + +enum UParseCommentsOption { + UPC_TRANSLATE, + UPC_NOTE, + UPC_LIMIT +}; + +typedef enum UParseCommentsOption UParseCommentsOption; + +U_CFUNC int32_t +getNote(const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status); +U_CFUNC int32_t +removeCmtText(UChar* source, int32_t srcLen, UErrorCode* status); + +U_CFUNC int32_t +getDescription( const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status); +U_CFUNC int32_t +getTranslate( const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + UErrorCode* status); + +U_CFUNC int32_t +getAt(const UChar* source, int32_t srcLen, + UChar** dest, int32_t destCapacity, + int32_t index, + UParseCommentsOption option, + UErrorCode* status); + +U_CFUNC int32_t +getCount(const UChar* source, int32_t srcLen, + UParseCommentsOption option, UErrorCode *status); + +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ + +#endif + diff --git a/intl/icu/source/tools/genrb/rbutil.c b/intl/icu/source/tools/genrb/rbutil.c new file mode 100644 index 000000000..d3882c452 --- /dev/null +++ b/intl/icu/source/tools/genrb/rbutil.c @@ -0,0 +1,113 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2008, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File util.c +* +* Modification History: +* +* Date Name Description +* 06/10/99 stephen Creation. +* 02/07/08 Spieth Correct XLIFF generation on EBCDIC platform +* +******************************************************************************* +*/ + +#include "unicode/putil.h" +#include "rbutil.h" +#include "cmemory.h" +#include "cstring.h" + + +/* go from "/usr/local/include/curses.h" to "/usr/local/include" */ +void +get_dirname(char *dirname, + const char *filename) +{ + const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1; + + if(lastSlash>filename) { + uprv_strncpy(dirname, filename, (lastSlash - filename)); + *(dirname + (lastSlash - filename)) = '\0'; + } else { + *dirname = '\0'; + } +} + +/* go from "/usr/local/include/curses.h" to "curses" */ +void +get_basename(char *basename, + const char *filename) +{ + /* strip off any leading directory portions */ + const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1; + char *lastDot; + + if(lastSlash>filename) { + uprv_strcpy(basename, lastSlash); + } else { + uprv_strcpy(basename, filename); + } + + /* strip off any suffix */ + lastDot = uprv_strrchr(basename, '.'); + + if(lastDot != NULL) { + *lastDot = '\0'; + } +} + +#define MAX_DIGITS 10 +int32_t +itostr(char * buffer, int32_t i, uint32_t radix, int32_t pad) +{ + const char digits[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; + int32_t length = 0; + int32_t num = 0; + int32_t save = i; + int digit; + int32_t j; + char temp; + + /* if i is negative make it positive */ + if(i<0){ + i=-i; + } + + do{ + digit = (int)(i % radix); + buffer[length++]= digits[digit]; + i=i/radix; + } while(i); + + while (length < pad){ + buffer[length++] = '0';/*zero padding */ + } + + /* if i is negative add the negative sign */ + if(save < 0){ + buffer[length++]='-'; + } + + /* null terminate the buffer */ + if(length<MAX_DIGITS){ + buffer[length] = 0x0000; + } + + num= (pad>=length) ? pad :length; + + + /* Reverses the string */ + for (j = 0; j < (num / 2); j++){ + temp = buffer[(length-1) - j]; + buffer[(length-1) - j] = buffer[j]; + buffer[j] = temp; + } + return length; +} diff --git a/intl/icu/source/tools/genrb/rbutil.h b/intl/icu/source/tools/genrb/rbutil.h new file mode 100644 index 000000000..d2a303516 --- /dev/null +++ b/intl/icu/source/tools/genrb/rbutil.h @@ -0,0 +1,33 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File rbutil.h +* +* Modification History: +* +* Date Name Description +* 06/10/99 stephen Creation. +******************************************************************************* +*/ + +#ifndef UTIL_H +#define UTIL_H 1 + +#include "unicode/utypes.h" + +U_CDECL_BEGIN + +void get_dirname(char *dirname, const char *filename); +void get_basename(char *basename, const char *filename); +int32_t itostr(char * buffer, int32_t i, uint32_t radix, int32_t pad); + +U_CDECL_END + +#endif /* ! UTIL_H */ diff --git a/intl/icu/source/tools/genrb/read.c b/intl/icu/source/tools/genrb/read.c new file mode 100644 index 000000000..468c27e07 --- /dev/null +++ b/intl/icu/source/tools/genrb/read.c @@ -0,0 +1,476 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2012, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File read.c +* +* Modification History: +* +* Date Name Description +* 05/26/99 stephen Creation. +* 5/10/01 Ram removed ustdio dependency +******************************************************************************* +*/ + +#include "read.h" +#include "errmsg.h" +#include "unicode/ustring.h" +#include "unicode/utf16.h" + +#define OPENBRACE 0x007B +#define CLOSEBRACE 0x007D +#define COMMA 0x002C +#define QUOTE 0x0022 +#define ESCAPE 0x005C +#define SLASH 0x002F +#define ASTERISK 0x002A +#define SPACE 0x0020 +#define COLON 0x003A +#define BADBOM 0xFFFE +#define CR 0x000D +#define LF 0x000A + +static int32_t lineCount; + +/* Protos */ +static enum ETokenType getStringToken(UCHARBUF *buf, + UChar32 initialChar, + struct UString *token, + UErrorCode *status); + +static UChar32 getNextChar (UCHARBUF *buf, UBool skipwhite, struct UString *token, UErrorCode *status); +static void seekUntilNewline (UCHARBUF *buf, struct UString *token, UErrorCode *status); +static void seekUntilEndOfComment (UCHARBUF *buf, struct UString *token, UErrorCode *status); +static UBool isWhitespace (UChar32 c); +static UBool isNewline (UChar32 c); + +U_CFUNC void resetLineNumber() { + lineCount = 1; +} + +/* Read and return the next token from the stream. If the token is of + type eString, fill in the token parameter with the token. If the + token is eError, then the status parameter will contain the + specific error. This will be eItemNotFound at the end of file, + indicating that all tokens have been returned. This method will + never return eString twice in a row; instead, multiple adjacent + string tokens will be merged into one, with no intervening + space. */ +U_CFUNC enum ETokenType +getNextToken(UCHARBUF* buf, + struct UString *token, + uint32_t *linenumber, /* out: linenumber of token */ + struct UString *comment, + UErrorCode *status) { + enum ETokenType result; + UChar32 c; + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + /* Skip whitespace */ + c = getNextChar(buf, TRUE, comment, status); + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + *linenumber = lineCount; + + switch(c) { + case BADBOM: + return TOK_ERROR; + case OPENBRACE: + return TOK_OPEN_BRACE; + case CLOSEBRACE: + return TOK_CLOSE_BRACE; + case COMMA: + return TOK_COMMA; + case U_EOF: + return TOK_EOF; + case COLON: + return TOK_COLON; + + default: + result = getStringToken(buf, c, token, status); + } + + *linenumber = lineCount; + return result; +} + +/* Copy a string token into the given UnicodeString. Upon entry, we + have already read the first character of the string token, which is + not a whitespace character (but may be a QUOTE or ESCAPE). This + function reads all subsequent characters that belong with this + string, and copy them into the token parameter. The other + important, and slightly convoluted purpose of this function is to + merge adjacent strings. It looks forward a bit, and if the next + non comment, non whitespace item is a string, it reads it in as + well. If two adjacent strings are quoted, they are merged without + intervening space. Otherwise a single SPACE character is + inserted. */ +static enum ETokenType getStringToken(UCHARBUF* buf, + UChar32 initialChar, + struct UString *token, + UErrorCode *status) { + UBool lastStringWasQuoted; + UChar32 c; + UChar target[3] = { '\0' }; + UChar *pTarget = target; + int len=0; + UBool isFollowingCharEscaped=FALSE; + UBool isNLUnescaped = FALSE; + UChar32 prevC=0; + + /* We are guaranteed on entry that initialChar is not a whitespace + character. If we are at the EOF, or have some other problem, it + doesn't matter; we still want to validly return the initialChar + (if nothing else) as a string token. */ + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + /* setup */ + lastStringWasQuoted = FALSE; + c = initialChar; + ustr_setlen(token, 0, status); + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + for (;;) { + if (c == QUOTE) { + if (!lastStringWasQuoted && token->fLength > 0) { + ustr_ucat(token, SPACE, status); + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + } + + lastStringWasQuoted = TRUE; + + for (;;) { + c = ucbuf_getc(buf,status); + + /* EOF reached */ + if (c == U_EOF) { + return TOK_EOF; + } + + /* Unterminated quoted strings */ + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + if (c == QUOTE && !isFollowingCharEscaped) { + break; + } + + if (c == ESCAPE && !isFollowingCharEscaped) { + pTarget = target; + c = unescape(buf, status); + + if (c == U_ERR) { + return TOK_ERROR; + } + if(c == CR || c == LF){ + isNLUnescaped = TRUE; + } + } + + if(c==ESCAPE && !isFollowingCharEscaped){ + isFollowingCharEscaped = TRUE; + }else{ + U_APPEND_CHAR32(c, pTarget,len); + pTarget = target; + ustr_uscat(token, pTarget,len, status); + isFollowingCharEscaped = FALSE; + len=0; + if(c == CR || c == LF){ + if(isNLUnescaped == FALSE && prevC!=CR){ + lineCount++; + } + isNLUnescaped = FALSE; + } + } + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + prevC = c; + } + } else { + if (token->fLength > 0) { + ustr_ucat(token, SPACE, status); + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + } + + if(lastStringWasQuoted){ + if(getShowWarning()){ + warning(lineCount, "Mixing quoted and unquoted strings"); + } + if(isStrict()){ + return TOK_ERROR; + } + + } + + lastStringWasQuoted = FALSE; + + /* if we reach here we are mixing + * quoted and unquoted strings + * warn in normal mode and error in + * pedantic mode + */ + + if (c == ESCAPE) { + pTarget = target; + c = unescape(buf, status); + + /* EOF reached */ + if (c == U_EOF) { + return TOK_ERROR; + } + } + + U_APPEND_CHAR32(c, pTarget,len); + pTarget = target; + ustr_uscat(token, pTarget,len, status); + len=0; + + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + + for (;;) { + /* DON'T skip whitespace */ + c = getNextChar(buf, FALSE, NULL, status); + + /* EOF reached */ + if (c == U_EOF) { + ucbuf_ungetc(c, buf); + return TOK_STRING; + } + + if (U_FAILURE(*status)) { + return TOK_STRING; + } + + if (c == QUOTE + || c == OPENBRACE + || c == CLOSEBRACE + || c == COMMA + || c == COLON) { + ucbuf_ungetc(c, buf); + break; + } + + if (isWhitespace(c)) { + break; + } + + if (c == ESCAPE) { + pTarget = target; + c = unescape(buf, status); + + if (c == U_ERR) { + return TOK_ERROR; + } + } + + U_APPEND_CHAR32(c, pTarget,len); + pTarget = target; + ustr_uscat(token, pTarget,len, status); + len=0; + if (U_FAILURE(*status)) { + return TOK_ERROR; + } + } + } + + /* DO skip whitespace */ + c = getNextChar(buf, TRUE, NULL, status); + + if (U_FAILURE(*status)) { + return TOK_STRING; + } + + if (c == OPENBRACE || c == CLOSEBRACE || c == COMMA || c == COLON) { + ucbuf_ungetc(c, buf); + return TOK_STRING; + } + } +} + +/* Retrieve the next character. If skipwhite is + true, whitespace is skipped as well. */ +static UChar32 getNextChar(UCHARBUF* buf, + UBool skipwhite, + struct UString *token, + UErrorCode *status) { + UChar32 c, c2; + + if (U_FAILURE(*status)) { + return U_EOF; + } + + for (;;) { + c = ucbuf_getc(buf,status); + + if (c == U_EOF) { + return U_EOF; + } + + if (skipwhite && isWhitespace(c)) { + continue; + } + + /* This also handles the get() failing case */ + if (c != SLASH) { + return c; + } + + c = ucbuf_getc(buf,status); /* "/c" */ + + if (c == U_EOF) { + return U_EOF; + } + + switch (c) { + case SLASH: /* "//" */ + seekUntilNewline(buf, NULL, status); + break; + + case ASTERISK: /* " / * " */ + c2 = ucbuf_getc(buf, status); /* "/ * c" */ + if(c2 == ASTERISK){ /* "/ * *" */ + /* parse multi-line comment and store it in token*/ + seekUntilEndOfComment(buf, token, status); + } else { + ucbuf_ungetc(c2, buf); /* c2 is the non-asterisk following "/ *". Include c2 back in buffer. */ + seekUntilEndOfComment(buf, NULL, status); + } + break; + + default: + ucbuf_ungetc(c, buf); /* "/c" - put back the c */ + /* If get() failed this is a NOP */ + return SLASH; + } + + } +} + +static void seekUntilNewline(UCHARBUF* buf, + struct UString *token, + UErrorCode *status) { + UChar32 c; + + if (U_FAILURE(*status)) { + return; + } + + do { + c = ucbuf_getc(buf,status); + /* add the char to token */ + if(token!=NULL){ + ustr_u32cat(token, c, status); + } + } while (!isNewline(c) && c != U_EOF && *status == U_ZERO_ERROR); +} + +static void seekUntilEndOfComment(UCHARBUF *buf, + struct UString *token, + UErrorCode *status) { + UChar32 c, d; + uint32_t line; + + if (U_FAILURE(*status)) { + return; + } + + line = lineCount; + + do { + c = ucbuf_getc(buf, status); + + if (c == ASTERISK) { + d = ucbuf_getc(buf, status); + + if (d != SLASH) { + ucbuf_ungetc(d, buf); + } else { + break; + } + } + /* add the char to token */ + if(token!=NULL){ + ustr_u32cat(token, c, status); + } + /* increment the lineCount */ + isNewline(c); + + } while (c != U_EOF && *status == U_ZERO_ERROR); + + if (c == U_EOF) { + *status = U_INVALID_FORMAT_ERROR; + error(line, "unterminated comment detected"); + } +} + +U_CFUNC UChar32 unescape(UCHARBUF *buf, UErrorCode *status) { + if (U_FAILURE(*status)) { + return U_EOF; + } + + /* We expect to be called after the ESCAPE has been seen, but + * u_fgetcx needs an ESCAPE to do its magic. */ + ucbuf_ungetc(ESCAPE, buf); + + return ucbuf_getcx32(buf, status); +} + +static UBool isWhitespace(UChar32 c) { + switch (c) { + /* ' ', '\t', '\n', '\r', 0x2029, 0xFEFF */ + case 0x000A: + case 0x2029: + lineCount++; + case 0x000D: + case 0x0020: + case 0x0009: + case 0xFEFF: + return TRUE; + + default: + return FALSE; + } +} + +static UBool isNewline(UChar32 c) { + switch (c) { + /* '\n', '\r', 0x2029 */ + case 0x000A: + case 0x2029: + lineCount++; + case 0x000D: + return TRUE; + + default: + return FALSE; + } +} diff --git a/intl/icu/source/tools/genrb/read.h b/intl/icu/source/tools/genrb/read.h new file mode 100644 index 000000000..74b8c823a --- /dev/null +++ b/intl/icu/source/tools/genrb/read.h @@ -0,0 +1,54 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2011, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File read.h +* +* Modification History: +* +* Date Name Description +* 05/26/99 stephen Creation. +* 5/10/01 Ram removed ustdio dependency +******************************************************************************* +*/ + +#ifndef READ_H +#define READ_H 1 + +#include "unicode/utypes.h" +#include "ustr.h" +#include "ucbuf.h" + +/* The types of tokens which may be returned by getNextToken. + NOTE: Keep these in sync with tokenNames in parse.c */ +enum ETokenType +{ + TOK_STRING, /* A string token, such as "MonthNames" */ + TOK_OPEN_BRACE, /* An opening brace character */ + TOK_CLOSE_BRACE, /* A closing brace character */ + TOK_COMMA, /* A comma */ + TOK_COLON, /* A colon */ + + TOK_EOF, /* End of the file has been reached successfully */ + TOK_ERROR, /* An error, such an unterminated quoted string */ + TOK_TOKEN_COUNT /* Number of "real" token types */ +}; + +U_CFUNC UChar32 unescape(UCHARBUF *buf, UErrorCode *status); + +U_CFUNC void resetLineNumber(void); + +U_CFUNC enum ETokenType +getNextToken(UCHARBUF *buf, + struct UString *token, + uint32_t *linenumber, /* out: linenumber of token */ + struct UString *comment, + UErrorCode *status); + +#endif diff --git a/intl/icu/source/tools/genrb/reslist.cpp b/intl/icu/source/tools/genrb/reslist.cpp new file mode 100644 index 000000000..9420184a9 --- /dev/null +++ b/intl/icu/source/tools/genrb/reslist.cpp @@ -0,0 +1,1693 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2000-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File reslist.cpp +* +* Modification History: +* +* Date Name Description +* 02/21/00 weiv Creation. +******************************************************************************* +*/ + +// Safer use of UnicodeString. +#ifndef UNISTR_FROM_CHAR_EXPLICIT +# define UNISTR_FROM_CHAR_EXPLICIT explicit +#endif + +// Less important, but still a good idea. +#ifndef UNISTR_FROM_STRING_EXPLICIT +# define UNISTR_FROM_STRING_EXPLICIT explicit +#endif + +#include <assert.h> +#include <stdio.h> +#include "unicode/localpointer.h" +#include "reslist.h" +#include "unewdata.h" +#include "unicode/ures.h" +#include "unicode/putil.h" +#include "errmsg.h" + +#include "uarrsort.h" +#include "uelement.h" +#include "uhash.h" +#include "uinvchar.h" +#include "ustr_imp.h" +#include "unicode/utf16.h" +/* + * Align binary data at a 16-byte offset from the start of the resource bundle, + * to be safe for any data type it may contain. + */ +#define BIN_ALIGNMENT 16 + +// This numeric constant must be at least 1. +// If StringResource.fNumUnitsSaved == 0 then the string occurs only once, +// and it makes no sense to move it to the pool bundle. +// The larger the threshold for fNumUnitsSaved +// the smaller the savings, and the smaller the pool bundle. +// We trade some total size reduction to reduce the pool bundle a bit, +// so that one can reasonably save data size by +// removing bundle files without rebuilding the pool bundle. +// This can also help to keep the pool and total (pool+local) string indexes +// within 16 bits, that is, within range of Table16 and Array16 containers. +#ifndef GENRB_MIN_16BIT_UNITS_SAVED_FOR_POOL_STRING +# define GENRB_MIN_16BIT_UNITS_SAVED_FOR_POOL_STRING 10 +#endif + +U_NAMESPACE_USE + +static UBool gIncludeCopyright = FALSE; +static UBool gUsePoolBundle = FALSE; +static UBool gIsDefaultFormatVersion = TRUE; +static int32_t gFormatVersion = 3; + +/* How do we store string values? */ +enum { + STRINGS_UTF16_V1, /* formatVersion 1: int length + UChars + NUL + padding to 4 bytes */ + STRINGS_UTF16_V2 /* formatVersion 2 & up: optional length in 1..3 UChars + UChars + NUL */ +}; + +static const int32_t MAX_IMPLICIT_STRING_LENGTH = 40; /* do not store the length explicitly for such strings */ + +static const ResFile kNoPoolBundle; + +/* + * res_none() returns the address of kNoResource, + * for use in non-error cases when no resource is to be added to the bundle. + * (NULL is used in error cases.) + */ +static SResource kNoResource; // TODO: const + +static UDataInfo dataInfo= { + sizeof(UDataInfo), + 0, + + U_IS_BIG_ENDIAN, + U_CHARSET_FAMILY, + sizeof(UChar), + 0, + + {0x52, 0x65, 0x73, 0x42}, /* dataFormat="ResB" */ + {1, 3, 0, 0}, /* formatVersion */ + {1, 4, 0, 0} /* dataVersion take a look at version inside parsed resb*/ +}; + +static const UVersionInfo gFormatVersions[4] = { /* indexed by a major-formatVersion integer */ + { 0, 0, 0, 0 }, + { 1, 3, 0, 0 }, + { 2, 0, 0, 0 }, + { 3, 0, 0, 0 } +}; +// Remember to update genrb.h GENRB_VERSION when changing the data format. +// (Or maybe we should remove GENRB_VERSION and report the ICU version number?) + +static uint8_t calcPadding(uint32_t size) { + /* returns space we need to pad */ + return (uint8_t) ((size % sizeof(uint32_t)) ? (sizeof(uint32_t) - (size % sizeof(uint32_t))) : 0); + +} + +void setIncludeCopyright(UBool val){ + gIncludeCopyright=val; +} + +UBool getIncludeCopyright(void){ + return gIncludeCopyright; +} + +void setFormatVersion(int32_t formatVersion) { + gIsDefaultFormatVersion = FALSE; + gFormatVersion = formatVersion; +} + +int32_t getFormatVersion() { + return gFormatVersion; +} + +void setUsePoolBundle(UBool use) { + gUsePoolBundle = use; +} + +// TODO: return const pointer, or find another way to express "none" +struct SResource* res_none() { + return &kNoResource; +} + +SResource::SResource() + : fType(URES_NONE), fWritten(FALSE), fRes(RES_BOGUS), fRes16(-1), fKey(-1), fKey16(-1), + line(0), fNext(NULL) { + ustr_init(&fComment); +} + +SResource::SResource(SRBRoot *bundle, const char *tag, int8_t type, const UString* comment, + UErrorCode &errorCode) + : fType(type), fWritten(FALSE), fRes(RES_BOGUS), fRes16(-1), + fKey(bundle != NULL ? bundle->addTag(tag, errorCode) : -1), fKey16(-1), + line(0), fNext(NULL) { + ustr_init(&fComment); + if(comment != NULL) { + ustr_cpy(&fComment, comment, &errorCode); + } +} + +SResource::~SResource() { + ustr_deinit(&fComment); +} + +ContainerResource::~ContainerResource() { + SResource *current = fFirst; + while (current != NULL) { + SResource *next = current->fNext; + delete current; + current = next; + } +} + +TableResource::~TableResource() {} + +// TODO: clarify that containers adopt new items, even in error cases; use LocalPointer +void TableResource::add(SResource *res, int linenumber, UErrorCode &errorCode) { + if (U_FAILURE(errorCode) || res == NULL || res == &kNoResource) { + return; + } + + /* remember this linenumber to report to the user if there is a duplicate key */ + res->line = linenumber; + + /* here we need to traverse the list */ + ++fCount; + + /* is the list still empty? */ + if (fFirst == NULL) { + fFirst = res; + res->fNext = NULL; + return; + } + + const char *resKeyString = fRoot->fKeys + res->fKey; + + SResource *current = fFirst; + + SResource *prev = NULL; + while (current != NULL) { + const char *currentKeyString = fRoot->fKeys + current->fKey; + int diff; + /* + * formatVersion 1: compare key strings in native-charset order + * formatVersion 2 and up: compare key strings in ASCII order + */ + if (gFormatVersion == 1 || U_CHARSET_FAMILY == U_ASCII_FAMILY) { + diff = uprv_strcmp(currentKeyString, resKeyString); + } else { + diff = uprv_compareInvCharsAsAscii(currentKeyString, resKeyString); + } + if (diff < 0) { + prev = current; + current = current->fNext; + } else if (diff > 0) { + /* we're either in front of the list, or in the middle */ + if (prev == NULL) { + /* front of the list */ + fFirst = res; + } else { + /* middle of the list */ + prev->fNext = res; + } + + res->fNext = current; + return; + } else { + /* Key already exists! ERROR! */ + error(linenumber, "duplicate key '%s' in table, first appeared at line %d", currentKeyString, current->line); + errorCode = U_UNSUPPORTED_ERROR; + return; + } + } + + /* end of list */ + prev->fNext = res; + res->fNext = NULL; +} + +ArrayResource::~ArrayResource() {} + +void ArrayResource::add(SResource *res) { + if (res != NULL && res != &kNoResource) { + if (fFirst == NULL) { + fFirst = res; + } else { + fLast->fNext = res; + } + fLast = res; + ++fCount; + } +} + +PseudoListResource::~PseudoListResource() {} + +void PseudoListResource::add(SResource *res) { + if (res != NULL && res != &kNoResource) { + res->fNext = fFirst; + fFirst = res; + ++fCount; + } +} + +StringBaseResource::StringBaseResource(SRBRoot *bundle, const char *tag, int8_t type, + const UChar *value, int32_t len, + const UString* comment, UErrorCode &errorCode) + : SResource(bundle, tag, type, comment, errorCode) { + if (len == 0 && gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(type); + fWritten = TRUE; + return; + } + + fString.setTo(value, len); + fString.getTerminatedBuffer(); // Some code relies on NUL-termination. + if (U_SUCCESS(errorCode) && fString.isBogus()) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + } +} + +StringBaseResource::StringBaseResource(SRBRoot *bundle, int8_t type, + const icu::UnicodeString &value, UErrorCode &errorCode) + : SResource(bundle, NULL, type, NULL, errorCode), fString(value) { + if (value.isEmpty() && gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(type); + fWritten = TRUE; + return; + } + + fString.getTerminatedBuffer(); // Some code relies on NUL-termination. + if (U_SUCCESS(errorCode) && fString.isBogus()) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + } +} + +// Pool bundle string, alias the buffer. Guaranteed NUL-terminated and not empty. +StringBaseResource::StringBaseResource(int8_t type, const UChar *value, int32_t len, + UErrorCode &errorCode) + : SResource(NULL, NULL, type, NULL, errorCode), fString(TRUE, value, len) { + assert(len > 0); + assert(!fString.isBogus()); +} + +StringBaseResource::~StringBaseResource() {} + +static int32_t U_CALLCONV +string_hash(const UElement key) { + const StringResource *res = static_cast<const StringResource *>(key.pointer); + return res->fString.hashCode(); +} + +static UBool U_CALLCONV +string_comp(const UElement key1, const UElement key2) { + const StringResource *res1 = static_cast<const StringResource *>(key1.pointer); + const StringResource *res2 = static_cast<const StringResource *>(key2.pointer); + return res1->fString == res2->fString; +} + +StringResource::~StringResource() {} + +AliasResource::~AliasResource() {} + +IntResource::IntResource(SRBRoot *bundle, const char *tag, int32_t value, + const UString* comment, UErrorCode &errorCode) + : SResource(bundle, tag, URES_INT, comment, errorCode) { + fValue = value; + fRes = URES_MAKE_RESOURCE(URES_INT, value & RES_MAX_OFFSET); + fWritten = TRUE; +} + +IntResource::~IntResource() {} + +IntVectorResource::IntVectorResource(SRBRoot *bundle, const char *tag, + const UString* comment, UErrorCode &errorCode) + : SResource(bundle, tag, URES_INT_VECTOR, comment, errorCode), + fCount(0), fArray(new uint32_t[RESLIST_MAX_INT_VECTOR]) { + if (fArray == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } +} + +IntVectorResource::~IntVectorResource() { + delete[] fArray; +} + +void IntVectorResource::add(int32_t value, UErrorCode &errorCode) { + if (U_SUCCESS(errorCode)) { + fArray[fCount++] = value; + } +} + +BinaryResource::BinaryResource(SRBRoot *bundle, const char *tag, + uint32_t length, uint8_t *data, const char* fileName, + const UString* comment, UErrorCode &errorCode) + : SResource(bundle, tag, URES_BINARY, comment, errorCode), + fLength(length), fData(NULL), fFileName(NULL) { + if (U_FAILURE(errorCode)) { + return; + } + if (fileName != NULL && *fileName != 0){ + fFileName = new char[uprv_strlen(fileName)+1]; + if (fFileName == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + uprv_strcpy(fFileName, fileName); + } + if (length > 0) { + fData = new uint8_t[length]; + if (fData == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + uprv_memcpy(fData, data, length); + } else { + if (gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(URES_BINARY); + fWritten = TRUE; + } + } +} + +BinaryResource::~BinaryResource() { + delete[] fData; + delete[] fFileName; +} + +/* Writing Functions */ + +void +StringResource::handlePreflightStrings(SRBRoot *bundle, UHashtable *stringSet, + UErrorCode &errorCode) { + assert(fSame == NULL); + fSame = static_cast<StringResource *>(uhash_get(stringSet, this)); + if (fSame != NULL) { + // This is a duplicate of a pool bundle string or of an earlier-visited string. + if (++fSame->fNumCopies == 1) { + assert(fSame->fWritten); + int32_t poolStringIndex = (int32_t)RES_GET_OFFSET(fSame->fRes); + if (poolStringIndex >= bundle->fPoolStringIndexLimit) { + bundle->fPoolStringIndexLimit = poolStringIndex + 1; + } + } + return; + } + /* Put this string into the set for finding duplicates. */ + fNumCopies = 1; + uhash_put(stringSet, this, this, &errorCode); + + if (bundle->fStringsForm != STRINGS_UTF16_V1) { + int32_t len = length(); + if (len <= MAX_IMPLICIT_STRING_LENGTH && + !U16_IS_TRAIL(fString[0]) && fString.indexOf((UChar)0) < 0) { + /* + * This string will be stored without an explicit length. + * Runtime will detect !U16_IS_TRAIL(s[0]) and call u_strlen(). + */ + fNumCharsForLength = 0; + } else if (len <= 0x3ee) { + fNumCharsForLength = 1; + } else if (len <= 0xfffff) { + fNumCharsForLength = 2; + } else { + fNumCharsForLength = 3; + } + bundle->f16BitStringsLength += fNumCharsForLength + len + 1; /* +1 for the NUL */ + } +} + +void +ContainerResource::handlePreflightStrings(SRBRoot *bundle, UHashtable *stringSet, + UErrorCode &errorCode) { + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + current->preflightStrings(bundle, stringSet, errorCode); + } +} + +void +SResource::preflightStrings(SRBRoot *bundle, UHashtable *stringSet, UErrorCode &errorCode) { + if (U_FAILURE(errorCode)) { + return; + } + if (fRes != RES_BOGUS) { + /* + * The resource item word was already precomputed, which means + * no further data needs to be written. + * This might be an integer, or an empty string/binary/etc. + */ + return; + } + handlePreflightStrings(bundle, stringSet, errorCode); +} + +void +SResource::handlePreflightStrings(SRBRoot * /*bundle*/, UHashtable * /*stringSet*/, + UErrorCode & /*errorCode*/) { + /* Neither a string nor a container. */ +} + +int32_t +SRBRoot::makeRes16(uint32_t resWord) const { + if (resWord == 0) { + return 0; /* empty string */ + } + uint32_t type = RES_GET_TYPE(resWord); + int32_t offset = (int32_t)RES_GET_OFFSET(resWord); + if (type == URES_STRING_V2) { + assert(offset > 0); + if (offset < fPoolStringIndexLimit) { + if (offset < fPoolStringIndex16Limit) { + return offset; + } + } else { + offset = offset - fPoolStringIndexLimit + fPoolStringIndex16Limit; + if (offset <= 0xffff) { + return offset; + } + } + } + return -1; +} + +int32_t +SRBRoot::mapKey(int32_t oldpos) const { + const KeyMapEntry *map = fKeyMap; + if (map == NULL) { + return oldpos; + } + int32_t i, start, limit; + + /* do a binary search for the old, pre-compactKeys() key offset */ + start = fUsePoolBundle->fKeysCount; + limit = start + fKeysCount; + while (start < limit - 1) { + i = (start + limit) / 2; + if (oldpos < map[i].oldpos) { + limit = i; + } else { + start = i; + } + } + assert(oldpos == map[start].oldpos); + return map[start].newpos; +} + +/* + * Only called for UTF-16 v1 strings and duplicate UTF-16 v2 strings. + * For unique UTF-16 v2 strings, write16() sees fRes != RES_BOGUS + * and exits early. + */ +void +StringResource::handleWrite16(SRBRoot * /*bundle*/) { + SResource *same; + if ((same = fSame) != NULL) { + /* This is a duplicate. */ + assert(same->fRes != RES_BOGUS && same->fWritten); + fRes = same->fRes; + fWritten = same->fWritten; + } +} + +void +ContainerResource::writeAllRes16(SRBRoot *bundle) { + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + bundle->f16BitUnits.append((UChar)current->fRes16); + } + fWritten = TRUE; +} + +void +ArrayResource::handleWrite16(SRBRoot *bundle) { + if (fCount == 0 && gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(URES_ARRAY); + fWritten = TRUE; + return; + } + + int32_t res16 = 0; + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + current->write16(bundle); + res16 |= current->fRes16; + } + if (fCount <= 0xffff && res16 >= 0 && gFormatVersion > 1) { + fRes = URES_MAKE_RESOURCE(URES_ARRAY16, bundle->f16BitUnits.length()); + bundle->f16BitUnits.append((UChar)fCount); + writeAllRes16(bundle); + } +} + +void +TableResource::handleWrite16(SRBRoot *bundle) { + if (fCount == 0 && gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(URES_TABLE); + fWritten = TRUE; + return; + } + /* Find the smallest table type that fits the data. */ + int32_t key16 = 0; + int32_t res16 = 0; + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + current->write16(bundle); + key16 |= current->fKey16; + res16 |= current->fRes16; + } + if(fCount > (uint32_t)bundle->fMaxTableLength) { + bundle->fMaxTableLength = fCount; + } + if (fCount <= 0xffff && key16 >= 0) { + if (res16 >= 0 && gFormatVersion > 1) { + /* 16-bit count, key offsets and values */ + fRes = URES_MAKE_RESOURCE(URES_TABLE16, bundle->f16BitUnits.length()); + bundle->f16BitUnits.append((UChar)fCount); + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + bundle->f16BitUnits.append((UChar)current->fKey16); + } + writeAllRes16(bundle); + } else { + /* 16-bit count, 16-bit key offsets, 32-bit values */ + fTableType = URES_TABLE; + } + } else { + /* 32-bit count, key offsets and values */ + fTableType = URES_TABLE32; + } +} + +void +PseudoListResource::handleWrite16(SRBRoot * /*bundle*/) { + fRes = URES_MAKE_EMPTY_RESOURCE(URES_TABLE); + fWritten = TRUE; +} + +void +SResource::write16(SRBRoot *bundle) { + if (fKey >= 0) { + // A tagged resource has a non-negative key index into the parsed key strings. + // compactKeys() built a map from parsed key index to the final key index. + // After the mapping, negative key indexes are used for shared pool bundle keys. + fKey = bundle->mapKey(fKey); + // If the key index fits into a Key16 for a Table or Table16, + // then set the fKey16 field accordingly. + // Otherwise keep it at -1. + if (fKey >= 0) { + if (fKey < bundle->fLocalKeyLimit) { + fKey16 = fKey; + } + } else { + int32_t poolKeyIndex = fKey & 0x7fffffff; + if (poolKeyIndex <= 0xffff) { + poolKeyIndex += bundle->fLocalKeyLimit; + if (poolKeyIndex <= 0xffff) { + fKey16 = poolKeyIndex; + } + } + } + } + /* + * fRes != RES_BOGUS: + * The resource item word was already precomputed, which means + * no further data needs to be written. + * This might be an integer, or an empty or UTF-16 v2 string, + * an empty binary, etc. + */ + if (fRes == RES_BOGUS) { + handleWrite16(bundle); + } + // Compute fRes16 for precomputed as well as just-computed fRes. + fRes16 = bundle->makeRes16(fRes); +} + +void +SResource::handleWrite16(SRBRoot * /*bundle*/) { + /* Only a few resource types write 16-bit units. */ +} + +/* + * Only called for UTF-16 v1 strings, and for aliases. + * For UTF-16 v2 strings, preWrite() sees fRes != RES_BOGUS + * and exits early. + */ +void +StringBaseResource::handlePreWrite(uint32_t *byteOffset) { + /* Write the UTF-16 v1 string. */ + fRes = URES_MAKE_RESOURCE(fType, *byteOffset >> 2); + *byteOffset += 4 + (length() + 1) * U_SIZEOF_UCHAR; +} + +void +IntVectorResource::handlePreWrite(uint32_t *byteOffset) { + if (fCount == 0 && gFormatVersion > 1) { + fRes = URES_MAKE_EMPTY_RESOURCE(URES_INT_VECTOR); + fWritten = TRUE; + } else { + fRes = URES_MAKE_RESOURCE(URES_INT_VECTOR, *byteOffset >> 2); + *byteOffset += (1 + fCount) * 4; + } +} + +void +BinaryResource::handlePreWrite(uint32_t *byteOffset) { + uint32_t pad = 0; + uint32_t dataStart = *byteOffset + sizeof(fLength); + + if (dataStart % BIN_ALIGNMENT) { + pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT); + *byteOffset += pad; /* pad == 4 or 8 or 12 */ + } + fRes = URES_MAKE_RESOURCE(URES_BINARY, *byteOffset >> 2); + *byteOffset += 4 + fLength; +} + +void +ContainerResource::preWriteAllRes(uint32_t *byteOffset) { + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + current->preWrite(byteOffset); + } +} + +void +ArrayResource::handlePreWrite(uint32_t *byteOffset) { + preWriteAllRes(byteOffset); + fRes = URES_MAKE_RESOURCE(URES_ARRAY, *byteOffset >> 2); + *byteOffset += (1 + fCount) * 4; +} + +void +TableResource::handlePreWrite(uint32_t *byteOffset) { + preWriteAllRes(byteOffset); + if (fTableType == URES_TABLE) { + /* 16-bit count, 16-bit key offsets, 32-bit values */ + fRes = URES_MAKE_RESOURCE(URES_TABLE, *byteOffset >> 2); + *byteOffset += 2 + fCount * 6; + } else { + /* 32-bit count, key offsets and values */ + fRes = URES_MAKE_RESOURCE(URES_TABLE32, *byteOffset >> 2); + *byteOffset += 4 + fCount * 8; + } +} + +void +SResource::preWrite(uint32_t *byteOffset) { + if (fRes != RES_BOGUS) { + /* + * The resource item word was already precomputed, which means + * no further data needs to be written. + * This might be an integer, or an empty or UTF-16 v2 string, + * an empty binary, etc. + */ + return; + } + handlePreWrite(byteOffset); + *byteOffset += calcPadding(*byteOffset); +} + +void +SResource::handlePreWrite(uint32_t * /*byteOffset*/) { + assert(FALSE); +} + +/* + * Only called for UTF-16 v1 strings, and for aliases. For UTF-16 v2 strings, + * write() sees fWritten and exits early. + */ +void +StringBaseResource::handleWrite(UNewDataMemory *mem, uint32_t *byteOffset) { + /* Write the UTF-16 v1 string. */ + int32_t len = length(); + udata_write32(mem, len); + udata_writeUString(mem, getBuffer(), len + 1); + *byteOffset += 4 + (len + 1) * U_SIZEOF_UCHAR; + fWritten = TRUE; +} + +void +ContainerResource::writeAllRes(UNewDataMemory *mem, uint32_t *byteOffset) { + uint32_t i = 0; + for (SResource *current = fFirst; current != NULL; ++i, current = current->fNext) { + current->write(mem, byteOffset); + } + assert(i == fCount); +} + +void +ContainerResource::writeAllRes32(UNewDataMemory *mem, uint32_t *byteOffset) { + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + udata_write32(mem, current->fRes); + } + *byteOffset += fCount * 4; +} + +void +ArrayResource::handleWrite(UNewDataMemory *mem, uint32_t *byteOffset) { + writeAllRes(mem, byteOffset); + udata_write32(mem, fCount); + *byteOffset += 4; + writeAllRes32(mem, byteOffset); +} + +void +IntVectorResource::handleWrite(UNewDataMemory *mem, uint32_t *byteOffset) { + udata_write32(mem, fCount); + for(uint32_t i = 0; i < fCount; ++i) { + udata_write32(mem, fArray[i]); + } + *byteOffset += (1 + fCount) * 4; +} + +void +BinaryResource::handleWrite(UNewDataMemory *mem, uint32_t *byteOffset) { + uint32_t pad = 0; + uint32_t dataStart = *byteOffset + sizeof(fLength); + + if (dataStart % BIN_ALIGNMENT) { + pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT); + udata_writePadding(mem, pad); /* pad == 4 or 8 or 12 */ + *byteOffset += pad; + } + + udata_write32(mem, fLength); + if (fLength > 0) { + udata_writeBlock(mem, fData, fLength); + } + *byteOffset += 4 + fLength; +} + +void +TableResource::handleWrite(UNewDataMemory *mem, uint32_t *byteOffset) { + writeAllRes(mem, byteOffset); + if(fTableType == URES_TABLE) { + udata_write16(mem, (uint16_t)fCount); + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + udata_write16(mem, current->fKey16); + } + *byteOffset += (1 + fCount)* 2; + if ((fCount & 1) == 0) { + /* 16-bit count and even number of 16-bit key offsets need padding before 32-bit resource items */ + udata_writePadding(mem, 2); + *byteOffset += 2; + } + } else /* URES_TABLE32 */ { + udata_write32(mem, fCount); + for (SResource *current = fFirst; current != NULL; current = current->fNext) { + udata_write32(mem, (uint32_t)current->fKey); + } + *byteOffset += (1 + fCount)* 4; + } + writeAllRes32(mem, byteOffset); +} + +void +SResource::write(UNewDataMemory *mem, uint32_t *byteOffset) { + if (fWritten) { + assert(fRes != RES_BOGUS); + return; + } + handleWrite(mem, byteOffset); + uint8_t paddingSize = calcPadding(*byteOffset); + if (paddingSize > 0) { + udata_writePadding(mem, paddingSize); + *byteOffset += paddingSize; + } + fWritten = TRUE; +} + +void +SResource::handleWrite(UNewDataMemory * /*mem*/, uint32_t * /*byteOffset*/) { + assert(FALSE); +} + +void SRBRoot::write(const char *outputDir, const char *outputPkg, + char *writtenFilename, int writtenFilenameLen, + UErrorCode &errorCode) { + UNewDataMemory *mem = NULL; + uint32_t byteOffset = 0; + uint32_t top, size; + char dataName[1024]; + int32_t indexes[URES_INDEX_TOP]; + + compactKeys(errorCode); + /* + * Add padding bytes to fKeys so that fKeysTop is 4-aligned. + * Safe because the capacity is a multiple of 4. + */ + while (fKeysTop & 3) { + fKeys[fKeysTop++] = (char)0xaa; + } + /* + * In URES_TABLE, use all local key offsets that fit into 16 bits, + * and use the remaining 16-bit offsets for pool key offsets + * if there are any. + * If there are no local keys, then use the whole 16-bit space + * for pool key offsets. + * Note: This cannot be changed without changing the major formatVersion. + */ + if (fKeysBottom < fKeysTop) { + if (fKeysTop <= 0x10000) { + fLocalKeyLimit = fKeysTop; + } else { + fLocalKeyLimit = 0x10000; + } + } else { + fLocalKeyLimit = 0; + } + + UHashtable *stringSet; + if (gFormatVersion > 1) { + stringSet = uhash_open(string_hash, string_comp, string_comp, &errorCode); + if (U_SUCCESS(errorCode) && + fUsePoolBundle != NULL && fUsePoolBundle->fStrings != NULL) { + for (SResource *current = fUsePoolBundle->fStrings->fFirst; + current != NULL; + current = current->fNext) { + StringResource *sr = static_cast<StringResource *>(current); + sr->fNumCopies = 0; + sr->fNumUnitsSaved = 0; + uhash_put(stringSet, sr, sr, &errorCode); + } + } + fRoot->preflightStrings(this, stringSet, errorCode); + } else { + stringSet = NULL; + } + if (fStringsForm == STRINGS_UTF16_V2 && f16BitStringsLength > 0) { + compactStringsV2(stringSet, errorCode); + } + uhash_close(stringSet); + if (U_FAILURE(errorCode)) { + return; + } + + int32_t formatVersion = gFormatVersion; + if (fPoolStringIndexLimit != 0) { + int32_t sum = fPoolStringIndexLimit + fLocalStringIndexLimit; + if ((sum - 1) > RES_MAX_OFFSET) { + errorCode = U_BUFFER_OVERFLOW_ERROR; + return; + } + if (fPoolStringIndexLimit < 0x10000 && sum <= 0x10000) { + // 16-bit indexes work for all pool + local strings. + fPoolStringIndex16Limit = fPoolStringIndexLimit; + } else { + // Set the pool index threshold so that 16-bit indexes work + // for some pool strings and some local strings. + fPoolStringIndex16Limit = (int32_t)( + ((int64_t)fPoolStringIndexLimit * 0xffff) / sum); + } + } else if (gIsDefaultFormatVersion && formatVersion == 3 && !fIsPoolBundle) { + // If we just default to formatVersion 3 + // but there are no pool bundle strings to share + // and we do not write a pool bundle, + // then write formatVersion 2 which is just as good. + formatVersion = 2; + } + + fRoot->write16(this); + if (f16BitUnits.isBogus()) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + if (f16BitUnits.length() & 1) { + f16BitUnits.append((UChar)0xaaaa); /* pad to multiple of 4 bytes */ + } + /* all keys have been mapped */ + uprv_free(fKeyMap); + fKeyMap = NULL; + + byteOffset = fKeysTop + f16BitUnits.length() * 2; + fRoot->preWrite(&byteOffset); + + /* total size including the root item */ + top = byteOffset; + + if (writtenFilename && writtenFilenameLen) { + *writtenFilename = 0; + } + + if (writtenFilename) { + int32_t off = 0, len = 0; + if (outputDir) { + len = (int32_t)uprv_strlen(outputDir); + if (len > writtenFilenameLen) { + len = writtenFilenameLen; + } + uprv_strncpy(writtenFilename, outputDir, len); + } + if (writtenFilenameLen -= len) { + off += len; + writtenFilename[off] = U_FILE_SEP_CHAR; + if (--writtenFilenameLen) { + ++off; + if(outputPkg != NULL) + { + uprv_strcpy(writtenFilename+off, outputPkg); + off += (int32_t)uprv_strlen(outputPkg); + writtenFilename[off] = '_'; + ++off; + } + + len = (int32_t)uprv_strlen(fLocale); + if (len > writtenFilenameLen) { + len = writtenFilenameLen; + } + uprv_strncpy(writtenFilename + off, fLocale, len); + if (writtenFilenameLen -= len) { + off += len; + len = 5; + if (len > writtenFilenameLen) { + len = writtenFilenameLen; + } + uprv_strncpy(writtenFilename + off, ".res", len); + } + } + } + } + + if(outputPkg) + { + uprv_strcpy(dataName, outputPkg); + uprv_strcat(dataName, "_"); + uprv_strcat(dataName, fLocale); + } + else + { + uprv_strcpy(dataName, fLocale); + } + + uprv_memcpy(dataInfo.formatVersion, gFormatVersions + formatVersion, sizeof(UVersionInfo)); + + mem = udata_create(outputDir, "res", dataName, + &dataInfo, (gIncludeCopyright==TRUE)? U_COPYRIGHT_STRING:NULL, &errorCode); + if(U_FAILURE(errorCode)){ + return; + } + + /* write the root item */ + udata_write32(mem, fRoot->fRes); + + /* + * formatVersion 1.1 (ICU 2.8): + * write int32_t indexes[] after root and before the key strings + * to make it easier to parse resource bundles in icuswap or from Java etc. + */ + uprv_memset(indexes, 0, sizeof(indexes)); + indexes[URES_INDEX_LENGTH]= fIndexLength; + indexes[URES_INDEX_KEYS_TOP]= fKeysTop>>2; + indexes[URES_INDEX_RESOURCES_TOP]= (int32_t)(top>>2); + indexes[URES_INDEX_BUNDLE_TOP]= indexes[URES_INDEX_RESOURCES_TOP]; + indexes[URES_INDEX_MAX_TABLE_LENGTH]= fMaxTableLength; + + /* + * formatVersion 1.2 (ICU 3.6): + * write indexes[URES_INDEX_ATTRIBUTES] with URES_ATT_NO_FALLBACK set or not set + * the memset() above initialized all indexes[] to 0 + */ + if (fNoFallback) { + indexes[URES_INDEX_ATTRIBUTES]=URES_ATT_NO_FALLBACK; + } + /* + * formatVersion 2.0 (ICU 4.4): + * more compact string value storage, optional pool bundle + */ + if (URES_INDEX_16BIT_TOP < fIndexLength) { + indexes[URES_INDEX_16BIT_TOP] = (fKeysTop>>2) + (f16BitUnits.length()>>1); + } + if (URES_INDEX_POOL_CHECKSUM < fIndexLength) { + if (fIsPoolBundle) { + indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_IS_POOL_BUNDLE | URES_ATT_NO_FALLBACK; + uint32_t checksum = computeCRC((const char *)(fKeys + fKeysBottom), + (uint32_t)(fKeysTop - fKeysBottom), 0); + if (f16BitUnits.length() <= 1) { + // no pool strings to checksum + } else if (U_IS_BIG_ENDIAN) { + checksum = computeCRC((const char *)f16BitUnits.getBuffer(), + (uint32_t)f16BitUnits.length() * 2, checksum); + } else { + // Swap to big-endian so we get the same checksum on all platforms + // (except for charset family, due to the key strings). + UnicodeString s(f16BitUnits); + s.append((UChar)1); // Ensure that we own this buffer. + assert(!s.isBogus()); + uint16_t *p = (uint16_t *)s.getBuffer(); + for (int32_t count = f16BitUnits.length(); count > 0; --count) { + uint16_t x = *p; + *p++ = (uint16_t)((x << 8) | (x >> 8)); + } + checksum = computeCRC((const char *)p, + (uint32_t)f16BitUnits.length() * 2, checksum); + } + indexes[URES_INDEX_POOL_CHECKSUM] = (int32_t)checksum; + } else if (gUsePoolBundle) { + indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_USES_POOL_BUNDLE; + indexes[URES_INDEX_POOL_CHECKSUM] = fUsePoolBundle->fChecksum; + } + } + // formatVersion 3 (ICU 56): + // share string values via pool bundle strings + indexes[URES_INDEX_LENGTH] |= fPoolStringIndexLimit << 8; // bits 23..0 -> 31..8 + indexes[URES_INDEX_ATTRIBUTES] |= (fPoolStringIndexLimit >> 12) & 0xf000; // bits 27..24 -> 15..12 + indexes[URES_INDEX_ATTRIBUTES] |= fPoolStringIndex16Limit << 16; + + /* write the indexes[] */ + udata_writeBlock(mem, indexes, fIndexLength*4); + + /* write the table key strings */ + udata_writeBlock(mem, fKeys+fKeysBottom, + fKeysTop-fKeysBottom); + + /* write the v2 UTF-16 strings, URES_TABLE16 and URES_ARRAY16 */ + udata_writeBlock(mem, f16BitUnits.getBuffer(), f16BitUnits.length()*2); + + /* write all of the bundle contents: the root item and its children */ + byteOffset = fKeysTop + f16BitUnits.length() * 2; + fRoot->write(mem, &byteOffset); + assert(byteOffset == top); + + size = udata_finish(mem, &errorCode); + if(top != size) { + fprintf(stderr, "genrb error: wrote %u bytes but counted %u\n", + (int)size, (int)top); + errorCode = U_INTERNAL_PROGRAM_ERROR; + } +} + +/* Opening Functions */ + +TableResource* table_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { + LocalPointer<TableResource> res(new TableResource(bundle, tag, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +ArrayResource* array_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { + LocalPointer<ArrayResource> res(new ArrayResource(bundle, tag, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +struct SResource *string_open(struct SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) { + LocalPointer<SResource> res( + new StringResource(bundle, tag, value, len, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +struct SResource *alias_open(struct SRBRoot *bundle, const char *tag, UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) { + LocalPointer<SResource> res( + new AliasResource(bundle, tag, value, len, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +IntVectorResource *intvector_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { + LocalPointer<IntVectorResource> res( + new IntVectorResource(bundle, tag, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +struct SResource *int_open(struct SRBRoot *bundle, const char *tag, int32_t value, const struct UString* comment, UErrorCode *status) { + LocalPointer<SResource> res(new IntResource(bundle, tag, value, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +struct SResource *bin_open(struct SRBRoot *bundle, const char *tag, uint32_t length, uint8_t *data, const char* fileName, const struct UString* comment, UErrorCode *status) { + LocalPointer<SResource> res( + new BinaryResource(bundle, tag, length, data, fileName, comment, *status), *status); + return U_SUCCESS(*status) ? res.orphan() : NULL; +} + +SRBRoot::SRBRoot(const UString *comment, UBool isPoolBundle, UErrorCode &errorCode) + : fRoot(NULL), fLocale(NULL), fIndexLength(0), fMaxTableLength(0), fNoFallback(FALSE), + fStringsForm(STRINGS_UTF16_V1), fIsPoolBundle(isPoolBundle), + fKeys(NULL), fKeyMap(NULL), + fKeysBottom(0), fKeysTop(0), fKeysCapacity(0), fKeysCount(0), fLocalKeyLimit(0), + f16BitUnits(), f16BitStringsLength(0), + fUsePoolBundle(&kNoPoolBundle), + fPoolStringIndexLimit(0), fPoolStringIndex16Limit(0), fLocalStringIndexLimit(0), + fWritePoolBundle(NULL) { + if (U_FAILURE(errorCode)) { + return; + } + + if (gFormatVersion > 1) { + // f16BitUnits must start with a zero for empty resources. + // We might be able to omit it if there are no empty 16-bit resources. + f16BitUnits.append((UChar)0); + } + + fKeys = (char *) uprv_malloc(sizeof(char) * KEY_SPACE_SIZE); + if (isPoolBundle) { + fRoot = new PseudoListResource(this, errorCode); + } else { + fRoot = new TableResource(this, NULL, comment, errorCode); + } + if (fKeys == NULL || fRoot == NULL || U_FAILURE(errorCode)) { + if (U_SUCCESS(errorCode)) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + } + return; + } + + fKeysCapacity = KEY_SPACE_SIZE; + /* formatVersion 1.1 and up: start fKeysTop after the root item and indexes[] */ + if (gUsePoolBundle || isPoolBundle) { + fIndexLength = URES_INDEX_POOL_CHECKSUM + 1; + } else if (gFormatVersion >= 2) { + fIndexLength = URES_INDEX_16BIT_TOP + 1; + } else /* formatVersion 1 */ { + fIndexLength = URES_INDEX_ATTRIBUTES + 1; + } + fKeysBottom = (1 /* root */ + fIndexLength) * 4; + uprv_memset(fKeys, 0, fKeysBottom); + fKeysTop = fKeysBottom; + + if (gFormatVersion == 1) { + fStringsForm = STRINGS_UTF16_V1; + } else { + fStringsForm = STRINGS_UTF16_V2; + } +} + +/* Closing Functions */ + +void res_close(struct SResource *res) { + delete res; +} + +SRBRoot::~SRBRoot() { + delete fRoot; + uprv_free(fLocale); + uprv_free(fKeys); + uprv_free(fKeyMap); +} + +/* Misc Functions */ + +void SRBRoot::setLocale(UChar *locale, UErrorCode &errorCode) { + if(U_FAILURE(errorCode)) { + return; + } + + uprv_free(fLocale); + fLocale = (char*) uprv_malloc(sizeof(char) * (u_strlen(locale)+1)); + if(fLocale == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + + u_UCharsToChars(locale, fLocale, u_strlen(locale)+1); +} + +const char * +SRBRoot::getKeyString(int32_t key) const { + if (key < 0) { + return fUsePoolBundle->fKeys + (key & 0x7fffffff); + } else { + return fKeys + key; + } +} + +const char * +SResource::getKeyString(const SRBRoot *bundle) const { + if (fKey == -1) { + return NULL; + } + return bundle->getKeyString(fKey); +} + +const char * +SRBRoot::getKeyBytes(int32_t *pLength) const { + *pLength = fKeysTop - fKeysBottom; + return fKeys + fKeysBottom; +} + +int32_t +SRBRoot::addKeyBytes(const char *keyBytes, int32_t length, UErrorCode &errorCode) { + int32_t keypos; + + if (U_FAILURE(errorCode)) { + return -1; + } + if (length < 0 || (keyBytes == NULL && length != 0)) { + errorCode = U_ILLEGAL_ARGUMENT_ERROR; + return -1; + } + if (length == 0) { + return fKeysTop; + } + + keypos = fKeysTop; + fKeysTop += length; + if (fKeysTop >= fKeysCapacity) { + /* overflow - resize the keys buffer */ + fKeysCapacity += KEY_SPACE_SIZE; + fKeys = static_cast<char *>(uprv_realloc(fKeys, fKeysCapacity)); + if(fKeys == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return -1; + } + } + + uprv_memcpy(fKeys + keypos, keyBytes, length); + + return keypos; +} + +int32_t +SRBRoot::addTag(const char *tag, UErrorCode &errorCode) { + int32_t keypos; + + if (U_FAILURE(errorCode)) { + return -1; + } + + if (tag == NULL) { + /* no error: the root table and array items have no keys */ + return -1; + } + + keypos = addKeyBytes(tag, (int32_t)(uprv_strlen(tag) + 1), errorCode); + if (U_SUCCESS(errorCode)) { + ++fKeysCount; + } + return keypos; +} + +static int32_t +compareInt32(int32_t lPos, int32_t rPos) { + /* + * Compare possibly-negative key offsets. Don't just return lPos - rPos + * because that is prone to negative-integer underflows. + */ + if (lPos < rPos) { + return -1; + } else if (lPos > rPos) { + return 1; + } else { + return 0; + } +} + +static int32_t U_CALLCONV +compareKeySuffixes(const void *context, const void *l, const void *r) { + const struct SRBRoot *bundle=(const struct SRBRoot *)context; + int32_t lPos = ((const KeyMapEntry *)l)->oldpos; + int32_t rPos = ((const KeyMapEntry *)r)->oldpos; + const char *lStart = bundle->getKeyString(lPos); + const char *lLimit = lStart; + const char *rStart = bundle->getKeyString(rPos); + const char *rLimit = rStart; + int32_t diff; + while (*lLimit != 0) { ++lLimit; } + while (*rLimit != 0) { ++rLimit; } + /* compare keys in reverse character order */ + while (lStart < lLimit && rStart < rLimit) { + diff = (int32_t)(uint8_t)*--lLimit - (int32_t)(uint8_t)*--rLimit; + if (diff != 0) { + return diff; + } + } + /* sort equal suffixes by descending key length */ + diff = (int32_t)(rLimit - rStart) - (int32_t)(lLimit - lStart); + if (diff != 0) { + return diff; + } + /* Sort pool bundle keys first (negative oldpos), and otherwise keys in parsing order. */ + return compareInt32(lPos, rPos); +} + +static int32_t U_CALLCONV +compareKeyNewpos(const void * /*context*/, const void *l, const void *r) { + return compareInt32(((const KeyMapEntry *)l)->newpos, ((const KeyMapEntry *)r)->newpos); +} + +static int32_t U_CALLCONV +compareKeyOldpos(const void * /*context*/, const void *l, const void *r) { + return compareInt32(((const KeyMapEntry *)l)->oldpos, ((const KeyMapEntry *)r)->oldpos); +} + +void +SRBRoot::compactKeys(UErrorCode &errorCode) { + KeyMapEntry *map; + char *keys; + int32_t i; + int32_t keysCount = fUsePoolBundle->fKeysCount + fKeysCount; + if (U_FAILURE(errorCode) || fKeysCount == 0 || fKeyMap != NULL) { + return; + } + map = (KeyMapEntry *)uprv_malloc(keysCount * sizeof(KeyMapEntry)); + if (map == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + keys = (char *)fUsePoolBundle->fKeys; + for (i = 0; i < fUsePoolBundle->fKeysCount; ++i) { + map[i].oldpos = + (int32_t)(keys - fUsePoolBundle->fKeys) | 0x80000000; /* negative oldpos */ + map[i].newpos = 0; + while (*keys != 0) { ++keys; } /* skip the key */ + ++keys; /* skip the NUL */ + } + keys = fKeys + fKeysBottom; + for (; i < keysCount; ++i) { + map[i].oldpos = (int32_t)(keys - fKeys); + map[i].newpos = 0; + while (*keys != 0) { ++keys; } /* skip the key */ + ++keys; /* skip the NUL */ + } + /* Sort the keys so that each one is immediately followed by all of its suffixes. */ + uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), + compareKeySuffixes, this, FALSE, &errorCode); + /* + * Make suffixes point into earlier, longer strings that contain them + * and mark the old, now unused suffix bytes as deleted. + */ + if (U_SUCCESS(errorCode)) { + keys = fKeys; + for (i = 0; i < keysCount;) { + /* + * This key is not a suffix of the previous one; + * keep this one and delete the following ones that are + * suffixes of this one. + */ + const char *key; + const char *keyLimit; + int32_t j = i + 1; + map[i].newpos = map[i].oldpos; + if (j < keysCount && map[j].oldpos < 0) { + /* Key string from the pool bundle, do not delete. */ + i = j; + continue; + } + key = getKeyString(map[i].oldpos); + for (keyLimit = key; *keyLimit != 0; ++keyLimit) {} + for (; j < keysCount && map[j].oldpos >= 0; ++j) { + const char *k; + char *suffix; + const char *suffixLimit; + int32_t offset; + suffix = keys + map[j].oldpos; + for (suffixLimit = suffix; *suffixLimit != 0; ++suffixLimit) {} + offset = (int32_t)(keyLimit - key) - (suffixLimit - suffix); + if (offset < 0) { + break; /* suffix cannot be longer than the original */ + } + /* Is it a suffix of the earlier, longer key? */ + for (k = keyLimit; suffix < suffixLimit && *--k == *--suffixLimit;) {} + if (suffix == suffixLimit && *k == *suffixLimit) { + map[j].newpos = map[i].oldpos + offset; /* yes, point to the earlier key */ + /* mark the suffix as deleted */ + while (*suffix != 0) { *suffix++ = 1; } + *suffix = 1; + } else { + break; /* not a suffix, restart from here */ + } + } + i = j; + } + /* + * Re-sort by newpos, then modify the key characters array in-place + * to squeeze out unused bytes, and readjust the newpos offsets. + */ + uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), + compareKeyNewpos, NULL, FALSE, &errorCode); + if (U_SUCCESS(errorCode)) { + int32_t oldpos, newpos, limit; + oldpos = newpos = fKeysBottom; + limit = fKeysTop; + /* skip key offsets that point into the pool bundle rather than this new bundle */ + for (i = 0; i < keysCount && map[i].newpos < 0; ++i) {} + if (i < keysCount) { + while (oldpos < limit) { + if (keys[oldpos] == 1) { + ++oldpos; /* skip unused bytes */ + } else { + /* adjust the new offsets for keys starting here */ + while (i < keysCount && map[i].newpos == oldpos) { + map[i++].newpos = newpos; + } + /* move the key characters to their new position */ + keys[newpos++] = keys[oldpos++]; + } + } + assert(i == keysCount); + } + fKeysTop = newpos; + /* Re-sort once more, by old offsets for binary searching. */ + uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), + compareKeyOldpos, NULL, FALSE, &errorCode); + if (U_SUCCESS(errorCode)) { + /* key size reduction by limit - newpos */ + fKeyMap = map; + map = NULL; + } + } + } + uprv_free(map); +} + +static int32_t U_CALLCONV +compareStringSuffixes(const void * /*context*/, const void *l, const void *r) { + const StringResource *left = *((const StringResource **)l); + const StringResource *right = *((const StringResource **)r); + const UChar *lStart = left->getBuffer(); + const UChar *lLimit = lStart + left->length(); + const UChar *rStart = right->getBuffer(); + const UChar *rLimit = rStart + right->length(); + int32_t diff; + /* compare keys in reverse character order */ + while (lStart < lLimit && rStart < rLimit) { + diff = (int32_t)*--lLimit - (int32_t)*--rLimit; + if (diff != 0) { + return diff; + } + } + /* sort equal suffixes by descending string length */ + return right->length() - left->length(); +} + +static int32_t U_CALLCONV +compareStringLengths(const void * /*context*/, const void *l, const void *r) { + const StringResource *left = *((const StringResource **)l); + const StringResource *right = *((const StringResource **)r); + int32_t diff; + /* Make "is suffix of another string" compare greater than a non-suffix. */ + diff = (int)(left->fSame != NULL) - (int)(right->fSame != NULL); + if (diff != 0) { + return diff; + } + /* sort by ascending string length */ + diff = left->length() - right->length(); + if (diff != 0) { + return diff; + } + // sort by descending size reduction + diff = right->fNumUnitsSaved - left->fNumUnitsSaved; + if (diff != 0) { + return diff; + } + // sort lexically + return left->fString.compare(right->fString); +} + +void +StringResource::writeUTF16v2(int32_t base, UnicodeString &dest) { + int32_t len = length(); + fRes = URES_MAKE_RESOURCE(URES_STRING_V2, base + dest.length()); + fWritten = TRUE; + switch(fNumCharsForLength) { + case 0: + break; + case 1: + dest.append((UChar)(0xdc00 + len)); + break; + case 2: + dest.append((UChar)(0xdfef + (len >> 16))); + dest.append((UChar)len); + break; + case 3: + dest.append((UChar)0xdfff); + dest.append((UChar)(len >> 16)); + dest.append((UChar)len); + break; + default: + break; /* will not occur */ + } + dest.append(fString); + dest.append((UChar)0); +} + +void +SRBRoot::compactStringsV2(UHashtable *stringSet, UErrorCode &errorCode) { + if (U_FAILURE(errorCode)) { + return; + } + // Store the StringResource pointers in an array for + // easy sorting and processing. + // We enumerate a set of strings, so there are no duplicates. + int32_t count = uhash_count(stringSet); + LocalArray<StringResource *> array(new StringResource *[count], errorCode); + if (U_FAILURE(errorCode)) { + return; + } + for (int32_t pos = UHASH_FIRST, i = 0; i < count; ++i) { + array[i] = (StringResource *)uhash_nextElement(stringSet, &pos)->key.pointer; + } + /* Sort the strings so that each one is immediately followed by all of its suffixes. */ + uprv_sortArray(array.getAlias(), count, (int32_t)sizeof(struct SResource **), + compareStringSuffixes, NULL, FALSE, &errorCode); + if (U_FAILURE(errorCode)) { + return; + } + /* + * Make suffixes point into earlier, longer strings that contain them. + * Temporarily use fSame and fSuffixOffset for suffix strings to + * refer to the remaining ones. + */ + for (int32_t i = 0; i < count;) { + /* + * This string is not a suffix of the previous one; + * write this one and subsume the following ones that are + * suffixes of this one. + */ + StringResource *res = array[i]; + res->fNumUnitsSaved = (res->fNumCopies - 1) * res->get16BitStringsLength(); + // Whole duplicates of pool strings are already account for in fPoolStringIndexLimit, + // see StringResource::handlePreflightStrings(). + int32_t j; + for (j = i + 1; j < count; ++j) { + StringResource *suffixRes = array[j]; + /* Is it a suffix of the earlier, longer string? */ + if (res->fString.endsWith(suffixRes->fString)) { + assert(res->length() != suffixRes->length()); // Set strings are unique. + if (suffixRes->fWritten) { + // Pool string, skip. + } else if (suffixRes->fNumCharsForLength == 0) { + /* yes, point to the earlier string */ + suffixRes->fSame = res; + suffixRes->fSuffixOffset = res->length() - suffixRes->length(); + if (res->fWritten) { + // Suffix-share res which is a pool string. + // Compute the resource word and collect the maximum. + suffixRes->fRes = + res->fRes + res->fNumCharsForLength + suffixRes->fSuffixOffset; + int32_t poolStringIndex = (int32_t)RES_GET_OFFSET(suffixRes->fRes); + if (poolStringIndex >= fPoolStringIndexLimit) { + fPoolStringIndexLimit = poolStringIndex + 1; + } + suffixRes->fWritten = TRUE; + } + res->fNumUnitsSaved += suffixRes->fNumCopies * suffixRes->get16BitStringsLength(); + } else { + /* write the suffix by itself if we need explicit length */ + } + } else { + break; /* not a suffix, restart from here */ + } + } + i = j; + } + /* + * Re-sort the strings by ascending length (except suffixes last) + * to optimize for URES_TABLE16 and URES_ARRAY16: + * Keep as many as possible within reach of 16-bit offsets. + */ + uprv_sortArray(array.getAlias(), count, (int32_t)sizeof(struct SResource **), + compareStringLengths, NULL, FALSE, &errorCode); + if (U_FAILURE(errorCode)) { + return; + } + if (fIsPoolBundle) { + // Write strings that are sufficiently shared. + // Avoid writing other strings. + int32_t numStringsWritten = 0; + int32_t numUnitsSaved = 0; + int32_t numUnitsNotSaved = 0; + for (int32_t i = 0; i < count; ++i) { + StringResource *res = array[i]; + // Maximum pool string index when suffix-sharing the last character. + int32_t maxStringIndex = + f16BitUnits.length() + res->fNumCharsForLength + res->length() - 1; + if (res->fNumUnitsSaved >= GENRB_MIN_16BIT_UNITS_SAVED_FOR_POOL_STRING && + maxStringIndex < RES_MAX_OFFSET) { + res->writeUTF16v2(0, f16BitUnits); + ++numStringsWritten; + numUnitsSaved += res->fNumUnitsSaved; + } else { + numUnitsNotSaved += res->fNumUnitsSaved; + res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_STRING); + res->fWritten = TRUE; + } + } + if (f16BitUnits.isBogus()) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + } + if (getShowWarning()) { // not quiet + printf("number of shared strings: %d\n", (int)numStringsWritten); + printf("16-bit units for strings: %6d = %6d bytes\n", + (int)f16BitUnits.length(), (int)f16BitUnits.length() * 2); + printf("16-bit units saved: %6d = %6d bytes\n", + (int)numUnitsSaved, (int)numUnitsSaved * 2); + printf("16-bit units not saved: %6d = %6d bytes\n", + (int)numUnitsNotSaved, (int)numUnitsNotSaved * 2); + } + } else { + assert(fPoolStringIndexLimit <= fUsePoolBundle->fStringIndexLimit); + /* Write the non-suffix strings. */ + int32_t i; + for (i = 0; i < count && array[i]->fSame == NULL; ++i) { + StringResource *res = array[i]; + if (!res->fWritten) { + int32_t localStringIndex = f16BitUnits.length(); + if (localStringIndex >= fLocalStringIndexLimit) { + fLocalStringIndexLimit = localStringIndex + 1; + } + res->writeUTF16v2(fPoolStringIndexLimit, f16BitUnits); + } + } + if (f16BitUnits.isBogus()) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + return; + } + if (fWritePoolBundle != NULL && gFormatVersion >= 3) { + PseudoListResource *poolStrings = + static_cast<PseudoListResource *>(fWritePoolBundle->fRoot); + for (i = 0; i < count && array[i]->fSame == NULL; ++i) { + assert(!array[i]->fString.isEmpty()); + StringResource *poolString = + new StringResource(fWritePoolBundle, array[i]->fString, errorCode); + if (poolString == NULL) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + break; + } + poolStrings->add(poolString); + } + } + /* Write the suffix strings. Make each point to the real string. */ + for (; i < count; ++i) { + StringResource *res = array[i]; + if (res->fWritten) { + continue; + } + StringResource *same = res->fSame; + assert(res->length() != same->length()); // Set strings are unique. + res->fRes = same->fRes + same->fNumCharsForLength + res->fSuffixOffset; + int32_t localStringIndex = (int32_t)RES_GET_OFFSET(res->fRes) - fPoolStringIndexLimit; + // Suffixes of pool strings have been set already. + assert(localStringIndex >= 0); + if (localStringIndex >= fLocalStringIndexLimit) { + fLocalStringIndexLimit = localStringIndex + 1; + } + res->fWritten = TRUE; + } + } + // +1 to account for the initial zero in f16BitUnits + assert(f16BitUnits.length() <= (f16BitStringsLength + 1)); +} diff --git a/intl/icu/source/tools/genrb/reslist.h b/intl/icu/source/tools/genrb/reslist.h new file mode 100644 index 000000000..de6faf6a7 --- /dev/null +++ b/intl/icu/source/tools/genrb/reslist.h @@ -0,0 +1,423 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2000-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File reslist.h +* +* Modification History: +* +* Date Name Description +* 02/21/00 weiv Creation. +******************************************************************************* +*/ + +#ifndef RESLIST_H +#define RESLIST_H + +#define KEY_SPACE_SIZE 65536 +#define RESLIST_MAX_INT_VECTOR 2048 + +#include "unicode/utypes.h" +#include "unicode/unistr.h" +#include "unicode/ures.h" +#include "unicode/ustring.h" +#include "cmemory.h" +#include "cstring.h" +#include "uhash.h" +#include "unewdata.h" +#include "uresdata.h" +#include "ustr.h" + +U_CDECL_BEGIN + +class PseudoListResource; + +struct ResFile { + ResFile() + : fBytes(NULL), fIndexes(NULL), + fKeys(NULL), fKeysLength(0), fKeysCount(0), + fStrings(NULL), fStringIndexLimit(0), + fChecksum(0) {} + ~ResFile() { close(); } + + void close(); + + uint8_t *fBytes; + const int32_t *fIndexes; + const char *fKeys; + int32_t fKeysLength; + int32_t fKeysCount; + + PseudoListResource *fStrings; + int32_t fStringIndexLimit; + + int32_t fChecksum; +}; + +struct SResource; + +typedef struct KeyMapEntry { + int32_t oldpos, newpos; +} KeyMapEntry; + +/* Resource bundle root table */ +struct SRBRoot { + SRBRoot(const UString *comment, UBool isPoolBundle, UErrorCode &errorCode); + ~SRBRoot(); + + void write(const char *outputDir, const char *outputPkg, + char *writtenFilename, int writtenFilenameLen, UErrorCode &errorCode); + + void setLocale(UChar *locale, UErrorCode &errorCode); + int32_t addTag(const char *tag, UErrorCode &errorCode); + + const char *getKeyString(int32_t key) const; + const char *getKeyBytes(int32_t *pLength) const; + + int32_t addKeyBytes(const char *keyBytes, int32_t length, UErrorCode &errorCode); + + void compactKeys(UErrorCode &errorCode); + + int32_t makeRes16(uint32_t resWord) const; + int32_t mapKey(int32_t oldpos) const; + +private: + void compactStringsV2(UHashtable *stringSet, UErrorCode &errorCode); + +public: + // TODO: private + + SResource *fRoot; // Normally a TableResource. + char *fLocale; + int32_t fIndexLength; + int32_t fMaxTableLength; + UBool fNoFallback; /* see URES_ATT_NO_FALLBACK */ + int8_t fStringsForm; /* default STRINGS_UTF16_V1 */ + UBool fIsPoolBundle; + + char *fKeys; + KeyMapEntry *fKeyMap; + int32_t fKeysBottom, fKeysTop; + int32_t fKeysCapacity; + int32_t fKeysCount; + int32_t fLocalKeyLimit; /* key offset < limit fits into URES_TABLE */ + + icu::UnicodeString f16BitUnits; + int32_t f16BitStringsLength; + + const ResFile *fUsePoolBundle; + int32_t fPoolStringIndexLimit; + int32_t fPoolStringIndex16Limit; + int32_t fLocalStringIndexLimit; + SRBRoot *fWritePoolBundle; +}; + +/* write a java resource file */ +// TODO: C++ify +void bundle_write_java(struct SRBRoot *bundle, const char *outputDir, const char* outputEnc, char *writtenFilename, + int writtenFilenameLen, const char* packageName, const char* bundleName, UErrorCode *status); + +/* write a xml resource file */ +// TODO: C++ify +void bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, const char* rbname, + char *writtenFilename, int writtenFilenameLen, const char* language, const char* package, UErrorCode *status); + +/* Various resource types */ + +/* + * Return a unique pointer to a dummy object, + * for use in non-error cases when no resource is to be added to the bundle. + * (NULL is used in error cases.) + */ +struct SResource* res_none(void); + +class ArrayResource; +class TableResource; +class IntVectorResource; + +TableResource *table_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status); + +ArrayResource *array_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status); + +struct SResource *string_open(struct SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, const struct UString* comment, UErrorCode *status); + +struct SResource *alias_open(struct SRBRoot *bundle, const char *tag, UChar *value, int32_t len, const struct UString* comment, UErrorCode *status); + +IntVectorResource *intvector_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status); + +struct SResource *int_open(struct SRBRoot *bundle, const char *tag, int32_t value, const struct UString* comment, UErrorCode *status); + +struct SResource *bin_open(struct SRBRoot *bundle, const char *tag, uint32_t length, uint8_t *data, const char* fileName, const struct UString* comment, UErrorCode *status); + +/* Resource place holder */ + +struct SResource { + SResource(); + SResource(SRBRoot *bundle, const char *tag, int8_t type, const UString* comment, + UErrorCode &errorCode); + virtual ~SResource(); + + UBool isTable() const { return fType == URES_TABLE; } + UBool isString() const { return fType == URES_STRING; } + + const char *getKeyString(const SRBRoot *bundle) const; + + /** + * Preflights strings. + * Finds duplicates and counts the total number of string code units + * so that they can be written first to the 16-bit array, + * for minimal string and container storage. + * + * We walk the final parse tree, rather than collecting this information while building it, + * so that we need not deal with changes to the parse tree (especially removing resources). + */ + void preflightStrings(SRBRoot *bundle, UHashtable *stringSet, UErrorCode &errorCode); + virtual void handlePreflightStrings(SRBRoot *bundle, UHashtable *stringSet, UErrorCode &errorCode); + + /** + * Writes resource values into f16BitUnits + * and determines the resource item word, if possible. + */ + void write16(SRBRoot *bundle); + virtual void handleWrite16(SRBRoot *bundle); + + /** + * Calculates ("preflights") and advances the *byteOffset + * by the size of the resource's data in the binary file and + * determines the resource item word. + * + * Most handlePreWrite() functions may add any number of bytes, but preWrite() + * will always pad it to a multiple of 4. + * The resource item type may be a related subtype of the fType. + * + * The preWrite() and write() functions start and end at the same + * byteOffset values. + * Prewriting allows bundle.write() to determine the root resource item word, + * before actually writing the bundle contents to the file, + * which is necessary because the root item is stored at the beginning. + */ + void preWrite(uint32_t *byteOffset); + virtual void handlePreWrite(uint32_t *byteOffset); + + /** + * Writes the resource's data to mem and updates the byteOffset + * in parallel. + */ + void write(UNewDataMemory *mem, uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + int8_t fType; /* nominal type: fRes (when != 0xffffffff) may use subtype */ + UBool fWritten; /* res_write() can exit early */ + uint32_t fRes; /* resource item word; RES_BOGUS=0xffffffff if not known yet */ + int32_t fRes16; /* Res16 version of fRes for Table, Table16, Array16; -1 if it does not fit. */ + int32_t fKey; /* Index into bundle->fKeys; -1 if no key. */ + int32_t fKey16; /* Key16 version of fKey for Table & Table16; -1 if no key or it does not fit. */ + int line; /* used internally to report duplicate keys in tables */ + SResource *fNext; /* This is for internal chaining while building */ + struct UString fComment; +}; + +class ContainerResource : public SResource { +public: + ContainerResource(SRBRoot *bundle, const char *tag, int8_t type, + const UString* comment, UErrorCode &errorCode) + : SResource(bundle, tag, type, comment, errorCode), + fCount(0), fFirst(NULL) {} + virtual ~ContainerResource(); + + virtual void handlePreflightStrings(SRBRoot *bundle, UHashtable *stringSet, UErrorCode &errorCode); +protected: + void writeAllRes16(SRBRoot *bundle); + void preWriteAllRes(uint32_t *byteOffset); + void writeAllRes(UNewDataMemory *mem, uint32_t *byteOffset); + void writeAllRes32(UNewDataMemory *mem, uint32_t *byteOffset); + +public: + // TODO: private with getter? + uint32_t fCount; + SResource *fFirst; +}; + +class TableResource : public ContainerResource { +public: + TableResource(SRBRoot *bundle, const char *tag, + const UString* comment, UErrorCode &errorCode) + : ContainerResource(bundle, tag, URES_TABLE, comment, errorCode), + fTableType(URES_TABLE), fRoot(bundle) {} + virtual ~TableResource(); + + void add(SResource *res, int linenumber, UErrorCode &errorCode); + + virtual void handleWrite16(SRBRoot *bundle); + virtual void handlePreWrite(uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + int8_t fTableType; // determined by table_write16() for table_preWrite() & table_write() + SRBRoot *fRoot; +}; + +class ArrayResource : public ContainerResource { +public: + ArrayResource(SRBRoot *bundle, const char *tag, + const UString* comment, UErrorCode &errorCode) + : ContainerResource(bundle, tag, URES_ARRAY, comment, errorCode), + fLast(NULL) {} + virtual ~ArrayResource(); + + void add(SResource *res); + + virtual void handleWrite16(SRBRoot *bundle); + virtual void handlePreWrite(uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + SResource *fLast; +}; + +/** + * List of resources for a pool bundle. + * Writes an empty table resource, rather than a container structure. + */ +class PseudoListResource : public ContainerResource { +public: + PseudoListResource(SRBRoot *bundle, UErrorCode &errorCode) + : ContainerResource(bundle, NULL, URES_TABLE, NULL, errorCode) {} + virtual ~PseudoListResource(); + + void add(SResource *res); + + virtual void handleWrite16(SRBRoot *bundle); +}; + +class StringBaseResource : public SResource { +public: + StringBaseResource(SRBRoot *bundle, const char *tag, int8_t type, + const UChar *value, int32_t len, + const UString* comment, UErrorCode &errorCode); + StringBaseResource(SRBRoot *bundle, int8_t type, + const icu::UnicodeString &value, UErrorCode &errorCode); + StringBaseResource(int8_t type, const UChar *value, int32_t len, UErrorCode &errorCode); + virtual ~StringBaseResource(); + + const UChar *getBuffer() const { return fString.getBuffer(); } + int32_t length() const { return fString.length(); } + + virtual void handlePreWrite(uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + // TODO: private with getter? + icu::UnicodeString fString; +}; + +class StringResource : public StringBaseResource { +public: + StringResource(SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, + const UString* comment, UErrorCode &errorCode) + : StringBaseResource(bundle, tag, URES_STRING, value, len, comment, errorCode), + fSame(NULL), fSuffixOffset(0), + fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(0) {} + StringResource(SRBRoot *bundle, const icu::UnicodeString &value, UErrorCode &errorCode) + : StringBaseResource(bundle, URES_STRING, value, errorCode), + fSame(NULL), fSuffixOffset(0), + fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(0) {} + StringResource(int32_t poolStringIndex, int8_t numCharsForLength, + const UChar *value, int32_t length, + UErrorCode &errorCode) + : StringBaseResource(URES_STRING, value, length, errorCode), + fSame(NULL), fSuffixOffset(0), + fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(numCharsForLength) { + // v3 pool string encoded as string-v2 with low offset + fRes = URES_MAKE_RESOURCE(URES_STRING_V2, poolStringIndex); + fWritten = TRUE; + } + virtual ~StringResource(); + + int32_t get16BitStringsLength() const { + return fNumCharsForLength + length() + 1; // +1 for the NUL + } + + virtual void handlePreflightStrings(SRBRoot *bundle, UHashtable *stringSet, UErrorCode &errorCode); + virtual void handleWrite16(SRBRoot *bundle); + + void writeUTF16v2(int32_t base, icu::UnicodeString &dest); + + StringResource *fSame; // used for duplicates + int32_t fSuffixOffset; // this string is a suffix of fSame at this offset + int32_t fNumCopies; // number of equal strings represented by one stringSet element + int32_t fNumUnitsSaved; // from not writing duplicates and suffixes + int8_t fNumCharsForLength; +}; + +class AliasResource : public StringBaseResource { +public: + AliasResource(SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, + const UString* comment, UErrorCode &errorCode) + : StringBaseResource(bundle, tag, URES_ALIAS, value, len, comment, errorCode) {} + virtual ~AliasResource(); +}; + +class IntResource : public SResource { +public: + IntResource(SRBRoot *bundle, const char *tag, int32_t value, + const UString* comment, UErrorCode &errorCode); + virtual ~IntResource(); + + // TODO: private with getter? + int32_t fValue; +}; + +class IntVectorResource : public SResource { +public: + IntVectorResource(SRBRoot *bundle, const char *tag, + const UString* comment, UErrorCode &errorCode); + virtual ~IntVectorResource(); + + void add(int32_t value, UErrorCode &errorCode); + + virtual void handlePreWrite(uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + // TODO: UVector32 + uint32_t fCount; + uint32_t *fArray; +}; + +class BinaryResource : public SResource { +public: + BinaryResource(SRBRoot *bundle, const char *tag, + uint32_t length, uint8_t *data, const char* fileName, + const UString* comment, UErrorCode &errorCode); + virtual ~BinaryResource(); + + virtual void handlePreWrite(uint32_t *byteOffset); + virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset); + + // TODO: CharString? + uint32_t fLength; + uint8_t *fData; + // TODO: CharString + char* fFileName; // file name for binary or import binary tags if any +}; + +// TODO: use LocalPointer or delete +void res_close(struct SResource *res); + +void setIncludeCopyright(UBool val); +UBool getIncludeCopyright(void); + +void setFormatVersion(int32_t formatVersion); + +int32_t getFormatVersion(); + +void setUsePoolBundle(UBool use); + +/* in wrtxml.cpp */ +uint32_t computeCRC(const char *ptr, uint32_t len, uint32_t lastcrc); + +U_CDECL_END +#endif /* #ifndef RESLIST_H */ diff --git a/intl/icu/source/tools/genrb/rle.c b/intl/icu/source/tools/genrb/rle.c new file mode 100644 index 000000000..33e42d62b --- /dev/null +++ b/intl/icu/source/tools/genrb/rle.c @@ -0,0 +1,407 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2000-2003, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File writejava.c +* +* Modification History: +* +* Date Name Description +* 01/11/02 Ram Creation. +******************************************************************************* +*/ +#include "rle.h" +/** + * The ESCAPE character is used during run-length encoding. It signals + * a run of identical chars. + */ +static const uint16_t ESCAPE = 0xA5A5; + +/** + * The ESCAPE_BYTE character is used during run-length encoding. It signals + * a run of identical bytes. + */ +static const uint8_t ESCAPE_BYTE = (uint8_t)0xA5; + +/** + * Append a byte to the given StringBuffer, packing two bytes into each + * character. The state parameter maintains intermediary data between + * calls. + * @param state A two-element array, with state[0] == 0 if this is the + * first byte of a pair, or state[0] != 0 if this is the second byte + * of a pair, in which case state[1] is the first byte. + */ +static uint16_t* +appendEncodedByte(uint16_t* buffer, uint16_t* buffLimit, uint8_t value, uint8_t state[],UErrorCode* status) { + if(!status || U_FAILURE(*status)){ + return NULL; + } + if (state[0] != 0) { + uint16_t c = (uint16_t) ((state[1] << 8) | (((int32_t) value) & 0xFF)); + if(buffer < buffLimit){ + *buffer++ = c; + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + } + state[0] = 0; + return buffer; + } + else { + state[0] = 1; + state[1] = value; + return buffer; + } +} +/** + * Encode a run, possibly a degenerate run (of < 4 values). + * @param length The length of the run; must be > 0 && <= 0xFF. + */ +static uint16_t* +encodeRunByte(uint16_t* buffer,uint16_t* bufLimit, uint8_t value, int32_t length, uint8_t state[], UErrorCode* status) { + if(!status || U_FAILURE(*status)){ + return NULL; + } + if (length < 4) { + int32_t j=0; + for (; j<length; ++j) { + if (value == ESCAPE_BYTE) { + buffer = appendEncodedByte(buffer,bufLimit, ESCAPE_BYTE, state,status); + } + buffer = appendEncodedByte(buffer,bufLimit, value, state, status); + } + } + else { + if (length == ESCAPE_BYTE) { + if (value == ESCAPE_BYTE){ + buffer = appendEncodedByte(buffer, bufLimit,ESCAPE_BYTE, state,status); + } + buffer = appendEncodedByte(buffer,bufLimit, value, state, status); + --length; + } + buffer = appendEncodedByte(buffer,bufLimit, ESCAPE_BYTE, state,status); + buffer = appendEncodedByte(buffer,bufLimit, (char)length, state, status); + buffer = appendEncodedByte(buffer,bufLimit, value, state, status); /* Don't need to escape this value*/ + } + return buffer; +} + +#define APPEND( buffer, bufLimit, value, num, status){ \ + if(buffer<bufLimit){ \ + *buffer++=(value); \ + }else{ \ + *status = U_BUFFER_OVERFLOW_ERROR; \ + } \ + num++; \ +} + +/** + * Encode a run, possibly a degenerate run (of < 4 values). + * @param length The length of the run; must be > 0 && <= 0xFFFF. + */ +static uint16_t* +encodeRunShort(uint16_t* buffer,uint16_t* bufLimit, uint16_t value, int32_t length,UErrorCode* status) { + int32_t num=0; + if (length < 4) { + int j=0; + for (; j<length; ++j) { + if (value == (int32_t) ESCAPE){ + APPEND(buffer,bufLimit,ESCAPE, num, status); + + } + APPEND(buffer,bufLimit,value,num, status); + } + } + else { + if (length == (int32_t) ESCAPE) { + if (value == (int32_t) ESCAPE){ + APPEND(buffer,bufLimit,ESCAPE,num,status); + + } + APPEND(buffer,bufLimit,value,num,status); + --length; + } + APPEND(buffer,bufLimit,ESCAPE,num,status); + APPEND(buffer,bufLimit,(uint16_t) length, num,status); + APPEND(buffer,bufLimit,(uint16_t)value, num, status); /* Don't need to escape this value */ + } + return buffer; +} + +/** + * Construct a string representing a char array. Use run-length encoding. + * A character represents itself, unless it is the ESCAPE character. Then + * the following notations are possible: + * ESCAPE ESCAPE ESCAPE literal + * ESCAPE n c n instances of character c + * Since an encoded run occupies 3 characters, we only encode runs of 4 or + * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. + * If we encounter a run where n == ESCAPE, we represent this as: + * c ESCAPE n-1 c + * The ESCAPE value is chosen so as not to collide with commonly + * seen values. + */ +int32_t +usArrayToRLEString(const uint16_t* src,int32_t srcLen,uint16_t* buffer, int32_t bufLen,UErrorCode* status) { + uint16_t* bufLimit = buffer+bufLen; + uint16_t* saveBuffer = buffer; + if(buffer < bufLimit){ + *buffer++ = (uint16_t)(srcLen>>16); + if(buffer<bufLimit){ + uint16_t runValue = src[0]; + int32_t runLength = 1; + int i=1; + *buffer++ = (uint16_t) srcLen; + + for (; i<srcLen; ++i) { + uint16_t s = src[i]; + if (s == runValue && runLength < 0xFFFF){ + ++runLength; + }else { + buffer = encodeRunShort(buffer,bufLimit, (uint16_t)runValue, runLength,status); + runValue = s; + runLength = 1; + } + } + buffer= encodeRunShort(buffer,bufLimit,(uint16_t)runValue, runLength,status); + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + } + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + } + return (int32_t)(buffer - saveBuffer); +} + +/** + * Construct a string representing a byte array. Use run-length encoding. + * Two bytes are packed into a single char, with a single extra zero byte at + * the end if needed. A byte represents itself, unless it is the + * ESCAPE_BYTE. Then the following notations are possible: + * ESCAPE_BYTE ESCAPE_BYTE ESCAPE_BYTE literal + * ESCAPE_BYTE n b n instances of byte b + * Since an encoded run occupies 3 bytes, we only encode runs of 4 or + * more bytes. Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. + * If we encounter a run where n == ESCAPE_BYTE, we represent this as: + * b ESCAPE_BYTE n-1 b + * The ESCAPE_BYTE value is chosen so as not to collide with commonly + * seen values. + */ +int32_t +byteArrayToRLEString(const uint8_t* src,int32_t srcLen, uint16_t* buffer,int32_t bufLen, UErrorCode* status) { + const uint16_t* saveBuf = buffer; + uint16_t* bufLimit = buffer+bufLen; + if(buffer < bufLimit){ + *buffer++ = ((uint16_t) (srcLen >> 16)); + + if(buffer<bufLimit){ + uint8_t runValue = src[0]; + int runLength = 1; + uint8_t state[2]= {0}; + int i=1; + *buffer++=((uint16_t) srcLen); + for (; i<srcLen; ++i) { + uint8_t b = src[i]; + if (b == runValue && runLength < 0xFF){ + ++runLength; + } + else { + buffer = encodeRunByte(buffer, bufLimit,runValue, runLength, state,status); + runValue = b; + runLength = 1; + } + } + buffer = encodeRunByte(buffer,bufLimit, runValue, runLength, state, status); + + /* We must save the final byte, if there is one, by padding + * an extra zero. + */ + if (state[0] != 0) { + buffer = appendEncodedByte(buffer,bufLimit, 0, state ,status); + } + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + } + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + } + return (int32_t) (buffer - saveBuf); +} + + +/** + * Construct an array of shorts from a run-length encoded string. + */ +int32_t +rleStringToUCharArray(uint16_t* src, int32_t srcLen, uint16_t* target, int32_t tgtLen, UErrorCode* status) { + int32_t length = 0; + int32_t ai = 0; + int i=2; + + if(!status || U_FAILURE(*status)){ + return 0; + } + /* the source is null terminated */ + if(srcLen == -1){ + srcLen = u_strlen(src); + } + if(srcLen <= 2){ + return 2; + } + length = (((int32_t) src[0]) << 16) | ((int32_t) src[1]); + + if(target == NULL){ + return length; + } + if(tgtLen < length){ + *status = U_BUFFER_OVERFLOW_ERROR; + return length; + } + + for (; i<srcLen; ++i) { + uint16_t c = src[i]; + if (c == ESCAPE) { + c = src[++i]; + if (c == ESCAPE) { + target[ai++] = c; + } else { + int32_t runLength = (int32_t) c; + uint16_t runValue = src[++i]; + int j=0; + for (; j<runLength; ++j) { + target[ai++] = runValue; + } + } + } + else { + target[ai++] = c; + } + } + + if (ai != length){ + *status = U_INTERNAL_PROGRAM_ERROR; + } + + return length; +} + +/** + * Construct an array of bytes from a run-length encoded string. + */ +int32_t +rleStringToByteArray(uint16_t* src, int32_t srcLen, uint8_t* target, int32_t tgtLen, UErrorCode* status) { + + int32_t length = 0; + UBool nextChar = TRUE; + uint16_t c = 0; + int32_t node = 0; + int32_t runLength = 0; + int32_t i = 2; + int32_t ai=0; + + if(!status || U_FAILURE(*status)){ + return 0; + } + /* the source is null terminated */ + if(srcLen == -1){ + srcLen = u_strlen(src); + } + if(srcLen <= 2){ + return 2; + } + length = (((int32_t) src[0]) << 16) | ((int32_t) src[1]); + + if(target == NULL){ + return length; + } + if(tgtLen < length){ + *status = U_BUFFER_OVERFLOW_ERROR; + return length; + } + + for (; ai<tgtLen; ) { + /* This part of the loop places the next byte into the local + * variable 'b' each time through the loop. It keeps the + * current character in 'c' and uses the boolean 'nextChar' + * to see if we've taken both bytes out of 'c' yet. + */ + uint8_t b; + if (nextChar) { + c = src[i++]; + b = (uint8_t) (c >> 8); + nextChar = FALSE; + } + else { + b = (uint8_t) (c & 0xFF); + nextChar = TRUE; + } + + /* This part of the loop is a tiny state machine which handles + * the parsing of the run-length encoding. This would be simpler + * if we could look ahead, but we can't, so we use 'node' to + * move between three nodes in the state machine. + */ + switch (node) { + case 0: + /* Normal idle node */ + if (b == ESCAPE_BYTE) { + node = 1; + } + else { + target[ai++] = b; + } + break; + case 1: + /* We have seen one ESCAPE_BYTE; we expect either a second + * one, or a run length and value. + */ + if (b == ESCAPE_BYTE) { + target[ai++] = ESCAPE_BYTE; + node = 0; + } + else { + runLength = b; + node = 2; + } + break; + case 2: + { + int j=0; + /* We have seen an ESCAPE_BYTE and length byte. We interpret + * the next byte as the value to be repeated. + */ + for (; j<runLength; ++j){ + if(ai<tgtLen){ + target[ai++] = b; + }else{ + *status = U_BUFFER_OVERFLOW_ERROR; + return ai; + } + } + node = 0; + break; + } + } + } + + if (node != 0){ + *status = U_INTERNAL_PROGRAM_ERROR; + /*("Bad run-length encoded byte array")*/ + return 0; + } + + + if (i != srcLen){ + /*("Excess data in RLE byte array string");*/ + *status = U_INTERNAL_PROGRAM_ERROR; + return ai; + } + + return ai; +} + diff --git a/intl/icu/source/tools/genrb/rle.h b/intl/icu/source/tools/genrb/rle.h new file mode 100644 index 000000000..682ee7a88 --- /dev/null +++ b/intl/icu/source/tools/genrb/rle.h @@ -0,0 +1,74 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2000, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File writejava.c +* +* Modification History: +* +* Date Name Description +* 01/11/02 Ram Creation. +******************************************************************************* +*/ + +#ifndef RLE_H +#define RLE_H 1 + +#include "unicode/utypes.h" +#include "unicode/ustring.h" + +U_CDECL_BEGIN +/** + * Construct a string representing a byte array. Use run-length encoding. + * Two bytes are packed into a single char, with a single extra zero byte at + * the end if needed. A byte represents itself, unless it is the + * ESCAPE_BYTE. Then the following notations are possible: + * ESCAPE_BYTE ESCAPE_BYTE ESCAPE_BYTE literal + * ESCAPE_BYTE n b n instances of byte b + * Since an encoded run occupies 3 bytes, we only encode runs of 4 or + * more bytes. Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. + * If we encounter a run where n == ESCAPE_BYTE, we represent this as: + * b ESCAPE_BYTE n-1 b + * The ESCAPE_BYTE value is chosen so as not to collide with commonly + * seen values. + */ +int32_t +byteArrayToRLEString(const uint8_t* src,int32_t srcLen, uint16_t* buffer,int32_t bufLen, UErrorCode* status); + + +/** + * Construct a string representing a char array. Use run-length encoding. + * A character represents itself, unless it is the ESCAPE character. Then + * the following notations are possible: + * ESCAPE ESCAPE ESCAPE literal + * ESCAPE n c n instances of character c + * Since an encoded run occupies 3 characters, we only encode runs of 4 or + * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. + * If we encounter a run where n == ESCAPE, we represent this as: + * c ESCAPE n-1 c + * The ESCAPE value is chosen so as not to collide with commonly + * seen values. + */ +int32_t +usArrayToRLEString(const uint16_t* src,int32_t srcLen,uint16_t* buffer, int32_t bufLen,UErrorCode* status); + +/** + * Construct an array of bytes from a run-length encoded string. + */ +int32_t +rleStringToByteArray(uint16_t* src, int32_t srcLen, uint8_t* target, int32_t tgtLen, UErrorCode* status); +/** + * Construct an array of shorts from a run-length encoded string. + */ +int32_t +rleStringToUCharArray(uint16_t* src, int32_t srcLen, uint16_t* target, int32_t tgtLen, UErrorCode* status); + +U_CDECL_END + +#endif diff --git a/intl/icu/source/tools/genrb/ustr.c b/intl/icu/source/tools/genrb/ustr.c new file mode 100644 index 000000000..07e9b1538 --- /dev/null +++ b/intl/icu/source/tools/genrb/ustr.c @@ -0,0 +1,219 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2012, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File ustr.c +* +* Modification History: +* +* Date Name Description +* 05/28/99 stephen Creation. +******************************************************************************* +*/ + +#include "ustr.h" +#include "cmemory.h" +#include "cstring.h" +#include "unicode/ustring.h" +#include "unicode/putil.h" +#include "unicode/utf16.h" + +/* Protos */ +static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status); + +/* Macros */ +#define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1)) + +U_CFUNC void +ustr_init(struct UString *s) +{ + s->fChars = 0; + s->fLength = s->fCapacity = 0; +} + +U_CFUNC void +ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status) +{ + int i = 0; + if (U_FAILURE(*status)) return; + s->fChars = 0; + s->fLength = s->fCapacity = 0; + if (length == -1) { + length = (int32_t)uprv_strlen(source); + } + if(s->fCapacity < length) { + ustr_resize(s, ALLOCATION(length), status); + if(U_FAILURE(*status)) return; + } + for (; i < length; i++) + { + UChar charToAppend; + u_charsToUChars(source+i, &charToAppend, 1); + ustr_ucat(s, charToAppend, status); + /* +#if U_CHARSET_FAMILY==U_ASCII_FAMILY + ustr_ucat(s, (UChar)(uint8_t)(source[i]), status); +#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY + ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status); +#else +# error U_CHARSET_FAMILY is not valid +#endif + */ + } +} + +U_CFUNC void +ustr_deinit(struct UString *s) +{ + if (s) { + uprv_free(s->fChars); + s->fChars = 0; + s->fLength = s->fCapacity = 0; + } +} + +U_CFUNC void +ustr_cpy(struct UString *dst, + const struct UString *src, + UErrorCode *status) +{ + if(U_FAILURE(*status) || dst == src) + return; + + if(dst->fCapacity < src->fLength) { + ustr_resize(dst, ALLOCATION(src->fLength), status); + if(U_FAILURE(*status)) + return; + } + if(src->fChars == NULL || dst->fChars == NULL){ + return; + } + u_memcpy(dst->fChars, src->fChars, src->fLength); + dst->fLength = src->fLength; + dst->fChars[dst->fLength] = 0x0000; +} + +U_CFUNC void +ustr_setlen(struct UString *s, + int32_t len, + UErrorCode *status) +{ + if(U_FAILURE(*status)) + return; + + if(s->fCapacity < (len + 1)) { + ustr_resize(s, ALLOCATION(len), status); + if(U_FAILURE(*status)) + return; + } + + s->fLength = len; + s->fChars[len] = 0x0000; +} + +U_CFUNC void +ustr_cat(struct UString *dst, + const struct UString *src, + UErrorCode *status) +{ + ustr_ncat(dst, src, src->fLength, status); +} + +U_CFUNC void +ustr_ncat(struct UString *dst, + const struct UString *src, + int32_t n, + UErrorCode *status) +{ + if(U_FAILURE(*status) || dst == src) + return; + + if(dst->fCapacity < (dst->fLength + n)) { + ustr_resize(dst, ALLOCATION(dst->fLength + n), status); + if(U_FAILURE(*status)) + return; + } + + uprv_memcpy(dst->fChars + dst->fLength, src->fChars, + sizeof(UChar) * n); + dst->fLength += src->fLength; + dst->fChars[dst->fLength] = 0x0000; +} + +U_CFUNC void +ustr_ucat(struct UString *dst, + UChar c, + UErrorCode *status) +{ + if(U_FAILURE(*status)) + return; + + if(dst->fCapacity < (dst->fLength + 1)) { + ustr_resize(dst, ALLOCATION(dst->fLength + 1), status); + if(U_FAILURE(*status)) + return; + } + + uprv_memcpy(dst->fChars + dst->fLength, &c, + sizeof(UChar) * 1); + dst->fLength += 1; + dst->fChars[dst->fLength] = 0x0000; +} +U_CFUNC void +ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status){ + if(c > 0x10FFFF){ + *status = U_ILLEGAL_CHAR_FOUND; + return; + } + if(c >0xFFFF){ + ustr_ucat(dst, U16_LEAD(c), status); + ustr_ucat(dst, U16_TRAIL(c), status); + }else{ + ustr_ucat(dst, (UChar) c, status); + } +} +U_CFUNC void +ustr_uscat(struct UString *dst, + const UChar* src,int len, + UErrorCode *status) +{ + if(U_FAILURE(*status)) + return; + + if(dst->fCapacity < (dst->fLength + len)) { + ustr_resize(dst, ALLOCATION(dst->fLength + len), status); + if(U_FAILURE(*status)) + return; + } + + uprv_memcpy(dst->fChars + dst->fLength, src, + sizeof(UChar) * len); + dst->fLength += len; + dst->fChars[dst->fLength] = 0x0000; +} + +/* Destroys data in the string */ +static void +ustr_resize(struct UString *s, + int32_t len, + UErrorCode *status) +{ + if(U_FAILURE(*status)) + return; + + /* +1 for trailing 0x0000 */ + s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1)); + if(s->fChars == 0) { + *status = U_MEMORY_ALLOCATION_ERROR; + s->fLength = s->fCapacity = 0; + return; + } + + s->fCapacity = len; +} diff --git a/intl/icu/source/tools/genrb/ustr.h b/intl/icu/source/tools/genrb/ustr.h new file mode 100644 index 000000000..c27a78104 --- /dev/null +++ b/intl/icu/source/tools/genrb/ustr.h @@ -0,0 +1,81 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2012, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File ustr.h +* +* Modification History: +* +* Date Name Description +* 05/28/99 stephen Creation. +******************************************************************************* +*/ + +#ifndef USTR_H +#define USTR_H 1 + +#include "unicode/utypes.h" + +#define U_APPEND_CHAR32(c,target,len) { \ + if (c <= 0xffff) \ + { \ + *(target)++ = (UChar) c; \ + len=1; \ + } \ + else \ + { \ + target[0] = U16_LEAD(c); \ + target[1] = U16_TRAIL(c); \ + len=2; \ + target +=2; \ + } \ +} + +#define U_APPEND_CHAR32_ONLY(c,target) { \ + if (c <= 0xffff) \ + { \ + *(target)++ = (UChar) c; \ + } \ + else \ + { \ + target[0] = U16_LEAD(c); \ + target[1] = U16_TRAIL(c); \ + target +=2; \ + } \ +} + +/* A C representation of a string "object" (to avoid realloc all the time) */ +struct UString { + UChar *fChars; + int32_t fLength; + int32_t fCapacity; +}; + +U_CFUNC void ustr_init(struct UString *s); + +U_CFUNC void +ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status); + +U_CFUNC void ustr_deinit(struct UString *s); + +U_CFUNC void ustr_setlen(struct UString *s, int32_t len, UErrorCode *status); + +U_CFUNC void ustr_cpy(struct UString *dst, const struct UString *src, + UErrorCode *status); + +U_CFUNC void ustr_cat(struct UString *dst, const struct UString *src, + UErrorCode *status); + +U_CFUNC void ustr_ncat(struct UString *dst, const struct UString *src, + int32_t n, UErrorCode *status); + +U_CFUNC void ustr_ucat(struct UString *dst, UChar c, UErrorCode *status); +U_CFUNC void ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status); +U_CFUNC void ustr_uscat(struct UString *dst, const UChar* src,int len,UErrorCode *status); +#endif diff --git a/intl/icu/source/tools/genrb/wrtjava.cpp b/intl/icu/source/tools/genrb/wrtjava.cpp new file mode 100644 index 000000000..d5cc942ef --- /dev/null +++ b/intl/icu/source/tools/genrb/wrtjava.cpp @@ -0,0 +1,689 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2000-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File wrtjava.cpp +* +* Modification History: +* +* Date Name Description +* 01/11/02 Ram Creation. +* 02/12/08 Spieth Fix errant 'new Object[][]{' insertion +* 02/19/08 Spieth Removed ICUListResourceBundle dependancy +******************************************************************************* +*/ + +#include <assert.h> +#include "unicode/unistr.h" +#include "reslist.h" +#include "unewdata.h" +#include "unicode/ures.h" +#include "errmsg.h" +#include "filestrm.h" +#include "cstring.h" +#include "unicode/ucnv.h" +#include "genrb.h" +#include "rle.h" +#include "uhash.h" +#include "uresimp.h" +#include "unicode/ustring.h" + +void res_write_java(struct SResource *res,UErrorCode *status); + + +static const char copyRight[] = + "/* \n" + " *******************************************************************************\n" + " *\n" + " * Copyright (C) International Business Machines\n" + " * Corporation and others. All Rights Reserved.\n" + " *\n" + " *******************************************************************************\n" + " * $" "Source: $ \n" + " * $" "Date: $ \n" + " * $" "Revision: $ \n" + " *******************************************************************************\n" + " */\n\n"; +static const char warningMsg[] = + "/*********************************************************************\n" + "######################################################################\n" + "\n" + " WARNING: This file is generated by genrb Version " GENRB_VERSION ".\n" + " If you edit this file, please make sure that, the source\n" + " of this file (XXXX.txt in LocaleElements_XXXX.java)\n" + " is also edited.\n" + "######################################################################\n" + " *********************************************************************\n" + " */\n\n"; +static const char* openBrace="{\n"; +static const char* closeClass=" };\n" + "}\n"; + +static const char* javaClass = "import java.util.ListResourceBundle;\n\n" + "public class "; + +static const char* javaClass1= " extends ListResourceBundle {\n\n" + " /**\n" + " * Overrides ListResourceBundle \n" + " */\n" + " public final Object[][] getContents() { \n" + " return contents;\n" + " }\n\n" + " private static Object[][] contents = {\n"; +/*static const char* javaClassICU= " extends ListResourceBundle {\n\n" + " public %s () {\n" + " super.contents = data;\n" + " }\n" + " static final Object[][] data = new Object[][] { \n";*/ +static int tabCount = 3; + +static FileStream* out=NULL; +static struct SRBRoot* srBundle ; +/*static const char* outDir = NULL;*/ + +static const char* bName=NULL; +static const char* pName=NULL; + +static void write_tabs(FileStream* os){ + int i=0; + for(;i<=tabCount;i++){ + T_FileStream_write(os," ",4); + } +} + +#define ZERO 0x30 + +static const char* enc =""; +static UConverter* conv = NULL; + +static int32_t +uCharsToChars(char *target, int32_t targetLen, const UChar *source, int32_t sourceLen, UErrorCode *status) { + int i=0, j=0; + char str[30]={'\0'}; + while(i<sourceLen){ + if (source[i] == '\n') { + if (j + 2 < targetLen) { + uprv_strcat(target, "\\n"); + } + j += 2; + }else if(source[i]==0x0D){ + if(j+2<targetLen){ + uprv_strcat(target,"\\f"); + } + j+=2; + }else if(source[i] == '"'){ + if(source[i-1]=='\''){ + if(j+2<targetLen){ + uprv_strcat(target,"\\"); + target[j+1]= (char)source[i]; + } + j+=2; + }else if(source[i-1]!='\\'){ + + if(j+2<targetLen){ + uprv_strcat(target,"\\"); + target[j+1]= (char)source[i]; + } + j+=2; + }else if(source[i-1]=='\\'){ + target[j++]= (char)source[i]; + } + }else if(source[i]=='\\'){ + if(i+1<sourceLen){ + switch(source[i+1]){ + case ',': + case '!': + case '?': + case '#': + case '.': + case '%': + case '&': + case ':': + case ';': + if(j+2<targetLen){ + uprv_strcat(target,"\\\\"); + } + j+=2; + break; + case '"': + case '\'': + if(j+3<targetLen){ + uprv_strcat(target,"\\\\\\"); + } + j+=3; + break; + default : + if(j<targetLen){ + target[j]=(char)source[i]; + } + j++; + break; + } + }else{ + if(j<targetLen){ + uprv_strcat(target,"\\\\"); + } + j+=2; + } + }else if(source[i]>=0x20 && source[i]<0x7F/*ASCII*/){ + if(j<targetLen){ + target[j] = (char) source[i]; + } + j++; + }else{ + if(*enc =='\0' || source[i]==0x0000){ + uprv_strcpy(str,"\\u"); + itostr(str+2,source[i],16,4); + if(j+6<targetLen){ + uprv_strcat(target,str); + } + j+=6; + }else{ + char dest[30] = {0}; + int retVal=ucnv_fromUChars(conv,dest,30,source+i,1,status); + if(U_FAILURE(*status)){ + return 0; + } + if(j+retVal<targetLen){ + uprv_strcat(target,dest); + } + j+=retVal; + } + } + i++; + } + return j; +} + + +static uint32_t +strrch(const char* source,uint32_t sourceLen,char find){ + const char* tSourceEnd =source + (sourceLen-1); + while(tSourceEnd>= source){ + if(*tSourceEnd==find){ + return (uint32_t)(tSourceEnd-source); + } + tSourceEnd--; + } + return (uint32_t)(tSourceEnd-source); +} + +static int32_t getColumnCount(int32_t len){ + int32_t columnCount = 80; + int32_t maxLines = 3000; + int32_t adjustedLen = len*5; /* assume that every codepoint is represented in \uXXXX format*/ + /* + * calculate the number of lines that + * may be required if column count is 80 + */ + if (maxLines < (adjustedLen / columnCount) ){ + columnCount = adjustedLen / maxLines; + } + return columnCount; +} +static void +str_write_java(const UChar *src, int32_t srcLen, UBool printEndLine, UErrorCode *status) { + + uint32_t length = srcLen*8; + uint32_t bufLen = 0; + uint32_t columnCount; + char* buf = (char*) malloc(sizeof(char)*length); + + if(buf == NULL) { + *status = U_MEMORY_ALLOCATION_ERROR; + return; + } + + columnCount = getColumnCount(srcLen); + memset(buf,0,length); + + bufLen = uCharsToChars(buf,length,src,srcLen,status); + + if(printEndLine) + write_tabs(out); + + if(U_FAILURE(*status)){ + uprv_free(buf); + return; + } + + if(bufLen+(tabCount*4) > columnCount ){ + uint32_t len = 0; + char* current = buf; + uint32_t add; + while(len < bufLen){ + add = columnCount-(tabCount*4)-5/* for ", +\n */; + current = buf +len; + if (add < (bufLen-len)) { + uint32_t idx = strrch(current,add,'\\'); + if (idx > add) { + idx = add; + } else { + int32_t num =idx-1; + uint32_t seqLen; + while(num>0){ + if(current[num]=='\\'){ + num--; + }else{ + break; + } + } + if ((idx-num)%2==0) { + idx--; + } + seqLen = (current[idx+1]=='u') ? 6 : 2; + if ((add-idx) < seqLen) { + add = idx + seqLen; + } + } + } + T_FileStream_write(out,"\"",1); + if(len+add<bufLen){ + T_FileStream_write(out,current,add); + T_FileStream_write(out,"\" +\n",4); + write_tabs(out); + }else{ + T_FileStream_write(out,current,bufLen-len); + } + len+=add; + } + }else{ + T_FileStream_write(out,"\"",1); + T_FileStream_write(out, buf,bufLen); + } + if(printEndLine){ + T_FileStream_write(out,"\",\n",3); + }else{ + T_FileStream_write(out,"\"",1); + } + uprv_free(buf); +} + +/* Writing Functions */ +static void +string_write_java(const StringResource *res,UErrorCode *status) { + (void)res->getKeyString(srBundle); + + str_write_java(res->getBuffer(), res->length(), TRUE, status); +} + +static void +array_write_java(const ArrayResource *res, UErrorCode *status) { + + uint32_t i = 0; + const char* arr ="new String[] { \n"; + struct SResource *current = NULL; + UBool allStrings = TRUE; + + if (U_FAILURE(*status)) { + return; + } + + if (res->fCount > 0) { + + current = res->fFirst; + i = 0; + while(current != NULL){ + if(!current->isString()){ + allStrings = FALSE; + break; + } + current= current->fNext; + } + + current = res->fFirst; + if(allStrings==FALSE){ + const char* object = "new Object[]{\n"; + write_tabs(out); + T_FileStream_write(out, object, (int32_t)uprv_strlen(object)); + tabCount++; + }else{ + write_tabs(out); + T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr)); + tabCount++; + } + while (current != NULL) { + /*if(current->isString()){ + write_tabs(out); + }*/ + res_write_java(current, status); + if(U_FAILURE(*status)){ + return; + } + i++; + current = current->fNext; + } + T_FileStream_write(out,"\n",1); + + tabCount--; + write_tabs(out); + T_FileStream_write(out,"},\n",3); + + } else { + write_tabs(out); + T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr)); + write_tabs(out); + T_FileStream_write(out,"},\n",3); + } +} + +static void +intvector_write_java(const IntVectorResource *res, UErrorCode * /*status*/) { + uint32_t i = 0; + const char* intArr = "new int[] {\n"; + /* const char* intC = "new Integer("; */ + const char* stringArr = "new String[]{\n"; + const char *resname = res->getKeyString(srBundle); + char buf[100]; + int len =0; + buf[0]=0; + write_tabs(out); + + if(resname != NULL && uprv_strcmp(resname,"DateTimeElements")==0){ + T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr)); + tabCount++; + for(i = 0; i<res->fCount; i++) { + write_tabs(out); + len=itostr(buf,res->fArray[i],10,0); + T_FileStream_write(out,"\"",1); + T_FileStream_write(out,buf,len); + T_FileStream_write(out,"\",",2); + T_FileStream_write(out,"\n",1); + } + }else{ + T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr)); + tabCount++; + for(i = 0; i<res->fCount; i++) { + write_tabs(out); + /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */ + len=itostr(buf,res->fArray[i],10,0); + T_FileStream_write(out,buf,len); + /* T_FileStream_write(out,"),",2); */ + /* T_FileStream_write(out,"\n",1); */ + T_FileStream_write(out,",\n",2); + } + } + tabCount--; + write_tabs(out); + T_FileStream_write(out,"},\n",3); +} + +static void +int_write_java(const IntResource *res, UErrorCode * /*status*/) { + const char* intC = "new Integer("; + char buf[100]; + int len =0; + buf[0]=0; + + /* write the binary data */ + write_tabs(out); + T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); + len=itostr(buf, res->fValue, 10, 0); + T_FileStream_write(out,buf,len); + T_FileStream_write(out,"),\n",3 ); + +} + +static void +bytes_write_java(const BinaryResource *res, UErrorCode * /*status*/) { + const char* type = "new byte[] {"; + const char* byteDecl = "%i, "; + char byteBuffer[100] = { 0 }; + uint8_t* byteArray = NULL; + int byteIterator = 0; + + int32_t srcLen=res->fLength; + + if(srcLen>0 ) + { + byteArray = res->fData; + + write_tabs(out); + T_FileStream_write(out, type, (int32_t)uprv_strlen(type)); + T_FileStream_write(out, "\n", 1); + tabCount++; + + for (;byteIterator<srcLen;byteIterator++) + { + if (byteIterator%16 == 0) + { + write_tabs(out); + } + + if (byteArray[byteIterator] < 128) + { + sprintf(byteBuffer, byteDecl, byteArray[byteIterator]); + } + else + { + sprintf(byteBuffer, byteDecl, (byteArray[byteIterator]-256)); + } + + T_FileStream_write(out, byteBuffer, (int32_t)uprv_strlen(byteBuffer)); + + if (byteIterator%16 == 15) + { + T_FileStream_write(out, "\n", 1); + } + + } + + if (((byteIterator-1)%16) != 15) + { + T_FileStream_write(out, "\n", 1); + } + + tabCount--; + write_tabs(out); + T_FileStream_write(out, "},\n", 3); + + } + else + { + /* Empty array */ + write_tabs(out); + T_FileStream_write(out,type,(int32_t)uprv_strlen(type)); + T_FileStream_write(out,"},\n",3); + } + +} + +static UBool start = TRUE; + +static void +table_write_java(const TableResource *res, UErrorCode *status) { + uint32_t i = 0; + struct SResource *current = NULL; + const char* obj = "new Object[][]{\n"; + + if (U_FAILURE(*status)) { + return ; + } + + if (res->fCount > 0) { + if(start==FALSE){ + write_tabs(out); + T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj)); + tabCount++; + } + start = FALSE; + current = res->fFirst; + i = 0; + + + while (current != NULL) { + const char *currentKeyString = current->getKeyString(srBundle); + + assert(i < res->fCount); + write_tabs(out); + + T_FileStream_write(out, openBrace, 2); + + + tabCount++; + + write_tabs(out); + if(currentKeyString != NULL) { + T_FileStream_write(out, "\"", 1); + T_FileStream_write(out, currentKeyString, + (int32_t)uprv_strlen(currentKeyString)); + T_FileStream_write(out, "\",\n", 2); + + T_FileStream_write(out, "\n", 1); + } + res_write_java(current, status); + if(U_FAILURE(*status)){ + return; + } + i++; + current = current->fNext; + tabCount--; + write_tabs(out); + T_FileStream_write(out, "},\n", 3); + } + if(tabCount>4){ + tabCount--; + write_tabs(out); + T_FileStream_write(out, "},\n", 3); + } + + } else { + write_tabs(out); + T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj)); + + write_tabs(out); + T_FileStream_write(out,"},\n",3); + + } + +} + +void +res_write_java(struct SResource *res,UErrorCode *status) { + + if (U_FAILURE(*status)) { + return ; + } + + if (res != NULL) { + switch (res->fType) { + case URES_STRING: + string_write_java (static_cast<const StringResource *>(res), status); + return; + case URES_ALIAS: + printf("Encountered unsupported resource type %d of alias\n", res->fType); + *status = U_UNSUPPORTED_ERROR; + return; + case URES_INT_VECTOR: + intvector_write_java (static_cast<const IntVectorResource *>(res), status); + return; + case URES_BINARY: + bytes_write_java (static_cast<const BinaryResource *>(res), status); + return; + case URES_INT: + int_write_java (static_cast<const IntResource *>(res), status); + return; + case URES_ARRAY: + array_write_java (static_cast<const ArrayResource *>(res), status); + return; + case URES_TABLE: + table_write_java (static_cast<const TableResource *>(res), status); + return; + default: + break; + } + } + + *status = U_INTERNAL_PROGRAM_ERROR; +} + +void +bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, + char *writtenFilename, int writtenFilenameLen, + const char* packageName, const char* bundleName, + UErrorCode *status) { + + char fileName[256] = {'\0'}; + char className[256]={'\0'}; + /*char constructor[1000] = { 0 };*/ + /*UBool j1 =FALSE;*/ + /*outDir = outputDir;*/ + + start = TRUE; /* Reset the start indictor*/ + + bName = (bundleName==NULL) ? "LocaleElements" : bundleName; + pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName; + + uprv_strcpy(className, bName); + srBundle = bundle; + if(uprv_strcmp(srBundle->fLocale,"root")!=0){ + uprv_strcat(className,"_"); + uprv_strcat(className,srBundle->fLocale); + } + if(outputDir){ + uprv_strcpy(fileName, outputDir); + if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){ + uprv_strcat(fileName,U_FILE_SEP_STRING); + } + uprv_strcat(fileName,className); + uprv_strcat(fileName,".java"); + }else{ + uprv_strcat(fileName,className); + uprv_strcat(fileName,".java"); + } + + if (writtenFilename) { + uprv_strncpy(writtenFilename, fileName, writtenFilenameLen); + } + + if (U_FAILURE(*status)) { + return; + } + + out= T_FileStream_open(fileName,"w"); + + if(out==NULL){ + *status = U_FILE_ACCESS_ERROR; + return; + } + if(getIncludeCopyright()){ + T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight)); + T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg)); + } + T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package ")); + T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName)); + T_FileStream_write(out,";\n\n",3); + T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass)); + T_FileStream_write(out, className, (int32_t)uprv_strlen(className)); + T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); + + /* if(j1){ + T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); + }else{ + sprintf(constructor,javaClassICU,className); + T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor)); + } + */ + + if(outputEnc && *outputEnc!='\0'){ + /* store the output encoding */ + enc = outputEnc; + conv=ucnv_open(enc,status); + if(U_FAILURE(*status)){ + return; + } + } + res_write_java(bundle->fRoot, status); + + T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass)); + + T_FileStream_close(out); + + ucnv_close(conv); +} diff --git a/intl/icu/source/tools/genrb/wrtxml.cpp b/intl/icu/source/tools/genrb/wrtxml.cpp new file mode 100644 index 000000000..62fdd2427 --- /dev/null +++ b/intl/icu/source/tools/genrb/wrtxml.cpp @@ -0,0 +1,1212 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 2002-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File wrtxml.cpp +* +* Modification History: +* +* Date Name Description +* 10/01/02 Ram Creation. +* 02/07/08 Spieth Correct XLIFF generation on EBCDIC platform +* +******************************************************************************* +*/ + +// Safer use of UnicodeString. +#ifndef UNISTR_FROM_CHAR_EXPLICIT +# define UNISTR_FROM_CHAR_EXPLICIT explicit +#endif + +// Less important, but still a good idea. +#ifndef UNISTR_FROM_STRING_EXPLICIT +# define UNISTR_FROM_STRING_EXPLICIT explicit +#endif + +#include "reslist.h" +#include "unewdata.h" +#include "unicode/ures.h" +#include "errmsg.h" +#include "filestrm.h" +#include "cstring.h" +#include "unicode/ucnv.h" +#include "genrb.h" +#include "rle.h" +#include "uhash.h" +#include "uresimp.h" +#include "unicode/ustring.h" +#include "unicode/uchar.h" +#include "ustr.h" +#include "prscmnts.h" +#include "unicode/unistr.h" +#include "unicode/utf8.h" +#include "unicode/utf16.h" +#include <time.h> + +U_NAMESPACE_USE + +static int tabCount = 0; + +static FileStream* out=NULL; +static struct SRBRoot* srBundle ; +static const char* outDir = NULL; +static const char* enc =""; +static UConverter* conv = NULL; + +const char* const* ISOLanguages; +const char* const* ISOCountries; +const char* textExt = ".txt"; +const char* xliffExt = ".xlf"; + +static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString) +{ + UErrorCode status = U_ZERO_ERROR; + int32_t len = 0; + + // preflight to get the destination buffer size + u_strToUTF8(NULL, + 0, + &len, + outString.getBuffer(), + outString.length(), + &status); + + // allocate the buffer + char* dest = (char*)uprv_malloc(len); + status = U_ZERO_ERROR; + + // convert the data + u_strToUTF8(dest, + len, + &len, + outString.getBuffer(), + outString.length(), + &status); + + // write data to out file + int32_t ret = T_FileStream_write(fileStream, dest, len); + uprv_free(dest); + return (ret); +} + +/*write indentation for formatting*/ +static void write_tabs(FileStream* os){ + int i=0; + for(;i<=tabCount;i++){ + write_utf8_file(os,UnicodeString(" ")); + } +} + +/*get ID for each element. ID is globally unique.*/ +static char* getID(const char* id, const char* curKey, char* result) { + if(curKey == NULL) { + result = (char *)uprv_malloc(sizeof(char)*uprv_strlen(id) + 1); + uprv_memset(result, 0, sizeof(char)*uprv_strlen(id) + 1); + uprv_strcpy(result, id); + } else { + result = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1); + uprv_memset(result, 0, sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1); + if(id[0]!='\0'){ + uprv_strcpy(result, id); + uprv_strcat(result, "_"); + } + uprv_strcat(result, curKey); + } + return result; +} + +/*compute CRC for binary code*/ +/* The code is from http://www.theorem.com/java/CRC32.java + * Calculates the CRC32 - 32 bit Cyclical Redundancy Check + * <P> This check is used in numerous systems to verify the integrity + * of information. It's also used as a hashing function. Unlike a regular + * checksum, it's sensitive to the order of the characters. + * It produces a 32 bit + * + * @author Michael Lecuyer (mjl@theorem.com) + * @version 1.1 August 11, 1998 + */ + +/* ICU is not endian portable, because ICU data generated on big endian machines can be + * ported to big endian machines but not to little endian machines and vice versa. The + * conversion is not portable across platforms with different endianess. + */ + +uint32_t computeCRC(const char *ptr, uint32_t len, uint32_t lastcrc){ + int32_t crc; + uint32_t temp1; + uint32_t temp2; + + int32_t crc_ta[256]; + int i = 0; + int j = 0; + uint32_t crc2 = 0; + +#define CRC32_POLYNOMIAL 0xEDB88320 + + /*build crc table*/ + for (i = 0; i <= 255; i++) { + crc2 = i; + for (j = 8; j > 0; j--) { + if ((crc2 & 1) == 1) { + crc2 = (crc2 >> 1) ^ CRC32_POLYNOMIAL; + } else { + crc2 >>= 1; + } + } + crc_ta[i] = crc2; + } + + crc = lastcrc; + while(len--!=0) { + temp1 = (uint32_t)crc>>8; + temp2 = crc_ta[(crc^*ptr) & 0xFF]; + crc = temp1^temp2; + ptr++; + } + return(crc); +} + +static void strnrepchr(char* src, int32_t srcLen, char s, char r){ + int32_t i = 0; + for(i=0;i<srcLen;i++){ + if(src[i]==s){ + src[i]=r; + } + } +} +/* Parse the filename, and get its language information. + * If it fails to get the language information from the filename, + * use "en" as the default value for language + */ +static char* parseFilename(const char* id, char* /*lang*/) { + int idLen = (int) uprv_strlen(id); + char* localeID = (char*) uprv_malloc(idLen); + int pos = 0; + int canonCapacity = 0; + char* canon = NULL; + int canonLen = 0; + /*int i;*/ + UErrorCode status = U_ZERO_ERROR; + const char *ext = uprv_strchr(id, '.'); + + if(ext != NULL){ + pos = (int) (ext - id); + } else { + pos = idLen; + } + uprv_memcpy(localeID, id, pos); + localeID[pos]=0; /* NUL terminate the string */ + + canonCapacity =pos*3; + canon = (char*) uprv_malloc(canonCapacity); + canonLen = uloc_canonicalize(localeID, canon, canonCapacity, &status); + + if(U_FAILURE(status)){ + fprintf(stderr, "Could not canonicalize the locale ID: %s. Error: %s\n", localeID, u_errorName(status)); + exit(status); + } + strnrepchr(canon, canonLen, '_', '-'); + return canon; +} + +static const char* xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; +#if 0 +static const char* bundleStart = "<xliff version = \"1.2\" " + "xmlns='urn:oasis:names:tc:xliff:document:1.2' " + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd'>\n"; +#else +static const char* bundleStart = "<xliff version = \"1.1\" " + "xmlns='urn:oasis:names:tc:xliff:document:1.1' " + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.1 http://www.oasis-open.org/committees/xliff/documents/xliff-core-1.1.xsd'>\n"; +#endif +static const char* bundleEnd = "</xliff>\n"; + +void res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status); + +static char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength, + const UChar* src, int32_t srcLen, UErrorCode* status){ + int32_t srcIndex=0; + char* dest=NULL; + char* temp=NULL; + int32_t destLen=0; + UChar32 c = 0; + + if(status==NULL || U_FAILURE(*status) || pDest==NULL || srcLen==0 || src == NULL){ + return NULL; + } + dest =*pDest; + if(dest==NULL || destCap <=0){ + destCap = srcLen * 8; + dest = (char*) uprv_malloc(sizeof(char) * destCap); + if(dest==NULL){ + *status=U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + } + + dest[0]=0; + + while(srcIndex<srcLen){ + U16_NEXT(src, srcIndex, srcLen, c); + + if (U16_IS_LEAD(c) || U16_IS_TRAIL(c)) { + *status = U_ILLEGAL_CHAR_FOUND; + fprintf(stderr, "Illegal Surrogate! \n"); + uprv_free(dest); + return NULL; + } + + if((destLen+U8_LENGTH(c)) < destCap){ + + /* ASCII Range */ + if(c <=0x007F){ + switch(c) { + case '\x26': + uprv_strcpy(dest+( destLen),"\x26\x61\x6d\x70\x3b"); /* &*/ + destLen+=(int32_t)uprv_strlen("\x26\x61\x6d\x70\x3b"); + break; + case '\x3c': + uprv_strcpy(dest+(destLen),"\x26\x6c\x74\x3b"); /* <*/ + destLen+=(int32_t)uprv_strlen("\x26\x6c\x74\x3b"); + break; + case '\x3e': + uprv_strcpy(dest+(destLen),"\x26\x67\x74\x3b"); /* >*/ + destLen+=(int32_t)uprv_strlen("\x26\x67\x74\x3b"); + break; + case '\x22': + uprv_strcpy(dest+(destLen),"\x26\x71\x75\x6f\x74\x3b"); /* "*/ + destLen+=(int32_t)uprv_strlen("\x26\x71\x75\x6f\x74\x3b"); + break; + case '\x27': + uprv_strcpy(dest+(destLen),"\x26\x61\x70\x6f\x73\x3b"); /* ' */ + destLen+=(int32_t)uprv_strlen("\x26\x61\x70\x6f\x73\x3b"); + break; + + /* Disallow C0 controls except TAB, CR, LF*/ + case 0x00: + case 0x01: + case 0x02: + case 0x03: + case 0x04: + case 0x05: + case 0x06: + case 0x07: + case 0x08: + /*case 0x09:*/ + /*case 0x0A: */ + case 0x0B: + case 0x0C: + /*case 0x0D:*/ + case 0x0E: + case 0x0F: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + case 0x18: + case 0x19: + case 0x1A: + case 0x1B: + case 0x1C: + case 0x1D: + case 0x1E: + case 0x1F: + *status = U_ILLEGAL_CHAR_FOUND; + fprintf(stderr, "Illegal Character \\u%04X!\n",(int)c); + uprv_free(dest); + return NULL; + default: + dest[destLen++]=(char)c; + } + }else{ + UBool isError = FALSE; + U8_APPEND((unsigned char*)dest,destLen,destCap,c,isError); + if(isError){ + *status = U_ILLEGAL_CHAR_FOUND; + fprintf(stderr, "Illegal Character \\U%08X!\n",(int)c); + uprv_free(dest); + return NULL; + } + } + }else{ + destCap += destLen; + + temp = (char*) uprv_malloc(sizeof(char)*destCap); + if(temp==NULL){ + *status=U_MEMORY_ALLOCATION_ERROR; + uprv_free(dest); + return NULL; + } + uprv_memmove(temp,dest,destLen); + destLen=0; + uprv_free(dest); + dest=temp; + temp=NULL; + } + + } + *destLength = destLen; + return dest; +} + +#define ASTERISK 0x002A +#define SPACE 0x0020 +#define CR 0x000A +#define LF 0x000D +#define AT_SIGN 0x0040 + +static void +trim(char **src, int32_t *len){ + + char *s = NULL; + int32_t i = 0; + if(src == NULL || *src == NULL){ + return; + } + s = *src; + /* trim from the end */ + for( i=(*len-1); i>= 0; i--){ + switch(s[i]){ + case ASTERISK: + case SPACE: + case CR: + case LF: + s[i] = 0; + continue; + default: + break; + } + break; + + } + *len = i+1; +} + +static void +print(UChar* src, int32_t srcLen,const char *tagStart,const char *tagEnd, UErrorCode *status){ + int32_t bufCapacity = srcLen*4; + char *buf = NULL; + int32_t bufLen = 0; + + if(U_FAILURE(*status)){ + return; + } + + buf = (char*) (uprv_malloc(bufCapacity)); + if(buf==0){ + fprintf(stderr, "Could not allocate memory!!"); + exit(U_MEMORY_ALLOCATION_ERROR); + } + buf = convertAndEscape(&buf, bufCapacity, &bufLen, src, srcLen,status); + if(U_SUCCESS(*status)){ + trim(&buf,&bufLen); + write_utf8_file(out,UnicodeString(tagStart)); + write_utf8_file(out,UnicodeString(buf, bufLen, "UTF-8")); + write_utf8_file(out,UnicodeString(tagEnd)); + write_utf8_file(out,UnicodeString("\n")); + + } +} +static void +printNoteElements(const UString *src, UErrorCode *status){ + +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */ + + int32_t capacity = 0; + UChar* note = NULL; + int32_t noteLen = 0; + int32_t count = 0,i; + + if(src == NULL){ + return; + } + + capacity = src->fLength; + note = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity); + + count = getCount(src->fChars,src->fLength, UPC_NOTE, status); + if(U_FAILURE(*status)){ + uprv_free(note); + return; + } + for(i=0; i < count; i++){ + noteLen = getAt(src->fChars,src->fLength, ¬e, capacity, i, UPC_NOTE, status); + if(U_FAILURE(*status)){ + uprv_free(note); + return; + } + if(noteLen > 0){ + write_tabs(out); + print(note, noteLen,"<note>", "</note>", status); + } + } + uprv_free(note); +#else + + fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n"); + +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ + +} + +static void printAttribute(const char *name, const char *value, int32_t /*len*/) +{ + write_utf8_file(out, UnicodeString(" ")); + write_utf8_file(out, UnicodeString(name)); + write_utf8_file(out, UnicodeString(" = \"")); + write_utf8_file(out, UnicodeString(value)); + write_utf8_file(out, UnicodeString("\"")); +} + +static void printAttribute(const char *name, const UnicodeString value, int32_t /*len*/) +{ + write_utf8_file(out, UnicodeString(" ")); + write_utf8_file(out, UnicodeString(name)); + write_utf8_file(out, UnicodeString(" = \"")); + write_utf8_file(out, value); + write_utf8_file(out, UnicodeString("\"")); +} + +static void +printComments(struct UString *src, const char *resName, UBool printTranslate, UErrorCode *status){ + +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */ + + if(status==NULL || U_FAILURE(*status)){ + return; + } + + int32_t capacity = src->fLength + 1; + char* buf = NULL; + int32_t bufLen = 0; + UChar* desc = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity); + UChar* trans = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity); + + int32_t descLen = 0, transLen=0; + if(desc==NULL || trans==NULL){ + *status = U_MEMORY_ALLOCATION_ERROR; + uprv_free(desc); + uprv_free(trans); + return; + } + // TODO: make src const, stop modifying it in-place, make printContainer() take const resource, etc. + src->fLength = removeCmtText(src->fChars, src->fLength, status); + descLen = getDescription(src->fChars,src->fLength, &desc, capacity, status); + transLen = getTranslate(src->fChars,src->fLength, &trans, capacity, status); + + /* first print translate attribute */ + if(transLen > 0){ + if(printTranslate){ + /* print translate attribute */ + buf = convertAndEscape(&buf, 0, &bufLen, trans, transLen, status); + if(U_SUCCESS(*status)){ + printAttribute("translate", UnicodeString(buf, bufLen, "UTF-8"), bufLen); + write_utf8_file(out,UnicodeString(">\n")); + } + }else if(getShowWarning()){ + fprintf(stderr, "Warning: Tranlate attribute for resource %s cannot be set. XLIFF prohibits it.\n", resName); + /* no translate attribute .. just close the tag */ + write_utf8_file(out,UnicodeString(">\n")); + } + }else{ + /* no translate attribute .. just close the tag */ + write_utf8_file(out,UnicodeString(">\n")); + } + + if(descLen > 0){ + write_tabs(out); + print(desc, descLen, "<!--", "-->", status); + } + + uprv_free(desc); + uprv_free(trans); +#else + + fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n"); + +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ + +} + +/* + * Print out a containing element, like: + * <trans-unit id = "blah" resname = "blah" restype = "x-id-alias" translate = "no"> + * <group id "calendar_gregorian" resname = "gregorian" restype = "x-icu-array"> + */ +static char *printContainer(SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status) +{ + const char *resname = NULL; + char *sid = NULL; + + write_tabs(out); + + resname = res->getKeyString(srBundle); + if (resname != NULL && *resname != 0) { + sid = getID(id, resname, sid); + } else { + sid = getID(id, NULL, sid); + } + + write_utf8_file(out, UnicodeString("<")); + write_utf8_file(out, UnicodeString(container)); + printAttribute("id", sid, (int32_t) uprv_strlen(sid)); + + if (resname != NULL) { + printAttribute("resname", resname, (int32_t) uprv_strlen(resname)); + } + + if (mimetype != NULL) { + printAttribute("mime-type", mimetype, (int32_t) uprv_strlen(mimetype)); + } + + if (restype != NULL) { + printAttribute("restype", restype, (int32_t) uprv_strlen(restype)); + } + + tabCount += 1; + if (res->fComment.fLength > 0) { + /* printComments will print the closing ">\n" */ + printComments(&res->fComment, resname, TRUE, status); + } else { + write_utf8_file(out, UnicodeString(">\n")); + } + + return sid; +} + +/* Writing Functions */ + +static const char *trans_unit = "trans-unit"; +static const char *close_trans_unit = "</trans-unit>\n"; +static const char *source = "<source>"; +static const char *close_source = "</source>\n"; +static const char *group = "group"; +static const char *close_group = "</group>\n"; + +static const char *bin_unit = "bin-unit"; +static const char *close_bin_unit = "</bin-unit>\n"; +static const char *bin_source = "<bin-source>\n"; +static const char *close_bin_source = "</bin-source>\n"; +static const char *external_file = "<external-file"; +/*static const char *close_external_file = "</external-file>\n";*/ +static const char *internal_file = "<internal-file"; +static const char *close_internal_file = "</internal-file>\n"; + +static const char *application_mimetype = "application"; /* add "/octet-stream"? */ + +static const char *alias_restype = "x-icu-alias"; +static const char *array_restype = "x-icu-array"; +static const char *binary_restype = "x-icu-binary"; +static const char *integer_restype = "x-icu-integer"; +static const char *intvector_restype = "x-icu-intvector"; +static const char *table_restype = "x-icu-table"; + +static void +string_write_xml(StringResource *res, const char* id, const char* /*language*/, UErrorCode *status) { + + char *sid = NULL; + char* buf = NULL; + int32_t bufLen = 0; + + if(status==NULL || U_FAILURE(*status)){ + return; + } + + sid = printContainer(res, trans_unit, NULL, NULL, id, status); + + write_tabs(out); + + write_utf8_file(out, UnicodeString(source)); + + buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status); + + if (U_FAILURE(*status)) { + return; + } + + write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8")); + write_utf8_file(out, UnicodeString(close_source)); + + printNoteElements(&res->fComment, status); + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_trans_unit)); + + uprv_free(buf); + uprv_free(sid); +} + +static void +alias_write_xml(AliasResource *res, const char* id, const char* /*language*/, UErrorCode *status) { + char *sid = NULL; + char* buf = NULL; + int32_t bufLen=0; + + sid = printContainer(res, trans_unit, alias_restype, NULL, id, status); + + write_tabs(out); + + write_utf8_file(out, UnicodeString(source)); + + buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status); + + if(U_FAILURE(*status)){ + return; + } + write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8")); + write_utf8_file(out, UnicodeString(close_source)); + + printNoteElements(&res->fComment, status); + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_trans_unit)); + + uprv_free(buf); + uprv_free(sid); +} + +static void +array_write_xml(ArrayResource *res, const char* id, const char* language, UErrorCode *status) { + char* sid = NULL; + int index = 0; + + struct SResource *current = NULL; + + sid = printContainer(res, group, array_restype, NULL, id, status); + + current = res->fFirst; + + while (current != NULL) { + char c[256] = {0}; + char* subId = NULL; + + itostr(c, index, 10, 0); + index += 1; + subId = getID(sid, c, subId); + + res_write_xml(current, subId, language, FALSE, status); + uprv_free(subId); + subId = NULL; + + if(U_FAILURE(*status)){ + return; + } + + current = current->fNext; + } + + tabCount -= 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(close_group)); + + uprv_free(sid); +} + +static void +intvector_write_xml(IntVectorResource *res, const char* id, const char* /*language*/, UErrorCode *status) { + char* sid = NULL; + char* ivd = NULL; + uint32_t i=0; + uint32_t len=0; + char buf[256] = {'0'}; + + sid = printContainer(res, group, intvector_restype, NULL, id, status); + + for(i = 0; i < res->fCount; i += 1) { + char c[256] = {0}; + + itostr(c, i, 10, 0); + ivd = getID(sid, c, ivd); + len = itostr(buf, res->fArray[i], 10, 0); + + write_tabs(out); + write_utf8_file(out, UnicodeString("<")); + write_utf8_file(out, UnicodeString(trans_unit)); + + printAttribute("id", ivd, (int32_t)uprv_strlen(ivd)); + printAttribute("restype", integer_restype, (int32_t) strlen(integer_restype)); + + write_utf8_file(out, UnicodeString(">\n")); + + tabCount += 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(source)); + + write_utf8_file(out, UnicodeString(buf, len)); + + write_utf8_file(out, UnicodeString(close_source)); + tabCount -= 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(close_trans_unit)); + + uprv_free(ivd); + ivd = NULL; + } + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_group)); + uprv_free(sid); + sid = NULL; +} + +static void +int_write_xml(IntResource *res, const char* id, const char* /*language*/, UErrorCode *status) { + char* sid = NULL; + char buf[256] = {0}; + uint32_t len = 0; + + sid = printContainer(res, trans_unit, integer_restype, NULL, id, status); + + write_tabs(out); + + write_utf8_file(out, UnicodeString(source)); + + len = itostr(buf, res->fValue, 10, 0); + write_utf8_file(out, UnicodeString(buf, len)); + + write_utf8_file(out, UnicodeString(close_source)); + + printNoteElements(&res->fComment, status); + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_trans_unit)); + + uprv_free(sid); + sid = NULL; +} + +static void +bin_write_xml(BinaryResource *res, const char* id, const char* /*language*/, UErrorCode *status) { + const char* m_type = application_mimetype; + char* sid = NULL; + uint32_t crc = 0xFFFFFFFF; + + char fileName[1024] ={0}; + int32_t tLen = ( outDir == NULL) ? 0 :(int32_t)uprv_strlen(outDir); + char* fn = (char*) uprv_malloc(sizeof(char) * (tLen+1024 + + (res->fFileName !=NULL ? + uprv_strlen(res->fFileName) :0))); + const char* ext = NULL; + + char* f = NULL; + + fn[0]=0; + + if(res->fFileName != NULL){ + uprv_strcpy(fileName, res->fFileName); + f = uprv_strrchr(fileName, '\\'); + + if (f != NULL) { + f++; + } else { + f = fileName; + } + + ext = uprv_strrchr(fileName, '.'); + + if (ext == NULL) { + fprintf(stderr, "Error: %s is an unknown binary filename type.\n", fileName); + exit(U_ILLEGAL_ARGUMENT_ERROR); + } + + if(uprv_strcmp(ext, ".jpg")==0 || uprv_strcmp(ext, ".jpeg")==0 || uprv_strcmp(ext, ".gif")==0 ){ + m_type = "image"; + } else if(uprv_strcmp(ext, ".wav")==0 || uprv_strcmp(ext, ".au")==0 ){ + m_type = "audio"; + } else if(uprv_strcmp(ext, ".avi")==0 || uprv_strcmp(ext, ".mpg")==0 || uprv_strcmp(ext, ".mpeg")==0){ + m_type = "video"; + } else if(uprv_strcmp(ext, ".txt")==0 || uprv_strcmp(ext, ".text")==0){ + m_type = "text"; + } + + sid = printContainer(res, bin_unit, binary_restype, m_type, id, status); + + write_tabs(out); + + write_utf8_file(out, UnicodeString(bin_source)); + + tabCount+= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(external_file)); + printAttribute("href", f, (int32_t)uprv_strlen(f)); + write_utf8_file(out, UnicodeString("/>\n")); + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_bin_source)); + + printNoteElements(&res->fComment, status); + tabCount -= 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(close_bin_unit)); + } else { + char temp[256] = {0}; + uint32_t i = 0; + int32_t len=0; + + sid = printContainer(res, bin_unit, binary_restype, m_type, id, status); + + write_tabs(out); + write_utf8_file(out, UnicodeString(bin_source)); + + tabCount += 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(internal_file)); + printAttribute("form", application_mimetype, (int32_t) uprv_strlen(application_mimetype)); + + while(i <res->fLength){ + len = itostr(temp, res->fData[i], 16, 2); + crc = computeCRC(temp, len, crc); + i++; + } + + len = itostr(temp, crc, 10, 0); + printAttribute("crc", temp, len); + + write_utf8_file(out, UnicodeString(">")); + + i = 0; + while(i <res->fLength){ + len = itostr(temp, res->fData[i], 16, 2); + write_utf8_file(out, UnicodeString(temp)); + i += 1; + } + + write_utf8_file(out, UnicodeString(close_internal_file)); + + tabCount -= 2; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_bin_source)); + printNoteElements(&res->fComment, status); + + tabCount -= 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(close_bin_unit)); + + uprv_free(sid); + sid = NULL; + } + + uprv_free(fn); +} + + + +static void +table_write_xml(TableResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) { + + uint32_t i = 0; + + struct SResource *current = NULL; + char* sid = NULL; + + if (U_FAILURE(*status)) { + return ; + } + + sid = printContainer(res, group, table_restype, NULL, id, status); + + if(isTopLevel) { + sid[0] = '\0'; + } + + current = res->fFirst; + i = 0; + + while (current != NULL) { + res_write_xml(current, sid, language, FALSE, status); + + if(U_FAILURE(*status)){ + return; + } + + i += 1; + current = current->fNext; + } + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(close_group)); + + uprv_free(sid); + sid = NULL; +} + +void +res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) { + + if (U_FAILURE(*status)) { + return ; + } + + if (res != NULL) { + switch (res->fType) { + case URES_STRING: + string_write_xml (static_cast<StringResource *>(res), id, language, status); + return; + + case URES_ALIAS: + alias_write_xml (static_cast<AliasResource *>(res), id, language, status); + return; + + case URES_INT_VECTOR: + intvector_write_xml (static_cast<IntVectorResource *>(res), id, language, status); + return; + + case URES_BINARY: + bin_write_xml (static_cast<BinaryResource *>(res), id, language, status); + return; + + case URES_INT: + int_write_xml (static_cast<IntResource *>(res), id, language, status); + return; + + case URES_ARRAY: + array_write_xml (static_cast<ArrayResource *>(res), id, language, status); + return; + + case URES_TABLE: + table_write_xml (static_cast<TableResource *>(res), id, language, isTopLevel, status); + return; + + default: + break; + } + } + + *status = U_INTERNAL_PROGRAM_ERROR; +} + +void +bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, const char* filename, + char *writtenFilename, int writtenFilenameLen, + const char* language, const char* outFileName, UErrorCode *status) { + + char* xmlfileName = NULL; + char* outputFileName = NULL; + char* originalFileName = NULL; + const char* fileStart = "<file xml:space = \"preserve\" source-language = \""; + const char* file1 = "\" datatype = \"x-icu-resource-bundle\" "; + const char* file2 = "original = \""; + const char* file4 = "\" date = \""; + const char* fileEnd = "</file>\n"; + const char* headerStart = "<header>\n"; + const char* headerEnd = "</header>\n"; + const char* bodyStart = "<body>\n"; + const char* bodyEnd = "</body>\n"; + + const char *tool_start = "<tool"; + const char *tool_id = "genrb-" GENRB_VERSION "-icu-" U_ICU_VERSION; + const char *tool_name = "genrb"; + + char* temp = NULL; + char* lang = NULL; + const char* pos = NULL; + int32_t first, index; + time_t currTime; + char timeBuf[128]; + + outDir = outputDir; + + srBundle = bundle; + + pos = uprv_strrchr(filename, '\\'); + if(pos != NULL) { + first = (int32_t)(pos - filename + 1); + } else { + first = 0; + } + index = (int32_t)(uprv_strlen(filename) - uprv_strlen(textExt) - first); + originalFileName = (char *)uprv_malloc(sizeof(char)*index+1); + uprv_memset(originalFileName, 0, sizeof(char)*index+1); + uprv_strncpy(originalFileName, filename + first, index); + + if(uprv_strcmp(originalFileName, srBundle->fLocale) != 0) { + fprintf(stdout, "Warning: The file name is not same as the resource name!\n"); + } + + temp = originalFileName; + originalFileName = (char *)uprv_malloc(sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1); + uprv_memset(originalFileName, 0, sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1); + uprv_strcat(originalFileName, temp); + uprv_strcat(originalFileName, textExt); + uprv_free(temp); + temp = NULL; + + + if (language == NULL) { +/* lang = parseFilename(filename, lang); + if (lang == NULL) {*/ + /* now check if locale name is valid or not + * this is to cater for situation where + * pegasusServer.txt contains + * + * en{ + * .. + * } + */ + lang = parseFilename(srBundle->fLocale, lang); + /* + * Neither the file name nor the table name inside the + * txt file contain a valid country and language codes + * throw an error. + * pegasusServer.txt contains + * + * testelements{ + * .... + * } + */ + if(lang==NULL){ + fprintf(stderr, "Error: The file name and table name do not contain a valid language code. Please use -l option to specify it.\n"); + exit(U_ILLEGAL_ARGUMENT_ERROR); + } + /* }*/ + } else { + lang = (char *)uprv_malloc(sizeof(char)*uprv_strlen(language) +1); + uprv_memset(lang, 0, sizeof(char)*uprv_strlen(language) +1); + uprv_strcpy(lang, language); + } + + if(outFileName) { + outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(outFileName) + 1); + uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(outFileName) + 1); + uprv_strcpy(outputFileName,outFileName); + } else { + outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(srBundle->fLocale) + 1); + uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(srBundle->fLocale) + 1); + uprv_strcpy(outputFileName,srBundle->fLocale); + } + + if(outputDir) { + xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputDir) + uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1); + uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputDir)+ uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1); + } else { + xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1); + uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1); + } + + if(outputDir){ + uprv_strcpy(xmlfileName, outputDir); + if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){ + uprv_strcat(xmlfileName,U_FILE_SEP_STRING); + } + } + uprv_strcat(xmlfileName,outputFileName); + uprv_strcat(xmlfileName,xliffExt); + + if (writtenFilename) { + uprv_strncpy(writtenFilename, xmlfileName, writtenFilenameLen); + } + + if (U_FAILURE(*status)) { + goto cleanup_bundle_write_xml; + } + + out= T_FileStream_open(xmlfileName,"w"); + + if(out==NULL){ + *status = U_FILE_ACCESS_ERROR; + goto cleanup_bundle_write_xml; + } + write_utf8_file(out, UnicodeString(xmlHeader)); + + if(outputEnc && *outputEnc!='\0'){ + /* store the output encoding */ + enc = outputEnc; + conv=ucnv_open(enc,status); + if(U_FAILURE(*status)){ + goto cleanup_bundle_write_xml; + } + } + write_utf8_file(out, UnicodeString(bundleStart)); + write_tabs(out); + write_utf8_file(out, UnicodeString(fileStart)); + /* check if lang and language are the same */ + if(language != NULL && uprv_strcmp(lang, srBundle->fLocale)!=0){ + fprintf(stderr,"Warning: The top level tag in the resource and language specified are not the same. Please check the input.\n"); + } + write_utf8_file(out, UnicodeString(lang)); + write_utf8_file(out, UnicodeString(file1)); + write_utf8_file(out, UnicodeString(file2)); + write_utf8_file(out, UnicodeString(originalFileName)); + write_utf8_file(out, UnicodeString(file4)); + + time(&currTime); + strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&currTime)); + write_utf8_file(out, UnicodeString(timeBuf)); + write_utf8_file(out, UnicodeString("\">\n")); + + tabCount += 1; + write_tabs(out); + write_utf8_file(out, UnicodeString(headerStart)); + + tabCount += 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(tool_start)); + printAttribute("tool-id", tool_id, (int32_t) uprv_strlen(tool_id)); + printAttribute("tool-name", tool_name, (int32_t) uprv_strlen(tool_name)); + write_utf8_file(out, UnicodeString("/>\n")); + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(headerEnd)); + + write_tabs(out); + tabCount += 1; + + write_utf8_file(out, UnicodeString(bodyStart)); + + + res_write_xml(bundle->fRoot, bundle->fLocale, lang, TRUE, status); + + tabCount -= 1; + write_tabs(out); + + write_utf8_file(out, UnicodeString(bodyEnd)); + tabCount--; + write_tabs(out); + write_utf8_file(out, UnicodeString(fileEnd)); + tabCount--; + write_tabs(out); + write_utf8_file(out, UnicodeString(bundleEnd)); + T_FileStream_close(out); + + ucnv_close(conv); + +cleanup_bundle_write_xml: + uprv_free(originalFileName); + uprv_free(lang); + if(xmlfileName != NULL) { + uprv_free(xmlfileName); + } + if(outputFileName != NULL){ + uprv_free(outputFileName); + } +} |