Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <stdio.h>
22 :
23 : #include <../../inc/tools/string.hxx>
24 : #include <vector>
25 :
26 : class TextFilter
27 : {
28 : protected:
29 : FILE *pIn, *pOut;
30 : virtual void Filter();
31 : public:
32 : TextFilter( rtl::OString aInFile = "stdin",
33 : rtl::OString aOutFile = "stdout" );
34 : virtual ~TextFilter();
35 :
36 : virtual void Execute();
37 : };
38 :
39 0 : TextFilter::TextFilter( rtl::OString aInFile, rtl::OString aOutFile )
40 : {
41 0 : if ( aInFile == "stdin" )
42 0 : pIn = stdin;
43 : else
44 0 : if (( pIn = fopen( aInFile.getStr(), "r" )) == NULL )
45 0 : printf( "Can't read %s\n", aInFile.getStr() );
46 :
47 0 : if ( aOutFile == "stdout" )
48 0 : pOut = stdout;
49 : else
50 0 : if (( pOut = fopen( aOutFile.getStr(), "w" )) == NULL )
51 0 : printf( "Can't write %s\n", aOutFile.getStr() );
52 0 : }
53 :
54 0 : TextFilter::~TextFilter()
55 : {
56 0 : fclose( pOut );
57 0 : fclose( pIn );
58 0 : }
59 :
60 0 : void TextFilter::Execute()
61 : {
62 0 : Filter();
63 0 : }
64 :
65 0 : void TextFilter::Filter()
66 : {
67 : int c;
68 0 : while ( (c = fgetc( pIn )) != EOF )
69 0 : fputc( c, pOut );
70 0 : }
71 :
72 : #define LINE_LEN 2048
73 :
74 : class MkLine;
75 : typedef ::std::vector< MkLine* > ByteStringList;
76 :
77 0 : class MkLine
78 : {
79 : public:
80 : rtl::OString aLine;
81 : ByteStringList* pPrivateTnrLst;
82 : sal_Bool bOut;
83 : sal_Bool bHier;
84 :
85 : MkLine();
86 : };
87 :
88 0 : MkLine::MkLine()
89 : {
90 0 : bOut = sal_False;
91 0 : bHier = sal_False;
92 0 : pPrivateTnrLst = NULL;
93 0 : }
94 :
95 :
96 : class MkFilter : public TextFilter
97 : {
98 : static rtl::OString aTnr;
99 : ByteStringList *pLst;
100 : ByteStringList *pTnrLst;
101 : protected:
102 : virtual void Filter();
103 : public:
104 : MkFilter( rtl::OString aInFile = "stdin", rtl::OString aOutFile = "stdout");
105 : ~MkFilter();
106 : };
107 :
108 0 : MkFilter::MkFilter( rtl::OString aInFile, rtl::OString aOutFile ) :
109 0 : TextFilter( aInFile, aOutFile )
110 : {
111 0 : pLst = new ByteStringList;
112 0 : pTnrLst = new ByteStringList;
113 0 : }
114 :
115 0 : MkFilter::~MkFilter()
116 : {
117 0 : for ( size_t i = 0, n = pTnrLst->size(); i < n; ++i ) {
118 0 : delete (*pTnrLst)[ i ];
119 : }
120 0 : delete pTnrLst;
121 0 : for ( size_t i = 0, n = pLst->size(); i < n; ++i ) {
122 0 : delete (*pLst)[ i ];
123 : }
124 0 : delete pLst;
125 0 : }
126 :
127 0 : rtl::OString MkFilter::aTnr(RTL_CONSTASCII_STRINGPARAM("$(TNR)"));
128 :
129 0 : void MkFilter::Filter()
130 : {
131 : char aLineBuf[LINE_LEN];
132 0 : int nState = 0;
133 :
134 0 : while(( fgets(aLineBuf, LINE_LEN, pIn)) != NULL )
135 : {
136 0 : rtl::OString aLine(aLineBuf);
137 0 : if (aLine.indexOf(rtl::OString(RTL_CONSTASCII_STRINGPARAM("mkfilter1"))) != -1)
138 : {
139 : // surpress lines
140 0 : fprintf( stderr, "mkfilter1\n" );
141 0 : nState = 0;
142 : }
143 0 : else if (aLine.indexOf(rtl::OString(RTL_CONSTASCII_STRINGPARAM("unroll begin"))) != -1)
144 : {
145 : // Print lines while replacing $(TNR) with int n
146 0 : fprintf( stderr, "\nunroll begin\n" );
147 0 : nState = 1;
148 : }
149 :
150 0 : if ( nState == 0 )
151 : {
152 0 : fprintf( stderr, "." );
153 0 : MkLine *pMkLine = new MkLine();
154 0 : pMkLine->aLine = aLineBuf;
155 0 : pMkLine->bOut = sal_False;
156 :
157 0 : pLst->push_back( pMkLine );
158 : }
159 0 : else if ( nState == 1 )
160 : {
161 0 : sal_Bool bInTnrList = sal_True;
162 0 : fprintf( stderr, ":" );
163 0 : MkLine *pMkLine = new MkLine();
164 0 : if (aLine.indexOf(rtl::OString(RTL_CONSTASCII_STRINGPARAM("unroll end"))) != -1)
165 : {
166 0 : fprintf( stderr, ";\nunroll end\n" );
167 0 : MkLine *p_MkLine = new MkLine();
168 0 : p_MkLine->bHier = sal_True;
169 : p_MkLine->aLine = rtl::OString(RTL_CONSTASCII_STRINGPARAM(
170 0 : "# do not delete this line === mkfilter3i\n"));
171 0 : p_MkLine->bOut = sal_False;
172 0 : p_MkLine->pPrivateTnrLst = pTnrLst;
173 0 : pTnrLst = new ByteStringList();
174 0 : pLst->push_back( p_MkLine );
175 0 : nState = 0;
176 0 : bInTnrList = sal_False;
177 : }
178 0 : pMkLine->aLine = rtl::OString(aLineBuf);
179 0 : pMkLine->bOut = sal_False;
180 :
181 0 : if ( bInTnrList )
182 0 : pTnrLst->push_back( pMkLine );
183 : }
184 : else {
185 : /* Skip these lines */;
186 : }
187 0 : } // End Of File
188 0 : fprintf( stderr, "\n" );
189 :
190 : // Output file again
191 0 : size_t nLines = pLst->size();
192 0 : for ( size_t j=0; j<nLines; j++ )
193 : {
194 0 : MkLine *pLine = (*pLst)[ j ];
195 0 : if ( pLine->bHier )
196 : {
197 : // Iterate list n times
198 0 : for ( sal_uInt16 n=1; n<11; n++)
199 : {
200 0 : size_t nCount = pLine->pPrivateTnrLst->size();
201 0 : for ( size_t i=0; i<nCount; i++ )
202 : {
203 0 : MkLine *pMkLine = (*pLine->pPrivateTnrLst)[ i ];
204 0 : rtl::OString aLine = pMkLine->aLine.replaceAll(aTnr, rtl::OString::valueOf(static_cast<sal_Int32>(n)));
205 0 : fputs( aLine.getStr(), pOut );
206 0 : fprintf( stderr, "o" );
207 0 : }
208 : }
209 0 : if ( pLine->pPrivateTnrLst != NULL ) {
210 0 : for ( size_t i = 0, n = pLine->pPrivateTnrLst->size(); i < n; ++i ) {
211 0 : delete (*pLine->pPrivateTnrLst)[ i ];
212 : }
213 0 : delete pLine->pPrivateTnrLst;
214 : }
215 0 : pLine->pPrivateTnrLst = NULL;
216 : }
217 0 : if ( pLine->bOut )
218 0 : fputs(pLine->aLine.getStr(), pOut );
219 : }
220 0 : fprintf( stderr, "\n" );
221 0 : }
222 :
223 0 : int main()
224 : {
225 0 : int nRet = 0;
226 :
227 0 : TextFilter *pFlt = new MkFilter();
228 0 : pFlt->Execute();
229 0 : delete pFlt;
230 :
231 0 : return nRet;
232 0 : }
233 :
234 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|