Strings.xba 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.
  5. *
  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/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  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
  22. &apos; Deletes out of a String &apos;BigString&apos; all possible PartStrings, that are summed up
  23. &apos; in the Array &apos;ElimArray&apos;
  24. Function ElimChar(ByVal BigString as String, ElimArray() as String)
  25. Dim i% ,n%
  26. For i = 0 to Ubound(ElimArray)
  27. BigString = DeleteStr(BigString,ElimArray(i))
  28. Next
  29. ElimChar = BigString
  30. End Function
  31. &apos; Deletes out of a String &apos;BigString&apos; a possible Partstring &apos;CompString&apos;
  32. Function DeleteStr(ByVal BigString,CompString as String) as String
  33. Dim i%, CompLen%, BigLen%
  34. CompLen = Len(CompString)
  35. i = 1
  36. While i &lt;&gt; 0
  37. i = Instr(i, BigString,CompString)
  38. If i &lt;&gt; 0 then
  39. BigLen = Len(BigString)
  40. BigString = Mid(BigString,1,i-1) + Mid(BigString,i+CompLen,BigLen-i+1-CompLen)
  41. End If
  42. Wend
  43. DeleteStr = BigString
  44. End Function
  45. &apos; Finds a PartString, that is framed by the Strings &apos;Prestring&apos; and &apos;PostString&apos;
  46. Function FindPartString(BigString, PreString, PostString as String, SearchPos as Integer) as String
  47. Dim StartPos%, EndPos%
  48. Dim BigLen%, PreLen%, PostLen%
  49. StartPos = Instr(SearchPos,BigString,PreString)
  50. If StartPos &lt;&gt; 0 Then
  51. PreLen = Len(PreString)
  52. EndPos = Instr(StartPos + PreLen,BigString,PostString)
  53. If EndPos &lt;&gt; 0 Then
  54. BigLen = Len(BigString)
  55. PostLen = Len(PostString)
  56. FindPartString = Mid(BigString,StartPos + PreLen, EndPos - (StartPos + PreLen))
  57. SearchPos = EndPos + PostLen
  58. Else
  59. Msgbox(&quot;No final tag for &apos;&quot; &amp; PreString &amp; &quot;&apos; existing&quot;, 16, GetProductName())
  60. FindPartString = &quot;&quot;
  61. End If
  62. Else
  63. FindPartString = &quot;&quot;
  64. End If
  65. End Function
  66. &apos; Note iCompare = 0 (Binary comparison)
  67. &apos; iCompare = 1 (Text comparison)
  68. Function PartStringInArray(BigArray(), SearchString as String, iCompare as Integer) as Integer
  69. Dim MaxIndex as Integer
  70. Dim i as Integer
  71. MaxIndex = Ubound(BigArray())
  72. For i = 0 To MaxIndex
  73. If Instr(1, BigArray(i), SearchString, iCompare) &lt;&gt; 0 Then
  74. PartStringInArray() = i
  75. Exit Function
  76. End If
  77. Next i
  78. PartStringInArray() = -1
  79. End Function
  80. &apos; Deletes the String &apos;SmallString&apos; out of the String &apos;BigString&apos;
  81. &apos; in case SmallString&apos;s Position in BigString is right at the end
  82. Function RTrimStr(ByVal BigString, SmallString as String) as String
  83. Dim SmallLen as Integer
  84. Dim BigLen as Integer
  85. SmallLen = Len(SmallString)
  86. BigLen = Len(BigString)
  87. If Instr(1,BigString, SmallString) &lt;&gt; 0 Then
  88. If Mid(BigString,BigLen + 1 - SmallLen, SmallLen) = SmallString Then
  89. RTrimStr = Mid(BigString,1,BigLen - SmallLen)
  90. Else
  91. RTrimStr = BigString
  92. End If
  93. Else
  94. RTrimStr = BigString
  95. End If
  96. End Function
  97. &apos; Deletes the Char &apos;CompChar&apos; out of the String &apos;BigString&apos;
  98. &apos; in case CompChar&apos;s Position in BigString is right at the beginning
  99. Function LTRimChar(ByVal BigString as String,CompChar as String) as String
  100. Dim BigLen as integer
  101. BigLen = Len(BigString)
  102. If BigLen &gt; 1 Then
  103. If Left(BigString,1) = CompChar then
  104. BigString = Mid(BigString,2,BigLen-1)
  105. End If
  106. ElseIf BigLen = 1 Then
  107. BigString = &quot;&quot;
  108. End If
  109. LTrimChar = BigString
  110. End Function
  111. &apos; Retrieves an Array out of a String.
  112. &apos; The fields of the Array are separated by the parameter &apos;Separator&apos;, that is contained
  113. &apos; in the Array
  114. &apos; The Array MaxIndex delivers the highest Index of this Array
  115. Function ArrayOutOfString(BigString, Separator as String, Optional MaxIndex as Integer)
  116. Dim LocList() as String
  117. LocList=Split(BigString,Separator)
  118. If not isMissing(MaxIndex) then maxIndex=ubound(LocList())
  119. ArrayOutOfString=LocList
  120. End Function
  121. &apos; Deletes all fieldvalues in one-dimensional Array
  122. Sub ClearArray(BigArray)
  123. Dim i as integer
  124. For i = Lbound(BigArray()) to Ubound(BigArray())
  125. BigArray(i) = &quot;&quot;
  126. Next
  127. End Sub
  128. &apos; Deletes all fieldvalues in a multidimensional Array
  129. Sub ClearMultiDimArray(BigArray,DimCount as integer)
  130. Dim n%, m%
  131. For n = Lbound(BigArray(),1) to Ubound(BigArray(),1)
  132. For m = 0 to Dimcount - 1
  133. BigArray(n,m) = &quot;&quot;
  134. Next m
  135. Next n
  136. End Sub
  137. &apos; Checks if a Field (LocField) is already defined in an Array
  138. &apos; Returns &apos;True&apos; or &apos;False&apos;
  139. Function FieldInArray(LocArray(), MaxIndex as integer, LocField as String) As Boolean
  140. Dim i as integer
  141. For i = Lbound(LocArray()) to MaxIndex
  142. If UCase(LocArray(i)) = UCase(LocField) Then
  143. FieldInArray = True
  144. Exit Function
  145. End if
  146. Next
  147. FieldInArray = False
  148. End Function
  149. &apos; Checks if a Field (LocField) is already defined in an Array
  150. &apos; Returns &apos;True&apos; or &apos;False&apos;
  151. Function FieldInList(LocField, BigList()) As Boolean
  152. Dim i as integer
  153. For i = Lbound(BigList()) to Ubound(BigList())
  154. If LocField = BigList(i) Then
  155. FieldInList = True
  156. Exit Function
  157. End if
  158. Next
  159. FieldInList = False
  160. End Function
  161. &apos; Retrieves the Index of the delivered String &apos;SearchString&apos; in
  162. &apos; the Array LocList()&apos;
  163. Function IndexInArray(SearchString as String, LocList()) as Integer
  164. Dim i as integer
  165. For i = Lbound(LocList(),1) to Ubound(LocList(),1)
  166. If UCase(LocList(i,0)) = UCase(SearchString) Then
  167. IndexInArray = i
  168. Exit Function
  169. End if
  170. Next
  171. IndexInArray = -1
  172. End Function
  173. Sub MultiArrayInListbox(oDialog as Object, ListboxName as String, ValList(), iDim as Integer)
  174. Dim oListbox as Object
  175. Dim i as integer
  176. Dim a as Integer
  177. a = 0
  178. oListbox = oDialog.GetControl(ListboxName)
  179. oListbox.RemoveItems(0, oListbox.GetItemCount)
  180. For i = 0 to Ubound(ValList(), 1)
  181. If ValList(i) &lt;&gt; &quot;&quot; Then
  182. oListbox.AddItem(ValList(i, iDim-1), a)
  183. a = a + 1
  184. End If
  185. Next
  186. End Sub
  187. &apos; Searches for a String in a two-dimensional Array by querying all Searchindexes of the second dimension
  188. &apos; and delivers the specific String of the ReturnIndex in the second dimension of the Searchlist()
  189. Function StringInMultiArray(SearchList(), SearchString as String, SearchIndex as Integer, ReturnIndex as Integer, Optional MaxIndex as Integer) as String
  190. Dim i as integer
  191. Dim CurFieldString as String
  192. If IsMissing(MaxIndex) Then
  193. MaxIndex = Ubound(SearchList(),1)
  194. End If
  195. For i = Lbound(SearchList()) to MaxIndex
  196. CurFieldString = SearchList(i,SearchIndex)
  197. If UCase(CurFieldString) = UCase(SearchString) Then
  198. StringInMultiArray() = SearchList(i,ReturnIndex)
  199. Exit Function
  200. End if
  201. Next
  202. StringInMultiArray() = &quot;&quot;
  203. End Function
  204. &apos; Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
  205. &apos; and delivers the Index where it is found.
  206. Function GetIndexInMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
  207. Dim i as integer
  208. Dim MaxIndex as Integer
  209. Dim CurFieldValue
  210. MaxIndex = Ubound(SearchList(),1)
  211. For i = Lbound(SearchList()) to MaxIndex
  212. CurFieldValue = SearchList(i,SearchIndex)
  213. If CurFieldValue = SearchValue Then
  214. GetIndexInMultiArray() = i
  215. Exit Function
  216. End if
  217. Next
  218. GetIndexInMultiArray() = -1
  219. End Function
  220. &apos; Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension
  221. &apos; and delivers the Index where the Searchvalue is found as a part string
  222. Function GetIndexForPartStringinMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
  223. Dim i as integer
  224. Dim MaxIndex as Integer
  225. Dim CurFieldValue
  226. MaxIndex = Ubound(SearchList(),1)
  227. For i = Lbound(SearchList()) to MaxIndex
  228. CurFieldValue = SearchList(i,SearchIndex)
  229. If Instr(CurFieldValue, SearchValue) &gt; 0 Then
  230. GetIndexForPartStringinMultiArray() = i
  231. Exit Function
  232. End if
  233. Next
  234. GetIndexForPartStringinMultiArray = -1
  235. End Function
  236. Function ArrayfromMultiArray(MultiArray as String, iDim as Integer)
  237. Dim MaxIndex as Integer
  238. Dim i as Integer
  239. MaxIndex = Ubound(MultiArray())
  240. Dim ResultArray(MaxIndex) as String
  241. For i = 0 To MaxIndex
  242. ResultArray(i) = MultiArray(i,iDim)
  243. Next i
  244. ArrayfromMultiArray() = ResultArray()
  245. End Function
  246. &apos; Replaces the string &quot;OldReplace&quot; through the String &quot;NewReplace&quot; in the String
  247. &apos; &apos;BigString&apos;
  248. Function ReplaceString(ByVal Bigstring, NewReplace, OldReplace as String) as String
  249. ReplaceString=join(split(BigString,OldReplace),NewReplace)
  250. End Function
  251. &apos; Retrieves the second value for a next to &apos;SearchString&apos; in
  252. &apos; a two-dimensional string-Array
  253. Function FindSecondValue(SearchString as String, TwoDimList() as String ) as String
  254. Dim i as Integer
  255. For i = 0 To Ubound(TwoDimList,1)
  256. If UCase(SearchString) = UCase(TwoDimList(i,0)) Then
  257. FindSecondValue = TwoDimList(i,1)
  258. Exit For
  259. End If
  260. Next
  261. End Function
  262. &apos; raises a base to a certain power
  263. Function Power(Basis as Double, Exponent as Double) as Double
  264. Power = Exp(Exponent*Log(Basis))
  265. End Function
  266. &apos; rounds a Real to a given Number of Decimals
  267. Function Round(BaseValue as Double, Decimals as Integer) as Double
  268. Dim Multiplicator as Long
  269. Dim DblValue#, RoundValue#
  270. Multiplicator = Power(10,Decimals)
  271. RoundValue = Int(BaseValue * Multiplicator)
  272. Round = RoundValue/Multiplicator
  273. End Function
  274. &apos;Retrieves the mere filename out of a whole path
  275. Function FileNameoutofPath(ByVal Path as String, Optional Separator as String) as String
  276. Dim i as Integer
  277. Dim SepList() as String
  278. If IsMissing(Separator) Then
  279. Path = ConvertFromUrl(Path)
  280. Separator = GetPathSeparator()
  281. End If
  282. SepList() = ArrayoutofString(Path, Separator,i)
  283. FileNameoutofPath = SepList(i)
  284. End Function
  285. Function GetFileNameExtension(ByVal FileName as String)
  286. Dim MaxIndex as Integer
  287. Dim SepList() as String
  288. SepList() = ArrayoutofString(FileName,&quot;.&quot;, MaxIndex)
  289. GetFileNameExtension = SepList(MaxIndex)
  290. End Function
  291. Function GetFileNameWithoutExtension(ByVal FileName as String, Optional Separator as String)
  292. Dim MaxIndex as Integer
  293. Dim SepList() as String
  294. If not IsMissing(Separator) Then
  295. FileName = FileNameoutofPath(FileName, Separator)
  296. End If
  297. SepList() = ArrayoutofString(FileName,&quot;.&quot;, MaxIndex)
  298. GetFileNameWithoutExtension = RTrimStr(FileName, &quot;.&quot; &amp; SepList(MaxIndex))
  299. End Function
  300. Function DirectoryNameoutofPath(sPath as String, Separator as String) as String
  301. Dim LocFileName as String
  302. LocFileName = FileNameoutofPath(sPath, Separator)
  303. DirectoryNameoutofPath = RTrimStr(sPath, Separator &amp; LocFileName)
  304. End Function
  305. Function CountCharsInString(BigString, LocChar as String, ByVal StartPos as Integer) as Integer
  306. Dim LocCount%, LocPos%
  307. LocCount = 0
  308. Do
  309. LocPos = Instr(StartPos,BigString,LocChar)
  310. If LocPos &lt;&gt; 0 Then
  311. LocCount = LocCount + 1
  312. StartPos = LocPos+1
  313. End If
  314. Loop until LocPos = 0
  315. CountCharsInString = LocCount
  316. End Function
  317. Function BubbleSortList(ByVal SortList(),optional sort2ndValue as Boolean)
  318. &apos;This function bubble sorts an array of maximum 2 dimensions.
  319. &apos;The default sorting order is the first dimension
  320. &apos;Only if sort2ndValue is True the second dimension is the relevant for the sorting order
  321. Dim s as Integer
  322. Dim t as Integer
  323. Dim i as Integer
  324. Dim k as Integer
  325. Dim dimensions as Integer
  326. Dim sortvalue as Integer
  327. Dim DisplayDummy
  328. dimensions = 2
  329. On Local Error Goto No2ndDim
  330. k = Ubound(SortList(),2)
  331. No2ndDim:
  332. If Err &lt;&gt; 0 Then dimensions = 1
  333. i = Ubound(SortList(),1)
  334. If ismissing(sort2ndValue) then
  335. sortvalue = 0
  336. else
  337. sortvalue = 1
  338. end if
  339. For s = 1 to i - 1
  340. For t = 0 to i-s
  341. Select Case dimensions
  342. Case 1
  343. If SortList(t) &gt; SortList(t+1) Then
  344. DisplayDummy = SortList(t)
  345. SortList(t) = SortList(t+1)
  346. SortList(t+1) = DisplayDummy
  347. End If
  348. Case 2
  349. If SortList(t,sortvalue) &gt; SortList(t+1,sortvalue) Then
  350. For k = 0 to UBound(SortList(),2)
  351. DisplayDummy = SortList(t,k)
  352. SortList(t,k) = SortList(t+1,k)
  353. SortList(t+1,k) = DisplayDummy
  354. Next k
  355. End If
  356. End Select
  357. Next t
  358. Next s
  359. BubbleSortList = SortList()
  360. End Function
  361. Function GetValueoutofList(SearchValue, BigList(), iDim as Integer, Optional ValueIndex)
  362. Dim i as Integer
  363. Dim MaxIndex as Integer
  364. MaxIndex = Ubound(BigList(),1)
  365. For i = 0 To MaxIndex
  366. If BigList(i,0) = SearchValue Then
  367. If Not IsMissing(ValueIndex) Then
  368. ValueIndex = i
  369. End If
  370. GetValueOutOfList() = BigList(i,iDim)
  371. End If
  372. Next i
  373. End Function
  374. Function AddListtoList(ByVal FirstArray(), ByVal SecondArray(), Optional StartIndex)
  375. Dim n as Integer
  376. Dim m as Integer
  377. Dim MaxIndex as Integer
  378. MaxIndex = Ubound(FirstArray()) + Ubound(SecondArray()) + 1
  379. If MaxIndex &gt; -1 Then
  380. Dim ResultArray(MaxIndex)
  381. For m = 0 To Ubound(FirstArray())
  382. ResultArray(m) = FirstArray(m)
  383. Next m
  384. For n = 0 To Ubound(SecondArray())
  385. ResultArray(m) = SecondArray(n)
  386. m = m + 1
  387. Next n
  388. AddListToList() = ResultArray()
  389. Else
  390. Dim NullArray()
  391. AddListToList() = NullArray()
  392. End If
  393. End Function
  394. Function CheckDouble(DoubleString as String)
  395. On Local Error Goto WRONGDATATYPE
  396. CheckDouble() = CDbl(DoubleString)
  397. WRONGDATATYPE:
  398. If Err &lt;&gt; 0 Then
  399. CheckDouble() = 0
  400. Resume NoErr:
  401. End If
  402. NOERR:
  403. End Function
  404. </script:module>