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