1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 #include <stdio.h>
31 #include <string.h>
32 #include "LocaleNode.hxx"
33 //-----------------------------------------
34 // The document handler, which is needed for the saxparser
35 // The Documenthandler for reading sax
36 //-----------------------------------------
37 OFileWriter::OFileWriter(const char *pcFile, const char *locale ) {
38 
39 	strncpy( m_pcFile , pcFile, sizeof(m_pcFile) );
40     m_pcFile[sizeof(m_pcFile)-1] = 0;
41 	printf("file generated=%s\n", m_pcFile);
42     m_f = fopen( m_pcFile , "w" );
43     strncpy( theLocale, locale, sizeof(theLocale) );
44     theLocale[sizeof(theLocale)-1] = 0;
45 }
46 
47 OFileWriter::~OFileWriter() {
48 	if(m_f)
49 		fclose( m_f );
50 }
51 
52 void OFileWriter::writeInt(sal_Int16 nb) const
53 {
54 	fprintf(m_f, "%d", nb);
55 }
56 
57 void OFileWriter::writeAsciiString(const char* str) const
58 {
59 	fprintf(m_f, "%s", str);
60 }
61 
62 void OFileWriter::writeStringCharacters(const ::rtl::OUString& str) const
63 {
64 	for(int i = 0; i < str.getLength(); i++)
65 	    fprintf(m_f, "0x%x, ", str[i]);
66 }
67 
68 void OFileWriter::writeFunction(const char *func, const char *count, const char *array) const
69 {
70 	fprintf(m_f, "sal_Unicode **  SAL_CALL %s%s(sal_Int16& count)\n{\n", func, theLocale);
71 	fprintf(m_f, "\tcount = %s;\n", count);
72 	fprintf(m_f, "\treturn (sal_Unicode**)%s;\n}\n", array);
73 }
74 
75 void OFileWriter::writeRefFunction(const char *func, const ::rtl::OUString& useLocale) const
76 {
77     OString aRefLocale( OUStringToOString(useLocale, RTL_TEXTENCODING_ASCII_US) );
78 	const char* locale = aRefLocale.getStr();
79 	fprintf(m_f, "extern sal_Unicode **  SAL_CALL %s%s(sal_Int16& count);\n", func, locale);
80 	fprintf(m_f, "sal_Unicode **  SAL_CALL %s%s(sal_Int16& count)\n{\n", func, theLocale);
81 	fprintf(m_f, "\treturn %s%s(count);\n}\n", func, locale);
82 }
83 
84 void OFileWriter::writeFunction(const char *func, const char *count, const char *array, const char *from, const char *to) const
85 {
86 	fprintf(m_f, "sal_Unicode **  SAL_CALL %s%s(sal_Int16& count, const sal_Unicode*& from, const sal_Unicode*& to)\n{\n", func, theLocale);
87 	fprintf(m_f, "\tcount = %s;\n", count);
88 	fprintf(m_f, "\tfrom = %s;\n", from);
89 	fprintf(m_f, "\tto = %s;\n", to);
90 	fprintf(m_f, "\treturn (sal_Unicode**)%s;\n}\n", array);
91 }
92 
93 void OFileWriter::writeRefFunction(const char *func, const ::rtl::OUString& useLocale, const char *to) const
94 {
95     OString aRefLocale( OUStringToOString(useLocale, RTL_TEXTENCODING_ASCII_US) );
96 	const char* locale = aRefLocale.getStr();
97 	fprintf(m_f, "extern sal_Unicode **  SAL_CALL %s%s(sal_Int16& count, const sal_Unicode*& from, const sal_Unicode*& to);\n", func, locale);
98 	fprintf(m_f, "sal_Unicode **  SAL_CALL %s%s(sal_Int16& count, const sal_Unicode*& from, const sal_Unicode*& to)\n{\n", func, theLocale);
99 	fprintf(m_f, "\tto = %s;\n", to);
100 	fprintf(m_f, "\tconst sal_Unicode* tmp;\n");
101 	fprintf(m_f, "\treturn %s%s(count, from, tmp);\n}\n", func, locale);
102 }
103 
104 void OFileWriter::writeFunction2(const char *func, const char *style, const char* attr, const char *array) const
105 {
106 	fprintf(m_f, "const sal_Unicode ***  SAL_CALL %s%s( sal_Int16& nStyles, sal_Int16& nAttributes )\n{\n", func, theLocale);
107 	fprintf(m_f, "\tnStyles     = %s;\n", style);
108 	fprintf(m_f, "\tnAttributes = %s;\n", attr);
109 	fprintf(m_f, "\treturn %s;\n}\n", array);
110 }
111 
112 void OFileWriter::writeRefFunction2(const char *func, const ::rtl::OUString& useLocale) const
113 {
114     OString aRefLocale( OUStringToOString(useLocale, RTL_TEXTENCODING_ASCII_US) );
115 	const char* locale = aRefLocale.getStr();
116 	fprintf(m_f, "extern const sal_Unicode ***  SAL_CALL %s%s(sal_Int16& nStyles, sal_Int16& nAttributes);\n", func, locale);
117 	fprintf(m_f, "const sal_Unicode ***  SAL_CALL %s%s(sal_Int16& nStyles, sal_Int16& nAttributes)\n{\n", func, theLocale);
118 	fprintf(m_f, "\treturn %s%s(nStyles, nAttributes);\n}\n", func, locale);
119 }
120 
121 void OFileWriter::writeFunction3(const char *func, const char *style, const char* levels, const char* attr, const char *array) const
122 {
123 	fprintf(m_f, "const sal_Unicode ****  SAL_CALL %s%s( sal_Int16& nStyles, sal_Int16& nLevels, sal_Int16& nAttributes )\n{\n", func, theLocale);
124 	fprintf(m_f, "\tnStyles     = %s;\n", style);
125 	fprintf(m_f, "\tnLevels     = %s;\n", levels);
126 	fprintf(m_f, "\tnAttributes = %s;\n", attr);
127 	fprintf(m_f, "\treturn %s;\n}\n", array);
128 }
129 
130 void OFileWriter::writeRefFunction3(const char *func, const ::rtl::OUString& useLocale) const
131 {
132     OString aRefLocale( OUStringToOString(useLocale, RTL_TEXTENCODING_ASCII_US) );
133 	const char* locale = aRefLocale.getStr();
134 	fprintf(m_f, "extern const sal_Unicode ****  SAL_CALL %s%s(sal_Int16& nStyles, sal_Int16& nLevels, sal_Int16& nAttributes);\n", func, locale);
135 	fprintf(m_f, "const sal_Unicode ****  SAL_CALL %s%s(sal_Int16& nStyles, sal_Int16& nLevels, sal_Int16& nAttributes)\n{\n", func, theLocale);
136 	fprintf(m_f, "\treturn %s%s(nStyles, nLevels, nAttributes);\n}\n", func, locale);
137 }
138 
139 void OFileWriter::writeIntParameter(const sal_Char* pAsciiStr, const sal_Int16 count, sal_Int16 val) const
140 {
141 	fprintf(m_f, "static const sal_Unicode %s%d[] = {%d};\n", pAsciiStr, count, val);
142 }
143 
144 bool OFileWriter::writeDefaultParameter(const sal_Char* pAsciiStr, const ::rtl::OUString& str, sal_Int16 count) const
145 {
146     bool bBool = (str.equalsAscii("true") ? 1 : 0);
147 	fprintf(m_f,"static const sal_Unicode default%s%d[] = {%d};\n", pAsciiStr, count, bBool);
148     return bBool;
149 }
150 
151 bool OFileWriter::writeDefaultParameter(const sal_Char* pAsciiStr, const ::rtl::OUString& str) const
152 {
153     bool bBool = (str.equalsAscii("true") ? 1 : 0);
154 	fprintf(m_f,"static const sal_Unicode default%s[] = {%d};\n", pAsciiStr, bBool);
155     return bBool;
156 }
157 
158 void OFileWriter::writeParameter(const sal_Char* pAsciiStr, const ::rtl::OUString& aChars) const
159 {
160 	fprintf(m_f, "static const sal_Unicode %s[] = {", pAsciiStr);
161 	writeStringCharacters(aChars);
162 	fprintf(m_f, "0x0};\n");
163 }
164 
165 void OFileWriter::writeParameter(const sal_Char* pAsciiStr, const ::rtl::OUString& aChars, sal_Int16 count) const
166 {
167 	fprintf(m_f, "static const sal_Unicode %s%d[] = {", pAsciiStr, count);
168 	writeStringCharacters(aChars);
169 	fprintf(m_f, "0x0};\n");
170 }
171 
172 void OFileWriter::writeParameter(const sal_Char* pAsciiStr, const ::rtl::OUString& aChars, sal_Int16 count0, sal_Int16 count1) const
173 {
174 	fprintf(m_f, "static const sal_Unicode %s%d%d[] = {", pAsciiStr, count0, count1);
175 	writeStringCharacters(aChars);
176 	fprintf(m_f, "0x0};\n");
177 }
178 
179 void OFileWriter::writeParameter(const sal_Char* pTagStr, const sal_Char* pAsciiStr, const ::rtl::OUString& aChars, const sal_Int16 count) const
180 {
181 	fprintf(m_f, "static const sal_Unicode %s%s%d[] = {", pTagStr, pAsciiStr, count);
182 	writeStringCharacters(aChars);
183 	fprintf(m_f, "0x0};\n");
184 }
185 
186 void OFileWriter::writeParameter(const sal_Char* pTagStr, const sal_Char* pAsciiStr, const ::rtl::OUString& aChars) const
187 {
188 	fprintf(m_f, "static const sal_Unicode %s%s[] = {", pTagStr, pAsciiStr);
189 	writeStringCharacters(aChars);
190 	fprintf(m_f, "0x0};\n");
191 }
192 
193 void OFileWriter::writeParameter(const sal_Char* pTagStr, const sal_Char* pAsciiStr, const ::rtl::OUString& aChars, sal_Int16 count0, sal_Int16 count1) const
194 {
195 	fprintf(m_f, "static const sal_Unicode %s%s%d%d[] = {", pTagStr, pAsciiStr, count0, count1);
196 	writeStringCharacters(aChars);
197 	fprintf(m_f, "0x0};\n");
198 }
199 
200 void OFileWriter::flush(void) const
201 {
202 	fflush( m_f );
203 }
204 
205 void OFileWriter::closeOutput(void) const
206 {
207 	if(m_f)
208 	{
209 		fclose( m_f );
210 		const_cast< OFileWriter * > ( this )->m_f = 0;
211 	}
212 }
213 
214