xref: /aoo41x/main/soldep/bootstrp/sstring.cxx (revision d9e04f7d)
1*d9e04f7dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d9e04f7dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d9e04f7dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d9e04f7dSAndrew Rist  * distributed with this work for additional information
6*d9e04f7dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d9e04f7dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d9e04f7dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d9e04f7dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d9e04f7dSAndrew Rist  *
11*d9e04f7dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d9e04f7dSAndrew Rist  *
13*d9e04f7dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d9e04f7dSAndrew Rist  * software distributed under the License is distributed on an
15*d9e04f7dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d9e04f7dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d9e04f7dSAndrew Rist  * specific language governing permissions and limitations
18*d9e04f7dSAndrew Rist  * under the License.
19*d9e04f7dSAndrew Rist  *
20*d9e04f7dSAndrew Rist  *************************************************************/
21*d9e04f7dSAndrew Rist 
22*d9e04f7dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _TOOLS_STRINGLIST
25cdf0e10cSrcweir #  define _TOOLS_STRINGLIST
26cdf0e10cSrcweir #endif
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #define ENABLE_BYTESTRING_STREAM_OPERATORS
29cdf0e10cSrcweir #include <tools/stream.hxx>
30cdf0e10cSrcweir #include "soldep/sstring.hxx"
31cdf0e10cSrcweir 
SByteStringList()32cdf0e10cSrcweir SByteStringList::SByteStringList()
33cdf0e10cSrcweir {
34cdf0e10cSrcweir }
35cdf0e10cSrcweir 
~SByteStringList()36cdf0e10cSrcweir SByteStringList::~SByteStringList()
37cdf0e10cSrcweir {
38cdf0e10cSrcweir }
39cdf0e10cSrcweir 
IsString(ByteString * pStr)40cdf0e10cSrcweir sal_uIntPtr SByteStringList::IsString( ByteString* pStr )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	sal_uIntPtr nRet = NOT_THERE;
43cdf0e10cSrcweir 	if ( (nRet = GetPrevString( pStr )) != 0 )
44cdf0e10cSrcweir 	{
45cdf0e10cSrcweir 		ByteString* pString = GetObject( nRet );
46cdf0e10cSrcweir 		if ( *pString == *pStr )
47cdf0e10cSrcweir 			return nRet;
48cdf0e10cSrcweir 		else
49cdf0e10cSrcweir 			return NOT_THERE;
50cdf0e10cSrcweir 	}
51cdf0e10cSrcweir 	else
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		ByteString* pString = GetObject( 0 );
54cdf0e10cSrcweir 		if ( pString && (*pString == *pStr) )
55cdf0e10cSrcweir 			return 0;
56cdf0e10cSrcweir 		else
57cdf0e10cSrcweir 			return NOT_THERE;
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
GetPrevString(ByteString * pStr)61cdf0e10cSrcweir sal_uIntPtr SByteStringList::GetPrevString( ByteString* pStr )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	sal_uIntPtr nRet = 0;
64cdf0e10cSrcweir 	sal_Bool bFound = sal_False;
65cdf0e10cSrcweir 	sal_uIntPtr nCountMember = Count();
66cdf0e10cSrcweir 	sal_uIntPtr nUpper = nCountMember;
67cdf0e10cSrcweir 	sal_uIntPtr nLower = 0;
68cdf0e10cSrcweir 	sal_uIntPtr nCurrent = nUpper / 2;
69cdf0e10cSrcweir 	sal_uIntPtr nRem = 0;
70cdf0e10cSrcweir 	ByteString* pString;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	do
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 		if ( (nCurrent == nLower) || (nCurrent == nUpper) )
75cdf0e10cSrcweir 			return nLower;
76cdf0e10cSrcweir 		pString = GetObject( nCurrent );
77cdf0e10cSrcweir 		StringCompare nResult =  pStr->CompareTo( *pString );
78cdf0e10cSrcweir 		if ( nResult == COMPARE_LESS )
79cdf0e10cSrcweir 		{
80cdf0e10cSrcweir 			nUpper = nCurrent;
81cdf0e10cSrcweir 			nCurrent = (nCurrent + nLower) /2;
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 		else if ( nResult == COMPARE_GREATER )
84cdf0e10cSrcweir 		{
85cdf0e10cSrcweir 			nLower = nCurrent;
86cdf0e10cSrcweir 			nCurrent = (nUpper + nCurrent) /2;
87cdf0e10cSrcweir 		}
88cdf0e10cSrcweir 		else if ( nResult == COMPARE_EQUAL )
89cdf0e10cSrcweir 			return nCurrent;
90cdf0e10cSrcweir 		if ( nRem == nCurrent )
91cdf0e10cSrcweir 			return nCurrent;
92cdf0e10cSrcweir 		nRem = nCurrent;
93cdf0e10cSrcweir 	}
94cdf0e10cSrcweir 	while ( !bFound );
95cdf0e10cSrcweir 	return nRet;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir /**************************************************************************
99cdf0e10cSrcweir *
100cdf0e10cSrcweir *	Sortiert einen ByteString in die Liste ein und gibt die Position,
101cdf0e10cSrcweir *	an der einsortiert wurde, zurueck
102cdf0e10cSrcweir *
103cdf0e10cSrcweir **************************************************************************/
104cdf0e10cSrcweir 
PutString(ByteString * pStr)105cdf0e10cSrcweir sal_uIntPtr SByteStringList::PutString( ByteString* pStr )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	sal_uIntPtr nPos = GetPrevString ( pStr );
108cdf0e10cSrcweir 	if ( Count() )
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 		{
111cdf0e10cSrcweir 			ByteString* pString = GetObject( 0 );
112cdf0e10cSrcweir 			if ( pString->CompareTo( *pStr ) == COMPARE_GREATER )
113cdf0e10cSrcweir 			{
114cdf0e10cSrcweir 				Insert( pStr, (sal_uIntPtr)0 );
115cdf0e10cSrcweir 				return (sal_uIntPtr)0;
116cdf0e10cSrcweir 			}
117cdf0e10cSrcweir 		}
118cdf0e10cSrcweir 		ByteString* pString = GetObject( nPos );
119cdf0e10cSrcweir 		if ( *pStr != *pString )
120cdf0e10cSrcweir 		{
121cdf0e10cSrcweir 			Insert( pStr, nPos+1 );
122cdf0e10cSrcweir 			return ( nPos +1 );
123cdf0e10cSrcweir 		}
124cdf0e10cSrcweir 	}
125cdf0e10cSrcweir 	else
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 		Insert( pStr );
128cdf0e10cSrcweir 		return (sal_uIntPtr)0;
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	return NOT_THERE;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
RemoveString(const ByteString & rName)134cdf0e10cSrcweir ByteString* SByteStringList::RemoveString( const ByteString& rName )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	sal_uIntPtr i;
137cdf0e10cSrcweir 	ByteString* pReturn;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	for( i = 0 ; i < Count(); i++ )
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		if ( rName == *GetObject( i ) )
142cdf0e10cSrcweir 		{
143cdf0e10cSrcweir 			pReturn = GetObject(i);
144cdf0e10cSrcweir 			Remove(i);
145cdf0e10cSrcweir 			return pReturn;
146cdf0e10cSrcweir 		}
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	return NULL;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
CleanUp()152cdf0e10cSrcweir void SByteStringList::CleanUp()
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	ByteString* pByteString = First();
155cdf0e10cSrcweir 	while (pByteString) {
156cdf0e10cSrcweir 		delete pByteString;
157cdf0e10cSrcweir 		pByteString = Next();
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 	Clear();
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
operator <<(SvStream & rStream)162cdf0e10cSrcweir SByteStringList& SByteStringList::operator<<  ( SvStream& rStream )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	sal_uInt32 nListCount;
165cdf0e10cSrcweir 	rStream >> nListCount;
166cdf0e10cSrcweir 	for ( sal_uInt16 i = 0; i < nListCount; i++ ) {
167cdf0e10cSrcweir         ByteString* pByteString = new ByteString();
168cdf0e10cSrcweir         rStream >> *pByteString;
169cdf0e10cSrcweir         Insert (pByteString, LIST_APPEND);
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 	return *this;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
operator >>(SvStream & rStream)174cdf0e10cSrcweir SByteStringList& SByteStringList::operator>>  ( SvStream& rStream )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	sal_uInt32 nListCount = Count();
177cdf0e10cSrcweir 	rStream << nListCount;
178cdf0e10cSrcweir 	ByteString* pByteString = First();
179cdf0e10cSrcweir 	while (pByteString) {
180cdf0e10cSrcweir 		rStream << *pByteString;
181cdf0e10cSrcweir 		pByteString = Next();
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 	return *this;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
SUniStringList()192cdf0e10cSrcweir SUniStringList::SUniStringList()
193cdf0e10cSrcweir {
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
~SUniStringList()196cdf0e10cSrcweir SUniStringList::~SUniStringList()
197cdf0e10cSrcweir {
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
IsString(UniString * pStr)200cdf0e10cSrcweir sal_uIntPtr SUniStringList::IsString( UniString* pStr )
201cdf0e10cSrcweir {
202cdf0e10cSrcweir 	sal_uIntPtr nRet = NOT_THERE;
203cdf0e10cSrcweir 	if ( (nRet = GetPrevString( pStr )) != 0 )
204cdf0e10cSrcweir 	{
205cdf0e10cSrcweir 		UniString* pString = GetObject( nRet );
206cdf0e10cSrcweir 		if ( *pString == *pStr )
207cdf0e10cSrcweir 			return nRet;
208cdf0e10cSrcweir 		else
209cdf0e10cSrcweir 			return NOT_THERE;
210cdf0e10cSrcweir 	}
211cdf0e10cSrcweir 	else
212cdf0e10cSrcweir 	{
213cdf0e10cSrcweir 		UniString* pString = GetObject( 0 );
214cdf0e10cSrcweir 		if ( pString && (*pString == *pStr) )
215cdf0e10cSrcweir 			return 0;
216cdf0e10cSrcweir 		else
217cdf0e10cSrcweir 			return NOT_THERE;
218cdf0e10cSrcweir 	}
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
GetPrevString(UniString * pStr)221cdf0e10cSrcweir sal_uIntPtr SUniStringList::GetPrevString( UniString* pStr )
222cdf0e10cSrcweir {
223cdf0e10cSrcweir 	sal_uIntPtr nRet = 0;
224cdf0e10cSrcweir 	sal_Bool bFound = sal_False;
225cdf0e10cSrcweir 	sal_uIntPtr nCountMember = Count();
226cdf0e10cSrcweir 	sal_uIntPtr nUpper = nCountMember;
227cdf0e10cSrcweir 	sal_uIntPtr nLower = 0;
228cdf0e10cSrcweir 	sal_uIntPtr nCurrent = nUpper / 2;
229cdf0e10cSrcweir 	sal_uIntPtr nRem = 0;
230cdf0e10cSrcweir 	UniString* pString;
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	do
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 		if ( (nCurrent == nLower) || (nCurrent == nUpper) )
235cdf0e10cSrcweir 			return nLower;
236cdf0e10cSrcweir 		pString = GetObject( nCurrent );
237cdf0e10cSrcweir 		StringCompare nResult =  pStr->CompareTo( *pString );
238cdf0e10cSrcweir 		if ( nResult == COMPARE_LESS )
239cdf0e10cSrcweir 		{
240cdf0e10cSrcweir 			nUpper = nCurrent;
241cdf0e10cSrcweir 			nCurrent = (nCurrent + nLower) /2;
242cdf0e10cSrcweir 		}
243cdf0e10cSrcweir 		else if ( nResult == COMPARE_GREATER )
244cdf0e10cSrcweir 		{
245cdf0e10cSrcweir 			nLower = nCurrent;
246cdf0e10cSrcweir 			nCurrent = (nUpper + nCurrent) /2;
247cdf0e10cSrcweir 		}
248cdf0e10cSrcweir 		else if ( nResult == COMPARE_EQUAL )
249cdf0e10cSrcweir 			return nCurrent;
250cdf0e10cSrcweir 		if ( nRem == nCurrent )
251cdf0e10cSrcweir 			return nCurrent;
252cdf0e10cSrcweir 		nRem = nCurrent;
253cdf0e10cSrcweir 	}
254cdf0e10cSrcweir 	while ( !bFound );
255cdf0e10cSrcweir 	return nRet;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir /**************************************************************************
259cdf0e10cSrcweir *
260cdf0e10cSrcweir *	Sortiert einen UniString in die Liste ein und gibt die Position,
261cdf0e10cSrcweir *	an der einsortiert wurde, zurueck
262cdf0e10cSrcweir *
263cdf0e10cSrcweir **************************************************************************/
264cdf0e10cSrcweir 
PutString(UniString * pStr)265cdf0e10cSrcweir sal_uIntPtr SUniStringList::PutString( UniString* pStr )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	sal_uIntPtr nPos = GetPrevString ( pStr );
268cdf0e10cSrcweir 	if ( Count() )
269cdf0e10cSrcweir 	{
270cdf0e10cSrcweir 		{
271cdf0e10cSrcweir 			UniString* pString = GetObject( 0 );
272cdf0e10cSrcweir 			if ( pString->CompareTo( *pStr ) == COMPARE_GREATER )
273cdf0e10cSrcweir 			{
274cdf0e10cSrcweir 				Insert( pStr, (sal_uIntPtr)0);
275cdf0e10cSrcweir 				return (sal_uIntPtr)0;
276cdf0e10cSrcweir 			}
277cdf0e10cSrcweir 		}
278cdf0e10cSrcweir 		UniString* pString = GetObject( nPos );
279cdf0e10cSrcweir 		if ( *pStr != *pString )
280cdf0e10cSrcweir 		{
281cdf0e10cSrcweir 			Insert( pStr, nPos+1 );
282cdf0e10cSrcweir 			return ( nPos +1 );
283cdf0e10cSrcweir 		}
284cdf0e10cSrcweir 	}
285cdf0e10cSrcweir 	else
286cdf0e10cSrcweir 	{
287cdf0e10cSrcweir 		Insert( pStr );
288cdf0e10cSrcweir 		return (sal_uIntPtr)0;
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	return NOT_THERE;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
RemoveString(const UniString & rName)294cdf0e10cSrcweir UniString* SUniStringList::RemoveString( const UniString& rName )
295cdf0e10cSrcweir {
296cdf0e10cSrcweir 	sal_uIntPtr i;
297cdf0e10cSrcweir 	UniString* pReturn;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	for( i = 0 ; i < Count(); i++ )
300cdf0e10cSrcweir 	{
301cdf0e10cSrcweir 		if ( rName == *GetObject( i ) )
302cdf0e10cSrcweir 		{
303cdf0e10cSrcweir 			pReturn = GetObject(i);
304cdf0e10cSrcweir 			Remove(i);
305cdf0e10cSrcweir 			return pReturn;
306cdf0e10cSrcweir 		}
307cdf0e10cSrcweir 	}
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	return NULL;
310cdf0e10cSrcweir }
311