1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: t_key_tools.inc,v $
13 '* last change: $Author: jsk $ $Date: 2008-06-20 07:58:19 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : joerg.skottke@sun.com
36 '* short description : tools for evaluation of key=value datalists
38 '*******************************************************************************
40 ' #1 hGetDataPairAsString ' retrieve key=value from a list
41 ' #1 hGetValueForPairAsString ' retrieve value from a key=value as string
42 ' #1 hGetValueForPairAsLong ' retrieve value from a key=value as long integer
43 ' #1 hGetValueForKeyAsInt ' retrieve value for a specified key as integer
44 ' #1 hGetValueForKeyAsLong ' retrieve value for a specified key as long integer
45 ' #1 hGetKeyForPairAsString ' retrieve key from key=value as string
46 ' #1 hGetKeyForValueAsString ' retrieve key for a specified value as string
47 ' #1 hGetKeyForValueAsInt ' retrieve key for a specified value as int
48 ' #1 hGetIndexForKeyAsInt ' retrieve the index for a key in a list
49 ' #1 KeyCompare ' compare two keys
51 '\******************************************************************************
53 ' Some variables explained:
55 ' cComp is the KEY belonging to the VALUE above for reverse test
57 ' NOTE: Other related functions can be found in global/tools/inc/t_filters.inc
59 '*******************************************************************************
61 function hGetDataPairAsString( sKey as string, sVal as long ) as string
63 '///<h3>Concatenate two strings and insert an = sign</h3>
64 '///<i>About "KEY"-functions: <br>
65 '///These functions are used whenever
66 '///+ data is to be processed that is stored in a key=value format.<br>
67 '///+ They handle a single string or a list of strings
68 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
73 '///+<li>KEY (string)</li>
75 '///+<li>Anything but should not contain # or '</li>
78 '///+<li>VALUE (string)</li>
80 '///+<li>Anything but should not contain # or '</li>
88 '///+<li>KEY=VALUE pair (string)</li>
92 '///<u>Description</u>:
95 '///+<li>Returns string1=string2</li>
96 hGetDataPairAsString() = sKey & "=" & sVal
102 '*******************************************************************************
104 function hGetValueForPairAsString( cLine as string ) as string
106 '///<h3>Retrieve value from a key=value string</h3>
107 '///<i>About "KEY"-functions: <br>
108 '///These functions are used whenever
109 '///+ data is to be processed that is stored in a key=value format.<br>
110 '///+ They handle a single string or a list of strings
111 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
116 '///+<li>KEY=VALUE pair (string)</li>
123 '///+<li>VALUE (string)</li>
125 '///+<li>Anything after the = character</li>
130 '///<u>Description</u>:
133 dim iCharPos as integer
135 '///+<li>Locate the equal sign</li>
136 iCharPos = instr( cLine , "=" )
137 iCharPos = len( cLine ) - iCharPos
139 '///+<li>Isolate everything to the right of the equal sign</li>
140 '///+<li>Return the value as string</li>
141 hGetValueForPairAsString() = right( cLine , iCharPos )
147 '*******************************************************************************
149 function hGetValueForPairAsLong( cLine as string ) as long
151 '///<h3>Retrieve the value from a key=value pair as long</h3>
152 '///<i>About "KEY"-functions: <br>
153 '///These functions are used whenever
154 '///+ data is to be processed that is stored in a key=value format.<br>
155 '///+ They handle a single string or a list of strings
156 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
161 '///+<li>KEY=VALUE pair (string)</li>
168 '///+<li>VALUE (long)</li>
170 '///+<li>Anything after the = character as long integer value</li>
175 '///<u>Description</u>:
178 dim iCharPos as integer
180 '///+<li>Locate the equal sign</li>
181 iCharPos = instr( cLine , "=" )
182 iCharPos = len( cLine ) - iCharPos
184 '///+<li>Isolate and return the Value as long datatype</li>
185 hGetValueForPairAsLong = val( right( cLine , iCharPos ))
191 '*******************************************************************************
193 function hGetValueForKeyAsInt( lsList() as string, sKey as string ) as integer
195 '///<h3>Retrieve the value of a key=value pair as integer</h3>
196 '///<i>About "KEY"-functions: <br>
197 '///These functions are used whenever
198 '///+ data is to be processed that is stored in a key=value format.<br>
199 '///+ They handle a single string or a list of strings
200 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
201 '///<i>Prerequisite: Array compatible to listfuncs, known, valid key</i><br><br>
206 '///+<li>List of KEY=VALUE pairs (string)</li>
208 '///+<li>List must be compatible to listfunctions (see t_listfuncs.inc)</li>
210 '///+<li>Key to be searched for within the list (string)</li>
212 '///+<li>Key must be valid</li>
220 '///+<li>VALUE (integer)</li>
222 '///+<li>The value for a given key</li>
227 '///<u>Description</u>:
231 dim cString as string
234 hGetValueForKeyAsInt() = 0
236 ' Scan through the list and look for sKey. If found, return the Value
237 ' (everything to the right of the '=')
238 '///+<li>Search the list for the key</li>
239 for iItem = 1 to listcount( lsList() )
240 if( instr( lsList( iItem ) , sKey ) <> 0 ) then
242 '///+<li>Verify that this is really the correct key by reverse-checking</li>
243 cComp = hGetKeyforPairAsString( lsList( iItem ) )
245 '///+<li>Retrieve the value for the key as string</li>
246 if( sKey = cComp ) then
247 cString = hGetValueForPairAsString( lsList( iItem ) )
248 iItem = listcount( lsList() ) + 1
254 '///+<li>Convert the stringvalue to integer and return it</li>
255 hGetValueForKeyAsInt() = val( cString )
261 '*******************************************************************************
263 function hGetValueForKeyAsLong( lsList() as string, sKey as string ) as long
265 '///<h3>Retrieve the value of a key=value pair as integer</h3>
266 '///<i>About "KEY"-functions: <br>
267 '///These functions are used whenever
268 '///+ data is to be processed that is stored in a key=value format.<br>
269 '///+ They handle a single string or a list of strings
270 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
271 '///<i>Prerequisite: Array compatible to listfuncs, known, valid key</i><br><br>
276 '///+<li>List of KEY=VALUE pairs (string)</li>
278 '///+<li>List must be compatible to listfunctions (see t_listfuncs.inc)</li>
280 '///+<li>Key to be searched for within the list (string)</li>
282 '///+<li>Key must be valid</li>
290 '///+<li>VALUE (long integer)</li>
292 '///+<li>The value for a given key</li>
297 '///<u>Description</u>:
301 dim cString as string
304 hGetValueForKeyAsInt() = 0
306 ' Scan through the list and look for sKey. If found, return the Value
307 ' (everything to the right of the '=')
308 '///+<li>Search the list for the key</li>
309 for iItem = 1 to listcount( lsList() )
310 if( instr( lsList( iItem ) , sKey ) <> 0 ) then
312 '///+<li>Verify that this is really the correct key by reverse-checking</li>
313 cComp = hGetKeyforPairAsString( lsList( iItem ) )
315 '///+<li>Retrieve the value for the key as string</li>
316 if( sKey = cComp ) then
317 cString = hGetValueForPairAsString( lsList( iItem ) )
318 iItem = listcount( lsList() ) + 1
324 '///+<li>Convert the stringvalue to long integer and return it</li>
325 hGetValueForKeyAsInt() = val( cString )
331 '*******************************************************************************
333 function hGetKeyForPairAsString( cLine as string ) as string
335 '///<h3>Retrieve the KEY from a key=value pair</h3>
336 '///<i>About "KEY"-functions: <br>
337 '///These functions are used whenever
338 '///+ data is to be processed that is stored in a key=value format.<br>
339 '///+ They handle a single string or a list of strings
340 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
345 '///+<li>KEY=VALUE pair (string)</li>
352 '///+<li>KEY (string)</li>
354 '///+<li>Anything before the = character</li>
359 '///<u>Description</u>:
362 dim iCharPos as integer
364 iCharPos = instr( cLine , "=" )
366 '///+<li>get the string to the left of the = char</li>
367 if ( iCharPos > 0 ) then
368 hGetKeyForPairAsString() = left( cLine , iCharPos -1 )
370 warnlog( "Invalid string passed to hGetKeyForPairAsString" )
371 printlog( "It was: " & cLine )
373 '///+<li>Return the key</li>
378 '*******************************************************************************
380 function hGetKeyForValueAsString( lsList() as string, sVal as string ) as string
382 '///<h3>Retrieve the KEY for a known VALUE from a key=value pair</h3>
383 '///<i>About "KEY"-functions: <br>
384 '///These functions are used whenever
385 '///+ data is to be processed that is stored in a key=value format.<br>
386 '///+ They handle a single string or a list of strings
387 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
388 '///<i>Prerequisite: List compatible to listfuncs, known VALUE for pair</i><br><br>
393 '///+<li>List of KEY=VALUE pairs (string)</li>
398 '///+<li>VALUE (string)</li>
400 '///+<li>Absolute match required.</li>
408 '///+<li>KEY (string)</li>
412 '///<u>Description</u>:
415 ' This function returns the value of a key as string.
416 ' The form of the input strings is 'key=value', the list is parsed
417 ' The key for the first occurrence of sVal is returned
419 dim iCurrentValue as integer
420 dim iListItems as integer
421 iListItems = listcount( lsList() )
424 ' preset a default return string
425 hGetKeyForValueAsString() = "Error: No matching KEY found for VALUE"
427 '///+<li>scan through a list to find an entry that matches sKey. Return the Key.</li>
428 for iCurrentValue = 1 to iListItems
430 cLine = lsList( iCurrentValue )
432 if( instr( cLine , sVal ) <> 0 ) then
433 hGetKeyForValueAsString() = hGetKeyForPairAsString( cLine )
434 iCurrentValue = iListItems + 1
438 '///+<li>Return the key from the first matching value</li>
443 '*******************************************************************************
445 function hGetKeyForValueAsInt( lsList() as string, sVal as string ) as integer
447 '///<h3>Retrieve the KEY for a known VALUE from a key=value pair</h3>
448 '///<i>About "KEY"-functions: <br>
449 '///These functions are used whenever
450 '///+ data is to be processed that is stored in a key=value format.<br>
451 '///+ They handle a single string or a list of strings
452 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
453 '///<i>Prerequisite: List compatible to listfuncs, known VALUE for pair</i><br><br>
458 '///+<li>List of KEY=VALUE pairs (string)</li>
460 '///+<li>VALUE (string)</li>
462 '///+<li>Absolute match required.</li>
470 '///+<li>KEY (integer)</li>
474 '///<u>Description</u>:
477 '///+<li>Use hgetKeyForValueAsString, convert result to integer, return it.</li>
478 hGetKeyForValueAsInt() = val( hGetKeyForValueAsString( lsList() , sVal ) )
483 '*******************************************************************************
485 function hGetIndexForKeyAsInt( lsList() as string, sKey as string ) as integer
487 '///<h3>Search a list of key=value pairs for a key and return its index</h3>
488 '///<i>About "KEY"-functions: <br>
489 '///These functions are used whenever
490 '///+ data is to be processed that is stored in a key=value format.<br>
491 '///+ They handle a single string or a list of strings
492 '///+ to e.g. isolate a key, a value or to just retrieve the pair.</i><br><br>
493 '///<i>Prerequisite: List compatible to listfuncs, known VALUE for pair</i><br><br>
498 '///+<li>List of KEY=VALUE pairs (string)</li>
500 '///+<li>KEY to search for (string)</li>
507 '///+<li>Index of the KEY in the list (integer)</li>
509 '///+<li>0 if not found</li>
510 '///+<li>Any number > 0 and ≤ listcount( List() )</li>
515 '///<u>Description</u>:
521 hGetIndexForNamedKeyAsInt() = 0
523 ' Scan through the list and look for sKey. If found, return the Value
524 ' (everything to the right of the '=')
525 '///+<li>Scan through the list for the KEY</li>
526 for iItem = 1 to listcount( lsList() )
527 if( instr( lsList( iItem ) , sKey ) <> 0 ) then
529 '///+<li>if found, perform reverse checking</li>
530 cComp = hGetKeyforValueAsString( lsList( iItem ) )
532 '///+<li>get the index of the item</li>
533 if( sKey = cComp ) then
534 hGetIndexForKeyAsInt() = iItem
535 iItem = listcount( lsList() ) + 1
540 '///+<li>Return the index</li>
545 '*******************************************************************************
547 function keycompare( found as string, expected as string, sKey as string, optional cBugID as string) as boolean
549 '///<h3>Function to compare two keys with each other</h3>
550 '///<u>Deprecated, do not use</u>
552 if ( isMissing( cBugID ) ) then
556 if ( lcase(found) = lcase(expected) ) then
557 printlog( " * '" & sKey & "': ok" )
560 warnlog( "#" + cBugID + "# Control has incorrect value: '" & sKey & "'")
561 printlog( " > Found...: '" & found & "'" )
562 printlog( " > Expected: '" & expected & "'" )