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