xref: /trunk/main/tools/bootstrp/addexes2/mkfilt.cxx (revision 89b56da7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 #include <stdio.h>
28 
29 #include <../../inc/tools/string.hxx>
30 #include <../../inc/tools/list.hxx>
31 
32 class TextFilter
33 {
34 protected:
35 	FILE			*pIn, *pOut;
36 	virtual void	Filter();
37 public:
38 					TextFilter( ByteString aInFile = "stdin",
39 						ByteString aOutFile = "stdout" );
40 	virtual			~TextFilter();
41 
42 	virtual void	Execute();
43 };
44 
TextFilter(ByteString aInFile,ByteString aOutFile)45 TextFilter::TextFilter( ByteString aInFile, ByteString aOutFile )
46 {
47 	if ( aInFile == "stdin" )
48 		pIn = stdin;
49 	else
50 		if (( pIn = fopen( aInFile.GetBuffer(), "r" )) == NULL )
51 			printf( "Can't read %s\n", aInFile.GetBuffer() );
52 
53 	if ( aOutFile == "stdout" )
54 		pOut = stdout;
55 	else
56 		if (( pOut = fopen( aOutFile.GetBuffer(), "w" )) == NULL )
57 			printf( "Can't write %s\n", aOutFile.GetBuffer() );
58 }
59 
~TextFilter()60 TextFilter::~TextFilter()
61 {
62 	fclose( pOut );
63 	fclose( pIn );
64 }
65 
Execute()66 void TextFilter::Execute()
67 {
68 	Filter();
69 }
70 
Filter()71 void TextFilter::Filter()
72 {
73 	int c;
74 	while ( (c = fgetc( pIn )) != EOF )
75 		fputc( c, pOut );
76 }
77 
78 #define LINE_LEN 2048
79 
80 class ByteStringList;
81 
82 class MkLine
83 {
84 public:
85 	ByteString			aLine;
86 	ByteStringList*		pPrivateTnrLst;
87 	sal_Bool			bOut;
88 	sal_Bool			bHier;
89 
90 					MkLine();
91 };
92 
MkLine()93 MkLine::MkLine()
94 {
95 	bOut = sal_False;
96 	bHier = sal_False;
97 	pPrivateTnrLst = NULL;
98 }
99 
100 DECLARE_LIST( ByteStringList, MkLine * )
101 
102 class MkFilter : public TextFilter
103 {
104 	static ByteString	aTnr;
105 	ByteStringList		*pLst;
106 	ByteStringList		*pTnrLst;
107 protected:
108 	virtual void	Filter();
109 public:
110 					MkFilter( ByteString aInFile = "stdin", ByteString aOutFile = "stdout");
111 					~MkFilter();
112 };
113 
MkFilter(ByteString aInFile,ByteString aOutFile)114 MkFilter::MkFilter( ByteString aInFile, ByteString aOutFile ) :
115 	TextFilter( aInFile, aOutFile )
116 {
117 	pLst = new ByteStringList;
118 	pTnrLst = new ByteStringList;
119 }
120 
~MkFilter()121 MkFilter::~MkFilter()
122 {
123 	delete pTnrLst;
124 	delete pLst;
125 }
126 
127 ByteString MkFilter::aTnr="$(TNR)";
128 
Filter()129 void MkFilter::Filter()
130 {
131 	char aLineBuf[LINE_LEN];
132 	int nState = 0;
133 
134 	while(( fgets(aLineBuf, LINE_LEN, pIn)) != NULL )
135 	{
136 		ByteString aLine( aLineBuf );
137 		//fprintf(stderr, "aLine :%s\n", aLine.GetBuffer());
138 		if ( aLine.Search("mkfilter1" ) != STRING_NOTFOUND )
139 		{
140 			// Zeilen unterdruecken
141 			fprintf( stderr, "mkfilter1\n" );
142 			nState = 0;
143 		}
144 		else if ( aLine.Search("unroll begin" ) != STRING_NOTFOUND )
145 		{
146 			// Zeilen raus schreiben mit ersetzen von $(TNR) nach int n
147 			fprintf( stderr, "\nunroll begin\n" );
148 			nState = 1;
149 		}
150 		;
151 
152 		if ( nState == 0  )
153 		{
154 			fprintf( stderr, "." );
155 			MkLine *pMkLine = new MkLine();
156 			ByteString *pStr = new ByteString( aLineBuf );
157 			pMkLine->aLine = *pStr;
158 			pMkLine->bOut = sal_False;
159 
160 			pLst->Insert( pMkLine, LIST_APPEND );
161 		}
162 		else if ( nState == 1 )
163 		{
164 			sal_Bool bInTnrList = sal_True;
165 			fprintf( stderr, ":" );
166 			MkLine *pMkLine = new MkLine();
167 			if ( aLine.Search("unroll end") != STRING_NOTFOUND )
168 			{
169 				fprintf( stderr, ";\nunroll end\n" );
170 				MkLine *p_MkLine = new MkLine();
171 				p_MkLine->bHier = sal_True;
172 				ByteString *pByteString = new ByteString("# do not delete this line === mkfilter3i\n");
173 				p_MkLine->aLine = *pByteString;
174 				p_MkLine->bOut = sal_False;
175 				p_MkLine->pPrivateTnrLst = pTnrLst;
176 				pTnrLst = new ByteStringList();
177 				pLst->Insert( p_MkLine, LIST_APPEND );
178 				nState = 0;
179 				bInTnrList = sal_False;
180 			}
181 			ByteString *pStr = new ByteString( aLineBuf );
182 			pMkLine->aLine = *pStr;
183 			pMkLine->bOut = sal_False;
184 
185 			if ( bInTnrList )
186 				pTnrLst->Insert( pMkLine, LIST_APPEND );
187 		}
188 		else {
189 			/* Zeilen ignorieren */;
190         }
191 	}	// End Of File
192 	fprintf( stderr, "\n" );
193 
194 	// das File wieder ausgegeben
195 	sal_uIntPtr nLines = pLst->Count();
196 	for ( sal_uIntPtr j=0; j<nLines; j++ )
197 	{
198 		MkLine *pLine = pLst->GetObject( j );
199 		if ( pLine->bHier )
200 		{
201 			// die List n - Mal abarbeiten
202 			for ( sal_uInt16 n=1; n<11; n++)
203 			{
204 				sal_uIntPtr nCount = pLine->pPrivateTnrLst->Count();
205 				for ( sal_uIntPtr i=0; i<nCount; i++ )
206 				{
207 					MkLine *pMkLine = pLine->pPrivateTnrLst->GetObject(i);
208 					ByteString aLine = pMkLine->aLine;
209 					while( aLine.SearchAndReplace( aTnr, ByteString::CreateFromInt32( n )) != (sal_uInt16)-1 ) ;
210 					fputs( aLine.GetBuffer(), pOut );
211 					fprintf( stderr, "o" );
212 				}
213 			}
214 			if ( pLine->pPrivateTnrLst != NULL )
215 				delete pLine->pPrivateTnrLst;
216 			pLine->pPrivateTnrLst = NULL;
217 		}
218 		if ( pLine->bOut )
219 				fputs(pLine->aLine.GetBuffer(), pOut );
220 	}
221 	fprintf( stderr, "\n" );
222 }
223 
main()224 int main()
225 {
226 	int nRet = 0;
227 
228 	TextFilter *pFlt = new MkFilter();
229 	pFlt->Execute();
230 	delete pFlt;
231 
232 	return nRet;
233 }
234