Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <stdio.h>
22 : : #include <string.h>
23 : : #include <unistd.h>
24 : :
25 : : #include <sys/stat.h>
26 : : #include <tools/stream.hxx>
27 : : #include <rtl/strbuf.hxx>
28 : : #include "cppdep.hxx"
29 : :
30 : 0 : CppDep::CppDep()
31 : : {
32 : 0 : }
33 : :
34 : 0 : CppDep::~CppDep()
35 : : {
36 : 0 : }
37 : :
38 : 0 : void CppDep::Execute()
39 : : {
40 : 0 : size_t nCount = m_aSources.size();
41 : 0 : for ( size_t n = 0; n < nCount; ++n )
42 : : {
43 : 0 : const rtl::OString &rStr = m_aSources[n];
44 : 0 : Search(rStr);
45 : : }
46 : 0 : }
47 : :
48 : 0 : sal_Bool CppDep::AddSearchPath( const char* pPath )
49 : : {
50 : 0 : m_aSearchPath.push_back( rtl::OString(pPath) );
51 : 0 : return sal_False;
52 : : }
53 : :
54 : 0 : sal_Bool CppDep::AddSource( const char* pSource )
55 : : {
56 : 0 : m_aSources.push_back( rtl::OString(pSource) );
57 : 0 : return sal_False;
58 : : }
59 : :
60 : 0 : sal_Bool CppDep::Search(const rtl::OString &rFileName)
61 : : {
62 : : #ifdef DEBUG_VERBOSE
63 : : fprintf( stderr, "SEARCH : %s\n", rFileName.getStr());
64 : : #endif
65 : 0 : sal_Bool bRet = sal_False;
66 : :
67 : 0 : SvFileStream aFile;
68 : 0 : rtl::OString aReadLine;
69 : :
70 : 0 : rtl::OUString suFileName(rtl::OStringToOUString(rFileName, osl_getThreadTextEncoding()));
71 : :
72 : 0 : aFile.Open( suFileName, STREAM_READ );
73 : 0 : while ( aFile.ReadLine( aReadLine ))
74 : : {
75 : : sal_Int32 nPos = aReadLine.indexOfL(
76 : 0 : RTL_CONSTASCII_STRINGPARAM("include"));
77 : 0 : if ( nPos != -1 )
78 : : {
79 : : #ifdef DEBUG_VERBOSE
80 : : fprintf( stderr, "found : %d %s\n", nPos, aReadLine.getStr() );
81 : : #endif
82 : 0 : rtl::OString aResult = IsIncludeStatement( aReadLine );
83 : : #ifdef DEBUG_VERBOSE
84 : : fprintf( stderr, "Result : %s\n", aResult.getStr() );
85 : : #endif
86 : :
87 : 0 : rtl::OString aNewFile;
88 : 0 : if (!aResult.isEmpty())
89 : 0 : if ( !(aNewFile = Exists( aResult )).isEmpty() )
90 : : {
91 : 0 : sal_Bool bFound = sal_False;
92 : 0 : size_t nCount = m_aFileList.size();
93 : 0 : for ( size_t i = 0; i < nCount; ++i )
94 : : {
95 : 0 : const rtl::OString &rStr = m_aFileList[i];
96 : 0 : if ( rStr == aNewFile )
97 : 0 : bFound = sal_True;
98 : : }
99 : : #ifdef DEBUG_VERBOSE
100 : : fprintf( stderr, "not in list : %d %s\n", nPos, aReadLine.getStr() );
101 : : #endif
102 : 0 : if ( !bFound )
103 : : {
104 : 0 : m_aFileList.push_back(aNewFile);
105 : : #ifdef DEBUG_VERBOSE
106 : : fprintf( stderr, " CppDep %s\\\n", aNewFile.getStr() );
107 : : #endif
108 : 0 : Search(aNewFile);
109 : : }
110 : 0 : }
111 : : }
112 : : }
113 : 0 : aFile.Close();
114 : :
115 : 0 : return bRet;
116 : : }
117 : :
118 : 0 : rtl::OString CppDep::Exists(const rtl::OString &rFileName)
119 : : {
120 : : char pFullName[1023];
121 : :
122 : : #ifdef DEBUG_VERBOSE
123 : : fprintf( stderr, "Searching %s \n", rFileName.getStr() );
124 : : #endif
125 : :
126 : 0 : size_t nCount = m_aSearchPath.size();
127 : 0 : for ( size_t n = 0; n < nCount; ++n )
128 : : {
129 : : struct stat aBuf;
130 : 0 : const rtl::OString &rPathName = m_aSearchPath[n];
131 : :
132 : 0 : strcpy( pFullName, rPathName.getStr());
133 : 0 : strcat( pFullName, DIR_SEP );
134 : 0 : strcat( pFullName, rFileName.getStr());
135 : :
136 : : #ifdef DEBUG_VERBOSE
137 : : fprintf( stderr, "looking for %s\t ", pFullName );
138 : : #endif
139 : 0 : if ( stat( pFullName, &aBuf ) == 0 )
140 : : {
141 : : #ifdef DEBUG_VERBOSE
142 : : fprintf( stderr, "Got Dependency %s \\\n", pFullName );
143 : : #endif
144 : 0 : return rtl::OString(pFullName);
145 : : }
146 : : }
147 : 0 : return rtl::OString();
148 : : }
149 : :
150 : 0 : rtl::OString CppDep::IsIncludeStatement(rtl::OString aLine)
151 : : {
152 : : sal_Int32 nIndex;
153 : :
154 : 0 : nIndex = aLine.indexOf("/*");
155 : 0 : if ( nIndex != -1 )
156 : : {
157 : : #ifdef DEBUG_VERBOSE
158 : : fprintf( stderr, "found starting C comment : %s\n", aLine.getStr() );
159 : : #endif
160 : 0 : aLine = aLine.copy(0, nIndex);
161 : : #ifdef DEBUG_VERBOSE
162 : : fprintf( stderr, "cleaned string : %s\n", aLine.getStr() );
163 : : #endif
164 : : }
165 : :
166 : 0 : nIndex = aLine.indexOf("//");
167 : 0 : if ( nIndex != -1 )
168 : : {
169 : : #ifdef DEBUG_VERBOSE
170 : : fprintf( stderr, "found C++ comment : %s\n", aLine.getStr() );
171 : : #endif
172 : 0 : aLine = aLine.copy(0, nIndex);
173 : : #ifdef DEBUG_VERBOSE
174 : : fprintf( stderr, "cleaned string : %s\n", aLine.getStr() );
175 : : #endif
176 : : }
177 : : // WhiteSpacesfressen
178 : : aLine = aLine.replaceAll(rtl::OString(' '), rtl::OString()).
179 : 0 : replaceAll(rtl::OString('\t'), rtl::OString());
180 : : #ifdef DEBUG_VERBOSE
181 : : fprintf( stderr, "now : %s\n", aLine.getStr() );
182 : : #endif
183 : : // ist der erste Teil ein #include ?
184 : 0 : rtl::OString aRetStr;
185 : 0 : if (
186 : 0 : aLine.getLength() >= 10 &&
187 : 0 : aLine.match(rtl::OString(RTL_CONSTASCII_STRINGPARAM("#include")))
188 : : )
189 : : {
190 : : //#include<foo> or #include"foo"
191 : 0 : aLine = aLine.copy(9, aLine.getLength()-10);
192 : : #ifdef DEBUG_VERBOSE
193 : : fprintf( stderr, "Gotcha : %s\n", aLine.getStr() );
194 : : #endif
195 : 0 : aRetStr = aLine;
196 : : }
197 : 0 : return aRetStr;
198 : : }
199 : :
200 : : #ifdef TEST
201 : :
202 : : int main( int argc, char **argv )
203 : : {
204 : : CppDep *pDep = new CppDep();
205 : : pDep->AddSearchPath(".");
206 : : pDep->AddSearchPath("/usr/include");
207 : : pDep->AddSearchPath("/usr/local/include");
208 : : pDep->AddSearchPath("/usr/include/sys");
209 : : pDep->AddSearchPath("/usr/include/X11");
210 : : pDep->Execute();
211 : : delete pDep;
212 : : return 0;
213 : : }
214 : :
215 : : #endif
216 : :
217 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|