2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-11 09:51:50 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
2000-09-18 16:07:07 +00:00
|
|
|
*
|
2012-07-11 09:51:50 +01:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2000-09-18 16:07:07 +00:00
|
|
|
*
|
2012-07-11 09:51:50 +01:00
|
|
|
* This file incorporates work covered by the following license notice:
|
2000-09-18 16:07:07 +00:00
|
|
|
*
|
2012-07-11 09:51:50 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2006-09-16 23:48:05 +00:00
|
|
|
|
2000-09-18 16:07:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2000-09-22 15:41:03 +00:00
|
|
|
#include <sys/stat.h>
|
2007-06-27 21:06:03 +00:00
|
|
|
#include <tools/stream.hxx>
|
2011-08-16 08:54:49 +01:00
|
|
|
#include <rtl/strbuf.hxx>
|
2000-09-18 16:07:07 +00:00
|
|
|
#include "cppdep.hxx"
|
|
|
|
|
|
|
|
CppDep::CppDep()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CppDep::~CppDep()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppDep::Execute()
|
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
size_t nCount = m_aSources.size();
|
|
|
|
for ( size_t n = 0; n < nCount; ++n )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
const rtl::OString &rStr = m_aSources[n];
|
|
|
|
Search(rStr);
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-27 08:21:00 +00:00
|
|
|
sal_Bool CppDep::AddSearchPath( const char* pPath )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
m_aSearchPath.push_back( rtl::OString(pPath) );
|
2010-07-29 10:56:19 +08:00
|
|
|
return sal_False;
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 08:21:00 +00:00
|
|
|
sal_Bool CppDep::AddSource( const char* pSource )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
m_aSources.push_back( rtl::OString(pSource) );
|
2010-07-29 10:56:19 +08:00
|
|
|
return sal_False;
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 23:32:42 +00:00
|
|
|
sal_Bool CppDep::Search(const rtl::OString &rFileName)
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-12-06 23:32:42 +00:00
|
|
|
fprintf( stderr, "SEARCH : %s\n", rFileName.getStr());
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2010-07-29 10:56:19 +08:00
|
|
|
sal_Bool bRet = sal_False;
|
2000-09-18 16:07:07 +00:00
|
|
|
|
|
|
|
SvFileStream aFile;
|
2011-11-13 23:17:06 +00:00
|
|
|
rtl::OString aReadLine;
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2012-03-25 21:15:20 +01:00
|
|
|
rtl::OUString suFileName(rtl::OStringToOUString(rFileName, osl_getThreadTextEncoding()));
|
2000-09-18 16:07:07 +00:00
|
|
|
|
|
|
|
aFile.Open( suFileName, STREAM_READ );
|
|
|
|
while ( aFile.ReadLine( aReadLine ))
|
|
|
|
{
|
2012-02-15 15:26:43 +01:00
|
|
|
sal_Int32 nPos = aReadLine.indexOfL(
|
2011-11-13 23:17:06 +00:00
|
|
|
RTL_CONSTASCII_STRINGPARAM("include"));
|
|
|
|
if ( nPos != -1 )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-11-13 23:17:06 +00:00
|
|
|
fprintf( stderr, "found : %d %s\n", nPos, aReadLine.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2011-12-06 23:32:42 +00:00
|
|
|
rtl::OString aResult = IsIncludeStatement( aReadLine );
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-12-06 23:32:42 +00:00
|
|
|
fprintf( stderr, "Result : %s\n", aResult.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2011-11-27 08:21:00 +00:00
|
|
|
rtl::OString aNewFile;
|
2011-12-06 23:32:42 +00:00
|
|
|
if (!aResult.isEmpty())
|
2012-01-16 15:46:57 -02:00
|
|
|
if ( !(aNewFile = Exists( aResult )).isEmpty() )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2010-07-29 10:56:19 +08:00
|
|
|
sal_Bool bFound = sal_False;
|
2011-11-27 08:21:00 +00:00
|
|
|
size_t nCount = m_aFileList.size();
|
|
|
|
for ( size_t i = 0; i < nCount; ++i )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
const rtl::OString &rStr = m_aFileList[i];
|
|
|
|
if ( rStr == aNewFile )
|
2010-07-29 10:56:19 +08:00
|
|
|
bFound = sal_True;
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-11-13 23:17:06 +00:00
|
|
|
fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
if ( !bFound )
|
|
|
|
{
|
2011-11-27 08:21:00 +00:00
|
|
|
m_aFileList.push_back(aNewFile);
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-11-27 08:21:00 +00:00
|
|
|
fprintf( stderr, " CppDep %s\\\n", aNewFile.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
Search(aNewFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aFile.Close();
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2011-12-06 23:32:42 +00:00
|
|
|
rtl::OString CppDep::Exists(const rtl::OString &rFileName)
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2003-03-27 16:05:12 +00:00
|
|
|
char pFullName[1023];
|
|
|
|
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-12-06 23:32:42 +00:00
|
|
|
fprintf( stderr, "Searching %s \n", rFileName.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2003-03-27 16:05:12 +00:00
|
|
|
|
2011-11-27 08:21:00 +00:00
|
|
|
size_t nCount = m_aSearchPath.size();
|
|
|
|
for ( size_t n = 0; n < nCount; ++n )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
|
|
|
struct stat aBuf;
|
2011-11-27 08:21:00 +00:00
|
|
|
const rtl::OString &rPathName = m_aSearchPath[n];
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2011-11-27 08:21:00 +00:00
|
|
|
strcpy( pFullName, rPathName.getStr());
|
2000-09-18 16:07:07 +00:00
|
|
|
strcat( pFullName, DIR_SEP );
|
2011-12-06 23:32:42 +00:00
|
|
|
strcat( pFullName, rFileName.getStr());
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2003-03-27 16:05:12 +00:00
|
|
|
fprintf( stderr, "looking for %s\t ", pFullName );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
if ( stat( pFullName, &aBuf ) == 0 )
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2012-02-05 09:27:48 +01:00
|
|
|
fprintf( stderr, "Got Dependency %s \\\n", pFullName );
|
2000-09-18 16:07:07 +00:00
|
|
|
#endif
|
2011-11-27 08:21:00 +00:00
|
|
|
return rtl::OString(pFullName);
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 08:21:00 +00:00
|
|
|
return rtl::OString();
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 08:54:49 +01:00
|
|
|
rtl::OString CppDep::IsIncludeStatement(rtl::OString aLine)
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-08-16 08:54:49 +01:00
|
|
|
sal_Int32 nIndex;
|
|
|
|
|
|
|
|
nIndex = aLine.indexOf("/*");
|
|
|
|
if ( nIndex != -1 )
|
2008-10-15 08:43:33 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "found starting C comment : %s\n", aLine.getStr() );
|
2008-10-15 08:43:33 +00:00
|
|
|
#endif
|
2011-08-16 08:54:49 +01:00
|
|
|
aLine = aLine.copy(0, nIndex);
|
2008-10-15 08:43:33 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "cleaned string : %s\n", aLine.getStr() );
|
2008-10-15 08:43:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-08-16 08:54:49 +01:00
|
|
|
|
|
|
|
nIndex = aLine.indexOf("//");
|
|
|
|
if ( nIndex != -1 )
|
2008-10-15 08:43:33 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "found C++ comment : %s\n", aLine.getStr() );
|
2008-10-15 08:43:33 +00:00
|
|
|
#endif
|
2011-08-16 08:54:49 +01:00
|
|
|
aLine = aLine.copy(0, nIndex);
|
2008-10-15 08:43:33 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "cleaned string : %s\n", aLine.getStr() );
|
2008-10-15 08:43:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
2000-09-18 16:07:07 +00:00
|
|
|
// WhiteSpacesfressen
|
2012-02-15 15:26:43 +01:00
|
|
|
aLine = aLine.replaceAll(rtl::OString(' '), rtl::OString()).
|
|
|
|
replaceAll(rtl::OString('\t'), rtl::OString());
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "now : %s\n", aLine.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
// ist der erste Teil ein #include ?
|
2011-08-16 08:54:49 +01:00
|
|
|
rtl::OString aRetStr;
|
|
|
|
if (
|
|
|
|
aLine.getLength() >= 10 &&
|
|
|
|
aLine.match(rtl::OString(RTL_CONSTASCII_STRINGPARAM("#include")))
|
|
|
|
)
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2011-08-16 08:54:49 +01:00
|
|
|
//#include<foo> or #include"foo"
|
|
|
|
aLine = aLine.copy(9, aLine.getLength()-10);
|
2001-06-13 12:43:51 +00:00
|
|
|
#ifdef DEBUG_VERBOSE
|
2011-08-16 08:54:49 +01:00
|
|
|
fprintf( stderr, "Gotcha : %s\n", aLine.getStr() );
|
2001-06-13 12:43:51 +00:00
|
|
|
#endif
|
2000-09-18 16:07:07 +00:00
|
|
|
aRetStr = aLine;
|
|
|
|
}
|
|
|
|
return aRetStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
2011-08-17 15:31:50 +01:00
|
|
|
CppDep *pDep = new CppDep();
|
2000-09-18 16:07:07 +00:00
|
|
|
pDep->AddSearchPath(".");
|
|
|
|
pDep->AddSearchPath("/usr/include");
|
|
|
|
pDep->AddSearchPath("/usr/local/include");
|
|
|
|
pDep->AddSearchPath("/usr/include/sys");
|
|
|
|
pDep->AddSearchPath("/usr/include/X11");
|
|
|
|
pDep->Execute();
|
|
|
|
delete pDep;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-10-14 08:27:31 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|