home *** CD-ROM | disk | FTP | other *** search
/ com!online 2001 December / COMCD1201.iso / openoffice / f_0179 / Strings.xba < prev   
Encoding:
Extensible Markup Language  |  2001-08-13  |  14.9 KB  |  490 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Strings" script:language="StarBasic">Option Explicit
  4. Public sProductname as String
  5.  
  6. ' Deletes out of a String 'BigString' all possible PartStrings, that are summed up
  7. ' in the Array 'ElimArray'
  8. Function ElimChar(ByVal BigString as String, ElimArray() as String)
  9. Dim i% ,n%
  10.     For i = 0 to Ubound(ElimArray)
  11.         BigString = DeleteStr(BigString,ElimArray(i)
  12.     Next
  13.     ElimChar = BigString
  14. End Function
  15.  
  16.  
  17. ' Deletes out of a String 'BigString' a possible Partstring 'CompString'
  18. Function DeleteStr(ByVal BigString,CompString as String) as String
  19. Dim i%, CompLen%, BigLen%
  20.     CompLen = Len(CompString)
  21.     i = 1
  22.     While i <> 0
  23.         i = Instr(i, BigString,CompString)
  24.         If i <> 0 then
  25.             BigLen = Len(BigString)
  26.             BigString = Mid(BigString,1,i-1) + Mid(BigString,i+CompLen,BigLen-i+1-CompLen)
  27.         End If
  28.     Wend
  29.     DeleteStr = BigString
  30. End Function
  31.  
  32.  
  33. ' Finds a PartString, that is framed by the Strings 'Prestring' and 'PostString'
  34. Function FindPartString(BigString, PreString, PostString as String, SearchPos as Integer) as String
  35. Dim StartPos%, EndPos%
  36. Dim BigLen%, PreLen%, PostLen%
  37.     StartPos = Instr(SearchPos,BigString,PreString)
  38.     If StartPos <> 0 Then
  39.         PreLen = Len(PreString)
  40.         EndPos = Instr(StartPos + PreLen,BigString,PostString)
  41.         If EndPos <> 0 Then
  42.             BigLen = Len(BigString)
  43.             PostLen = Len(PostString)
  44.             FindPartString = Mid(BigString,StartPos + PreLen, EndPos - (StartPos + PreLen))
  45.             ' Da diese Funktion daf├╝r programmiert wurde, in einer Schleife abgearbeitet zu werden
  46.             ' muss die initiale Suchposition hinter die Position des gefundenen Teilstrings gesetzt werden.
  47.             SearchPos = EndPos + PostLen
  48.         Else
  49.             Msgbox("No final tag for '" & PreString & "' existing", 16, GetProductName())
  50.             FindPartString = ""
  51.         End If
  52.     Else
  53.         FindPartString = ""
  54.     End If
  55. End Function
  56.  
  57.  
  58. ' Note iCompare = 0 (Binary comparison)
  59. '        iCompare = 1 (Text comparison)
  60. Function PartStringInArray(BigArray(), SearchString as String, iCompare as Integer) as Integer
  61. Dim MaxIndex as Integer
  62. Dim i as Integer
  63.     MaxIndex = Ubound(BigArray())
  64.     For i = 0 To MaxIndex
  65.         If Instr(1, BigArray(i), SearchString, iCompare) <> 0 Then
  66.             PartStringInArray() = i
  67.             Exit Function
  68.         End If
  69.     Next i
  70.     PartStringInArray() = -1
  71. End Function        
  72.  
  73.  
  74. ' Deletes the String 'SmallString' out of the String 'BigString'
  75. ' in case SmallString's Position in BigString is right at the end
  76. Function RTrimStr(ByVal BigString, SmallString as String) as String
  77. Dim SmallLen as Integer
  78. Dim BigLen as Integer
  79.     SmallLen = Len(SmallString)
  80.     BigLen = Len(BigString)
  81.     If Mid(BigString,BigLen + 1 - SmallLen, SmallLen) = SmallString Then
  82.         RTrimStr = Mid(BigString,1,BigLen - SmallLen)
  83.     Else
  84.         RTrimStr = BigString
  85.     End If
  86. End Function
  87.  
  88.  
  89. ' Deletes the Char 'CompChar' out of the String 'BigString'
  90. ' in case CompChar's Position in BigString is right at the beginning
  91. Function LTRimChar(ByVal BigString as String,CompChar as String) as String
  92. Dim BigLen as integer
  93.     BigLen = Len(BigString)
  94.     If BigLen > 1 Then
  95.         If Left(BigString,1) = CompChar then
  96.              BigString = Mid(BigString,2,BigLen-1)
  97.          End If
  98.     ElseIf BigLen = 1 Then
  99.          BigString = ""
  100.     End If
  101.     LTrimChar = BigString
  102. End Function
  103.  
  104.  
  105. ' Retrieves an Array out of a String.
  106. ' The fields of the Array are separated by the parameter 'Separator', that is contained
  107. ' in the Array
  108. ' The Array MaxLocindex delivers the highest Index of this Array
  109. Function ArrayOutOfString(BigString, Separator as String, Optional MaxIndex as integer)
  110. Dim i%, OldPos%, Pos%, SepLen%, BigLen%
  111. Dim CurUbound as Integer
  112. Dim StartUbound as Integer
  113.     StartUbound = 50
  114.     Dim LocList(StartUbound) as String
  115.     CurUbound = StartUbound    
  116.     OldPos = 1
  117.     i = -1
  118.     SepLen = Len(Separator)
  119.     BigLen = Len(BigString)
  120.     Do
  121.         Pos = Instr(OldPos,BigString, Separator)
  122.         i = i + 1
  123.         If Pos = 0 Then
  124.             LocList(i) = Mid(BigString, OldPos, BigLen - OldPos + 1 )
  125.         Else
  126.             LocList(i) = Mid(BigString, OldPos, Pos-OldPos )
  127.             OldPos = Pos + SepLen
  128.         End If
  129.         If i = CurUbound Then
  130.             CurUbound = CurUbound + StartUbound
  131.             ReDim Preserve LocList(CurUbound) as String
  132.         End If
  133.     Loop until Pos = 0
  134.     If Not IsMissing(Maxindex) Then
  135.         MaxIndex = i    
  136.     End If
  137.     If i <> -1 Then
  138.         ReDim Preserve LocList(i) as String
  139.     Else
  140.         ReDim LocList() as String
  141.     End If
  142.     ArrayOutofString = LocList()
  143. End Function
  144.  
  145.  
  146. ' Deletes all fieldvalues in one-dimensional Array
  147. Sub ClearArray(BigArray)
  148. Dim i as integer
  149.     For i = Lbound(BigArray()) to Ubound(BigArray())
  150.         BigArray(i) = ""
  151.     Next
  152. End Sub
  153.  
  154.  
  155. ' Deletes all fieldvalues in a multidimensional Array
  156. Sub ClearMultiDimArray(BigArray,DimCount as integer)
  157. Dim n%, m%
  158.     For n = Lbound(BigArray(),1) to Ubound(BigArray(),1)
  159.         For m = 0 to Dimcount - 1
  160.             BigArray(n,m) = ""
  161.         Next m
  162.     Next n
  163. End Sub
  164.  
  165.  
  166. ' Checks if a Field (LocField) is already defined in an Array
  167. ' Returns 'True' or 'False'
  168. Function FieldinArray(LocArray(), MaxIndex as integer, LocField as String) As Boolean
  169. Dim i as integer
  170.     For i = Lbound(LocArray()) to MaxIndex
  171.         If Ucase(LocArray(i)) = Ucase(LocField) Then
  172.             FieldInArray = True
  173.             Exit Function
  174.         End if
  175.     Next
  176.     FieldInArray = False
  177. End Function
  178.  
  179.  
  180. ' Checks if a Field (LocField) is already defined in an Array
  181. ' Returns 'True' or 'False'
  182. Function FieldinList(LocField, BigList()) As Boolean
  183. Dim i as integer
  184.     For i = Lbound(BigList()) to Ubound(BigList())
  185.         If LocField = BigList(i) Then
  186.             FieldInList = True
  187.             Exit Function
  188.         End if
  189.     Next
  190.     FieldInList = False
  191. End Function
  192.  
  193.  
  194. ' Retrieves the Index of the delivered String 'SearchString' in
  195. ' the Array LocList()'
  196. Function IndexinArray(SearchString as String, LocList()) as Integer
  197. Dim i as integer
  198.     For i = Lbound(LocList(),1) to Ubound(LocList(),1)
  199.         If Ucase(LocList(i,0)) = Ucase(SearchString) Then
  200.             IndexinArray = i
  201.             Exit Function
  202.         End if
  203.     Next
  204.     IndexinArray = -1
  205. End Function
  206.  
  207.  
  208. Sub MultiArrayInListbox(oDialog as Object, ListboxName as String, ValList(), iDim as Integer)
  209. Dim oListbox as Object
  210. Dim i as integer
  211. Dim a as Integer
  212.     a = 0
  213.     oListbox = oDialog.GetControl(ListboxName)    
  214.     oListbox.RemoveItems(0, oListbox.GetItemCount)
  215.     For i = 0 to Ubound(ValList(), 1)
  216.         If ValList(i) <> "" Then
  217.             oListbox.AddItem(ValList(i, iDim-1), a)
  218.             a = a + 1
  219.         End If
  220.     Next
  221. End Sub
  222.  
  223.  
  224. ' Searches for a String in a two-dimensional Array by querying all Searchindexex of the second dimension 
  225. ' and delivers the specific String of the ReturnIndex in the second dimension of the Searchlist()
  226. Function StringInMultiArray(SearchList(), SearchString as String, SearchIndex as Integer, ReturnIndex as Integer, Optional MaxIndex as Integer) as String
  227. Dim i as integer
  228. Dim CurFieldString as String
  229.     If IsMissing(MaxIndex) Then
  230.         MaxIndex = Ubound(SearchList(),1)
  231.     End If
  232.     For i = Lbound(SearchList()) to MaxIndex
  233.         CurFieldString = SearchList(i,SearchIndex)
  234.         If  Ucase(CurFieldString) = Ucase(SearchString) Then
  235.             StringInMultiArray() = SearchList(i,ReturnIndex)
  236.             Exit Function
  237.         End if
  238.     Next
  239.     StringInMultiArray() = ""
  240. End Function
  241.  
  242.  
  243. ' Searches for a Value in multidimensial Array by querying all Searchindices of the passed dimension 
  244. ' and delivers the Index where it is found.
  245. Function GetIndexInMultiArray(SearchList(), SearchValue, SearchIndex as Integer) as Integer
  246. Dim i as integer
  247. Dim MaxIndex as Integer
  248. Dim CurFieldValue
  249.     MaxIndex = Ubound(SearchList(),1)
  250.     For i = Lbound(SearchList()) to MaxIndex
  251.         CurFieldValue = SearchList(i,SearchIndex)
  252.         If CurFieldValue = SearchValue Then
  253.             GetIndexInMultiArray() = i
  254.             Exit Function
  255.         End if
  256.     Next
  257.     GetIndexInMultiArray() = -1
  258. End Function
  259.  
  260.  
  261. ' Replaces the string "OldReplace" through the String "NewReplace" in the String
  262. ' 'BigString'
  263. Function ReplaceString(ByVal Bigstring, NewReplace, OldReplace as String)  as String
  264. Dim i%, OldReplLen%, BigLen%
  265.  
  266.     If NewReplace <> OldReplace Then
  267.         OldReplLen = Len(OldReplace)
  268.         i = 1
  269.         Do
  270.             Biglen = Len(BigString)
  271.             i = Instr(i,BigString,OldReplace)
  272.             If i <> 0 then
  273.                 BigString = Mid(BigString,1,i-1) & NewReplace & Mid(BigString,i + OldReplLen,BigLen  - i + 1 - OldReplLen
  274.                 i = i + Len(NewReplace)
  275.             End If
  276.         Loop until i = 0
  277.     End If
  278.     ReplaceString = BigString
  279. End Function
  280.  
  281.  
  282. ' Converts an "ordinary" path to a "URL-Path"
  283. 'Function ConverttoURL(ByVal BigString as String) as String
  284. 'Dim Separator as String
  285. '    If sProductname = "" Then
  286. '        sProductname = GetProductname()
  287. '    End If
  288. '    If BigString <> "" Then
  289. '        If IsFatOffice() Then
  290. '            Separator = GetPathSeparator()
  291. '            ' Is the delivered Path already a URL
  292. '            If Instr(1,UCase(BigString),"FILE:///") = 0 Then
  293. '                BigString = ReplaceString(BigString,"/",Separator)
  294. '                BigString = "file:///" & BigString
  295. '            End If
  296. '        End If
  297. '        ConvertToURL = BigString
  298. '    Else
  299. '        ConvertToUrl = ""
  300. '    End If
  301. 'End Function
  302.  
  303.  
  304. ' Converts an "URL-Path" to an ordinary "Path"
  305. 'Function ConvertfromURL(ByVal BigString as String) as String
  306. 'Dim Separator as String
  307. '    Separator = GetPathSeparator()
  308. '    If Left(Ucase(BigString),8)= "FILE:///" Then
  309. '        BigString = Mid(BigString, 9, Len(BigString)-8)
  310. '        BigString = ReplaceString(BigString,Separator,"/")
  311. '        BigString = ReplaceString(BigString,":","|")
  312. '        ConvertFromUrl = BigString
  313. '    End If
  314. 'End Function
  315.  
  316.  
  317. ' Retrieves the second value for a next to 'SearchString' in
  318. ' a two-dimensional string-Array
  319. Function FindSecondValue(SearchString as String, TwoDimList() as String ) as String
  320. Dim i as Integer
  321.     For i = 0 To Ubound(TwoDimList,1)
  322.         If Ucase(SearchString) = Ucase(TwoDimList(i,0)) Then
  323.             FindSecondValue = TwoDimList(i,1)
  324.             Exit For
  325.         End If
  326.     Next
  327. End Function
  328.  
  329.  
  330. ' raises a base to a certain power
  331. Function Power(Basis as Double, Exponent as Double) as Double
  332.     Power = Exp(Exponent*Log(Basis))
  333. End Function
  334.  
  335.  
  336. ' rounds a Real to a given Number of Decimals
  337. Function Round(BaseValue as Double, Decimals as Integer) as Double
  338. Dim Multiplicator as Long
  339. Dim DblValue#, RoundValue#
  340.     Multiplicator = Power(10,Decimals)
  341.     RoundValue = Int(BaseValue * Multiplicator)
  342.     Round = RoundValue/Multiplicator
  343. End Function
  344.  
  345.  
  346. 'Retrieves the mere filename out of a whole path
  347. Function FileNameoutofPath(ByVal Path as String, Separator as String) as String
  348. Dim i as Integer
  349. Dim SepList() as String
  350.     SepList() = ArrayoutofString(Path,"/",i)
  351.     FileNameoutofPath = SepList(i)
  352. End Function
  353.  
  354.  
  355. Function GetFileNameExtension(ByVal FileName as String)
  356. Dim MaxIndex as Integer
  357. Dim SepList() as String
  358.     SepList() = ArrayoutofString(FileName,".", MaxIndex)
  359.     GetFileNameExtension = SepList(MaxIndex)
  360. End Function
  361.  
  362.  
  363. Function GetFileNameWithoutExtension(ByVal FileName as String, Optional Separator as String)
  364. Dim MaxIndex as Integer
  365. Dim SepList() as String
  366.     If not IsMissing(Separator) Then
  367.         FileName = FileNameoutofPath(FileName, Separator)
  368.     End If
  369.     SepList() = ArrayoutofString(FileName,".", MaxIndex)
  370.     GetFileNameWithoutExtension = RTrimStr(FileName, "." & SepList(MaxIndex)
  371. End Function
  372.  
  373.  
  374. Function DirectoryNameoutofPath(sPath as String, Separator as String) as String
  375. Dim LocFileName as String
  376.     LocFileName = FileNameoutofPath(sPath, Separator)
  377.     DirectoryNameoutofPath = RTrimStr(sPath, Separator & LocFileName)
  378. End Function
  379.  
  380.  
  381. Function CountCharsinString(BigString, LocChar as String, ByVal StartPos as Integer) as Integer
  382. Dim LocCount%, LocPos%
  383.     LocCount = 0
  384.     Do
  385.         LocPos = Instr(StartPos,BigString,LocChar)
  386.         If LocPos <> 0 Then
  387.             LocCount = LocCount + 1
  388.             StartPos = LocPos+1
  389.         End If
  390.     Loop until LocPos = 0
  391.     CountCharsInString = LocCount
  392. End Function
  393.  
  394.  
  395. Function BubbleSortList(ByVal SortList(),optional sort2ndValue as Boolean)
  396. 'This function bubble sorts an array of maximum 2 dimensions.
  397. 'The default sorting order is the first dimension
  398. 'Only if sort2ndValue is True the second dimension is the relevant for the sorting order
  399.     Dim s as Integer
  400.     Dim t as Integer
  401.     Dim i as Integer
  402.     Dim k as Integer
  403.     Dim dimensions as Integer
  404.     Dim sortvalue as Integer
  405.     Dim DisplayDummy
  406.     dimensions = 2
  407.     
  408. On Local Error Goto No2ndDim    
  409.     k = Ubound(SortList(),2)
  410.     No2ndDim:
  411.     If Err <> 0 Then dimensions = 1
  412.     
  413.     i = Ubound(SortList(),1)
  414.     If ismissing(sort2ndValue) then
  415.         sortvalue = 0
  416.     else
  417.         sortvalue = 1
  418.     end if
  419.     
  420.     For s = 1 to i - 1
  421.         For t = 0 to i-s
  422.             Select Case dimensions
  423.             Case 1
  424.                 If SortList(t) > SortList(t+1) Then                             
  425.                     DisplayDummy = SortList(t)
  426.                     SortList(t) = SortList(t+1)
  427.                     SortList(t+1) = DisplayDummy    
  428.                 End If
  429.             Case 2
  430.                 If SortList(t,sortvalue) > SortList(t+1,sortvalue) Then 
  431.                     For k = 0 to UBound(SortList(),2)                        
  432.                             DisplayDummy = SortList(t,k)
  433.                             SortList(t,k) = SortList(t+1,k)
  434.                             SortList(t+1,k) = DisplayDummy 
  435.                     Next k
  436.                 End If
  437.             End Select
  438.         Next t
  439.     Next s 
  440.     BubbleSortList = SortList()             
  441. End Function
  442.  
  443.  
  444. Function GetValueoutofList(SearchValue, BigList(), iDim as Integer, Optional ValueIndex)
  445. Dim i as Integer
  446. Dim MaxIndex as Integer
  447.     MaxIndex = Ubound(BigList(),1)
  448.     For i = 0 To MaxIndex
  449.         If BigList(i,0) = SearchValue Then
  450.             If Not IsMissing(ValueIndex) Then
  451.                 ValueIndex = i
  452.             End If
  453.             GetValueOutOfList() = BigList(i,iDim)
  454.         End If
  455.     Next i
  456. End Function
  457.  
  458.  
  459. Function AddListtoList(ByVal FirstArray(), ByVal SecondArray(), Optional StartIndex)
  460. Dim n as Integer
  461. Dim m as Integer
  462. Dim MaxIndex as Integer
  463.     MaxIndex = Ubound(FirstArray()) + Ubound(SecondArray()) + 1
  464.     If MaxIndex > -1 Then
  465.         Dim ResultArray(MaxIndex)
  466.         For m = 0 To Ubound(FirstArray())
  467.             ResultArray(m) = FirstArray(m)
  468.         Next m
  469.         For n = 0 To Ubound(SecondArray())
  470.             ResultArray(m) = SecondArray(n)
  471.             m = m + 1
  472.         Next n
  473.         AddListToList() = ResultArray()
  474.     Else
  475.         Dim NullArray()
  476.         AddListToList() = NullArray()
  477.     End If
  478. End Function
  479.  
  480.  
  481. Function CheckDouble(DoubleString as String)
  482. On Local Error Goto WRONGDATATYPE
  483.     CheckDouble() = CDbl(DoubleString) 
  484. WRONGDATATYPE:
  485.     If Err <> 0 Then
  486.         CheckDouble() = 0
  487.         Resume NoErr:
  488.     End If
  489. NOERR:    
  490. End Function</script:module>