merge the formfield patch from ooo-build
[ooovba.git] / tools / bootstrp / cppdep.cxx
blobb9f965e8f2f55ee092ae2821f64c49b6fba85747
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: cppdep.cxx,v $
10 * $Revision: 1.16.42.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>
35 #include <string.h>
37 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <tools/stream.hxx>
41 #include "cppdep.hxx"
43 //#define TEST
45 CppDep::CppDep( ByteString aFileName )
47 aSourceFile = aFileName;
49 pSearchPath = new ByteStringList;
50 pFileList = new ByteStringList;
53 CppDep::CppDep()
55 pSources = new ByteStringList;
56 pSearchPath = new ByteStringList;
57 pFileList = new ByteStringList;
60 CppDep::~CppDep()
62 delete pSources;
63 delete pSearchPath;
64 delete pFileList;
67 void CppDep::Execute()
69 ULONG nCount = pSources->Count();
70 for ( ULONG n=0; n<nCount;n++)
72 ByteString *pStr = pSources->GetObject(n);
73 Search( *pStr );
77 BOOL CppDep::AddSearchPath( const char* aPath )
79 ByteString *pStr = new ByteString( aPath );
80 pSearchPath->Insert( pStr, LIST_APPEND );
81 return FALSE;
84 BOOL CppDep::AddSource( const char* aSource )
86 ByteString *pStr = new ByteString( aSource );
87 pSources->Insert( pStr, LIST_APPEND );
88 return FALSE;
91 BOOL CppDep::Search( ByteString aFileName )
93 #ifdef DEBUG_VERBOSE
94 fprintf( stderr, "SEARCH : %s\n", aFileName.GetBuffer());
95 #endif
96 BOOL bRet = FALSE;
98 SvFileStream aFile;
99 ByteString aReadLine;
101 UniString suFileName( aFileName, gsl_getSystemTextEncoding());
103 aFile.Open( suFileName, STREAM_READ );
104 while ( aFile.ReadLine( aReadLine ))
106 USHORT nPos = aReadLine.Search( "include" );
107 if ( nPos != STRING_NOTFOUND )
109 #ifdef DEBUG_VERBOSE
110 fprintf( stderr, "found : %d %s\n", nPos, aReadLine.GetBuffer() );
111 #endif
112 ByteString aResult = IsIncludeStatement( aReadLine );
113 #ifdef DEBUG_VERBOSE
114 fprintf( stderr, "Result : %s\n", aResult.GetBuffer() );
115 #endif
117 ByteString aNewFile;
118 if ( aResult !="")
119 if ( (aNewFile = Exists( aResult )) != "" )
121 BOOL bFound = FALSE;
122 ULONG nCount = pFileList->Count();
123 for ( ULONG i=0; i<nCount; i++ )
125 ByteString *pStr = pFileList->GetObject(i);
126 if ( *pStr == aNewFile )
127 bFound = TRUE;
129 #ifdef DEBUG_VERBOSE
130 fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.GetBuffer() );
131 #endif
132 if ( !bFound )
134 pFileList->Insert( new ByteString( aNewFile ), LIST_APPEND );
135 #ifdef DEBUG_VERBOSE
136 fprintf( stderr, " CppDep %s\\\n", aNewFile.GetBuffer() );
137 #endif
138 Search(aNewFile);
143 aFile.Close();
145 return bRet;
148 ByteString CppDep::Exists( ByteString aFileName )
150 char pFullName[1023];
151 ByteString aString;
153 #ifdef DEBUG_VERBOSE
154 fprintf( stderr, "Searching %s \n", aFileName.GetBuffer() );
155 #endif
157 ULONG nCount = pSearchPath->Count();
158 for ( ULONG n=0; n<nCount; n++)
160 struct stat aBuf;
161 ByteString *pPathName = pSearchPath->GetObject(n);
163 strcpy( pFullName, pPathName->GetBuffer());
164 strcat( pFullName, DIR_SEP );
165 strcat( pFullName, aFileName.GetBuffer());
167 #ifdef DEBUG_VERBOSE
168 fprintf( stderr, "looking for %s\t ", pFullName );
169 #endif
170 if ( stat( pFullName, &aBuf ) == 0 )
172 #ifdef DEBUG_VERBOSE
173 fprintf( stderr, "Got Dependency ", pFullName );
174 #endif
175 #ifdef DEBUG_VERBOSE
176 fprintf( stderr, "%s \\\n", pFullName );
177 #endif
179 return ByteString(pFullName);
182 return aString;
185 ByteString CppDep::IsIncludeStatement( ByteString aLine )
187 ByteString aRetStr;
188 if ( aLine.Search("/*",0) != STRING_NOTFOUND )
190 #ifdef DEBUG_VERBOSE
191 fprintf( stderr, "found starting C comment : %s\n", aLine.GetBuffer() );
192 #endif
193 aLine.Erase(aLine.Search("/*",0), aLine.Len() - 1);
194 #ifdef DEBUG_VERBOSE
195 fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
196 #endif
198 if ( aLine.Search("//",0) != STRING_NOTFOUND )
200 #ifdef DEBUG_VERBOSE
201 fprintf( stderr, "found C++ comment : %s\n", aLine.GetBuffer() );
202 #endif
203 aLine.Erase(aLine.Search("//",0), aLine.Len() - 1);
204 #ifdef DEBUG_VERBOSE
205 fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
206 #endif
208 // WhiteSpacesfressen
209 aLine.EraseAllChars(' ');
210 aLine.EraseAllChars('\t');
211 #ifdef DEBUG_VERBOSE
212 fprintf( stderr, "now : %s\n", aLine.GetBuffer() );
213 #endif
214 // ist der erste Teil ein #include ?
215 ByteString aTmpStr;
216 aTmpStr = aLine.Copy( 0, 8 );
217 #ifdef DEBUG_VERBOSE
218 fprintf( stderr, "is include : %s\n", aTmpStr.GetBuffer() );
219 #endif
220 if ( aTmpStr.Equals("#include") )
222 aTmpStr = aLine.Erase( 0, 8 );
223 USHORT nLen = aLine.Len();
224 aLine.Erase( nLen-1, 1 );
225 aLine.Erase( 0, 1 );
226 #ifdef DEBUG_VERBOSE
227 fprintf( stderr, "Gotcha : %s\n", aLine.GetBuffer() );
228 #endif
229 aRetStr = aLine;
231 return aRetStr;
234 #ifdef TEST
236 int main( int argc, char **argv )
238 CppDep *pDep = new CppDep( "cppdep.cxx" );
239 pDep->AddSearchPath(".");
240 pDep->AddSearchPath("/usr/include");
241 pDep->AddSearchPath("/usr/local/include");
242 pDep->AddSearchPath("/usr/include/sys");
243 pDep->AddSearchPath("/usr/include/X11");
244 pDep->Execute();
245 delete pDep;
246 return 0;
249 #endif