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