xref: /trunk/main/tools/bootstrp/addexes2/mkfilt.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_tools.hxx"
30 
31 #include <stdio.h>
32 
33 #include <../../inc/tools/string.hxx>
34 #include <../../inc/tools/list.hxx>
35 
36 class TextFilter
37 {
38 protected:
39     FILE            *pIn, *pOut;
40     virtual void    Filter();
41 public:
42                     TextFilter( ByteString aInFile = "stdin",
43                         ByteString aOutFile = "stdout" );
44     virtual         ~TextFilter();
45 
46     virtual void    Execute();
47 };
48 
49 TextFilter::TextFilter( ByteString aInFile, ByteString aOutFile )
50 {
51     if ( aInFile == "stdin" )
52         pIn = stdin;
53     else
54         if (( pIn = fopen( aInFile.GetBuffer(), "r" )) == NULL )
55             printf( "Can't read %s\n", aInFile.GetBuffer() );
56 
57     if ( aOutFile == "stdout" )
58         pOut = stdout;
59     else
60         if (( pOut = fopen( aOutFile.GetBuffer(), "w" )) == NULL )
61             printf( "Can't write %s\n", aOutFile.GetBuffer() );
62 }
63 
64 TextFilter::~TextFilter()
65 {
66     fclose( pOut );
67     fclose( pIn );
68 }
69 
70 void TextFilter::Execute()
71 {
72     Filter();
73 }
74 
75 void TextFilter::Filter()
76 {
77     int c;
78     while ( (c = fgetc( pIn )) != EOF )
79         fputc( c, pOut );
80 }
81 
82 #define LINE_LEN 2048
83 
84 class ByteStringList;
85 
86 class MkLine
87 {
88 public:
89     ByteString          aLine;
90     ByteStringList*     pPrivateTnrLst;
91     sal_Bool            bOut;
92     sal_Bool            bHier;
93 
94                     MkLine();
95 };
96 
97 MkLine::MkLine()
98 {
99     bOut = sal_False;
100     bHier = sal_False;
101     pPrivateTnrLst = NULL;
102 }
103 
104 DECLARE_LIST( ByteStringList, MkLine * )
105 
106 class MkFilter : public TextFilter
107 {
108     static ByteString   aTnr;
109     ByteStringList      *pLst;
110     ByteStringList      *pTnrLst;
111 protected:
112     virtual void    Filter();
113 public:
114                     MkFilter( ByteString aInFile = "stdin", ByteString aOutFile = "stdout");
115                     ~MkFilter();
116 };
117 
118 MkFilter::MkFilter( ByteString aInFile, ByteString aOutFile ) :
119     TextFilter( aInFile, aOutFile )
120 {
121     pLst = new ByteStringList;
122     pTnrLst = new ByteStringList;
123 }
124 
125 MkFilter::~MkFilter()
126 {
127     delete pTnrLst;
128     delete pLst;
129 }
130 
131 ByteString MkFilter::aTnr="$(TNR)";
132 
133 void MkFilter::Filter()
134 {
135     char aLineBuf[LINE_LEN];
136     int nState = 0;
137 
138     while(( fgets(aLineBuf, LINE_LEN, pIn)) != NULL )
139     {
140         ByteString aLine( aLineBuf );
141         //fprintf(stderr, "aLine :%s\n", aLine.GetBuffer());
142         if ( aLine.Search("mkfilter1" ) != STRING_NOTFOUND )
143         {
144             // Zeilen unterdruecken
145             fprintf( stderr, "mkfilter1\n" );
146             nState = 0;
147         }
148         else if ( aLine.Search("unroll begin" ) != STRING_NOTFOUND )
149         {
150             // Zeilen raus schreiben mit ersetzen von $(TNR) nach int n
151             fprintf( stderr, "\nunroll begin\n" );
152             nState = 1;
153         }
154         ;
155 
156         if ( nState == 0  )
157         {
158             fprintf( stderr, "." );
159             MkLine *pMkLine = new MkLine();
160             ByteString *pStr = new ByteString( aLineBuf );
161             pMkLine->aLine = *pStr;
162             pMkLine->bOut = sal_False;
163 
164             pLst->Insert( pMkLine, LIST_APPEND );
165         }
166         else if ( nState == 1 )
167         {
168             sal_Bool bInTnrList = sal_True;
169             fprintf( stderr, ":" );
170             MkLine *pMkLine = new MkLine();
171             if ( aLine.Search("unroll end") != STRING_NOTFOUND )
172             {
173                 fprintf( stderr, ";\nunroll end\n" );
174                 MkLine *p_MkLine = new MkLine();
175                 p_MkLine->bHier = sal_True;
176                 ByteString *pByteString = new ByteString("# do not delete this line === mkfilter3i\n");
177                 p_MkLine->aLine = *pByteString;
178                 p_MkLine->bOut = sal_False;
179                 p_MkLine->pPrivateTnrLst = pTnrLst;
180                 pTnrLst = new ByteStringList();
181                 pLst->Insert( p_MkLine, LIST_APPEND );
182                 nState = 0;
183                 bInTnrList = sal_False;
184             }
185             ByteString *pStr = new ByteString( aLineBuf );
186             pMkLine->aLine = *pStr;
187             pMkLine->bOut = sal_False;
188 
189             if ( bInTnrList )
190                 pTnrLst->Insert( pMkLine, LIST_APPEND );
191         }
192         else {
193             /* Zeilen ignorieren */;
194         }
195     }   // End Of File
196     fprintf( stderr, "\n" );
197 
198     // das File wieder ausgegeben
199     sal_uIntPtr nLines = pLst->Count();
200     for ( sal_uIntPtr j=0; j<nLines; j++ )
201     {
202         MkLine *pLine = pLst->GetObject( j );
203         if ( pLine->bHier )
204         {
205             // die List n - Mal abarbeiten
206             for ( sal_uInt16 n=1; n<11; n++)
207             {
208                 sal_uIntPtr nCount = pLine->pPrivateTnrLst->Count();
209                 for ( sal_uIntPtr i=0; i<nCount; i++ )
210                 {
211                     MkLine *pMkLine = pLine->pPrivateTnrLst->GetObject(i);
212                     ByteString aLine = pMkLine->aLine;
213                     while( aLine.SearchAndReplace( aTnr, ByteString::CreateFromInt32( n )) != (sal_uInt16)-1 ) ;
214                     fputs( aLine.GetBuffer(), pOut );
215                     fprintf( stderr, "o" );
216                 }
217             }
218             if ( pLine->pPrivateTnrLst != NULL )
219                 delete pLine->pPrivateTnrLst;
220             pLine->pPrivateTnrLst = NULL;
221         }
222         if ( pLine->bOut )
223                 fputs(pLine->aLine.GetBuffer(), pOut );
224     }
225     fprintf( stderr, "\n" );
226 }
227 
228 int main()
229 {
230     int nRet = 0;
231 
232     TextFilter *pFlt = new MkFilter();
233     pFlt->Execute();
234     delete pFlt;
235 
236     return nRet;
237 }
238