merge the formfield patch from ooo-build
[ooovba.git] / tools / bootstrp / addexes2 / mkfilt.cxx
blobf6e411e4cf5159d47aff4dfdd40e035d2e80d861
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mkfilt.cxx,v $
10 * $Revision: 1.11.34.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 #include <stdio.h>
36 #include <../../inc/tools/string.hxx>
37 #include <../../inc/tools/list.hxx>
39 class TextFilter
41 protected:
42 FILE *pIn, *pOut;
43 virtual void Filter();
44 public:
45 TextFilter( ByteString aInFile = "stdin",
46 ByteString aOutFile = "stdout" );
47 virtual ~TextFilter();
49 virtual void Execute();
52 TextFilter::TextFilter( ByteString aInFile, ByteString aOutFile )
54 if ( aInFile == "stdin" )
55 pIn = stdin;
56 else
57 if (( pIn = fopen( aInFile.GetBuffer(), "r" )) == NULL )
58 printf( "Can't read %s\n", aInFile.GetBuffer() );
60 if ( aOutFile == "stdout" )
61 pOut = stdout;
62 else
63 if (( pOut = fopen( aOutFile.GetBuffer(), "w" )) == NULL )
64 printf( "Can't write %s\n", aOutFile.GetBuffer() );
67 TextFilter::~TextFilter()
69 fclose( pOut );
70 fclose( pIn );
73 void TextFilter::Execute()
75 Filter();
78 void TextFilter::Filter()
80 int c;
81 while ( (c = fgetc( pIn )) != EOF )
82 fputc( c, pOut );
85 #define LINE_LEN 2048
87 class ByteStringList;
89 class MkLine
91 public:
92 ByteString aLine;
93 ByteStringList* pPrivateTnrLst;
94 BOOL bOut;
95 BOOL bHier;
97 MkLine();
100 MkLine::MkLine()
102 bOut = FALSE;
103 bHier = FALSE;
104 pPrivateTnrLst = NULL;
107 DECLARE_LIST( ByteStringList, MkLine * )
109 class MkFilter : public TextFilter
111 static ByteString aTnr;
112 ByteStringList *pLst;
113 ByteStringList *pTnrLst;
114 protected:
115 virtual void Filter();
116 public:
117 MkFilter( ByteString aInFile = "stdin", ByteString aOutFile = "stdout");
118 ~MkFilter();
121 MkFilter::MkFilter( ByteString aInFile, ByteString aOutFile ) :
122 TextFilter( aInFile, aOutFile )
124 pLst = new ByteStringList;
125 pTnrLst = new ByteStringList;
128 MkFilter::~MkFilter()
130 delete pTnrLst;
131 delete pLst;
134 ByteString MkFilter::aTnr="$(TNR)";
136 void MkFilter::Filter()
138 char aLineBuf[LINE_LEN];
139 int nState = 0;
141 while(( fgets(aLineBuf, LINE_LEN, pIn)) != NULL )
143 ByteString aLine( aLineBuf );
144 //fprintf(stderr, "aLine :%s\n", aLine.GetBuffer());
145 if ( aLine.Search("mkfilter1" ) != STRING_NOTFOUND )
147 // Zeilen unterdruecken
148 fprintf( stderr, "mkfilter1\n" );
149 nState = 0;
151 else if ( aLine.Search("unroll begin" ) != STRING_NOTFOUND )
153 // Zeilen raus schreiben mit ersetzen von $(TNR) nach int n
154 fprintf( stderr, "\nunroll begin\n" );
155 nState = 1;
159 if ( nState == 0 )
161 fprintf( stderr, "." );
162 MkLine *pMkLine = new MkLine();
163 ByteString *pStr = new ByteString( aLineBuf );
164 pMkLine->aLine = *pStr;
165 pMkLine->bOut = FALSE;
167 pLst->Insert( pMkLine, LIST_APPEND );
169 else if ( nState == 1 )
171 BOOL bInTnrList = TRUE;
172 fprintf( stderr, ":" );
173 MkLine *pMkLine = new MkLine();
174 if ( aLine.Search("unroll end") != STRING_NOTFOUND )
176 fprintf( stderr, ";\nunroll end\n" );
177 MkLine *p_MkLine = new MkLine();
178 p_MkLine->bHier = TRUE;
179 ByteString *pByteString = new ByteString("# do not delete this line === mkfilter3i\n");
180 p_MkLine->aLine = *pByteString;
181 p_MkLine->bOut = FALSE;
182 p_MkLine->pPrivateTnrLst = pTnrLst;
183 pTnrLst = new ByteStringList();
184 pLst->Insert( p_MkLine, LIST_APPEND );
185 nState = 0;
186 bInTnrList = FALSE;
188 ByteString *pStr = new ByteString( aLineBuf );
189 pMkLine->aLine = *pStr;
190 pMkLine->bOut = FALSE;
192 if ( bInTnrList )
193 pTnrLst->Insert( pMkLine, LIST_APPEND );
195 else {
196 /* Zeilen ignorieren */;
198 } // End Of File
199 fprintf( stderr, "\n" );
201 // das File wieder ausgegeben
202 ULONG nLines = pLst->Count();
203 for ( ULONG j=0; j<nLines; j++ )
205 MkLine *pLine = pLst->GetObject( j );
206 if ( pLine->bHier )
208 // die List n - Mal abarbeiten
209 for ( USHORT n=1; n<11; n++)
211 ULONG nCount = pLine->pPrivateTnrLst->Count();
212 for ( ULONG i=0; i<nCount; i++ )
214 MkLine *pMkLine = pLine->pPrivateTnrLst->GetObject(i);
215 ByteString aLine = pMkLine->aLine;
216 while( aLine.SearchAndReplace( aTnr, ByteString::CreateFromInt32( n )) != (USHORT)-1 ) ;
217 fputs( aLine.GetBuffer(), pOut );
218 fprintf( stderr, "o" );
221 if ( pLine->pPrivateTnrLst != NULL )
222 delete pLine->pPrivateTnrLst;
223 pLine->pPrivateTnrLst = NULL;
225 if ( pLine->bOut )
226 fputs(pLine->aLine.GetBuffer(), pOut );
228 fprintf( stderr, "\n" );
231 int main()
233 int nRet = 0;
235 TextFilter *pFlt = new MkFilter();
236 pFlt->Execute();
237 delete pFlt;
239 return nRet;