Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / soltools / giparser / gi_list.cxx
blobbb9b65518321c3ed979c4775d1feb6e110eb0906
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include <gi_list.hxx>
34 #include <gen_info.hxx>
38 const char C_cKeySeparator = '/';
41 List_GenericInfo::List_GenericInfo()
45 List_GenericInfo::List_GenericInfo( const List_GenericInfo & i_rList )
46 : aChildren(i_rList.aChildren)
50 List_GenericInfo::~List_GenericInfo()
54 List_GenericInfo &
55 List_GenericInfo::operator=( const List_GenericInfo & i_rList )
57 aChildren = i_rList.aChildren;
58 return *this;
61 const GenericInfo *
62 List_GenericInfo::operator[]( KeyPath i_sKeyPath ) const
64 return const_cast< List_GenericInfo& >(*this)[i_sKeyPath];
67 GenericInfo *
68 List_GenericInfo::operator[]( KeyPath i_sKeyPath )
70 bool bExists = false;
71 const char * sNextPathSegment = 0;
72 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
74 if ( bExists )
76 if ( sNextPathSegment == 0 )
77 return (*it);
78 else
79 return (*it)->SubList()[sNextPathSegment];
81 else
83 return 0;
87 bool
88 List_GenericInfo::InsertInfo( GenericInfo * let_dpInfo,
89 bool i_bOverwrite )
91 if ( let_dpInfo == 0 )
92 return false;
94 bool bExists = false;
95 const char * sNextPathSegment = 0;
96 sub_iterator it = lower_bound(bExists, sNextPathSegment, let_dpInfo->Key() );
98 if ( ! bExists )
100 aChildren.insert( it, let_dpInfo );
102 else if ( i_bOverwrite )
104 delete (*it);
105 (*it) = let_dpInfo;
107 else
109 delete let_dpInfo;
110 return false;
113 return true;
116 bool
117 List_GenericInfo::InsertInfoByPath( GenericInfo * let_dpInfo,
118 KeyPath i_sKeyPath,
119 bool i_bCreatePath,
120 bool i_bOverwrite )
122 if ( let_dpInfo == 0 )
123 return false;
125 if ( i_sKeyPath == 0 ? true : *i_sKeyPath == 0 )
126 return InsertInfo(let_dpInfo, i_bOverwrite);
128 bool bExists = false;
129 const char * sNextPathSegment = 0;
130 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath);
132 if ( bExists )
134 return (*it)->SubList().InsertInfoByPath(
135 let_dpInfo,
136 sNextPathSegment,
137 i_bCreatePath,
138 i_bOverwrite );
140 else if ( i_bCreatePath )
142 Simstr aKey( i_sKeyPath,
144 (int)(sNextPathSegment -
145 ( *sNextPathSegment == 0 ? 0 : 1)
146 - i_sKeyPath ));
148 GenericInfo * pNew = new GenericInfo(aKey);
149 InsertInfo(pNew,false);
151 return pNew->SubList().InsertInfoByPath(
152 let_dpInfo,
153 sNextPathSegment,
154 i_bCreatePath,
155 i_bOverwrite );
157 else
159 delete let_dpInfo;
160 return false;
164 GenericInfo *
165 List_GenericInfo::ReleaseInfo( KeyPath i_sKeyPath )
167 bool bExists = false;
168 const char * sNextPathSegment = 0;
169 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
171 if ( bExists )
173 if ( *sNextPathSegment == 0 )
174 return (*it);
175 else
176 return (*it)->SubList().ReleaseInfo(sNextPathSegment);
178 else
180 return 0;
184 void
185 List_GenericInfo::DeleteInfo( KeyPath i_sKeyPath )
187 bool bExists = false;
188 const char * sNextPathSegment = 0;
189 sub_iterator it = lower_bound(bExists, sNextPathSegment, i_sKeyPath );
191 if ( bExists )
193 if ( *sNextPathSegment == 0 )
195 aChildren.remove(it);
197 else
199 (*it)->SubList().DeleteInfo(sNextPathSegment);
204 List_GenericInfo::sub_iterator
205 List_GenericInfo::lower_bound( bool & o_bExists,
206 const char * & o_sNextPathSegment,
207 KeyPath i_sKeyPath )
209 o_sNextPathSegment = strchr(i_sKeyPath, '/');
210 Simstr sKey( i_sKeyPath, (int)(o_sNextPathSegment == 0 ? strlen(i_sKeyPath) : o_sNextPathSegment++ - i_sKeyPath) );
211 GenericInfo aSearch(sKey);
213 unsigned low = 0;
214 unsigned high = aChildren.size();
216 for ( unsigned cur = high / 2; high > low; cur = (low + high) / 2 )
218 if ( *aChildren[cur] < aSearch )
220 low = cur+1;
222 else
224 high = cur;
226 } // end for
228 o_bExists = low < aChildren.size()
229 ? !(aSearch < *aChildren[low] )
230 : false;
231 return &aChildren[low];
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */