xref: /aoo41x/main/tools/bootstrp/cppdep.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 #include <string.h>
29 
30 #include <unistd.h>
31 
32 #include <sys/stat.h>
33 #include <tools/stream.hxx>
34 #include "cppdep.hxx"
35 
36 //#define TEST
37 
CppDep(ByteString aFileName)38 CppDep::CppDep( ByteString aFileName )
39 {
40 	aSourceFile = aFileName;
41 
42 	pSearchPath = new ByteStringList;
43 	pFileList = new ByteStringList;
44 }
45 
CppDep()46 CppDep::CppDep()
47 {
48 	pSources = new ByteStringList;
49 	pSearchPath = new ByteStringList;
50 	pFileList = new ByteStringList;
51 }
52 
~CppDep()53 CppDep::~CppDep()
54 {
55 	delete pSources;
56 	delete pSearchPath;
57 	delete pFileList;
58 }
59 
Execute()60 void CppDep::Execute()
61 {
62 	sal_uIntPtr nCount = pSources->Count();
63 	for ( sal_uIntPtr n=0; n<nCount;n++)
64 	{
65 		ByteString *pStr = pSources->GetObject(n);
66 		Search( *pStr );
67 	}
68 }
69 
AddSearchPath(const char * aPath)70 sal_Bool CppDep::AddSearchPath( const char* aPath )
71 {
72 	ByteString *pStr = new ByteString( aPath );
73 	pSearchPath->Insert( pStr, LIST_APPEND );
74 	return sal_False;
75 }
76 
AddSource(const char * aSource)77 sal_Bool CppDep::AddSource( const char* aSource )
78 {
79 	ByteString *pStr = new ByteString( aSource );
80 	pSources->Insert( pStr, LIST_APPEND );
81 	return sal_False;
82 }
83 
Search(ByteString aFileName)84 sal_Bool CppDep::Search( ByteString aFileName )
85 {
86 #ifdef DEBUG_VERBOSE
87 	fprintf( stderr, "SEARCH : %s\n", aFileName.GetBuffer());
88 #endif
89 	sal_Bool bRet = sal_False;
90 
91 	SvFileStream aFile;
92 	ByteString aReadLine;
93 
94 	UniString suFileName( aFileName, gsl_getSystemTextEncoding());
95 
96 	aFile.Open( suFileName, STREAM_READ );
97 	while ( aFile.ReadLine( aReadLine ))
98 	{
99 		sal_uInt16 nPos = aReadLine.Search( "include" );
100 		if ( nPos != STRING_NOTFOUND  )
101 		{
102 #ifdef DEBUG_VERBOSE
103 			fprintf( stderr, "found : %d %s\n", nPos, aReadLine.GetBuffer() );
104 #endif
105 			ByteString aResult = IsIncludeStatement( aReadLine );
106 #ifdef DEBUG_VERBOSE
107 			fprintf( stderr, "Result : %s\n", aResult.GetBuffer() );
108 #endif
109 
110 			ByteString aNewFile;
111 			if ( aResult !="")
112 			if ( (aNewFile = Exists( aResult )) != "" )
113 			{
114 				sal_Bool bFound = sal_False;
115 				sal_uIntPtr nCount = pFileList->Count();
116 				for ( sal_uIntPtr i=0; i<nCount; i++ )
117 				{
118 					ByteString *pStr = pFileList->GetObject(i);
119 					if ( *pStr == aNewFile )
120 						bFound = sal_True;
121 				}
122 #ifdef DEBUG_VERBOSE
123 				fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.GetBuffer() );
124 #endif
125 				if ( !bFound )
126 				{
127 					pFileList->Insert( new ByteString( aNewFile ), LIST_APPEND );
128 #ifdef DEBUG_VERBOSE
129 					fprintf( stderr, " CppDep %s\\\n", aNewFile.GetBuffer() );
130 #endif
131 					Search(aNewFile);
132 				}
133 			}
134 		}
135 	}
136 	aFile.Close();
137 
138 	return bRet;
139 }
140 
Exists(ByteString aFileName)141 ByteString CppDep::Exists( ByteString aFileName )
142 {
143 	char pFullName[1023];
144 	ByteString aString;
145 
146 #ifdef DEBUG_VERBOSE
147 	fprintf( stderr, "Searching %s \n", aFileName.GetBuffer() );
148 #endif
149 
150 	sal_uIntPtr nCount = pSearchPath->Count();
151 	for ( sal_uIntPtr n=0; n<nCount; n++)
152 	{
153 		struct stat aBuf;
154 		ByteString *pPathName = pSearchPath->GetObject(n);
155 
156 		strcpy( pFullName, pPathName->GetBuffer());
157 		strcat( pFullName, DIR_SEP );
158 		strcat( pFullName, aFileName.GetBuffer());
159 
160 #ifdef DEBUG_VERBOSE
161 		fprintf( stderr, "looking for %s\t ", pFullName );
162 #endif
163 		if ( stat( pFullName, &aBuf ) == 0 )
164 		{
165 #ifdef DEBUG_VERBOSE
166 			fprintf( stderr, "Got Dependency ", pFullName );
167 #endif
168 #ifdef DEBUG_VERBOSE
169 			fprintf( stderr, "%s \\\n", pFullName );
170 #endif
171 
172 			return ByteString(pFullName);
173 		}
174 	}
175 	return aString;
176 }
177 
IsIncludeStatement(ByteString aLine)178 ByteString CppDep::IsIncludeStatement( ByteString aLine )
179 {
180 	ByteString aRetStr;
181 	if ( aLine.Search("/*",0) != STRING_NOTFOUND )
182 	{
183 #ifdef DEBUG_VERBOSE
184 		fprintf( stderr, "found starting C comment : %s\n", aLine.GetBuffer() );
185 #endif
186 		aLine.Erase(aLine.Search("/*",0), aLine.Len() - 1);
187 #ifdef DEBUG_VERBOSE
188 		fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
189 #endif
190 	}
191 	if ( aLine.Search("//",0) != STRING_NOTFOUND )
192 	{
193 #ifdef DEBUG_VERBOSE
194 		fprintf( stderr, "found C++ comment : %s\n", aLine.GetBuffer() );
195 #endif
196 		aLine.Erase(aLine.Search("//",0), aLine.Len() - 1);
197 #ifdef DEBUG_VERBOSE
198 		fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
199 #endif
200 	}
201 	// WhiteSpacesfressen
202 	aLine.EraseAllChars(' ');
203 	aLine.EraseAllChars('\t');
204 #ifdef DEBUG_VERBOSE
205 	fprintf( stderr, "now : %s\n", aLine.GetBuffer() );
206 #endif
207 	// ist der erste Teil ein #include ?
208 	ByteString aTmpStr;
209 	aTmpStr = aLine.Copy( 0, 8 );
210 #ifdef DEBUG_VERBOSE
211 	fprintf( stderr, "is include : %s\n", aTmpStr.GetBuffer() );
212 #endif
213 	if ( aTmpStr.Equals("#include") )
214 	{
215 		aTmpStr = aLine.Erase( 0, 8 );
216 		sal_uInt16 nLen = aLine.Len();
217 		aLine.Erase( nLen-1, 1 );
218 		aLine.Erase( 0, 1 );
219 #ifdef DEBUG_VERBOSE
220 		fprintf( stderr, "Gotcha : %s\n", aLine.GetBuffer() );
221 #endif
222 		aRetStr = aLine;
223 	}
224 	return aRetStr;
225 }
226 
227 #ifdef TEST
228 
main(int argc,char ** argv)229 int main( int argc, char **argv )
230 {
231 	CppDep *pDep = new CppDep( "cppdep.cxx" );
232 	pDep->AddSearchPath(".");
233 	pDep->AddSearchPath("/usr/include");
234 	pDep->AddSearchPath("/usr/local/include");
235 	pDep->AddSearchPath("/usr/include/sys");
236 	pDep->AddSearchPath("/usr/include/X11");
237 	pDep->Execute();
238 	delete pDep;
239 	return 0;
240 }
241 
242 #endif
243