xref: /trunk/main/xml2cmp/source/finder/dependy.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*ab595ff6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ab595ff6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ab595ff6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ab595ff6SAndrew Rist  * distributed with this work for additional information
6*ab595ff6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ab595ff6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ab595ff6SAndrew Rist  * "License"); you may not use this file except in compliance
9*ab595ff6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ab595ff6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ab595ff6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ab595ff6SAndrew Rist  * software distributed under the License is distributed on an
15*ab595ff6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ab595ff6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ab595ff6SAndrew Rist  * specific language governing permissions and limitations
18*ab595ff6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ab595ff6SAndrew Rist  *************************************************************/
21*ab595ff6SAndrew Rist 
22*ab595ff6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "dependy.hxx"
26cdf0e10cSrcweir #include <iostream>
27cdf0e10cSrcweir #include "../support/syshelp.hxx"
28cdf0e10cSrcweir #include "../support/list.hxx"
29cdf0e10cSrcweir #include "../xcd/xmltree.hxx"
30cdf0e10cSrcweir #include "../xcd/parse.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir Simstr              ShortName(const Simstr & i_rService);
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
Service(const char * i_sName)38cdf0e10cSrcweir Service::Service( const char * i_sName )
39cdf0e10cSrcweir     :   sName(i_sName)
40cdf0e10cSrcweir         // aImplementations
41cdf0e10cSrcweir {
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir ServiceInfo &
AddImplementation(const char * i_sLibrary)45cdf0e10cSrcweir Service::AddImplementation( const char * i_sLibrary )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     ServiceInfo * ret = new ServiceInfo(i_sLibrary);
48cdf0e10cSrcweir     aImplementations.push_back(ret);
49cdf0e10cSrcweir     return *ret;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
ServiceInfo(const char * i_sLibrary)52cdf0e10cSrcweir ServiceInfo::ServiceInfo( const char * i_sLibrary )
53cdf0e10cSrcweir     :   sImplementingLibrary(i_sLibrary)
54cdf0e10cSrcweir         // aNeededServices
55cdf0e10cSrcweir {
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir void
AddDependency(const char * i_sNeededService)59cdf0e10cSrcweir ServiceInfo::AddDependency( const char * i_sNeededService )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     aNeededServices.push_back(i_sNeededService);
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
DependencyFinder()64cdf0e10cSrcweir DependencyFinder::DependencyFinder()
65cdf0e10cSrcweir {
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
~DependencyFinder()68cdf0e10cSrcweir DependencyFinder::~DependencyFinder()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir void
GatherData(const char * i_sSearchDirectory)73cdf0e10cSrcweir DependencyFinder::GatherData( const char * i_sSearchDirectory )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     List<Simstr> aFiles;
76cdf0e10cSrcweir     GatherFileNames( aFiles, i_sSearchDirectory );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     for ( unsigned i = 0; i < aFiles.size(); ++i )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         ReadFile( aFiles[i].str() );
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir void
FindNeededServices(StringVector & o_rLibraries,StringVector & o_rServices,const Simstr & i_rService)85cdf0e10cSrcweir DependencyFinder::FindNeededServices( StringVector &        o_rLibraries,
86cdf0e10cSrcweir                                       StringVector &        o_rServices,
87cdf0e10cSrcweir                                       const Simstr &        i_rService )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     Map_Services::const_iterator itService = aServices.find(i_rService);
90cdf0e10cSrcweir     if ( itService == aServices.end() )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         std::cerr << "Error: Service \""
93cdf0e10cSrcweir                   << i_rService.str()
94cdf0e10cSrcweir                   << "\" not found."
95cdf0e10cSrcweir                   << std::endl;
96cdf0e10cSrcweir         return ;
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     aResult_Libraries.erase( aResult_Libraries.begin(), aResult_Libraries.end() );
100cdf0e10cSrcweir     aResult_Services.erase( aResult_Services.begin(), aResult_Services.end() );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir //  const ServiceInfo & rSInfo = (*itService).second->FirstImplementation();
103cdf0e10cSrcweir     Add2Result( *(*itService).second );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     for ( std::set< Simstr >::const_iterator il = aResult_Libraries.begin();
106cdf0e10cSrcweir           il != aResult_Libraries.end();
107cdf0e10cSrcweir           ++il )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         o_rLibraries.push_back(*il);
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     for ( std::set< Simstr >::const_iterator is = aResult_Services.begin();
113cdf0e10cSrcweir           is != aResult_Services.end();
114cdf0e10cSrcweir           ++is )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         o_rServices.push_back(*is);
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void
ReadFile(const char * i_sFilename)121cdf0e10cSrcweir DependencyFinder::ReadFile(  const char * i_sFilename )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     ModuleDescription   aModule;
124cdf0e10cSrcweir     X2CParser           aParser(aModule);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     if ( !aParser.Parse(i_sFilename) )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         std::cerr << "Error: File \""
129cdf0e10cSrcweir              << i_sFilename
130cdf0e10cSrcweir              << "\" could not be parsed."
131cdf0e10cSrcweir              << std::endl;
132cdf0e10cSrcweir         return;
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     // GetResults:
136cdf0e10cSrcweir     Simstr sModule = aModule.ModuleName();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     List < const MultipleTextElement* > aImplServices;
139cdf0e10cSrcweir     List < const MultipleTextElement* > aNeededServices;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     aModule.Get_SupportedServices(aImplServices);
142cdf0e10cSrcweir     aModule.Get_ServiceDependencies(aNeededServices);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     unsigned nImplServicesSize = aImplServices.size();
145cdf0e10cSrcweir     unsigned nNeededServicesSize = aNeededServices.size();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     for ( unsigned i = 0; i < nImplServicesSize; ++i )
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         const MultipleTextElement & rImpl = *aImplServices[i];
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         unsigned nImplDataSize = rImpl.Size();
152cdf0e10cSrcweir         for ( unsigned di = 0; di < nImplDataSize; ++di )
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             Simstr sService = ShortName(rImpl.Data(di));
155cdf0e10cSrcweir             Service * pService = aServices[sService];
156cdf0e10cSrcweir             if (pService == 0)
157cdf0e10cSrcweir             {
158cdf0e10cSrcweir                 pService = new Service(rImpl.Data(di));
159cdf0e10cSrcweir                 aServices[sService] = pService;
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir             ServiceInfo & rSInfo = pService->AddImplementation(sModule);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             for ( unsigned n = 0; n < nNeededServicesSize; ++n )
164cdf0e10cSrcweir             {
165cdf0e10cSrcweir                 unsigned nNeededDataSize = aNeededServices[n]->Size();
166cdf0e10cSrcweir                 for ( unsigned dn = 0; dn < nNeededDataSize; ++dn )
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     if (! aNeededServices[n]->Data(dn).is_no_text())
169cdf0e10cSrcweir                         rSInfo.AddDependency( ShortName(aNeededServices[n]->Data(dn)) );
170cdf0e10cSrcweir                 }   // end for dn
171cdf0e10cSrcweir             }   // end for n
172cdf0e10cSrcweir         }   //  end for di
173cdf0e10cSrcweir     }   // end for i
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir void
Add2Result(const Service & i_rService)177cdf0e10cSrcweir DependencyFinder::Add2Result( const Service & i_rService )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     const ServiceInfo & rSInfo = i_rService.FirstImplementation();
180cdf0e10cSrcweir     aResult_Libraries.insert(rSInfo.Library());
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     const ServiceInfo::List_NeededServices & rNeededs
183cdf0e10cSrcweir             = rSInfo.NeededServices();
184cdf0e10cSrcweir     for ( StringVector::const_iterator it = rNeededs.begin();
185cdf0e10cSrcweir           it != rNeededs.end();
186cdf0e10cSrcweir           ++it )
187cdf0e10cSrcweir     {
188cdf0e10cSrcweir         std::pair< std::set< Simstr >::iterator, bool > aInsertResult
189cdf0e10cSrcweir                 = aResult_Services.insert(*it);
190cdf0e10cSrcweir         if (aInsertResult.second)
191cdf0e10cSrcweir         {   // Needed service not yet known
192cdf0e10cSrcweir             Map_Services::const_iterator itFound = aServices.find(*it);
193cdf0e10cSrcweir             if ( itFound == aServices.end() )
194cdf0e10cSrcweir             {
195cdf0e10cSrcweir                 std::cerr << "Needed service \""
196cdf0e10cSrcweir                           << (*it).str()
197cdf0e10cSrcweir                           << "\" not found,"
198cdf0e10cSrcweir                           << std::endl;
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir             else
201cdf0e10cSrcweir             {
202cdf0e10cSrcweir                 Add2Result( *(*itFound).second );
203cdf0e10cSrcweir             }
204cdf0e10cSrcweir         }   // endif (! aInsertResult.second)
205cdf0e10cSrcweir     }   // end for (it)
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 
210cdf0e10cSrcweir Simstr
ShortName(const Simstr & i_rService)211cdf0e10cSrcweir ShortName(const Simstr & i_rService)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     const char * pStart = i_rService.str();
214cdf0e10cSrcweir     const char * pEnd = strchr(pStart,' ');
215cdf0e10cSrcweir     if (pEnd != 0)
216cdf0e10cSrcweir         return Simstr(pStart, 0, int(pEnd-pStart));
217cdf0e10cSrcweir     else
218cdf0e10cSrcweir         return i_rService;
219cdf0e10cSrcweir }
220