1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: t_stringtools.inc,v $
13 '* last change: $Author: jsk $ $Date: 2008-06-20 07:59:14 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : joerg.skottke@sun.com
36 '* short description : Functions for manipulation of strings
38 '\******************************************************************************
40 function hRemoveLineBreaks( cString as string ) as string
42 '///<h3>Remove linebreaks and tabs from a string</h3>
43 '///<i>Used to "beautify" content of messageboxes when printed to the log</i><br><br>
44 '///<u>Parameter(s):</u>
46 '///+<li>Content of a messagebox as captured with .getText() (string)</li>
50 '///+<li>A string without tabs, linebreaks and linefeed (string)</li>
52 '///<u>Description</u>:
54 '///+<li>Walk through the string, replace linebreaks, tabs etc. with spaces</li>
56 ' Function (undocumented) to remove LF and CR from strings.
57 ' When a messagebox appears with multiple lines of text this usually
58 ' breaks the output of the printlog into multiple lines making it
59 ' hard to read. So this function puts the entire text in one line.
61 dim iCharPos as integer
62 dim cCurrentChar as string
63 dim cNewString as string
65 ' walk through the string character by character and replace those
66 ' characters that break the line. Tabs and linebreaks become spaces
67 for iCharPos = 1 to len( cString )
69 cCurrentChar = mid( cString , iCharPos , 1 )
71 select case cCurrentChar
72 case CHR$(13) : cNewString = cNewString & " " ' replace linebreak
73 case CHR$(10) : ' Simply ignore linefeed
74 case CHR$(09) : cNewString = cNewString & " " ' replace tab with space
75 case else : cNewString = cNewString & cCurrentChar ' append char
80 hRemoveLineBreaks() = cNewString
85 '*******************************************************************************
87 function hCompareSubStrings( cRef as string, cSub as string ) as integer
89 '///<h3>Find substring in a reference string</h3>
90 '///<i>Used to determine that we are on "The first doc!"</i><br><br>
91 '///<u>Parameter(s):</u>
93 '///+<li>Term to search for (string)</li>
94 '///+<li>Term to be searched (string)</li>
98 '///+<li>Errorcode (integer)</li>
100 '///+<li>-1: Invalid parameter(s)</li>
101 '///+<li>0: Strings do not match</li>
102 '///+<li>1: Term is exact match</li>
103 '///+<li>2: Term is a substring</li>
106 '///<u>Description</u>:
109 const CFN = "hCompareSubStrings::"
111 '///+<li>Test function parameters</li>
112 if ( ( cRef = "" ) or ( cSub = "" ) ) then
113 warnlog( CFN & "invalid parameter(s): Empty string passed." )
114 hCompareSubStrings() = -1
120 '///+<li>Test if we have a substring</li>
121 if ( instr( cRef, cSub ) > 0 ) then
127 '///+<li>Test if we have an exact match</li>
129 if ( ( cRef = cSub ) and ( len( cRef ) = len( cSub ) ) ) then
135 case 0 : printlog( CFN & "No matching substring found" )
136 case 1 : printlog( CFN & "Strings are identical" )
137 case 2 : printlog( CFN & "String is substring" )
140 hCompareSubStrings() = irc
145 '******************************************************************************
147 function hGetDirTreeLevel( cFullPath as string ) as integer
149 '///<h3>Count the numbers of pathseparators in a path-string</h3>
150 '///<i>Used to find the level of current directory within the directory tree.<br>
151 '///+The function prints a warning when no pathseparators were found</i><br><br>
152 '///<u>Parameter(s):</u>
154 '///+<li>Path (string) with <b>no trailing pathseparator</b></li>
158 '///+<li>Number of Pathseparators within the string (integer)</li>
160 '///+<li>0 = failure</li>
161 '///+<li>> 0 = level</li>
164 '///<u>Description</u>:
167 const CFN = "hGetDirTreeLevel::"
169 dim iCurrentChar as integer
170 dim cCurrentChar as string
171 dim iSeparatorCount as integer
174 '///+<li>Walk through the string</li>
176 for iCurrentChar = 1 to len( cFullPath )
178 '///+<li>Get the next character</li>
179 cCurrentChar = mid( cFullPath , iCurrentChar , 1 )
181 '///+<li>If it is a separtator, increase the counter</li>
182 if ( cCurrentChar = gPathSigne ) then
183 iSeparatorCount = iSeparatorCount + 1
189 '///+<li>Print a warning if no separators were found</li>
190 if ( iSeparatorCount = 0 ) then
191 warnlog( CFN & "Did not find any separators in given string: " & cFullPath )
195 hGetDirTreeLevel() = iSeparatorCount
200 '*******************************************************************************
202 function hGetStringFromStaticTextField( oControl as object ) as string
204 use "global\tools\includes\optional\t_accels.inc"
206 '///<h3>Get the string from a static textfield</h3>
208 '///<i>Static textfields like those in the document properties dialog are
209 '///+ in certain places designed in a way that the string can be selected
210 '///+ and copied to the clipboard. This has been introduced with SRC680m212
211 '///+ (9156). This function uses keyboard shortcuts for "Select All"
212 '///+ and "Copy" to get the string into the clipboard as .uno.Copy
213 '///+ is not enabled.</i><br><br>
215 '///<u>Parameter(s):</u><br>
218 '///+<li>Name of the control (Object)</li>
220 '///+<li>The object must exist in the current context</li>
226 '///<u>Returns:</u><br>
228 '///+<li>Content of the field (String)</li>
230 '///+<li>Blank if string is empty</li>
234 const CFN = "hGetStringFromStaticTextField::"
235 dim brc as boolean 'a multi purpose boolean returnvalue
237 dim cSelectAll as string
241 printlog( CFN & "Enter" )
243 '///<u>Description:</u>
245 '///+<li>Verify that the object exists</li>
246 '///+<li>Get the accelerator for SelectAll and Copy</li>
247 '///+<li>Execute SelectAll and Copy on control</li>
248 '///+<li>Get the string from the clipboard</li>
249 if ( oControl.exists() ) then
250 if ( oControl.isVisible() ) then
251 cSelectAll = hGetAccel( "SelectAll" )
252 cCopy = hGetAccel( "Copy" )
254 oControl.typeKeys( "<right>" )
255 oControl.typeKeys( cSelectAll )
256 oControl.typeKeys( cCopy )
258 cText = getClipboardText()
259 printlog( CFN & "Exit with result: " & cText )
262 qaerrorlog( CFN & "Exit: Control exists but is not visible" )
266 qaerrorlog( CFN & "Exit: Control does not exist in this context" )
271 hGetStringFromStaticTextField() = cText
276 '*******************************************************************************
278 function hConvertStringToLong( cValue as string ) as long
281 '///<h3>Convert a stringvalue to long int</h3>
283 '///<i>The purpose of this function is to isolate the filesize from a string
284 '///+ of the type "1.345 Bytes" by removing the thousands-separator
285 '///+ and the trailing unit. The result is then a filesize as long integer
286 '///+ which then can be compared to the result from the BASIC function
287 '///+ FileLen( FileSpec )</i><br><br>
289 '///<u>Parameter(s):</u><br>
292 '///+<li>String containing a long integer value (String)</li>
294 '///+<li>No floating point values allowed</li>
295 '///+<li>Numeric value must be at the beginning of the string (no leading characters/spaces)</li>
296 '///+<li>Negative values are allowed</li>
297 '///+<li>Leading "+" is not allowed</li>
303 '///<u>Returns:</u><br>
305 '///+<li>Value of the string as long integer (Long)</li>
307 '///+<li>Thousands separator (. or ,) have been removed</li>
308 '///+<li>Decimal separators (though not allowed) have been removed</li>
312 const CFN = "hConvertStringToLong::"
313 printlog( CFN & "Enter with option: " & cValue )
314 dim brc as boolean 'a multi purpose boolean returnvalue
322 dim cStringValue as string
325 '///<u>Description:</u>
327 '///+<li>Walk through the single chars of the string</li>
329 for iChar = 1 to iLen
331 '///+<li>Get the current character</li>
332 cChar = mid( cValue , iChar , 1 )
334 '///+<li>Copy valid characters to temporary string, drop invalid, exit on first space or other character</li>
336 case "-" : cStringValue = cStringValue & cChar
337 case "1" : cStringValue = cStringValue & cChar
338 case "2" : cStringValue = cStringValue & cChar
339 case "3" : cStringValue = cStringValue & cChar
340 case "4" : cStringValue = cStringValue & cChar
341 case "5" : cStringValue = cStringValue & cChar
342 case "6" : cStringValue = cStringValue & cChar
343 case "7" : cStringValue = cStringValue & cChar
344 case "8" : cStringValue = cStringValue & cChar
345 case "9" : cStringValue = cStringValue & cChar
346 case "0" : cStringValue = cStringValue & cChar
347 case else: ' do nothing
353 printlog( CFN & "Exit with value: " & cStringValue )
355 '///+<li>Convert string to long integer and return to calling function</li>
356 hConvertStringToLong() = val( cStringValue )