xref: /trunk/main/tools/bootstrp/cppdep.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <unistd.h>
35 
36 #include <sys/stat.h>
37 #include <tools/stream.hxx>
38 #include "cppdep.hxx"
39 
40 //#define TEST
41 
42 CppDep::CppDep( ByteString aFileName )
43 {
44     aSourceFile = aFileName;
45 
46     pSearchPath = new ByteStringList;
47     pFileList = new ByteStringList;
48 }
49 
50 CppDep::CppDep()
51 {
52     pSources = new ByteStringList;
53     pSearchPath = new ByteStringList;
54     pFileList = new ByteStringList;
55 }
56 
57 CppDep::~CppDep()
58 {
59     delete pSources;
60     delete pSearchPath;
61     delete pFileList;
62 }
63 
64 void CppDep::Execute()
65 {
66     sal_uIntPtr nCount = pSources->Count();
67     for ( sal_uIntPtr n=0; n<nCount;n++)
68     {
69         ByteString *pStr = pSources->GetObject(n);
70         Search( *pStr );
71     }
72 }
73 
74 sal_Bool CppDep::AddSearchPath( const char* aPath )
75 {
76     ByteString *pStr = new ByteString( aPath );
77     pSearchPath->Insert( pStr, LIST_APPEND );
78     return sal_False;
79 }
80 
81 sal_Bool CppDep::AddSource( const char* aSource )
82 {
83     ByteString *pStr = new ByteString( aSource );
84     pSources->Insert( pStr, LIST_APPEND );
85     return sal_False;
86 }
87 
88 sal_Bool CppDep::Search( ByteString aFileName )
89 {
90 #ifdef DEBUG_VERBOSE
91     fprintf( stderr, "SEARCH : %s\n", aFileName.GetBuffer());
92 #endif
93     sal_Bool bRet = sal_False;
94 
95     SvFileStream aFile;
96     ByteString aReadLine;
97 
98     UniString suFileName( aFileName, gsl_getSystemTextEncoding());
99 
100     aFile.Open( suFileName, STREAM_READ );
101     while ( aFile.ReadLine( aReadLine ))
102     {
103         sal_uInt16 nPos = aReadLine.Search( "include" );
104         if ( nPos != STRING_NOTFOUND  )
105         {
106 #ifdef DEBUG_VERBOSE
107             fprintf( stderr, "found : %d %s\n", nPos, aReadLine.GetBuffer() );
108 #endif
109             ByteString aResult = IsIncludeStatement( aReadLine );
110 #ifdef DEBUG_VERBOSE
111             fprintf( stderr, "Result : %s\n", aResult.GetBuffer() );
112 #endif
113 
114             ByteString aNewFile;
115             if ( aResult !="")
116             if ( (aNewFile = Exists( aResult )) != "" )
117             {
118                 sal_Bool bFound = sal_False;
119                 sal_uIntPtr nCount = pFileList->Count();
120                 for ( sal_uIntPtr i=0; i<nCount; i++ )
121                 {
122                     ByteString *pStr = pFileList->GetObject(i);
123                     if ( *pStr == aNewFile )
124                         bFound = sal_True;
125                 }
126 #ifdef DEBUG_VERBOSE
127                 fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.GetBuffer() );
128 #endif
129                 if ( !bFound )
130                 {
131                     pFileList->Insert( new ByteString( aNewFile ), LIST_APPEND );
132 #ifdef DEBUG_VERBOSE
133                     fprintf( stderr, " CppDep %s\\\n", aNewFile.GetBuffer() );
134 #endif
135                     Search(aNewFile);
136                 }
137             }
138         }
139     }
140     aFile.Close();
141 
142     return bRet;
143 }
144 
145 ByteString CppDep::Exists( ByteString aFileName )
146 {
147     char pFullName[1023];
148     ByteString aString;
149 
150 #ifdef DEBUG_VERBOSE
151     fprintf( stderr, "Searching %s \n", aFileName.GetBuffer() );
152 #endif
153 
154     sal_uIntPtr nCount = pSearchPath->Count();
155     for ( sal_uIntPtr n=0; n<nCount; n++)
156     {
157         struct stat aBuf;
158         ByteString *pPathName = pSearchPath->GetObject(n);
159 
160         strcpy( pFullName, pPathName->GetBuffer());
161         strcat( pFullName, DIR_SEP );
162         strcat( pFullName, aFileName.GetBuffer());
163 
164 #ifdef DEBUG_VERBOSE
165         fprintf( stderr, "looking for %s\t ", pFullName );
166 #endif
167         if ( stat( pFullName, &aBuf ) == 0 )
168         {
169 #ifdef DEBUG_VERBOSE
170             fprintf( stderr, "Got Dependency ", pFullName );
171 #endif
172 #ifdef DEBUG_VERBOSE
173             fprintf( stderr, "%s \\\n", pFullName );
174 #endif
175 
176             return ByteString(pFullName);
177         }
178     }
179     return aString;
180 }
181 
182 ByteString CppDep::IsIncludeStatement( ByteString aLine )
183 {
184     ByteString aRetStr;
185     if ( aLine.Search("/*",0) != STRING_NOTFOUND )
186     {
187 #ifdef DEBUG_VERBOSE
188         fprintf( stderr, "found starting C comment : %s\n", aLine.GetBuffer() );
189 #endif
190         aLine.Erase(aLine.Search("/*",0), aLine.Len() - 1);
191 #ifdef DEBUG_VERBOSE
192         fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
193 #endif
194     }
195     if ( aLine.Search("//",0) != STRING_NOTFOUND )
196     {
197 #ifdef DEBUG_VERBOSE
198         fprintf( stderr, "found C++ comment : %s\n", aLine.GetBuffer() );
199 #endif
200         aLine.Erase(aLine.Search("//",0), aLine.Len() - 1);
201 #ifdef DEBUG_VERBOSE
202         fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
203 #endif
204     }
205     // WhiteSpacesfressen
206     aLine.EraseAllChars(' ');
207     aLine.EraseAllChars('\t');
208 #ifdef DEBUG_VERBOSE
209     fprintf( stderr, "now : %s\n", aLine.GetBuffer() );
210 #endif
211     // ist der erste Teil ein #include ?
212     ByteString aTmpStr;
213     aTmpStr = aLine.Copy( 0, 8 );
214 #ifdef DEBUG_VERBOSE
215     fprintf( stderr, "is include : %s\n", aTmpStr.GetBuffer() );
216 #endif
217     if ( aTmpStr.Equals("#include") )
218     {
219         aTmpStr = aLine.Erase( 0, 8 );
220         sal_uInt16 nLen = aLine.Len();
221         aLine.Erase( nLen-1, 1 );
222         aLine.Erase( 0, 1 );
223 #ifdef DEBUG_VERBOSE
224         fprintf( stderr, "Gotcha : %s\n", aLine.GetBuffer() );
225 #endif
226         aRetStr = aLine;
227     }
228     return aRetStr;
229 }
230 
231 #ifdef TEST
232 
233 int main( int argc, char **argv )
234 {
235     CppDep *pDep = new CppDep( "cppdep.cxx" );
236     pDep->AddSearchPath(".");
237     pDep->AddSearchPath("/usr/include");
238     pDep->AddSearchPath("/usr/local/include");
239     pDep->AddSearchPath("/usr/include/sys");
240     pDep->AddSearchPath("/usr/include/X11");
241     pDep->Execute();
242     delete pDep;
243     return 0;
244 }
245 
246 #endif
247