1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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"
40 #include <tools/stream.hxx>
45 CppDep::CppDep( ByteString aFileName
)
47 aSourceFile
= aFileName
;
49 pSearchPath
= new ByteStringList
;
50 pFileList
= new ByteStringList
;
55 pSources
= new ByteStringList
;
56 pSearchPath
= new ByteStringList
;
57 pFileList
= new ByteStringList
;
67 void CppDep::Execute()
69 ULONG nCount
= pSources
->Count();
70 for ( ULONG n
=0; n
<nCount
;n
++)
72 ByteString
*pStr
= pSources
->GetObject(n
);
77 BOOL
CppDep::AddSearchPath( const char* aPath
)
79 ByteString
*pStr
= new ByteString( aPath
);
80 pSearchPath
->Insert( pStr
, LIST_APPEND
);
84 BOOL
CppDep::AddSource( const char* aSource
)
86 ByteString
*pStr
= new ByteString( aSource
);
87 pSources
->Insert( pStr
, LIST_APPEND
);
91 BOOL
CppDep::Search( ByteString aFileName
)
94 fprintf( stderr
, "SEARCH : %s\n", aFileName
.GetBuffer());
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
)
110 fprintf( stderr
, "found : %d %s\n", nPos
, aReadLine
.GetBuffer() );
112 ByteString aResult
= IsIncludeStatement( aReadLine
);
114 fprintf( stderr
, "Result : %s\n", aResult
.GetBuffer() );
119 if ( (aNewFile
= Exists( aResult
)) != "" )
122 ULONG nCount
= pFileList
->Count();
123 for ( ULONG i
=0; i
<nCount
; i
++ )
125 ByteString
*pStr
= pFileList
->GetObject(i
);
126 if ( *pStr
== aNewFile
)
130 fprintf( stderr
, "not in list : %d %s\n", nPos
, aReadLine
.GetBuffer() );
134 pFileList
->Insert( new ByteString( aNewFile
), LIST_APPEND
);
136 fprintf( stderr
, " CppDep %s\\\n", aNewFile
.GetBuffer() );
148 ByteString
CppDep::Exists( ByteString aFileName
)
150 char pFullName
[1023];
154 fprintf( stderr
, "Searching %s \n", aFileName
.GetBuffer() );
157 ULONG nCount
= pSearchPath
->Count();
158 for ( ULONG n
=0; n
<nCount
; n
++)
161 ByteString
*pPathName
= pSearchPath
->GetObject(n
);
163 strcpy( pFullName
, pPathName
->GetBuffer());
164 strcat( pFullName
, DIR_SEP
);
165 strcat( pFullName
, aFileName
.GetBuffer());
168 fprintf( stderr
, "looking for %s\t ", pFullName
);
170 if ( stat( pFullName
, &aBuf
) == 0 )
173 fprintf( stderr
, "Got Dependency ", pFullName
);
176 fprintf( stderr
, "%s \\\n", pFullName
);
179 return ByteString(pFullName
);
185 ByteString
CppDep::IsIncludeStatement( ByteString aLine
)
188 if ( aLine
.Search("/*",0) != STRING_NOTFOUND
)
191 fprintf( stderr
, "found starting C comment : %s\n", aLine
.GetBuffer() );
193 aLine
.Erase(aLine
.Search("/*",0), aLine
.Len() - 1);
195 fprintf( stderr
, "cleaned string : %s\n", aLine
.GetBuffer() );
198 if ( aLine
.Search("//",0) != STRING_NOTFOUND
)
201 fprintf( stderr
, "found C++ comment : %s\n", aLine
.GetBuffer() );
203 aLine
.Erase(aLine
.Search("//",0), aLine
.Len() - 1);
205 fprintf( stderr
, "cleaned string : %s\n", aLine
.GetBuffer() );
208 // WhiteSpacesfressen
209 aLine
.EraseAllChars(' ');
210 aLine
.EraseAllChars('\t');
212 fprintf( stderr
, "now : %s\n", aLine
.GetBuffer() );
214 // ist der erste Teil ein #include ?
216 aTmpStr
= aLine
.Copy( 0, 8 );
218 fprintf( stderr
, "is include : %s\n", aTmpStr
.GetBuffer() );
220 if ( aTmpStr
.Equals("#include") )
222 aTmpStr
= aLine
.Erase( 0, 8 );
223 USHORT nLen
= aLine
.Len();
224 aLine
.Erase( nLen
-1, 1 );
227 fprintf( stderr
, "Gotcha : %s\n", aLine
.GetBuffer() );
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");