bump product version to 5.0.4.1
[LibreOffice.git] / wizards / source / tools / Strings.xba
blobd05555fd990104ac5121b6a3422aacf1218b9f02
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <!--
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 .
19 -->
20 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Strings" script:language="StarBasic">Option Explicit
21 Public sProductname as String
24 &apos; Deletes out of a String &apos;BigString&apos; all possible PartStrings, that are summed up
25 &apos; in the Array &apos;ElimArray&apos;
26 Function ElimChar(ByVal BigString as String, ElimArray() as String)
27 Dim i% ,n%
28 For i = 0 to Ubound(ElimArray)
29 BigString = DeleteStr(BigString,ElimArray(i))
30 Next
31 ElimChar = BigString
32 End Function
35 &apos; Deletes out of a String &apos;BigString&apos; a possible Partstring &apos;CompString&apos;
36 Function DeleteStr(ByVal BigString,CompString as String) as String
37 Dim i%, CompLen%, BigLen%
38 CompLen = Len(CompString)
39 i = 1
40 While i &lt;&gt; 0
41 i = Instr(i, BigString,CompString)
42 If i &lt;&gt; 0 then
43 BigLen = Len(BigString)
44 BigString = Mid(BigString,1,i-1) + Mid(BigString,i+CompLen,BigLen-i+1-CompLen)
45 End If
46 Wend
47 DeleteStr = BigString
48 End Function
51 &apos; Finds a PartString, that is framed by the Strings &apos;Prestring&apos; and &apos;PostString&apos;
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 &lt;&gt; 0 Then
57 PreLen = Len(PreString)
58 EndPos = Instr(StartPos + PreLen,BigString,PostString)
59 If EndPos &lt;&gt; 0 Then
60 BigLen = Len(BigString)
61 PostLen = Len(PostString)
62 FindPartString = Mid(BigString,StartPos + PreLen, EndPos - (StartPos + PreLen))
63 SearchPos = EndPos + PostLen
64 Else
65 Msgbox(&quot;No final tag for &apos;&quot; &amp; PreString &amp; &quot;&apos; existing&quot;, 16, GetProductName())
66 FindPartString = &quot;&quot;
67 End If
68 Else
69 FindPartString = &quot;&quot;
70 End If
71 End Function
74 &apos; Note iCompare = 0 (Binary comparison)
75 &apos; iCompare = 1 (Text comparison)
76 Function PartStringInArray(BigArray(), SearchString as String, iCompare as Integer) as Integer
77 Dim MaxIndex as Integer
78 Dim i as Integer
79 MaxIndex = Ubound(BigArray())
80 For i = 0 To MaxIndex
81 If Instr(1, BigArray(i), SearchString, iCompare) &lt;&gt; 0 Then
82 PartStringInArray() = i
83 Exit Function
84 End If
85 Next i
86 PartStringInArray() = -1
87 End Function
90 &apos; Deletes the String &apos;SmallString&apos; out of the String &apos;BigString&apos;
91 &apos; in case SmallString&apos;s Position in BigString is right at the end
92 Function RTrimStr(ByVal BigString, SmallString as String) as String
93 Dim SmallLen as Integer
94 Dim BigLen as Integer
95 SmallLen = Len(SmallString)
96 BigLen = Len(BigString)
97 If Instr(1,BigString, SmallString) &lt;&gt; 0 Then
98 If Mid(BigString,BigLen + 1 - SmallLen, SmallLen) = SmallString Then
99 RTrimStr = Mid(BigString,1,BigLen - SmallLen)
100 Else
101 RTrimStr = BigString
102 End If
103 Else
104 RTrimStr = BigString
105 End If
106 End Function
109 &apos; Deletes the Char &apos;CompChar&apos; out of the String &apos;BigString&apos;
110 &apos; in case CompChar&apos;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 &gt; 1 Then
115 If Left(BigString,1) = CompChar then
116 BigString = Mid(BigString,2,BigLen-1)
117 End If
118 ElseIf BigLen = 1 Then
119 BigString = &quot;&quot;
120 End If
121 LTrimChar = BigString
122 End Function
125 &apos; Retrieves an Array out of a String.
126 &apos; The fields of the Array are separated by the parameter &apos;Separator&apos;, that is contained
127 &apos; in the Array
128 &apos; 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
136 End Function
139 &apos; Deletes all fieldvalues in one-dimensional Array
140 Sub ClearArray(BigArray)
141 Dim i as integer
142 For i = Lbound(BigArray()) to Ubound(BigArray())
143 BigArray(i) = &quot;&quot;
144 Next
145 End Sub
148 &apos; Deletes all fieldvalues in a multidimensional Array
149 Sub ClearMultiDimArray(BigArray,DimCount as integer)
150 Dim n%, m%
151 For n = Lbound(BigArray(),1) to Ubound(BigArray(),1)
152 For m = 0 to Dimcount - 1
153 BigArray(n,m) = &quot;&quot;
154 Next m
155 Next n
156 End Sub
159 &apos; Checks if a Field (LocField) is already defined in an Array
160 &apos; Returns &apos;True&apos; or &apos;False&apos;
161 Function FieldInArray(LocArray(), MaxIndex as integer, LocField as String) As Boolean
162 Dim i as integer
163 For i = Lbound(LocArray()) to MaxIndex
164 If Ucase(LocArray(i)) = Ucase(LocField) Then
165 FieldInArray = True
166 Exit Function
167 End if
168 Next
169 FieldInArray = False
170 End Function
173 &apos; Checks if a Field (LocField) is already defined in an Array
174 &apos; Returns &apos;True&apos; or &apos;False&apos;
175 Function FieldInList(LocField, BigList()) As Boolean
176 Dim i as integer
177 For i = Lbound(BigList()) to Ubound(BigList())
178 If LocField = BigList(i) Then
179 FieldInList = True
180 Exit Function
181 End if
182 Next
183 FieldInList = False
184 End Function
187 &apos; Retrieves the Index of the delivered String &apos;SearchString&apos; in
188 &apos; the Array LocList()&apos;
189 Function IndexInArray(SearchString as String, LocList()) as Integer
190 Dim i as integer
191 For i = Lbound(LocList(),1) to Ubound(LocList(),1)
192 If Ucase(LocList(i,0)) = Ucase(SearchString) Then
193 IndexInArray = i
194 Exit Function
195 End if
196 Next
197 IndexInArray = -1
198 End Function
201 Sub MultiArrayInListbox(oDialog as Object, ListboxName as String, ValList(), iDim as Integer)
202 Dim oListbox as Object
203 Dim i as integer
204 Dim a as Integer
205 a = 0
206 oListbox = oDialog.GetControl(ListboxName)
207 oListbox.RemoveItems(0, oListbox.GetItemCount)
208 For i = 0 to Ubound(ValList(), 1)
209 If ValList(i) &lt;&gt; &quot;&quot; Then
210 oListbox.AddItem(ValList(i, iDim-1), a)
211 a = a + 1
212 End If
213 Next
214 End Sub
217 &apos; Searches for a String in a two-dimensional Array by querying all Searchindexex of the second dimension
218 &apos; 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
220 Dim i as integer
221 Dim CurFieldString as String
222 If IsMissing(MaxIndex) Then
223 MaxIndex = Ubound(SearchList(),1)
224 End If
225 For i = Lbound(SearchList()) to MaxIndex
226 CurFieldString = SearchList(i,SearchIndex)
227 If Ucase(CurFieldString) = Ucase(SearchString) Then
228 StringInMultiArray() = SearchList(i,ReturnIndex)
229 Exit Function
230 End if
231 Next
232 StringInMultiArray() = &quot;&quot;
233 End Function
236 &apos; Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
237 &apos; and delivers the Index where it is found.
238 Function GetIndexInMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
239 Dim i as integer
240 Dim MaxIndex as Integer
241 Dim CurFieldValue
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
247 Exit Function
248 End if
249 Next
250 GetIndexInMultiArray() = -1
251 End Function
254 &apos; Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
255 &apos; and delivers the Index where the Searchvalue is found as a part string
256 Function GetIndexForPartStringinMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
257 Dim i as integer
258 Dim MaxIndex as Integer
259 Dim CurFieldValue
260 MaxIndex = Ubound(SearchList(),1)
261 For i = Lbound(SearchList()) to MaxIndex
262 CurFieldValue = SearchList(i,SearchIndex)
263 If Instr(CurFieldValue, SearchValue) &gt; 0 Then
264 GetIndexForPartStringinMultiArray() = i
265 Exit Function
266 End if
267 Next
268 GetIndexForPartStringinMultiArray = -1
269 End Function
272 Function ArrayfromMultiArray(MultiArray as String, iDim as Integer)
273 Dim MaxIndex as Integer
274 Dim i as Integer
275 MaxIndex = Ubound(MultiArray())
276 Dim ResultArray(MaxIndex) as String
277 For i = 0 To MaxIndex
278 ResultArray(i) = MultiArray(i,iDim)
279 Next i
280 ArrayfromMultiArray() = ResultArray()
281 End Function
284 &apos; Replaces the string &quot;OldReplace&quot; through the String &quot;NewReplace&quot; in the String
285 &apos; &apos;BigString&apos;
286 Function ReplaceString(ByVal Bigstring, NewReplace, OldReplace as String) as String
287 ReplaceString=join(split(BigString,OldReplace),NewReplace)
288 End Function
291 &apos; Retrieves the second value for a next to &apos;SearchString&apos; in
292 &apos; a two-dimensional string-Array
293 Function FindSecondValue(SearchString as String, TwoDimList() as String ) as String
294 Dim i as Integer
295 For i = 0 To Ubound(TwoDimList,1)
296 If Ucase(SearchString) = Ucase(TwoDimList(i,0)) Then
297 FindSecondValue = TwoDimList(i,1)
298 Exit For
299 End If
300 Next
301 End Function
304 &apos; raises a base to a certain power
305 Function Power(Basis as Double, Exponent as Double) as Double
306 Power = Exp(Exponent*Log(Basis))
307 End Function
310 &apos; 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
317 End Function
320 &apos;Retrieves the mere filename out of a whole path
321 Function FileNameoutofPath(ByVal Path as String, Optional Separator as String) as String
322 Dim i as Integer
323 Dim SepList() as String
324 If IsMissing(Separator) Then
325 Path = ConvertFromUrl(Path)
326 Separator = GetPathSeparator()
327 End If
328 SepList() = ArrayoutofString(Path, Separator,i)
329 FileNameoutofPath = SepList(i)
330 End Function
333 Function GetFileNameExtension(ByVal FileName as String)
334 Dim MaxIndex as Integer
335 Dim SepList() as String
336 SepList() = ArrayoutofString(FileName,&quot;.&quot;, MaxIndex)
337 GetFileNameExtension = SepList(MaxIndex)
338 End Function
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)
346 End If
347 SepList() = ArrayoutofString(FileName,&quot;.&quot;, MaxIndex)
348 GetFileNameWithoutExtension = RTrimStr(FileName, &quot;.&quot; &amp; SepList(MaxIndex)
349 End Function
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 &amp; LocFileName)
356 End Function
359 Function CountCharsInString(BigString, LocChar as String, ByVal StartPos as Integer) as Integer
360 Dim LocCount%, LocPos%
361 LocCount = 0
363 LocPos = Instr(StartPos,BigString,LocChar)
364 If LocPos &lt;&gt; 0 Then
365 LocCount = LocCount + 1
366 StartPos = LocPos+1
367 End If
368 Loop until LocPos = 0
369 CountCharsInString = LocCount
370 End Function
373 Function BubbleSortList(ByVal SortList(),optional sort2ndValue as Boolean)
374 &apos;This function bubble sorts an array of maximum 2 dimensions.
375 &apos;The default sorting order is the first dimension
376 &apos;Only if sort2ndValue is True the second dimension is the relevant for the sorting order
377 Dim s as Integer
378 Dim t as Integer
379 Dim i as Integer
380 Dim k as Integer
381 Dim dimensions as Integer
382 Dim sortvalue as Integer
383 Dim DisplayDummy
384 dimensions = 2
386 On Local Error Goto No2ndDim
387 k = Ubound(SortList(),2)
388 No2ndDim:
389 If Err &lt;&gt; 0 Then dimensions = 1
391 i = Ubound(SortList(),1)
392 If ismissing(sort2ndValue) then
393 sortvalue = 0
394 else
395 sortvalue = 1
396 end if
398 For s = 1 to i - 1
399 For t = 0 to i-s
400 Select Case dimensions
401 Case 1
402 If SortList(t) &gt; SortList(t+1) Then
403 DisplayDummy = SortList(t)
404 SortList(t) = SortList(t+1)
405 SortList(t+1) = DisplayDummy
406 End If
407 Case 2
408 If SortList(t,sortvalue) &gt; 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
413 Next k
414 End If
415 End Select
416 Next t
417 Next s
418 BubbleSortList = SortList()
419 End Function
422 Function GetValueoutofList(SearchValue, BigList(), iDim as Integer, Optional ValueIndex)
423 Dim i as Integer
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
429 ValueIndex = i
430 End If
431 GetValueOutOfList() = BigList(i,iDim)
432 End If
433 Next i
434 End Function
437 Function AddListtoList(ByVal FirstArray(), ByVal SecondArray(), Optional StartIndex)
438 Dim n as Integer
439 Dim m as Integer
440 Dim MaxIndex as Integer
441 MaxIndex = Ubound(FirstArray()) + Ubound(SecondArray()) + 1
442 If MaxIndex &gt; -1 Then
443 Dim ResultArray(MaxIndex)
444 For m = 0 To Ubound(FirstArray())
445 ResultArray(m) = FirstArray(m)
446 Next m
447 For n = 0 To Ubound(SecondArray())
448 ResultArray(m) = SecondArray(n)
449 m = m + 1
450 Next n
451 AddListToList() = ResultArray()
452 Else
453 Dim NullArray()
454 AddListToList() = NullArray()
455 End If
456 End Function
459 Function CheckDouble(DoubleString as String)
460 On Local Error Goto WRONGDATATYPE
461 CheckDouble() = CDbl(DoubleString)
462 WRONGDATATYPE:
463 If Err &lt;&gt; 0 Then
464 CheckDouble() = 0
465 Resume NoErr:
466 End If
467 NOERR:
468 End Function
469 </script:module>