update dev300-m58
[ooovba.git] / soltools / giparser / gi_list.cxx
blob8f95ef41bebba1128dfe45a2ad53e7490f7b5421
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gi_list.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_soltools.hxx"
35 #include <gi_list.hxx>
38 #include <gen_info.hxx>
42 const char C_cKeySeparator = '/';
45 List_GenericInfo::List_GenericInfo()
49 List_GenericInfo::List_GenericInfo( const List_GenericInfo & i_rList )
50 : aChildren(i_rList.aChildren)
54 List_GenericInfo::~List_GenericInfo()
58 List_GenericInfo &
59 List_GenericInfo::operator=( const List_GenericInfo & i_rList )
61 aChildren = i_rList.aChildren;
62 return *this;
65 const GenericInfo *
66 List_GenericInfo::operator[]( KeyPath i_sKeyPath ) const
68 return const_cast< List_GenericInfo& >(*this)[i_sKeyPath];
71 GenericInfo *
72 List_GenericInfo::operator[]( KeyPath i_sKeyPath )
74 bool bExists = false;
75 const char * sNextPathSegment = 0;
76 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
78 if ( bExists )
80 if ( sNextPathSegment == 0 )
81 return (*it);
82 else
83 return (*it)->SubList()[sNextPathSegment];
85 else
87 return 0;
91 bool
92 List_GenericInfo::InsertInfo( GenericInfo * let_dpInfo,
93 bool i_bOverwrite )
95 if ( let_dpInfo == 0 )
96 return false;
98 bool bExists = false;
99 const char * sNextPathSegment = 0;
100 sub_iterator it = lower_bound(bExists, sNextPathSegment, let_dpInfo->Key() );
102 if ( ! bExists )
104 aChildren.insert( it, let_dpInfo );
106 else if ( i_bOverwrite )
108 delete (*it);
109 (*it) = let_dpInfo;
111 else
113 delete let_dpInfo;
114 return false;
117 return true;
120 bool
121 List_GenericInfo::InsertInfoByPath( GenericInfo * let_dpInfo,
122 KeyPath i_sKeyPath,
123 bool i_bCreatePath,
124 bool i_bOverwrite )
126 if ( let_dpInfo == 0 )
127 return false;
129 if ( i_sKeyPath == 0 ? true : *i_sKeyPath == 0 )
130 return InsertInfo(let_dpInfo, i_bOverwrite);
132 bool bExists = false;
133 const char * sNextPathSegment = 0;
134 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
136 if ( bExists )
138 return (*it)->SubList().InsertInfoByPath(
139 let_dpInfo,
140 sNextPathSegment,
141 i_bCreatePath,
142 i_bOverwrite );
144 else if ( i_bCreatePath )
146 Simstr aKey( i_sKeyPath,
148 sNextPathSegment -
149 ( *sNextPathSegment == 0 ? 0 : 1)
150 - i_sKeyPath );
152 GenericInfo * pNew = new GenericInfo(aKey);
153 InsertInfo(pNew,false);
155 return pNew->SubList().InsertInfoByPath(
156 let_dpInfo,
157 sNextPathSegment,
158 i_bCreatePath,
159 i_bOverwrite );
161 else
163 delete let_dpInfo;
164 return false;
168 GenericInfo *
169 List_GenericInfo::ReleaseInfo( KeyPath i_sKeyPath )
171 bool bExists = false;
172 const char * sNextPathSegment = 0;
173 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
175 if ( bExists )
177 if ( *sNextPathSegment == 0 )
178 return (*it);
179 else
180 return (*it)->SubList().ReleaseInfo(sNextPathSegment);
182 else
184 return 0;
188 void
189 List_GenericInfo::DeleteInfo( KeyPath i_sKeyPath )
191 bool bExists = false;
192 const char * sNextPathSegment = 0;
193 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
195 if ( bExists )
197 if ( *sNextPathSegment == 0 )
199 aChildren.remove(it);
201 else
203 (*it)->SubList().DeleteInfo(sNextPathSegment);
208 List_GenericInfo::sub_iterator
209 List_GenericInfo::lower_bound( bool & o_bExists,
210 const char * & o_sNextPathSegment,
211 KeyPath i_sKeyPath )
213 o_sNextPathSegment = strchr(i_sKeyPath, '/');
214 Simstr sKey( i_sKeyPath, (o_sNextPathSegment == 0 ? strlen(i_sKeyPath) : o_sNextPathSegment++ - i_sKeyPath) );
215 GenericInfo aSearch(sKey);
217 unsigned low = 0;
218 unsigned high = aChildren.size();
220 for ( unsigned cur = high / 2; high > low; cur = (low + high) / 2 )
222 if ( *aChildren[cur] < aSearch )
224 low = cur+1;
226 else
228 high = cur;
230 } // end for
232 o_bExists = low < aChildren.size()
233 ? !(aSearch < *aChildren[low] )
234 : false;
235 return &aChildren[low];