1*449ab281SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*449ab281SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*449ab281SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*449ab281SAndrew Rist * distributed with this work for additional information 6*449ab281SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*449ab281SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*449ab281SAndrew Rist * "License"); you may not use this file except in compliance 9*449ab281SAndrew Rist * with the License. You may obtain a copy of the License at 10*449ab281SAndrew Rist * 11*449ab281SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*449ab281SAndrew Rist * 13*449ab281SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*449ab281SAndrew Rist * software distributed under the License is distributed on an 15*449ab281SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*449ab281SAndrew Rist * KIND, either express or implied. See the License for the 17*449ab281SAndrew Rist * specific language governing permissions and limitations 18*449ab281SAndrew Rist * under the License. 19*449ab281SAndrew Rist * 20*449ab281SAndrew Rist *************************************************************/ 21*449ab281SAndrew Rist 22*449ab281SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_i18npool.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <stdio.h> 28cdf0e10cSrcweir #include <string.h> 29cdf0e10cSrcweir #include <stdlib.h> 30cdf0e10cSrcweir #include <sal/main.h> 31cdf0e10cSrcweir #include <sal/types.h> 32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include "warnings_guard_unicode_tblcoll.h" 35cdf0e10cSrcweir 36cdf0e10cSrcweir U_CAPI void U_EXPORT2 uprv_free(void *mem); 37cdf0e10cSrcweir 38cdf0e10cSrcweir using namespace ::rtl; 39cdf0e10cSrcweir 40cdf0e10cSrcweir /* Main Procedure */ 41cdf0e10cSrcweir 42cdf0e10cSrcweir void data_write(char* file, char* name, sal_uInt8 *data, sal_Int32 len) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir FILE *fp = fopen(file, "wb"); 45cdf0e10cSrcweir if (fp == NULL) { 46cdf0e10cSrcweir printf("Can't create the C source file."); 47cdf0e10cSrcweir return; 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir fprintf(fp, "/*\n"); 51cdf0e10cSrcweir fprintf(fp, " * Copyright(c) 1999 - 2000, Sun Microsystems, Inc.\n"); 52cdf0e10cSrcweir fprintf(fp, " * All Rights Reserved.\n"); 53cdf0e10cSrcweir fprintf(fp, " */\n\n"); 54cdf0e10cSrcweir fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n"); 55cdf0e10cSrcweir fprintf(fp, "#include <sal/types.h>\n"); 56cdf0e10cSrcweir fprintf(fp, "\nextern \"C\" {\n"); 57cdf0e10cSrcweir 58cdf0e10cSrcweir // generate main dict. data array 59cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt8 %s[] = {", name); 60cdf0e10cSrcweir 61cdf0e10cSrcweir sal_Int32 count = 0; 62cdf0e10cSrcweir for (sal_Int32 i = 0; i < len; i++) { 63cdf0e10cSrcweir 64cdf0e10cSrcweir if (count++ % 16 == 0) 65cdf0e10cSrcweir fprintf(fp, "\n\t"); 66cdf0e10cSrcweir 67cdf0e10cSrcweir fprintf(fp, "0x%04x, ", data[i]); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir fprintf(fp, "\n};\n\n"); 70cdf0e10cSrcweir 71cdf0e10cSrcweir fprintf(fp, "const sal_uInt8* get_%s() { return %s; }\n\n", name, name); 72cdf0e10cSrcweir fprintf (fp, "}\n"); 73cdf0e10cSrcweir 74cdf0e10cSrcweir fclose(fp); 75cdf0e10cSrcweir 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) 79cdf0e10cSrcweir { 80cdf0e10cSrcweir FILE *fp; 81cdf0e10cSrcweir 82cdf0e10cSrcweir if (argc < 4) exit(-1); 83cdf0e10cSrcweir 84cdf0e10cSrcweir fp = fopen(argv[1], "rb"); // open the source file for read; 85cdf0e10cSrcweir if (fp == NULL) 86cdf0e10cSrcweir printf("Open the rule source file failed."); 87cdf0e10cSrcweir 88cdf0e10cSrcweir 89cdf0e10cSrcweir sal_Char str[1024]; 90cdf0e10cSrcweir OUStringBuffer Obuf; 91cdf0e10cSrcweir while (fgets(str, 1024, fp)) { 92cdf0e10cSrcweir // don't convert last new line character to Ostr. 93cdf0e10cSrcweir sal_Int32 len = strlen(str) - 1; 94cdf0e10cSrcweir // skip comment line 95cdf0e10cSrcweir if (len == 0 || str[0] == '#') 96cdf0e10cSrcweir continue; 97cdf0e10cSrcweir 98cdf0e10cSrcweir // input file is in UTF-8 encoding 99cdf0e10cSrcweir OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8).trim(); 100cdf0e10cSrcweir 101cdf0e10cSrcweir len = Ostr.getLength(); 102cdf0e10cSrcweir if (len == 0) 103cdf0e10cSrcweir continue; // skip empty line. 104cdf0e10cSrcweir 105cdf0e10cSrcweir Obuf.append(Ostr); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir fclose(fp); 108cdf0e10cSrcweir 109cdf0e10cSrcweir UErrorCode status = U_ZERO_ERROR; 110cdf0e10cSrcweir //UParseError parseError; 111cdf0e10cSrcweir //UCollator *coll = ucol_openRules(Obuf.getStr(), Obuf.getLength(), UCOL_OFF, 112cdf0e10cSrcweir // UCOL_DEFAULT_STRENGTH, &parseError, &status); 113cdf0e10cSrcweir 114cdf0e10cSrcweir RuleBasedCollator *coll = new RuleBasedCollator(reinterpret_cast<const UChar *>(Obuf.getStr()), status); // UChar != sal_Unicode in MinGW 115cdf0e10cSrcweir 116cdf0e10cSrcweir if (U_SUCCESS(status)) { 117cdf0e10cSrcweir 118cdf0e10cSrcweir int32_t len = 0; 119cdf0e10cSrcweir uint8_t *data = coll->cloneRuleData(len, status); 120cdf0e10cSrcweir 121cdf0e10cSrcweir if (U_SUCCESS(status) && data != NULL) 122cdf0e10cSrcweir data_write(argv[2], argv[3], data, len); 123cdf0e10cSrcweir else { 124cdf0e10cSrcweir printf("Could not get rule data from collator\n"); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir if (data) uprv_free(data); 128cdf0e10cSrcweir } else { 129cdf0e10cSrcweir printf("\nRule parsering error\n"); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir if (coll) 133cdf0e10cSrcweir delete coll; 134cdf0e10cSrcweir 135cdf0e10cSrcweir return U_SUCCESS(status) ? 0 : 1; 136cdf0e10cSrcweir } // End of main 137