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_lists.inc,v $
13 '* last change: $Author: jsi $ $Date: 2008-06-13 10:27:10 $
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 : helge.delfs@sun.com
36 '* short description : general routines to work with lists (arrays)
38 '\******************************************************************
40 function ListCount ( lsList() as String ) as Integer
42 '///Returns the number of list entries.
43 '///+<u>Input</u>: The list (only string lists are possible)
44 '///+<u>Return</u>: The number of entries
45 ListCount = Val(lsList(0))
48 '-------------------------------------------------------------------------
50 function ListCopy ( lsList1() as String, lsList2() as String ) as Boolean
52 '///Copies all entries out of one list into another list.
53 '///+<u>Input</u>:<ol><lo>list which should be copied</li><li>An empty list</li></ol>After this function the 2nd list is a copy of the 1st list.
54 '///+<u>Return</u>: If copy of the list is correct this function returns TRUE otherweise FALSE
58 ListAllDelete ( lsList2() )
59 for ii=1 to ListCount ( lsList1() )
60 ListAppend ( lsList2(), lsList1(ii) )
63 if ListCount ( lsList1() ) = ListCount ( lsList2 () ) then
70 '-------------------------------------------------------------------------
72 sub ListAllDelete ( lsList() as String )
74 '///Deletes a complete list.
75 '///+<u>Input</u>: The list (only string lists are possible)
79 '-------------------------------------------------------------------------
81 sub ListAppend ( lsList() as String, sNewEntry as String )
83 '///Appends a new entry at the end of the list.
84 '///+<u>Input</u>: <ol><li>the list (only string lists are possible)</li><li>The new entry</li></ol>
85 lsList(0) = Val(lsList(0)) + 1
86 lsList( lsList(0) ) = sNewEntry
89 '-------------------------------------------------------------------------
91 function ListDelete ( lsList() as String, iNr as Integer ) as Boolean
93 '///Deletes an entry out of the list on a defined position (iNr).
94 '///+<u>Input</u>: <ol><li>The list (only string lists are possible)</li><li>The position of the entry</li></ol>
95 '///+<u>Return</u>: TRUE if the entry was deleted otherweise FALSE
98 Dim i%, ListenAnzahl as Integer
100 ListenAnzahl = listcount( lsList() )
102 if iNr > ListenAnzahl then
107 for i% = iNr to ListenAnzahl
108 lsList( i% ) = lsList( i% + 1 )
111 lsList(0) = ListenAnzahl - 1
116 '-------------------------------------------------------------------------
118 function ListDeleteString ( lsList() as String, sText as String ) as Boolean
120 '///Deletes the 1st string in the list which is equal to the input string.
121 '///+<u>Input</u>: <ol><li>The list (only string lists are possible)</li><li>The string</li></ol>
122 '///+<u>Return</u>: TRUE if the entry was deleted otherwise FALSE
123 Dim i as Integer : Dim EintragsNr as Integer : Dim ListenAnzahl as Integer
125 ListenAnzahl = Val(lsList(0))
127 for i = 1 to ListenAnzahl
128 if lsList(i) = sText then
133 if EintragsNr = 0 then
134 ListDeleteString = FALSE
136 ListDeleteString = ListDelete ( lsList(), EintragsNr )
140 '-------------------------------------------------------------------------
142 function ListInsert ( lsList() as String, ZeileNr%, sWert$ ) as Boolean
144 '///Inserts a string at a defined position in the list.
145 '///+<u>Input</u>: <ol><li>The list (only string lists are possible)</li><li>The position</li><li>The string</li></ol>
146 '///+<u>Return</u>: TRUE if the entry was inserted otherwise FALSE
147 Dim i% : Dim ListenAnzahl as Integer
149 ListenAnzahl = Val(lsList(0))
150 if ZeileNr% > ListenAnzahl then
155 ' Nach hinten verschieben, hinten beginnend
156 for i% = ListenAnzahl to ZeileNr% step -1
157 lsList( i%+1 ) = lsList( i% )
161 lsList( ZeileNr% ) = sWert$
162 lsFile(0) = ListenAnzahl + 1
167 '-------------------------------------------------------------------------
169 function ListRead ( lsList() as String, Datei$, optional sEncode as String ) as Boolean
171 '///+Opens a file and insert all rows into a list (row for row).
172 '///+<u>Input</u>: <ol><li>The list (old list entries will be deleted)</li><li>The file</li><li><b>optional</b>: The encoding "UTF8"</li></ol>
173 '///+<u>Return</u>: TRUE or FALSE if this routine can read the file.
176 Dim CompareList(15000) as String
178 if Dir( Datei$ ) = "" then
179 Warnlog "ListRead: " + Datei$ + " is missing!"
184 if IsMissing ( sEncode ) = TRUE then
187 if UCASE ( sEncode ) = "UFT8" OR UCASE ( sEncode ) = "UTF8" then
190 Warnlog "ListRead :" + sEncode + " - Encoding is unkown!"
195 ListAllDelete ( lsList() ) ' clean up the list
198 Dim textin as object, sfa as object, xInput as object ' for UTF-8-input-routines
201 textin = createUnoService( "com.sun.star.io.TextInputStream" ) ' uno-handling to input an UFT-8-File
202 textin.setEncoding("utf8") '
203 sfa = createUnoService( "com.sun.star.ucb.SimpleFileAccess" ) '
204 xInput = sfa.openFileRead( Datei$ ) '
205 textin.setInputStream( xInput ) '
207 do until textin.isEOF() '
208 i% = Val(lsList(0)) + 1
210 lsList( i% ) = textin.readLine() '
212 xInput.closeInput ' uno-file-close
214 'INFO: (TZ) Only to workaround a problem with UNIX-Files...
215 if Right ( lsList(i%), 1 ) = Chr(10) then
216 lsList(i%) = Left ( lsList(i%), Len ( lsList(i%) ) - 1 )
218 'INFO: (TBO) Remove the BOM http://www.unicode.org/versions/Unicode4.0.0/ch15.pdf
219 if (left(lsList(1), 1) = chr(&HFEFF)) then
220 lsList(1) = right(lsList(1), Len(lsList(1)) - 1)
226 Open Datei$ for input as #FileNum%
228 do until EOF(#FileNum%) ' all from LIS-file
229 i% = Val(lsList(0)) + 1
231 Line Input #FileNum%, lsList( i% )
238 '-------------------------------------------------------------------------
240 function ListWrite ( lsList() as String, Datei$, optional sEncode as String) as Boolean
242 '///+Writes a list into a file (an existing file will be deleted before)
243 '///+<u>Input</u>: <ol><li>The list</li><li>The file</li><li><b>optional</b>: The encoding "UTF8"</li></ol>
244 '///+<u>return</u>: TRUE or FALSE if this routine can read the file.
249 if Dir (Datei$) <> "" then
250 Kill(Datei$) ' the file must be deleted if you use 'UTF8'
253 if IsMissing ( sEncode ) = TRUE then
256 if UCASE ( sEncode ) = "UTF8" then
259 Warnlog "ListWrite :" + sEncode + " - Encoding is unkown!"
265 Dim textout as object, sfa as object, xOutput as object ' for UTF-8-output-routines
267 textout = createUnoService( "com.sun.star.io.TextOutputStream" ) ' uno-handling to output an UFT-8-File
268 textout.setEncoding("utf8") '
269 sfa = createUnoService( "com.sun.star.ucb.SimpleFileAccess" ) '
270 xOutput = sfa.openFileWrite( Datei$ ) '
271 textout.setOutputStream( xOutput ) '
273 for i%=1 to ListCount ( lsList() )
274 textout.writeString( lsList( i% ) + Chr(13) + Chr(10) ) '
276 xOutput.closeOutput ' uno-file-close
278 Dim FileNum% : Dim iLast%
281 Open Datei$ for Output as #FileNum%
282 iLast% = Val(lsList(0))
284 do while i% <= iLast%
285 Print #FileNum%, lsList(i%)
294 '-------------------------------------------------------------------------
296 function ListReadAppend( lsList() as String , Datei$, optional sEncode as String ) as Boolean
298 '///+Appends a list into a file (If the file exists the file will be deleted before!).
299 '///+<u>Input</u>: <ol><li>The list</li><li>The file</li><li><b>optional</b>: The encoding "UTF8"</li></ol>
300 '///+<u>return</u>: TRUE or FALSE if this routine can read the file.
304 Dim CompareList() as String
305 Dim isCounter as Integer
308 if Dir( Datei$ ) = "" then
309 Warnlog "ListReadAppend : " + Datei$ + " is missing!"
310 ListReadAppend = FALSE
314 isCounter = ListCount ( lsList() )
316 if IsMissing ( sEncode ) = TRUE then
319 if UCASE ( sEncode ) = "UFT8" OR UCASE ( sEncode ) = "UTF8" then
322 Warnlog "ListRead : " + sEncode + " - Encoding is unkown!"
328 Dim textin as object, sfa as object, xInput as object ' for UTF-8-input-routines
330 textin = createUnoService( "com.sun.star.io.TextInputStream" ) ' uno-handling to input an UFT-8-File
331 textin.setEncoding("utf8") '
332 sfa = createUnoService( "com.sun.star.ucb.SimpleFileAccess" ) '
333 xInput = sfa.openFileRead( Datei$ ) '
334 textin.setInputStream( xInput ) '
336 do until textin.isEOF() '
337 i% = Val(lsList(0)) + 1
339 lsList( i% ) = textin.readLine() '
341 xInput.closeInput ' uno-file-close
343 'INFO: (TZ) Only to workaround a problem with UNIX-Files...
344 if Right ( lsList(i%), 1 ) = Chr(10) then
345 lsList(i%) = Left ( lsList(i%), Len ( lsList(i%) ) - 1 )
350 Open Datei$ for input as #FileNum%
352 do until EOF(FileNum%) ' All from LIST-file
353 i% = Val(lsList(0)) + 1
355 Line Input #FileNum%, lsList( i% )
360 ListReadAppend = TRUE
364 '-------------------------------------------------------------------------
366 function ListWriteAppend( lsList() as String, Datei$, optional sEncode as String ) as Boolean
368 '///+Writes a list into a file (If the files exist all entries will be appended).
369 '///+<u>Input</u>: <ol><li>The list</li><li>The file</li><li><b>optional</b>: The encoding "UTF8"</li></ol>
370 '///+<u>return</u>: TRUE or FALSE if this routine can read the file.
374 Dim DummyList ( 15000 ) as String
376 if IsMissing ( sEncode ) = TRUE then
379 if UCASE ( sEncode ) = "UTF8" then
382 Warnlog "ListRead :" + sEncode + " - Encoding is unkown!"
388 Dim sfa as object, xOutput as object, textout as object ' for UTF-8-output-routines
390 ListRead ( DummyList(), Datei$, "utf8" ) ' read old file in another list
391 for i% = 1 to ListCount ( lsList() )
392 ListAppend ( DummyList(), lsList(i%) ) ' add the new list at the old list
395 textout = createUnoService( "com.sun.star.io.TextOutputStream" ) ' uno-handling to output an UFT-8-File
396 textout.setEncoding("utf8") '
397 sfa = createUnoService( "com.sun.star.ucb.SimpleFileAccess" ) '
398 xOutput = sfa.openFileWrite( Datei$ ) '
399 textout.setOutputStream( xOutput ) '
401 for i%=1 to ListCount (DummyList())
402 textout.writeString( DummyList( i% ) + Chr(13) + Chr(10 ) '
404 xOutput.closeOutput ' uno-file-close
409 Open Datei$ for Append as #FileNum%
411 for i% = 1 to Val(lsList(0))
412 Print #FileNum%, lsList(i%)
417 ListWriteAppend = TRUE
421 '-------------------------------------------------------------------------
423 sub ListSort ( lsList() as String, optional UpDown as Boolean )
425 '///+Sorts a list upward per default or downward if optional parameter is FALSE with quicksort method.
426 '///+<u>Input</u>: Unsorted list
428 Dim Listenanzahl as Integer, i as Integer, j as Integer
429 Dim Zwischenspeicher as String
431 ListenAnzahl = Val(lsList(0))
432 for i=ListenAnzahl-1 to 1 step -1
434 if UpDown = FALSE then
436 if uCase ( lsList(j) ) < uCase ( lsList(j+1) ) then
437 Zwischenspeicher = lsList (j) ' invert value (i) with value (i+1)
438 lsList (j) = lsList(j+1)
439 lsList (j+1) = Zwischenspeicher
443 if uCase ( lsList(j) ) > uCase ( lsList(j+1) ) then
444 Zwischenspeicher = lsList (j) ' invert value (i) with value (i+1)
445 lsList (j) = lsList(j+1)
446 lsList (j+1) = Zwischenspeicher
453 '*******************************************************************************
455 function gCompare2Lists( aListOne() as String, aListTwo() as String ) as boolean
457 const CFN = "global::tools::inc::t_list.inc::gCompare2Lists: "
459 '///<h3>Compare two lists with each other, where <b>list TWO</b> is the reference</h3>
462 dim aOneOnlyList( ubound( aListOne() ) ) as string
463 dim aTwoOnlyList( ubound( aListTwo() ) ) as string
465 dim iListOneIndex as integer
466 dim iListTwoIndex as integer
468 dim bFound as boolean
469 dim brc as boolean ' returncode: true if lists are identical
472 '///+<li>Create a copy of list two so we do not change the original list</li>
473 ListCopy( aListTwo() , aTwoOnlyList() )
475 '///+<li>Step through each item in list one</li>
476 for iListOneIndex = 1 to ListCount( aListOne() )
480 '///+<li>Compare it to each item in list two</li>
481 for iListTwoIndex = 1 to ListCount( aTwoOnlyList() )
483 '///+<li>If the entries match, delete it from the TwoOnly list</li>
484 if ( aListOne( iListOneIndex ) = aTwoOnlyList( iListTwoIndex ) ) then
487 ListDelete( aTwoOnlyList() , iListTwoIndex )
494 '///+<li>If there is no match, the item exists in list one only -> copy</li>
495 if ( not bFound ) then
496 ListAppend( aOneOnlyList() , aListOne( iListOneIndex ) )
501 '///+<li>List all items that exist in List One only</li>
502 if ( ListCount( aOneOnlyList() ) > 0 ) then
503 warnlog( CFN & "Objects have been added to the list" )
504 hListPrint( aOneOnlyList() , "Items found in list ONE only (NEW)" )
508 '///+<li>List all items that exist in List Two only</li>
509 if ( ListCount( aTwoOnlyList() ) > 0 ) then
510 warnlog( CFN & "Objects have been removed from the list" )
511 hListPrint( aTwoOnlyList() , "Items found in list TWO only (MISSING)" )
515 gCompare2Lists() = brc
520 '*******************************************************************************
522 function hListPrint( lsList() as string , optional cComment as string ) as integer
524 const CFN = "global::tools::inc::t_list.inc::hListPrint: "
526 '///<h3>Print the content of a list to the log with a heading comment</h3>
529 dim iListItem as integer
531 '///+<li>If no comment is provided we print a qaerrorlog</li>
532 if ( ismissing( cComment ) ) then
533 qaerrorlog( CFN & "Please provide any string as second parameter." )
537 '///+<li>Print a comment if desired</li>
538 if ( cComment <> "" ) then
540 printlog( CFN & cComment )
544 '///+<li>Print all items in the list to the log</li>
545 for iListItem = 1 to listcount( lsList() )
546 printlog( "(" & iListItem & ") : " & lsList( iListItem ) )
549 '///+<li>Return the number of listitems to the calling function</li>
550 hListPrint() = listcount( lsList() )
556 '*******************************************************************************
558 function hListClearPattern( lsList() as string, cPattern as string ) as integer
560 '///<h3>Search a list for the occurrence of a special pattern.</h3>
561 '///+ If the pattern is found, the entries are deleted, the new size of the
562 '///+ array is returned.
564 dim iCurItem as integer
567 do while ( iCurItem <= listcount( lsList() ) )
569 if ( instr( lsList( iCurItem ) , cPattern ) <> 0 ) then
570 listdelete( lsList() , iCurItem )
572 iCurItem = iCurItem + 1
577 hListClearPattern() = listcount( lsList() )
581 '*******************************************************************************
583 function hListClearBlank( lsList() as string ) as integer
585 '///<h3>Search a list for blank lines and remove them.</h3>
587 dim iCurItem as integer
590 do while ( iCurItem <= listcount( lsList() ) )
592 if ( len( lsList( iCurItem ) ) = 0 ) then
593 listdelete( lsList() , iCurItem )
595 iCurItem = iCurItem + 1
600 hListClearBlank() = listcount( lsList() )
604 '*******************************************************************************
606 function hListIntegrityTest( sList() as string ) as boolean
608 const CFN = "global::tools::inc::t_list.inc::hListIntegrityTest:"
610 '///<h3>Verify that listcount( array ) < ubound( array )</h3>
611 ' NOTE: some listfunctions fail if ubound = listcount
613 dim iListCount as integer
614 dim iUbound as integer
616 iListCount = listcount( sList() )
617 iUbound = ubound( sList() )
619 if ( iListCount >= iUbound ) then
620 warnlog( CFN & "ListCount points beyond array boundary" )
621 warnlog( CFN & "ListCount: " & iListCount )
622 warnlog( CFN & "UBOUND...: " & iUbound )
623 hListIntegrityTest() = false
627 hListIntegrityTest() = true