xref: /aoo41x/main/svl/source/misc/ownlist.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir #include <ctype.h>
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValues.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <svl/ownlist.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace com::sun::star;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //=========================================================================
35cdf0e10cSrcweir //============== SvCommandList ============================================
36cdf0e10cSrcweir //=========================================================================
PRV_SV_IMPL_OWNER_LIST(SvCommandList,SvCommand)37cdf0e10cSrcweir PRV_SV_IMPL_OWNER_LIST(SvCommandList,SvCommand)
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir static String parseString(const String & rCmd, sal_uInt16 * pIndex)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	String result;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	if(rCmd.GetChar( *pIndex ) == '\"') {
45cdf0e10cSrcweir 		(*pIndex) ++;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 		sal_uInt16 begin = *pIndex;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 		while(*pIndex < rCmd.Len() && rCmd.GetChar((*pIndex) ++) != '\"') ;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 		result = String(rCmd.Copy(begin, *pIndex - begin - 1));
52cdf0e10cSrcweir 	}
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	return result;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
parseWord(const String & rCmd,sal_uInt16 * pIndex)57cdf0e10cSrcweir static String parseWord(const String & rCmd, sal_uInt16 * pIndex)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	sal_uInt16 begin = *pIndex;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	while(*pIndex < rCmd.Len() && !isspace(rCmd.GetChar(*pIndex)) && rCmd.GetChar(*pIndex) != '=')
62cdf0e10cSrcweir 		(*pIndex) ++;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	return String(rCmd.Copy(begin, *pIndex - begin));
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
eatSpace(const String & rCmd,sal_uInt16 * pIndex)67cdf0e10cSrcweir static void eatSpace(const String & rCmd, sal_uInt16 * pIndex)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	while(*pIndex < rCmd.Len() && isspace(rCmd.GetChar(*pIndex)))
70cdf0e10cSrcweir 		(*pIndex) ++;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir //=========================================================================
AppendCommands(const String & rCmd,sal_uInt16 * pEaten)75cdf0e10cSrcweir sal_Bool SvCommandList::AppendCommands
76cdf0e10cSrcweir (
77cdf0e10cSrcweir 	const String & rCmd,	/* Dieser Text wird in Kommandos umgesetzt */
78cdf0e10cSrcweir 	sal_uInt16 * pEaten			/* Anzahl der Zeichen, die gelesen wurden */
79cdf0e10cSrcweir )
80cdf0e10cSrcweir /*	[Beschreibung]
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	Es wird eine Text geparsed und die einzelnen Kommandos werden an
83cdf0e10cSrcweir 	die Liste angeh"angt.
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	[R"uckgabewert]
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	sal_Bool		sal_True, der Text wurde korrekt geparsed.
88cdf0e10cSrcweir 				sal_False, der Text wurde nicht korrekt geparsed.
89cdf0e10cSrcweir */
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	sal_uInt16 index = 0;
92cdf0e10cSrcweir 	while(index < rCmd.Len())
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		eatSpace(rCmd, &index);
96cdf0e10cSrcweir 		String name = (rCmd.GetChar(index) == '\"') ? parseString(rCmd, &index) : parseWord(rCmd, &index);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 		eatSpace(rCmd, &index);
99cdf0e10cSrcweir 		String value;
100cdf0e10cSrcweir 		if(index < rCmd.Len() && rCmd.GetChar(index) == '=')
101cdf0e10cSrcweir 		{
102cdf0e10cSrcweir 			index ++;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 			eatSpace(rCmd, &index);
105cdf0e10cSrcweir 			value = (rCmd.GetChar(index) == '\"') ? parseString(rCmd, &index) : parseWord(rCmd, &index);
106cdf0e10cSrcweir 		}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		SvCommand * pCmd = new SvCommand(name, value);
109cdf0e10cSrcweir 		aTypes.Insert(pCmd, LIST_APPEND);
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	*pEaten = index;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir //  	sal_uInt16 nPos = 0;
115cdf0e10cSrcweir //  	while( nPos < rCmd.Len() )
116cdf0e10cSrcweir //  	{
117cdf0e10cSrcweir //  		// ein Zeichen ? Dann faengt hier eine Option an
118cdf0e10cSrcweir //    		if( isalpha( rCmd[nPos] ) )
119cdf0e10cSrcweir //  		{
120cdf0e10cSrcweir //  			String aValue;
121cdf0e10cSrcweir //  			sal_uInt16 nStt = nPos;
122cdf0e10cSrcweir //  			register char c;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir //  			while( nPos < rCmd.Len() &&
125cdf0e10cSrcweir //  					( isalnum(c=rCmd[nPos]) || '-'==c || '.'==c ) )
126cdf0e10cSrcweir //  				nPos++;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir //  			String aToken( rCmd.Copy( nStt, nPos-nStt ) );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir //  			while( nPos < rCmd.Len() &&
131cdf0e10cSrcweir //  					( !String::IsPrintable( (c=rCmd[nPos]),
132cdf0e10cSrcweir //  					RTL_TEXTENCODING_MS_1252 ) || isspace(c) ) )
133cdf0e10cSrcweir //  				nPos++;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir //  			// hat die Option auch einen Wert?
136cdf0e10cSrcweir //  			if( nPos!=rCmd.Len() && '='==c )
137cdf0e10cSrcweir //  			{
138cdf0e10cSrcweir //  				nPos++;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir //  				while( nPos < rCmd.Len() &&
141cdf0e10cSrcweir //  					 	( !String::IsPrintable( (c=rCmd[nPos]),
142cdf0e10cSrcweir //  						RTL_TEXTENCODING_MS_1252 ) || isspace(c) ) )
143cdf0e10cSrcweir //  					nPos++;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir //  				if( nPos != rCmd.Len() )
146cdf0e10cSrcweir //  				{
147cdf0e10cSrcweir //  					sal_uInt16 nLen = 0;
148cdf0e10cSrcweir //  					nStt = nPos;
149cdf0e10cSrcweir //  					if( '"' == c )
150cdf0e10cSrcweir //  					{
151cdf0e10cSrcweir //  						nPos++; nStt++;
152cdf0e10cSrcweir //  						while( nPos < rCmd.Len() &&
153cdf0e10cSrcweir //  					    	 	'"' != rCmd[nPos] )
154cdf0e10cSrcweir //  							nPos++, nLen++;
155cdf0e10cSrcweir //  						if( nPos!=rCmd.Len() )
156cdf0e10cSrcweir //  							nPos++;
157cdf0e10cSrcweir //  					}
158cdf0e10cSrcweir //  					else
159cdf0e10cSrcweir //  						// hier sind wir etwas laxer als der
160cdf0e10cSrcweir //  						// Standard und erlauben alles druckbare
161cdf0e10cSrcweir //  						while( nPos < rCmd.Len() &&
162cdf0e10cSrcweir //  		 				    	String::IsPrintable( (c=rCmd[nPos]),
163cdf0e10cSrcweir //  								RTL_TEXTENCODING_MS_1252 ) &&
164cdf0e10cSrcweir //  								!isspace( c ) )
165cdf0e10cSrcweir //  							nPos++, nLen++;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir //  					if( nLen )
168cdf0e10cSrcweir //  						aValue = rCmd( nStt, nLen );
169cdf0e10cSrcweir //  				}
170cdf0e10cSrcweir //  			}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir //  			SvCommand * pCmd = new SvCommand( aToken, aValue );
173cdf0e10cSrcweir //  		    aTypes.Insert( pCmd, LIST_APPEND );
174cdf0e10cSrcweir //  		}
175cdf0e10cSrcweir //  		else
176cdf0e10cSrcweir //  			// white space un unerwartete Zeichen ignorieren wie
177cdf0e10cSrcweir //  			nPos++;
178cdf0e10cSrcweir //  	}
179cdf0e10cSrcweir //  	*pEaten = nPos;
180cdf0e10cSrcweir 	return sal_True;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir //=========================================================================
GetCommands() const184cdf0e10cSrcweir String SvCommandList::GetCommands() const
185cdf0e10cSrcweir /*	[Beschreibung]
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	Die Kommandos in der Liste werden als Text hintereinander, durch ein
188cdf0e10cSrcweir 	Leerzeichen getrennt geschrieben. Der Text muss nicht genauso
189cdf0e10cSrcweir 	aussehen wie der in <SvCommandList::AppendCommands()> "ubergebene.
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	[R"uckgabewert]
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	String		Die Kommandos werden zur"uckgegeben.
194cdf0e10cSrcweir */
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	String aRet;
197cdf0e10cSrcweir 	for( sal_uLong i = 0; i < aTypes.Count(); i++ )
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		if( i != 0 )
200cdf0e10cSrcweir 			aRet += ' ';
201cdf0e10cSrcweir 		SvCommand * pCmd = (SvCommand *)aTypes.GetObject( i );
202cdf0e10cSrcweir 		aRet += pCmd->GetCommand();
203cdf0e10cSrcweir 		if( pCmd->GetArgument().Len() )
204cdf0e10cSrcweir 		{
205cdf0e10cSrcweir 			aRet.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "=\"" ) );
206cdf0e10cSrcweir 			aRet += pCmd->GetArgument();
207cdf0e10cSrcweir 			aRet.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "\"" ) );
208cdf0e10cSrcweir 		}
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 	return aRet;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir //=========================================================================
Append(const String & rCommand,const String & rArg)214cdf0e10cSrcweir SvCommand & SvCommandList::Append
215cdf0e10cSrcweir (
216cdf0e10cSrcweir 	const String & rCommand,	/* das Kommando	*/
217cdf0e10cSrcweir 	const String & rArg			/* dasArgument des Kommandos */
218cdf0e10cSrcweir )
219cdf0e10cSrcweir /*	[Beschreibung]
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	Es wird eine Objekt vom Typ SvCommand erzeugt und an die Liste
222cdf0e10cSrcweir 	angeh"angt.
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	[R"uckgabewert]
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	SvCommand &		Das erteugte Objekt wird zur"uckgegeben.
227cdf0e10cSrcweir */
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	SvCommand * pCmd = new SvCommand( rCommand, rArg );
230cdf0e10cSrcweir 	aTypes.Insert( pCmd, LIST_APPEND );
231cdf0e10cSrcweir 	return *pCmd;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir //=========================================================================
operator >>(SvStream & rStm,SvCommandList & rThis)235cdf0e10cSrcweir SvStream & operator >>
236cdf0e10cSrcweir (
237cdf0e10cSrcweir 	SvStream & rStm,     	/* Stream aus dem gelesen wird */
238cdf0e10cSrcweir 	SvCommandList & rThis	/* Die zu f"ullende Liste */
239cdf0e10cSrcweir )
240cdf0e10cSrcweir /*	[Beschreibung]
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	Die Liste mit ihren Elementen wird gelesen. Das Format ist:
243cdf0e10cSrcweir 	1. Anzahl der Elemente
244cdf0e10cSrcweir 	2. Alle Elemente
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	[R"uckgabewert]
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	SvStream &		Der "ubergebene Stream.
249cdf0e10cSrcweir */
250cdf0e10cSrcweir {
251cdf0e10cSrcweir 	sal_uInt32 nCount = 0;
252cdf0e10cSrcweir 	rStm >> nCount;
253cdf0e10cSrcweir 	if( !rStm.GetError() )
254cdf0e10cSrcweir 	{
255cdf0e10cSrcweir 		while( nCount-- )
256cdf0e10cSrcweir 		{
257cdf0e10cSrcweir 			SvCommand * pCmd = new SvCommand();
258cdf0e10cSrcweir 			rStm >> *pCmd;
259cdf0e10cSrcweir 			rThis.aTypes.Insert( pCmd, LIST_APPEND );
260cdf0e10cSrcweir 		}
261cdf0e10cSrcweir 	}
262cdf0e10cSrcweir 	return rStm;
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //=========================================================================
operator <<(SvStream & rStm,const SvCommandList & rThis)266cdf0e10cSrcweir SvStream & operator <<
267cdf0e10cSrcweir (
268cdf0e10cSrcweir 	SvStream & rStm,     		/* Stream in den geschrieben wird */
269cdf0e10cSrcweir 	const SvCommandList & rThis	/* Die zu schreibende Liste */
270cdf0e10cSrcweir )
271cdf0e10cSrcweir /*	[Beschreibung]
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	Die Liste mit ihren Elementen wir geschrieben. Das Format ist:
274cdf0e10cSrcweir 	1. Anzahl der Elemente
275cdf0e10cSrcweir 	2. Alle Elemente
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	[R"uckgabewert]
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	SvStream &		Der "ubergebene Stream.
280cdf0e10cSrcweir */
281cdf0e10cSrcweir {
282cdf0e10cSrcweir 	sal_uInt32 nCount = rThis.aTypes.Count();
283cdf0e10cSrcweir 	rStm << nCount;
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	for( sal_uInt32 i = 0; i < nCount; i++ )
286cdf0e10cSrcweir 	{
287cdf0e10cSrcweir 		SvCommand * pCmd = (SvCommand *)rThis.aTypes.GetObject( i );
288cdf0e10cSrcweir 		rStm << *pCmd;
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir 	return rStm;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
FillFromSequence(const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & aCommandSequence)293cdf0e10cSrcweir sal_Bool SvCommandList::FillFromSequence( const com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue >& aCommandSequence )
294cdf0e10cSrcweir {
295cdf0e10cSrcweir 	const sal_Int32 nCount = aCommandSequence.getLength();
296cdf0e10cSrcweir 	String aCommand, aArg;
297cdf0e10cSrcweir     ::rtl::OUString aApiArg;
298cdf0e10cSrcweir     for( sal_Int32 nIndex=0; nIndex<nCount; nIndex++ )
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 		aCommand = aCommandSequence[nIndex].Name;
301cdf0e10cSrcweir 		if( !( aCommandSequence[nIndex].Value >>= aApiArg ) )
302cdf0e10cSrcweir 			return sal_False;
303cdf0e10cSrcweir 		aArg = aApiArg;
304cdf0e10cSrcweir         Append( aCommand, aArg );
305cdf0e10cSrcweir 	}
306cdf0e10cSrcweir 
307cdf0e10cSrcweir     return sal_True;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
FillSequence(com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & aCommandSequence)310cdf0e10cSrcweir void SvCommandList::FillSequence( com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue >& aCommandSequence )
311cdf0e10cSrcweir {
312cdf0e10cSrcweir     const sal_Int32 nCount = Count();
313cdf0e10cSrcweir 	aCommandSequence.realloc( nCount );
314cdf0e10cSrcweir 	for( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ )
315cdf0e10cSrcweir 	{
316cdf0e10cSrcweir         const SvCommand& rCommand = (*this)[ nIndex ];
317cdf0e10cSrcweir 		aCommandSequence[nIndex].Name = rCommand.GetCommand();
318cdf0e10cSrcweir 		aCommandSequence[nIndex].Handle = -1;
319cdf0e10cSrcweir         aCommandSequence[nIndex].Value = uno::makeAny( ::rtl::OUString( rCommand.GetArgument() ) );
320cdf0e10cSrcweir         aCommandSequence[nIndex].State = beans::PropertyState_DIRECT_VALUE;
321cdf0e10cSrcweir 	}
322cdf0e10cSrcweir }
323cdf0e10cSrcweir 
324