1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_tools.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include <stdio.h>
28*b1cdbd2cSJim Jagielski #include <string.h>
29*b1cdbd2cSJim Jagielski
30*b1cdbd2cSJim Jagielski #include <unistd.h>
31*b1cdbd2cSJim Jagielski
32*b1cdbd2cSJim Jagielski #include <sys/stat.h>
33*b1cdbd2cSJim Jagielski #include <tools/stream.hxx>
34*b1cdbd2cSJim Jagielski #include "cppdep.hxx"
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielski //#define TEST
37*b1cdbd2cSJim Jagielski
CppDep(ByteString aFileName)38*b1cdbd2cSJim Jagielski CppDep::CppDep( ByteString aFileName )
39*b1cdbd2cSJim Jagielski {
40*b1cdbd2cSJim Jagielski aSourceFile = aFileName;
41*b1cdbd2cSJim Jagielski
42*b1cdbd2cSJim Jagielski pSearchPath = new ByteStringList;
43*b1cdbd2cSJim Jagielski pFileList = new ByteStringList;
44*b1cdbd2cSJim Jagielski }
45*b1cdbd2cSJim Jagielski
CppDep()46*b1cdbd2cSJim Jagielski CppDep::CppDep()
47*b1cdbd2cSJim Jagielski {
48*b1cdbd2cSJim Jagielski pSources = new ByteStringList;
49*b1cdbd2cSJim Jagielski pSearchPath = new ByteStringList;
50*b1cdbd2cSJim Jagielski pFileList = new ByteStringList;
51*b1cdbd2cSJim Jagielski }
52*b1cdbd2cSJim Jagielski
~CppDep()53*b1cdbd2cSJim Jagielski CppDep::~CppDep()
54*b1cdbd2cSJim Jagielski {
55*b1cdbd2cSJim Jagielski delete pSources;
56*b1cdbd2cSJim Jagielski delete pSearchPath;
57*b1cdbd2cSJim Jagielski delete pFileList;
58*b1cdbd2cSJim Jagielski }
59*b1cdbd2cSJim Jagielski
Execute()60*b1cdbd2cSJim Jagielski void CppDep::Execute()
61*b1cdbd2cSJim Jagielski {
62*b1cdbd2cSJim Jagielski sal_uIntPtr nCount = pSources->Count();
63*b1cdbd2cSJim Jagielski for ( sal_uIntPtr n=0; n<nCount;n++)
64*b1cdbd2cSJim Jagielski {
65*b1cdbd2cSJim Jagielski ByteString *pStr = pSources->GetObject(n);
66*b1cdbd2cSJim Jagielski Search( *pStr );
67*b1cdbd2cSJim Jagielski }
68*b1cdbd2cSJim Jagielski }
69*b1cdbd2cSJim Jagielski
AddSearchPath(const char * aPath)70*b1cdbd2cSJim Jagielski sal_Bool CppDep::AddSearchPath( const char* aPath )
71*b1cdbd2cSJim Jagielski {
72*b1cdbd2cSJim Jagielski ByteString *pStr = new ByteString( aPath );
73*b1cdbd2cSJim Jagielski pSearchPath->Insert( pStr, LIST_APPEND );
74*b1cdbd2cSJim Jagielski return sal_False;
75*b1cdbd2cSJim Jagielski }
76*b1cdbd2cSJim Jagielski
AddSource(const char * aSource)77*b1cdbd2cSJim Jagielski sal_Bool CppDep::AddSource( const char* aSource )
78*b1cdbd2cSJim Jagielski {
79*b1cdbd2cSJim Jagielski ByteString *pStr = new ByteString( aSource );
80*b1cdbd2cSJim Jagielski pSources->Insert( pStr, LIST_APPEND );
81*b1cdbd2cSJim Jagielski return sal_False;
82*b1cdbd2cSJim Jagielski }
83*b1cdbd2cSJim Jagielski
Search(ByteString aFileName)84*b1cdbd2cSJim Jagielski sal_Bool CppDep::Search( ByteString aFileName )
85*b1cdbd2cSJim Jagielski {
86*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
87*b1cdbd2cSJim Jagielski fprintf( stderr, "SEARCH : %s\n", aFileName.GetBuffer());
88*b1cdbd2cSJim Jagielski #endif
89*b1cdbd2cSJim Jagielski sal_Bool bRet = sal_False;
90*b1cdbd2cSJim Jagielski
91*b1cdbd2cSJim Jagielski SvFileStream aFile;
92*b1cdbd2cSJim Jagielski ByteString aReadLine;
93*b1cdbd2cSJim Jagielski
94*b1cdbd2cSJim Jagielski UniString suFileName( aFileName, gsl_getSystemTextEncoding());
95*b1cdbd2cSJim Jagielski
96*b1cdbd2cSJim Jagielski aFile.Open( suFileName, STREAM_READ );
97*b1cdbd2cSJim Jagielski while ( aFile.ReadLine( aReadLine ))
98*b1cdbd2cSJim Jagielski {
99*b1cdbd2cSJim Jagielski sal_uInt16 nPos = aReadLine.Search( "include" );
100*b1cdbd2cSJim Jagielski if ( nPos != STRING_NOTFOUND )
101*b1cdbd2cSJim Jagielski {
102*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
103*b1cdbd2cSJim Jagielski fprintf( stderr, "found : %d %s\n", nPos, aReadLine.GetBuffer() );
104*b1cdbd2cSJim Jagielski #endif
105*b1cdbd2cSJim Jagielski ByteString aResult = IsIncludeStatement( aReadLine );
106*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
107*b1cdbd2cSJim Jagielski fprintf( stderr, "Result : %s\n", aResult.GetBuffer() );
108*b1cdbd2cSJim Jagielski #endif
109*b1cdbd2cSJim Jagielski
110*b1cdbd2cSJim Jagielski ByteString aNewFile;
111*b1cdbd2cSJim Jagielski if ( aResult !="")
112*b1cdbd2cSJim Jagielski if ( (aNewFile = Exists( aResult )) != "" )
113*b1cdbd2cSJim Jagielski {
114*b1cdbd2cSJim Jagielski sal_Bool bFound = sal_False;
115*b1cdbd2cSJim Jagielski sal_uIntPtr nCount = pFileList->Count();
116*b1cdbd2cSJim Jagielski for ( sal_uIntPtr i=0; i<nCount; i++ )
117*b1cdbd2cSJim Jagielski {
118*b1cdbd2cSJim Jagielski ByteString *pStr = pFileList->GetObject(i);
119*b1cdbd2cSJim Jagielski if ( *pStr == aNewFile )
120*b1cdbd2cSJim Jagielski bFound = sal_True;
121*b1cdbd2cSJim Jagielski }
122*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
123*b1cdbd2cSJim Jagielski fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.GetBuffer() );
124*b1cdbd2cSJim Jagielski #endif
125*b1cdbd2cSJim Jagielski if ( !bFound )
126*b1cdbd2cSJim Jagielski {
127*b1cdbd2cSJim Jagielski pFileList->Insert( new ByteString( aNewFile ), LIST_APPEND );
128*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
129*b1cdbd2cSJim Jagielski fprintf( stderr, " CppDep %s\\\n", aNewFile.GetBuffer() );
130*b1cdbd2cSJim Jagielski #endif
131*b1cdbd2cSJim Jagielski Search(aNewFile);
132*b1cdbd2cSJim Jagielski }
133*b1cdbd2cSJim Jagielski }
134*b1cdbd2cSJim Jagielski }
135*b1cdbd2cSJim Jagielski }
136*b1cdbd2cSJim Jagielski aFile.Close();
137*b1cdbd2cSJim Jagielski
138*b1cdbd2cSJim Jagielski return bRet;
139*b1cdbd2cSJim Jagielski }
140*b1cdbd2cSJim Jagielski
Exists(ByteString aFileName)141*b1cdbd2cSJim Jagielski ByteString CppDep::Exists( ByteString aFileName )
142*b1cdbd2cSJim Jagielski {
143*b1cdbd2cSJim Jagielski char pFullName[1023];
144*b1cdbd2cSJim Jagielski ByteString aString;
145*b1cdbd2cSJim Jagielski
146*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
147*b1cdbd2cSJim Jagielski fprintf( stderr, "Searching %s \n", aFileName.GetBuffer() );
148*b1cdbd2cSJim Jagielski #endif
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski sal_uIntPtr nCount = pSearchPath->Count();
151*b1cdbd2cSJim Jagielski for ( sal_uIntPtr n=0; n<nCount; n++)
152*b1cdbd2cSJim Jagielski {
153*b1cdbd2cSJim Jagielski struct stat aBuf;
154*b1cdbd2cSJim Jagielski ByteString *pPathName = pSearchPath->GetObject(n);
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim Jagielski strcpy( pFullName, pPathName->GetBuffer());
157*b1cdbd2cSJim Jagielski strcat( pFullName, DIR_SEP );
158*b1cdbd2cSJim Jagielski strcat( pFullName, aFileName.GetBuffer());
159*b1cdbd2cSJim Jagielski
160*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
161*b1cdbd2cSJim Jagielski fprintf( stderr, "looking for %s\t ", pFullName );
162*b1cdbd2cSJim Jagielski #endif
163*b1cdbd2cSJim Jagielski if ( stat( pFullName, &aBuf ) == 0 )
164*b1cdbd2cSJim Jagielski {
165*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
166*b1cdbd2cSJim Jagielski fprintf( stderr, "Got Dependency ", pFullName );
167*b1cdbd2cSJim Jagielski #endif
168*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
169*b1cdbd2cSJim Jagielski fprintf( stderr, "%s \\\n", pFullName );
170*b1cdbd2cSJim Jagielski #endif
171*b1cdbd2cSJim Jagielski
172*b1cdbd2cSJim Jagielski return ByteString(pFullName);
173*b1cdbd2cSJim Jagielski }
174*b1cdbd2cSJim Jagielski }
175*b1cdbd2cSJim Jagielski return aString;
176*b1cdbd2cSJim Jagielski }
177*b1cdbd2cSJim Jagielski
IsIncludeStatement(ByteString aLine)178*b1cdbd2cSJim Jagielski ByteString CppDep::IsIncludeStatement( ByteString aLine )
179*b1cdbd2cSJim Jagielski {
180*b1cdbd2cSJim Jagielski ByteString aRetStr;
181*b1cdbd2cSJim Jagielski if ( aLine.Search("/*",0) != STRING_NOTFOUND )
182*b1cdbd2cSJim Jagielski {
183*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
184*b1cdbd2cSJim Jagielski fprintf( stderr, "found starting C comment : %s\n", aLine.GetBuffer() );
185*b1cdbd2cSJim Jagielski #endif
186*b1cdbd2cSJim Jagielski aLine.Erase(aLine.Search("/*",0), aLine.Len() - 1);
187*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
188*b1cdbd2cSJim Jagielski fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
189*b1cdbd2cSJim Jagielski #endif
190*b1cdbd2cSJim Jagielski }
191*b1cdbd2cSJim Jagielski if ( aLine.Search("//",0) != STRING_NOTFOUND )
192*b1cdbd2cSJim Jagielski {
193*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
194*b1cdbd2cSJim Jagielski fprintf( stderr, "found C++ comment : %s\n", aLine.GetBuffer() );
195*b1cdbd2cSJim Jagielski #endif
196*b1cdbd2cSJim Jagielski aLine.Erase(aLine.Search("//",0), aLine.Len() - 1);
197*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
198*b1cdbd2cSJim Jagielski fprintf( stderr, "cleaned string : %s\n", aLine.GetBuffer() );
199*b1cdbd2cSJim Jagielski #endif
200*b1cdbd2cSJim Jagielski }
201*b1cdbd2cSJim Jagielski // WhiteSpacesfressen
202*b1cdbd2cSJim Jagielski aLine.EraseAllChars(' ');
203*b1cdbd2cSJim Jagielski aLine.EraseAllChars('\t');
204*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
205*b1cdbd2cSJim Jagielski fprintf( stderr, "now : %s\n", aLine.GetBuffer() );
206*b1cdbd2cSJim Jagielski #endif
207*b1cdbd2cSJim Jagielski // ist der erste Teil ein #include ?
208*b1cdbd2cSJim Jagielski ByteString aTmpStr;
209*b1cdbd2cSJim Jagielski aTmpStr = aLine.Copy( 0, 8 );
210*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
211*b1cdbd2cSJim Jagielski fprintf( stderr, "is include : %s\n", aTmpStr.GetBuffer() );
212*b1cdbd2cSJim Jagielski #endif
213*b1cdbd2cSJim Jagielski if ( aTmpStr.Equals("#include") )
214*b1cdbd2cSJim Jagielski {
215*b1cdbd2cSJim Jagielski aTmpStr = aLine.Erase( 0, 8 );
216*b1cdbd2cSJim Jagielski sal_uInt16 nLen = aLine.Len();
217*b1cdbd2cSJim Jagielski aLine.Erase( nLen-1, 1 );
218*b1cdbd2cSJim Jagielski aLine.Erase( 0, 1 );
219*b1cdbd2cSJim Jagielski #ifdef DEBUG_VERBOSE
220*b1cdbd2cSJim Jagielski fprintf( stderr, "Gotcha : %s\n", aLine.GetBuffer() );
221*b1cdbd2cSJim Jagielski #endif
222*b1cdbd2cSJim Jagielski aRetStr = aLine;
223*b1cdbd2cSJim Jagielski }
224*b1cdbd2cSJim Jagielski return aRetStr;
225*b1cdbd2cSJim Jagielski }
226*b1cdbd2cSJim Jagielski
227*b1cdbd2cSJim Jagielski #ifdef TEST
228*b1cdbd2cSJim Jagielski
main(int argc,char ** argv)229*b1cdbd2cSJim Jagielski int main( int argc, char **argv )
230*b1cdbd2cSJim Jagielski {
231*b1cdbd2cSJim Jagielski CppDep *pDep = new CppDep( "cppdep.cxx" );
232*b1cdbd2cSJim Jagielski pDep->AddSearchPath(".");
233*b1cdbd2cSJim Jagielski pDep->AddSearchPath("/usr/include");
234*b1cdbd2cSJim Jagielski pDep->AddSearchPath("/usr/local/include");
235*b1cdbd2cSJim Jagielski pDep->AddSearchPath("/usr/include/sys");
236*b1cdbd2cSJim Jagielski pDep->AddSearchPath("/usr/include/X11");
237*b1cdbd2cSJim Jagielski pDep->Execute();
238*b1cdbd2cSJim Jagielski delete pDep;
239*b1cdbd2cSJim Jagielski return 0;
240*b1cdbd2cSJim Jagielski }
241*b1cdbd2cSJim Jagielski
242*b1cdbd2cSJim Jagielski #endif
243