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