Create branches
[apertium.git] / apertium / apertium / reformat.xsl
blob548082b9a9838c427444140bc40054d56663411a
1 <?xml version="1.0" encoding="ISO-8859-1"?> <!-- -*- nxml -*- -->
2 <!--
3 Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 02111-1307, USA.
19 -->
20 <xsl:stylesheet version="1.0"
21 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
22 <xsl:output method="text" encoding="ISO-8859-1"/>
24 <xsl:template match="format">
28 #include &lt;apertium/ApertiumConfig.H&gt;
30 #if !HAVE_DECL_FPUTS_UNLOCKED
31 #define fputs_unlocked fputs
32 #endif
34 #if !HAVE_DECL_FPUTC_UNLOCKED
35 #define fputc_unlocked fputc
36 #endif
38 #include &lt;cstdlib&gt;
39 #include &lt;iostream&gt;
40 #include &lt;libgen.h&gt;
41 #include &lt;map&gt;
42 #include &lt;string&gt;
43 #include &lt;unistd.h&gt;
46 using namespace std;
48 <xsl:for-each select="./rules/replacement-rule">
49 <xsl:variable name="varname"
50 select="concat(concat(string('S'),position()),string('_substitution'))"/>
51 <xsl:value-of select="string('map&lt;string, string&gt; S')"/>
52 <xsl:value-of select="position()"/>
53 <xsl:value-of select="string('_substitution;&#xA;&#xA;void S')"/>
54 <xsl:value-of select="position()"/>
55 <xsl:value-of select="string('_init()&#xA;{')"/>
57 <xsl:for-each select="./replace">
58 <xsl:if test="./@prefer = string('yes')">
59 <xsl:value-of select="string('&#xA; ')"/>
60 <xsl:value-of select="$varname"/>
61 <xsl:value-of select="string('[&quot;')"/>
62 <xsl:value-of select="./@target"/>
63 <xsl:value-of select="string('&quot;] = &quot;')"/>
64 <xsl:value-of select="./@source"/>
65 <xsl:value-of select="string('&quot;;')"/>
66 </xsl:if>
67 </xsl:for-each>
69 <xsl:value-of select="string('&#xA;}&#xA;')"/>
70 </xsl:for-each>
74 %option nounput
75 %option noyywrap<xsl:if test="./options/case-sensitive/@value=string('no')">
76 %option caseless</xsl:if>
80 "["|"]"&#x9;{
81 // do nothing
84 "[@"[^]]+"]"&#x9;{
85 string filename = yytext;
86 filename = filename.substr(2, filename.size()-3);
87 FILE *temp = fopen(filename.c_str(), "r");
88 int mychar;
90 if(!temp)
92 cerr &lt;&lt; "ERROR: File '" &lt;&lt; filename &lt;&lt;"' not found." &lt;&lt; endl;
93 exit(EXIT_FAILURE);
95 while((mychar = fgetc_unlocked(temp)) != EOF)
97 fputc_unlocked(mychar, yyout);
99 fclose(temp);
100 unlink(filename.c_str());
103 "[\\@"&#x9;{
104 fputc_unlocked('@', yyout);
107 ".[]"&#x9;{
108 // do nothing
111 "\\"<xsl:value-of select="/format/options/escape-chars/@regexp"/>&#x9;{
112 fputc_unlocked(yytext[1], yyout);
117 .|\n&#x9;{
118 <xsl:for-each select="./rules/replacement-rule">
119 <xsl:variable name="varname"
120 select="concat(concat(string('S'),position()),string('_substitution'))"/>
122 <xsl:value-of select="string(' ')"/>
123 <xsl:if test="not(position()=1)">
124 <xsl:value-of select="string('else ')"/>
125 </xsl:if>
126 <xsl:value-of select="string('if(')"/>
127 <xsl:value-of select="$varname"/>
128 <xsl:value-of select="string('.find(yytext) != ')"/>
129 <xsl:value-of select="$varname"/>
130 <xsl:value-of select="string('.end())&#xA; {&#xA;')"/>
131 <xsl:value-of select="string(' fputs_unlocked(')"/>
132 <xsl:value-of select="$varname"/>
133 <xsl:value-of select="string('[yytext].c_str(), yyout);')"/>
134 <xsl:value-of select="string('&#xA; }&#xA;')"/>
135 </xsl:for-each>
137 <xsl:if test="not(count(./rules/replacement-rule)=0)">
138 <xsl:value-of select="string(' else&#xA; {&#xA; ')"/>
139 </xsl:if>
140 <xsl:value-of select="string(' fputc_unlocked(yytext[0], yyout);&#xA;')"/>
141 <xsl:if test="not(count(./rules/replacement-rule)=0)">
142 <xsl:value-of select="string(' }')"/>
143 </xsl:if>
147 &lt;&lt;EOF&gt;&gt;&#x9;{
148 return 0;
153 void usage(string const &amp;progname)
155 cerr &lt;&lt; "USAGE: " &lt;&lt; progname &lt;&lt; " [input_file [output_file]" &lt;&lt; ']' &lt;&lt; endl;
156 cerr &lt;&lt; "<xsl:value-of select="./@name"/> format processor " &lt;&lt; endl;
157 exit(EXIT_SUCCESS);
160 int main(int argc, char *argv[])
162 if(argc &gt; 3)
164 usage(argv[0]);
167 switch(argc)
169 case 3:
170 yyout = fopen(argv[2], "w");
171 if(!yyout)
173 usage(argv[0]);
175 case 2:
176 yyin = fopen(argv[1], "r");
177 if(!yyin)
179 usage(argv[0]);
181 break;
182 default:
183 break;
186 <xsl:for-each select="./rules/replacement-rule">
187 <xsl:value-of select="string(' S')"/>
188 <xsl:value-of select="position()"/>
189 <xsl:value-of select="string('_init();&#xA;')"/>
190 </xsl:for-each>
192 yylex();
193 fclose(yyin);
194 fclose(yyout);
196 </xsl:template>
197 </xsl:stylesheet>