1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_i18npool.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <stdio.h> 32*cdf0e10cSrcweir #include <string.h> 33*cdf0e10cSrcweir #include <stdlib.h> 34*cdf0e10cSrcweir #include <sal/main.h> 35*cdf0e10cSrcweir #include <sal/types.h> 36*cdf0e10cSrcweir #include <rtl/ustring.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #define MAX_ADDRESS 0x30000 39*cdf0e10cSrcweir #define MAX_INDEX MAX_ADDRESS/0x100 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir using namespace ::rtl; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /* Main Procedure */ 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir FILE *fp; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir if (argc < 4) exit(-1); 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir fp = fopen(argv[1], "rb"); // open the source file for read; 52*cdf0e10cSrcweir if (fp == NULL) { 53*cdf0e10cSrcweir printf("Open the rule source file failed."); 54*cdf0e10cSrcweir return 1; 55*cdf0e10cSrcweir } 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir sal_Int32 i, j, k; 59*cdf0e10cSrcweir sal_Int32 address[MAX_ADDRESS]; 60*cdf0e10cSrcweir for (i=0; i<MAX_ADDRESS; i++) address[i]=-1; 61*cdf0e10cSrcweir OUString sep=OUString(sal_Unicode('|')); 62*cdf0e10cSrcweir OUString result=sep; 63*cdf0e10cSrcweir sal_Int32 max=0; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir sal_Char str[1024]; 66*cdf0e10cSrcweir while (fgets(str, 1024, fp)) { 67*cdf0e10cSrcweir // don't convert last new line character to Ostr. 68*cdf0e10cSrcweir sal_Int32 len = strlen(str) - 1; 69*cdf0e10cSrcweir // skip comment line 70*cdf0e10cSrcweir if (len == 0 || str[0] == '#') 71*cdf0e10cSrcweir continue; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir // input file is in UTF-8 encoding 74*cdf0e10cSrcweir OUString Ostr = OUString((const sal_Char *)str, len, RTL_TEXTENCODING_UTF8); 75*cdf0e10cSrcweir len = Ostr.getLength(); 76*cdf0e10cSrcweir if (len == 0) 77*cdf0e10cSrcweir continue; // skip empty line. 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir sal_Int32 nPos=0; 80*cdf0e10cSrcweir sal_uInt32 nChar = Ostr.iterateCodePoints(&nPos, 2); 81*cdf0e10cSrcweir if (nChar > MAX_ADDRESS) { 82*cdf0e10cSrcweir printf("Code point 0x%lx exceeds MAX_ADDRESS 0x%x, Please increase MAX_ADDRESS", static_cast<long unsigned int>(nChar), MAX_ADDRESS); 83*cdf0e10cSrcweir exit(1); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir OUString key=Ostr.copy(nPos)+sep; 86*cdf0e10cSrcweir sal_Int32 idx = result.indexOf(key); 87*cdf0e10cSrcweir if (key.getLength() > max) max=key.getLength(); 88*cdf0e10cSrcweir if (idx >= 0) { 89*cdf0e10cSrcweir address[nChar]=idx; 90*cdf0e10cSrcweir } else { 91*cdf0e10cSrcweir address[nChar]=result.getLength(); 92*cdf0e10cSrcweir result+=key; 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir fclose(fp); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir fp = fopen(argv[2], "wb"); 98*cdf0e10cSrcweir if (fp == NULL) { 99*cdf0e10cSrcweir printf("Can't create the C source file."); 100*cdf0e10cSrcweir return 1; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir fprintf(fp, "/*\n"); 104*cdf0e10cSrcweir fprintf(fp, " * Copyright(c) 1999 - 2006, Sun Microsystems, Inc.\n"); 105*cdf0e10cSrcweir fprintf(fp, " * All Rights Reserved.\n"); 106*cdf0e10cSrcweir fprintf(fp, " */\n\n"); 107*cdf0e10cSrcweir fprintf(fp, "/* !!!The file is generated automatically. DONOT edit the file manually!!! */\n\n"); 108*cdf0e10cSrcweir fprintf(fp, "#include <sal/types.h>\n"); 109*cdf0e10cSrcweir fprintf(fp, "\nextern \"C\" {\n"); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir sal_Int32 index[MAX_INDEX]; 112*cdf0e10cSrcweir sal_Int32 max_index=0; 113*cdf0e10cSrcweir for (i=k=0; i<MAX_INDEX; i++) { 114*cdf0e10cSrcweir index[i] = 0xFFFF; 115*cdf0e10cSrcweir for (j=0; j<0x100; j++) { 116*cdf0e10cSrcweir if (address[i*0x100+j] >=0) { 117*cdf0e10cSrcweir max_index=i; 118*cdf0e10cSrcweir index[i]=0x100*k++; 119*cdf0e10cSrcweir break; 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx1[] = {"); 125*cdf0e10cSrcweir for (i = k = 0; i <= max_index; i++) { 126*cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t"); 127*cdf0e10cSrcweir fprintf( 128*cdf0e10cSrcweir fp, "0x%04lx, ", sal::static_int_cast< unsigned long >(index[i])); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir fprintf(fp, "\n};\n\n"); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir sal_Int32 len=result.getLength(); 133*cdf0e10cSrcweir const sal_Unicode *ustr=result.getStr(); 134*cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx2[] = {"); 135*cdf0e10cSrcweir for (i = k = 0; i <= max_index; i++) { 136*cdf0e10cSrcweir if (index[i] != 0xFFFF) { 137*cdf0e10cSrcweir for (j = 0; j<0x100; j++) { 138*cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t"); 139*cdf0e10cSrcweir sal_Int32 ad=address[i*0x100+j]; 140*cdf0e10cSrcweir fprintf( 141*cdf0e10cSrcweir fp, "0x%04lx, ", 142*cdf0e10cSrcweir sal::static_int_cast< unsigned long >( 143*cdf0e10cSrcweir ad == -1 ? 0 : max == 2 ? ustr[ad] : ad)); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir fprintf(fp, "\n\t"); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir fprintf(fp, "\n};\n\n"); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir if (max == 2) { 151*cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 *idx3 = NULL;\n\n"); 152*cdf0e10cSrcweir } else { 153*cdf0e10cSrcweir fprintf(fp, "\nstatic const sal_uInt16 idx3[] = {"); 154*cdf0e10cSrcweir for (i = k = 0; i < len; i++) { 155*cdf0e10cSrcweir if (k++ % 16 == 0) fprintf(fp, "\n\t"); 156*cdf0e10cSrcweir fprintf(fp, "0x%04x, ", (sep.toChar() == ustr[i]) ? 0 : ustr[i]); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir fprintf(fp, "\n};\n\n"); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir fprintf(fp, "const sal_uInt16** get_%s(sal_Int16 &max_index)\n{\n\tstatic const sal_uInt16 *idx[]={idx1, idx2, idx3};\n\tmax_index=0x%x;\n\treturn idx;\n}\n\n", argv[3], static_cast<unsigned int>(max_index)); 162*cdf0e10cSrcweir fprintf (fp, "}\n"); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir fclose(fp); 165*cdf0e10cSrcweir return 0; 166*cdf0e10cSrcweir } // End of main 167