xref: /aoo41x/main/soltools/giparser/gi_list.cxx (revision 7a4715d3)
1*7a4715d3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*7a4715d3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*7a4715d3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*7a4715d3SAndrew Rist  * distributed with this work for additional information
6*7a4715d3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*7a4715d3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*7a4715d3SAndrew Rist  * "License"); you may not use this file except in compliance
9*7a4715d3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*7a4715d3SAndrew Rist  *
11*7a4715d3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*7a4715d3SAndrew Rist  *
13*7a4715d3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*7a4715d3SAndrew Rist  * software distributed under the License is distributed on an
15*7a4715d3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7a4715d3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*7a4715d3SAndrew Rist  * specific language governing permissions and limitations
18*7a4715d3SAndrew Rist  * under the License.
19*7a4715d3SAndrew Rist  *
20*7a4715d3SAndrew Rist  *************************************************************/
21*7a4715d3SAndrew Rist 
22*7a4715d3SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_soltools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <gi_list.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <gen_info.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir const char C_cKeySeparator = '/';
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
List_GenericInfo()38cdf0e10cSrcweir List_GenericInfo::List_GenericInfo()
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
List_GenericInfo(const List_GenericInfo & i_rList)42cdf0e10cSrcweir List_GenericInfo::List_GenericInfo( const List_GenericInfo & i_rList )
43cdf0e10cSrcweir     :   aChildren(i_rList.aChildren)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
~List_GenericInfo()47cdf0e10cSrcweir List_GenericInfo::~List_GenericInfo()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir List_GenericInfo &
operator =(const List_GenericInfo & i_rList)52cdf0e10cSrcweir List_GenericInfo::operator=( const List_GenericInfo & i_rList )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     aChildren = i_rList.aChildren;
55cdf0e10cSrcweir     return *this;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir const GenericInfo *
operator [](KeyPath i_sKeyPath) const59cdf0e10cSrcweir List_GenericInfo::operator[]( KeyPath i_sKeyPath ) const
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     return const_cast< List_GenericInfo& >(*this)[i_sKeyPath];
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir GenericInfo *
operator [](KeyPath i_sKeyPath)65cdf0e10cSrcweir List_GenericInfo::operator[]( KeyPath i_sKeyPath )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     bool bExists = false;
68cdf0e10cSrcweir     const char * sNextPathSegment = 0;
69cdf0e10cSrcweir     sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     if ( bExists )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         if ( sNextPathSegment == 0 )
74cdf0e10cSrcweir             return (*it);
75cdf0e10cSrcweir         else
76cdf0e10cSrcweir             return (*it)->SubList()[sNextPathSegment];
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir     else
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         return 0;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir bool
InsertInfo(GenericInfo * let_dpInfo,bool i_bOverwrite)85cdf0e10cSrcweir List_GenericInfo::InsertInfo( GenericInfo *       let_dpInfo,
86cdf0e10cSrcweir                               bool                i_bOverwrite )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     if ( let_dpInfo == 0 )
89cdf0e10cSrcweir         return false;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     bool bExists = false;
92cdf0e10cSrcweir     const char * sNextPathSegment = 0;
93cdf0e10cSrcweir     sub_iterator it = lower_bound(bExists, sNextPathSegment, let_dpInfo->Key() );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     if ( ! bExists )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         aChildren.insert( it, let_dpInfo );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir     else if ( i_bOverwrite )
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         delete (*it);
102cdf0e10cSrcweir         (*it) = let_dpInfo;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir     else
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir      	delete let_dpInfo;
107cdf0e10cSrcweir         return false;
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     return true;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir bool
InsertInfoByPath(GenericInfo * let_dpInfo,KeyPath i_sKeyPath,bool i_bCreatePath,bool i_bOverwrite)114cdf0e10cSrcweir List_GenericInfo::InsertInfoByPath( GenericInfo *       let_dpInfo,
115cdf0e10cSrcweir                                     KeyPath             i_sKeyPath,
116cdf0e10cSrcweir                                     bool                i_bCreatePath,
117cdf0e10cSrcweir                                     bool                i_bOverwrite )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     if ( let_dpInfo == 0 )
120cdf0e10cSrcweir         return false;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     if ( i_sKeyPath == 0 ? true : *i_sKeyPath == 0 )
123cdf0e10cSrcweir      	return InsertInfo(let_dpInfo, i_bOverwrite);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     bool bExists = false;
126cdf0e10cSrcweir     const char * sNextPathSegment = 0;
127cdf0e10cSrcweir     sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     if ( bExists )
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         return (*it)->SubList().InsertInfoByPath(
132cdf0e10cSrcweir                                     let_dpInfo,
133cdf0e10cSrcweir                                     sNextPathSegment,
134cdf0e10cSrcweir                                     i_bCreatePath,
135cdf0e10cSrcweir                                     i_bOverwrite );
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir     else if ( i_bCreatePath )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         Simstr aKey( i_sKeyPath,
140cdf0e10cSrcweir                      0,
141cdf0e10cSrcweir                      sNextPathSegment -
142cdf0e10cSrcweir                         ( *sNextPathSegment == 0 ? 0 : 1)
143cdf0e10cSrcweir                         - i_sKeyPath );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         GenericInfo * pNew = new GenericInfo(aKey);
146cdf0e10cSrcweir         InsertInfo(pNew,false);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         return pNew->SubList().InsertInfoByPath(
149cdf0e10cSrcweir                                     let_dpInfo,
150cdf0e10cSrcweir                                     sNextPathSegment,
151cdf0e10cSrcweir                                     i_bCreatePath,
152cdf0e10cSrcweir                                     i_bOverwrite );
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir     else
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir      	delete let_dpInfo;
157cdf0e10cSrcweir         return false;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir GenericInfo *
ReleaseInfo(KeyPath i_sKeyPath)162cdf0e10cSrcweir List_GenericInfo::ReleaseInfo( KeyPath i_sKeyPath )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     bool bExists = false;
165cdf0e10cSrcweir     const char * sNextPathSegment = 0;
166cdf0e10cSrcweir     sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     if ( bExists )
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         if ( *sNextPathSegment == 0 )
171cdf0e10cSrcweir             return (*it);
172cdf0e10cSrcweir         else
173cdf0e10cSrcweir             return (*it)->SubList().ReleaseInfo(sNextPathSegment);
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     else
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir      	return 0;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir void
DeleteInfo(KeyPath i_sKeyPath)182cdf0e10cSrcweir List_GenericInfo::DeleteInfo( KeyPath i_sKeyPath )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     bool bExists = false;
185cdf0e10cSrcweir     const char * sNextPathSegment = 0;
186cdf0e10cSrcweir     sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     if ( bExists )
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         if ( *sNextPathSegment == 0 )
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             aChildren.remove(it);
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir         else
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir             (*it)->SubList().DeleteInfo(sNextPathSegment);
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir List_GenericInfo::sub_iterator
lower_bound(bool & o_bExists,const char * & o_sNextPathSegment,KeyPath i_sKeyPath)202cdf0e10cSrcweir List_GenericInfo::lower_bound( bool &              o_bExists,
203cdf0e10cSrcweir                                const char * &      o_sNextPathSegment,
204cdf0e10cSrcweir                                KeyPath             i_sKeyPath )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     o_sNextPathSegment = strchr(i_sKeyPath, '/');
207cdf0e10cSrcweir     Simstr sKey( i_sKeyPath, (o_sNextPathSegment == 0 ? strlen(i_sKeyPath) : o_sNextPathSegment++ - i_sKeyPath) );
208cdf0e10cSrcweir     GenericInfo aSearch(sKey);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     unsigned low = 0;
211cdf0e10cSrcweir     unsigned high = aChildren.size();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     for ( unsigned cur = high / 2; high > low; cur = (low + high) / 2 )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         if ( *aChildren[cur] < aSearch )
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             low = cur+1;
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir         else
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             high = cur;
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir     }   // end for
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     o_bExists = low < aChildren.size()
226cdf0e10cSrcweir                     ? !(aSearch < *aChildren[low] )
227cdf0e10cSrcweir                     : false;
228cdf0e10cSrcweir     return &aChildren[low];
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231