1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Strings" script:
language=
"StarBasic">Option Explicit
21 Public sProductname as String
24 ' Deletes out of a String
'BigString
' all possible PartStrings, that are summed up
25 ' in the Array
'ElimArray
'
26 Function ElimChar(ByVal BigString as String, ElimArray() as String)
28 For i =
0 to Ubound(ElimArray)
29 BigString = DeleteStr(BigString,ElimArray(i))
35 ' Deletes out of a String
'BigString
' a possible Partstring
'CompString
'
36 Function DeleteStr(ByVal BigString,CompString as String) as String
37 Dim i%, CompLen%, BigLen%
38 CompLen = Len(CompString)
41 i = Instr(i, BigString,CompString)
43 BigLen = Len(BigString)
44 BigString = Mid(BigString,
1,i-
1) + Mid(BigString,i+CompLen,BigLen-i+
1-CompLen)
51 ' Finds a PartString, that is framed by the Strings
'Prestring
' and
'PostString
'
52 Function FindPartString(BigString, PreString, PostString as String, SearchPos as Integer) as String
53 Dim StartPos%, EndPos%
54 Dim BigLen%, PreLen%, PostLen%
55 StartPos = Instr(SearchPos,BigString,PreString)
56 If StartPos
<> 0 Then
57 PreLen = Len(PreString)
58 EndPos = Instr(StartPos + PreLen,BigString,PostString)
59 If EndPos
<> 0 Then
60 BigLen = Len(BigString)
61 PostLen = Len(PostString)
62 FindPartString = Mid(BigString,StartPos + PreLen, EndPos - (StartPos + PreLen))
63 SearchPos = EndPos + PostLen
65 Msgbox(
"No final tag for
'" & PreString
& "' existing
",
16, GetProductName())
66 FindPartString =
""
69 FindPartString =
""
74 ' Note iCompare =
0 (Binary comparison)
75 ' iCompare =
1 (Text comparison)
76 Function PartStringInArray(BigArray(), SearchString as String, iCompare as Integer) as Integer
77 Dim MaxIndex as Integer
79 MaxIndex = Ubound(BigArray())
81 If Instr(
1, BigArray(i), SearchString, iCompare)
<> 0 Then
82 PartStringInArray() = i
86 PartStringInArray() = -
1
90 ' Deletes the String
'SmallString
' out of the String
'BigString
'
91 ' in case SmallString
's Position in BigString is right at the end
92 Function RTrimStr(ByVal BigString, SmallString as String) as String
93 Dim SmallLen as Integer
95 SmallLen = Len(SmallString)
96 BigLen = Len(BigString)
97 If Instr(
1,BigString, SmallString)
<> 0 Then
98 If Mid(BigString,BigLen +
1 - SmallLen, SmallLen) = SmallString Then
99 RTrimStr = Mid(BigString,
1,BigLen - SmallLen)
109 ' Deletes the Char
'CompChar
' out of the String
'BigString
'
110 ' in case CompChar
's Position in BigString is right at the beginning
111 Function LTRimChar(ByVal BigString as String,CompChar as String) as String
112 Dim BigLen as integer
113 BigLen = Len(BigString)
114 If BigLen
> 1 Then
115 If Left(BigString,
1) = CompChar then
116 BigString = Mid(BigString,
2,BigLen-
1)
118 ElseIf BigLen =
1 Then
119 BigString =
""
121 LTrimChar = BigString
125 ' Retrieves an Array out of a String.
126 ' The fields of the Array are separated by the parameter
'Separator
', that is contained
128 ' The Array MaxIndex delivers the highest Index of this Array
129 Function ArrayOutOfString(BigString, Separator as String, Optional MaxIndex as Integer)
130 Dim LocList() as String
131 LocList=Split(BigString,Separator)
133 If not isMissing(MaxIndex) then maxIndex=ubound(LocList())
135 ArrayOutOfString=LocList
139 ' Deletes all fieldvalues in one-dimensional Array
140 Sub ClearArray(BigArray)
142 For i = Lbound(BigArray()) to Ubound(BigArray())
143 BigArray(i) =
""
148 ' Deletes all fieldvalues in a multidimensional Array
149 Sub ClearMultiDimArray(BigArray,DimCount as integer)
151 For n = Lbound(BigArray(),
1) to Ubound(BigArray(),
1)
152 For m =
0 to Dimcount -
1
153 BigArray(n,m) =
""
159 ' Checks if a Field (LocField) is already defined in an Array
160 ' Returns
'True
' or
'False
'
161 Function FieldInArray(LocArray(), MaxIndex as integer, LocField as String) As Boolean
163 For i = Lbound(LocArray()) to MaxIndex
164 If Ucase(LocArray(i)) = Ucase(LocField) Then
173 ' Checks if a Field (LocField) is already defined in an Array
174 ' Returns
'True
' or
'False
'
175 Function FieldInList(LocField, BigList()) As Boolean
177 For i = Lbound(BigList()) to Ubound(BigList())
178 If LocField = BigList(i) Then
187 ' Retrieves the Index of the delivered String
'SearchString
' in
188 ' the Array LocList()
'
189 Function IndexInArray(SearchString as String, LocList()) as Integer
191 For i = Lbound(LocList(),
1) to Ubound(LocList(),
1)
192 If Ucase(LocList(i,
0)) = Ucase(SearchString) Then
201 Sub MultiArrayInListbox(oDialog as Object, ListboxName as String, ValList(), iDim as Integer)
202 Dim oListbox as Object
206 oListbox = oDialog.GetControl(ListboxName)
207 oListbox.RemoveItems(
0, oListbox.GetItemCount)
208 For i =
0 to Ubound(ValList(),
1)
209 If ValList(i)
<> "" Then
210 oListbox.AddItem(ValList(i, iDim-
1), a)
217 ' Searches for a String in a two-dimensional Array by querying all Searchindexex of the second dimension
218 ' and delivers the specific String of the ReturnIndex in the second dimension of the Searchlist()
219 Function StringInMultiArray(SearchList(), SearchString as String, SearchIndex as Integer, ReturnIndex as Integer, Optional MaxIndex as Integer) as String
221 Dim CurFieldString as String
222 If IsMissing(MaxIndex) Then
223 MaxIndex = Ubound(SearchList(),
1)
225 For i = Lbound(SearchList()) to MaxIndex
226 CurFieldString = SearchList(i,SearchIndex)
227 If Ucase(CurFieldString) = Ucase(SearchString) Then
228 StringInMultiArray() = SearchList(i,ReturnIndex)
232 StringInMultiArray() =
""
236 ' Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
237 ' and delivers the Index where it is found.
238 Function GetIndexInMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
240 Dim MaxIndex as Integer
242 MaxIndex = Ubound(SearchList(),
1)
243 For i = Lbound(SearchList()) to MaxIndex
244 CurFieldValue = SearchList(i,SearchIndex)
245 If CurFieldValue = SearchValue Then
246 GetIndexInMultiArray() = i
250 GetIndexInMultiArray() = -
1
254 ' Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
255 ' and delivers the Index where the Searchvalue is found as a part string
256 Function GetIndexForPartStringinMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
258 Dim MaxIndex as Integer
260 MaxIndex = Ubound(SearchList(),
1)
261 For i = Lbound(SearchList()) to MaxIndex
262 CurFieldValue = SearchList(i,SearchIndex)
263 If Instr(CurFieldValue, SearchValue)
> 0 Then
264 GetIndexForPartStringinMultiArray() = i
268 GetIndexForPartStringinMultiArray = -
1
272 Function ArrayfromMultiArray(MultiArray as String, iDim as Integer)
273 Dim MaxIndex as Integer
275 MaxIndex = Ubound(MultiArray())
276 Dim ResultArray(MaxIndex) as String
277 For i =
0 To MaxIndex
278 ResultArray(i) = MultiArray(i,iDim)
280 ArrayfromMultiArray() = ResultArray()
284 ' Replaces the string
"OldReplace
" through the String
"NewReplace
" in the String
285 ' 'BigString
'
286 Function ReplaceString(ByVal Bigstring, NewReplace, OldReplace as String) as String
287 ReplaceString=join(split(BigString,OldReplace),NewReplace)
291 ' Retrieves the second value for a next to
'SearchString
' in
292 ' a two-dimensional string-Array
293 Function FindSecondValue(SearchString as String, TwoDimList() as String ) as String
295 For i =
0 To Ubound(TwoDimList,
1)
296 If Ucase(SearchString) = Ucase(TwoDimList(i,
0)) Then
297 FindSecondValue = TwoDimList(i,
1)
304 ' raises a base to a certain power
305 Function Power(Basis as Double, Exponent as Double) as Double
306 Power = Exp(Exponent*Log(Basis))
310 ' rounds a Real to a given Number of Decimals
311 Function Round(BaseValue as Double, Decimals as Integer) as Double
312 Dim Multiplicator as Long
313 Dim DblValue#, RoundValue#
314 Multiplicator = Power(
10,Decimals)
315 RoundValue = Int(BaseValue * Multiplicator)
316 Round = RoundValue/Multiplicator
320 'Retrieves the mere filename out of a whole path
321 Function FileNameoutofPath(ByVal Path as String, Optional Separator as String) as String
323 Dim SepList() as String
324 If IsMissing(Separator) Then
325 Path = ConvertFromUrl(Path)
326 Separator = GetPathSeparator()
328 SepList() = ArrayoutofString(Path, Separator,i)
329 FileNameoutofPath = SepList(i)
333 Function GetFileNameExtension(ByVal FileName as String)
334 Dim MaxIndex as Integer
335 Dim SepList() as String
336 SepList() = ArrayoutofString(FileName,
".
", MaxIndex)
337 GetFileNameExtension = SepList(MaxIndex)
341 Function GetFileNameWithoutExtension(ByVal FileName as String, Optional Separator as String)
342 Dim MaxIndex as Integer
343 Dim SepList() as String
344 If not IsMissing(Separator) Then
345 FileName = FileNameoutofPath(FileName, Separator)
347 SepList() = ArrayoutofString(FileName,
".
", MaxIndex)
348 GetFileNameWithoutExtension = RTrimStr(FileName,
".
" & SepList(MaxIndex)
352 Function DirectoryNameoutofPath(sPath as String, Separator as String) as String
353 Dim LocFileName as String
354 LocFileName = FileNameoutofPath(sPath, Separator)
355 DirectoryNameoutofPath = RTrimStr(sPath, Separator
& LocFileName)
359 Function CountCharsInString(BigString, LocChar as String, ByVal StartPos as Integer) as Integer
360 Dim LocCount%, LocPos%
363 LocPos = Instr(StartPos,BigString,LocChar)
364 If LocPos
<> 0 Then
365 LocCount = LocCount +
1
368 Loop until LocPos =
0
369 CountCharsInString = LocCount
373 Function BubbleSortList(ByVal SortList(),optional sort2ndValue as Boolean)
374 'This function bubble sorts an array of maximum
2 dimensions.
375 'The default sorting order is the first dimension
376 'Only if sort2ndValue is True the second dimension is the relevant for the sorting order
381 Dim dimensions as Integer
382 Dim sortvalue as Integer
386 On Local Error Goto No2ndDim
387 k = Ubound(SortList(),
2)
389 If Err
<> 0 Then dimensions =
1
391 i = Ubound(SortList(),
1)
392 If ismissing(sort2ndValue) then
400 Select Case dimensions
402 If SortList(t)
> SortList(t+
1) Then
403 DisplayDummy = SortList(t)
404 SortList(t) = SortList(t+
1)
405 SortList(t+
1) = DisplayDummy
408 If SortList(t,sortvalue)
> SortList(t+
1,sortvalue) Then
409 For k =
0 to UBound(SortList(),
2)
410 DisplayDummy = SortList(t,k)
411 SortList(t,k) = SortList(t+
1,k)
412 SortList(t+
1,k) = DisplayDummy
418 BubbleSortList = SortList()
422 Function GetValueoutofList(SearchValue, BigList(), iDim as Integer, Optional ValueIndex)
424 Dim MaxIndex as Integer
425 MaxIndex = Ubound(BigList(),
1)
426 For i =
0 To MaxIndex
427 If BigList(i,
0) = SearchValue Then
428 If Not IsMissing(ValueIndex) Then
431 GetValueOutOfList() = BigList(i,iDim)
437 Function AddListtoList(ByVal FirstArray(), ByVal SecondArray(), Optional StartIndex)
440 Dim MaxIndex as Integer
441 MaxIndex = Ubound(FirstArray()) + Ubound(SecondArray()) +
1
442 If MaxIndex
> -
1 Then
443 Dim ResultArray(MaxIndex)
444 For m =
0 To Ubound(FirstArray())
445 ResultArray(m) = FirstArray(m)
447 For n =
0 To Ubound(SecondArray())
448 ResultArray(m) = SecondArray(n)
451 AddListToList() = ResultArray()
454 AddListToList() = NullArray()
459 Function CheckDouble(DoubleString as String)
460 On Local Error Goto WRONGDATATYPE
461 CheckDouble() = CDbl(DoubleString)
463 If Err
<> 0 Then