Utils.xba 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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="Utils" script:language="StarBasic">
  4. REM =======================================================================================================================
  5. REM === The Access2Base library is a part of the LibreOffice project. ===
  6. REM === Full documentation is available on http://www.access2base.com ===
  7. REM =======================================================================================================================
  8. Option Explicit
  9. Global _A2B_ As Variant
  10. REM -----------------------------------------------------------------------------------------------------------------------
  11. REM --- PRIVATE FUNCTIONS ---
  12. REM -----------------------------------------------------------------------------------------------------------------------
  13. Public Function _AddArray(ByVal pvArray As Variant, pvItem As Variant) As Variant
  14. &apos;Add the item at the end of the array
  15. Dim vArray() As Variant
  16. If IsArray(pvArray) Then vArray = pvArray Else vArray = Array()
  17. ReDim Preserve vArray(LBound(vArray) To UBound(vArray) + 1)
  18. vArray(UBound(vArray)) = pvItem
  19. _AddArray() = vArray()
  20. End Function
  21. REM -----------------------------------------------------------------------------------------------------------------------
  22. Public Function _AddNumeric(ByVal Optional pvTypes As Variant) As Variant
  23. &apos;Return on top of argument the list of all numeric types
  24. &apos;Facilitates the entry of the list of allowed types in _CheckArgument calls
  25. Dim i As Integer, vNewList() As Variant, vNumeric() As Variant, iSize As Integer
  26. If IsMissing(pvTypes) Then
  27. vNewList = Array()
  28. ElseIf IsArray(pvTypes) Then
  29. vNewList = pvTypes
  30. Else
  31. vNewList = Array(pvTypes)
  32. End If
  33. vNumeric = Array(vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal, vbBoolean)
  34. iSize = UBound(vNewlist)
  35. ReDim Preserve vNewList(iSize + UBound(vNumeric) + 1)
  36. For i = 0 To UBound(vNumeric)
  37. vNewList(iSize + i + 1) = vNumeric(i)
  38. Next i
  39. _AddNumeric = vNewList
  40. End Function &apos; _AddNumeric V0.8.0
  41. REM -----------------------------------------------------------------------------------------------------------------------
  42. Public Function _BitShift(piValue As Integer, piConstant As Integer) As Boolean
  43. _BitShift = False
  44. If piValue = 0 Then Exit Function
  45. Select Case piConstant
  46. Case 1
  47. Select Case piValue
  48. Case 1, 3, 5, 7, 9, 11, 13, 15: _BitShift = True
  49. Case Else
  50. End Select
  51. Case 2
  52. Select Case piValue
  53. Case 2, 3, 6, 7, 10, 11, 14, 15: _BitShift = True
  54. Case Else
  55. End Select
  56. Case 4
  57. Select Case piValue
  58. Case 4, 5, 6, 7, 12, 13, 14, 15: _BitShift = True
  59. Case Else
  60. End Select
  61. Case 8
  62. Select Case piValue
  63. Case 8, 9, 10, 11, 12, 13, 14, 15: _BitShift = True
  64. Case Else
  65. End Select
  66. End Select
  67. End Function &apos; BitShift
  68. REM -----------------------------------------------------------------------------------------------------------------------
  69. Public Function _CalledSub() As String
  70. _CalledSub = Iif(_A2B_.CalledSub = &quot;&quot;, &quot;&quot;, _GetLabel(&quot;CALLTO&quot;) &amp; &quot; &apos;&quot; &amp; _A2B_.CalledSub &amp; &quot;&apos;&quot;)
  71. End Function &apos; CalledSub V0.8.9
  72. REM -----------------------------------------------------------------------------------------------------------------------
  73. Public Function _CheckArgument(pvItem As Variant _
  74. , ByVal piArgNr As Integer _
  75. , ByVal pvType As Variant _
  76. , ByVal Optional pvValid As Variant _
  77. , ByVal Optional pvError As Boolean _
  78. ) As Variant
  79. &apos; Called by public functions to check the validity of their arguments
  80. &apos; pvItem Argument to be checked
  81. &apos; piArgNr Argument sequence number
  82. &apos; pvType Single value or array of allowed variable types
  83. &apos; If of string type must contain one or more valid pseudo-object types
  84. &apos; pvValid Single value or array of allowed values - comparison for strings is case-insensitive
  85. &apos; pvError If True (default), error handling in this routine. False in _setProperty methods in class modules.
  86. _CheckArgument = False
  87. Dim iVarType As Integer, bValidIsMissing As Boolean
  88. If IsArray(pvType) Then iVarType = VarType(pvType(LBound(pvType))) Else iVarType = VarType(pvType)
  89. If iVarType = vbString Then &apos; pvType is a pseudo-type string
  90. _CheckArgument = Utils._IsPseudo(pvItem, pvType)
  91. Else
  92. bValidIsMissing = ( VarType(pvValid) = vbError )
  93. If Not bValidIsMissing Then bValidIsMissing = IsMissing(pvValid)
  94. If bValidIsMissing Then _CheckArgument = Utils._IsScalar(pvItem, pvType) Else _CheckArgument = Utils._IsScalar(pvItem, pvType, pvValid)
  95. End If
  96. If VarType(pvItem) = vbCurrency Or VarType(pvItem) = vbDecimal Or VarType(pvItem) = vbBigint Then pvItem = CDbl(pvItem)
  97. Exit_Function:
  98. If Not _CheckArgument Then
  99. If IsMissing(pvError) Then pvError = True
  100. If pvError Then
  101. TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), 0, 1, Array(piArgNr, pvItem))
  102. End If
  103. End If
  104. Exit Function
  105. End Function &apos; CheckArgument V0.9.0
  106. REM -----------------------------------------------------------------------------------------------------------------------
  107. Public Function _CStr(ByVal pvArg As Variant, ByVal Optional pbShort As Boolean) As String
  108. &apos; Convert pvArg into a readable string (truncated if too long and pbShort = True or missing)
  109. &apos; pvArg may be a byte-array. Other arrays are processed recursively into a semicolon separated string
  110. Dim sArg As String, sObject As String, oArg As Object, sLength As String, i As Long, iMax As Long
  111. Const cstLength = 50
  112. Const cstByteLength = 25
  113. If IsMissing(pbShort) Then pbShort = True
  114. If IsArray(pvArg) Then
  115. sArg = &quot;&quot;
  116. If VarType(pvArg) = vbByte Or VarType(pvArg) = vbArray + vbByte Then
  117. If pbShort And UBound(pvArg) &gt; cstByteLength Then iMax = cstByteLength Else iMax = UBound(pvArg)
  118. For i = 0 To iMax
  119. sArg = sArg &amp; Right(&quot;00&quot; &amp; Hex(pvArg(i)), 2)
  120. Next i
  121. Else
  122. If pbShort Then
  123. sArg = &quot;[ARRAY]&quot;
  124. Else &apos; One-dimension arrays only
  125. For i = LBound(pvArg) To UBound(pvArg)
  126. sArg = sArg &amp; Utils._CStr(pvArg(i), pbShort) &amp; &quot;;&quot; &apos; Recursive call
  127. Next i
  128. If Len(sArg) &gt; 1 Then sArg = Left(sArg, Len(sArg) - 1)
  129. End If
  130. End If
  131. Else
  132. Select Case VarType(pvArg)
  133. Case vbEmpty : sArg = &quot;[EMPTY]&quot;
  134. Case vbNull : sArg = &quot;[NULL]&quot;
  135. Case vbObject
  136. If IsNull(pvArg) Then
  137. sArg = &quot;[NULL]&quot;
  138. Else
  139. sObject = Utils._ImplementationName(pvArg)
  140. If Utils._IsPseudo(pvArg, Array(OBJDATABASE, OBJCOLLECTION, OBJPROPERTY, OBJFORM, OBJSUBFORM, OBJCONTROL, OBJOPTIONGROUP _
  141. , OBJEVENT, OBJFIELD, OBJTABLEDEF, OBJQUERYDEF, OBJRECORDSET, OBJTEMPVAR, OBJCOMMANDBAR, OBJCOMMANDBARCONTROL _
  142. , OBJDIALOG _
  143. )) Then
  144. Set oArg = pvArg &apos; To avoid &quot;Object variable not set&quot; error message
  145. sArg = &quot;[&quot; &amp; oArg._Type &amp; &quot;] &quot; &amp; oArg._Name
  146. ElseIf sObject &lt;&gt; &quot;&quot; Then
  147. sArg = &quot;[&quot; &amp; sObject &amp; &quot;]&quot;
  148. Else
  149. sArg = &quot;[OBJECT]&quot;
  150. End If
  151. End If
  152. Case vbVariant : sArg = &quot;[VARIANT]&quot;
  153. Case vbString
  154. &apos; Replace CR + LF by \n and HT by \t
  155. &apos; Replace semicolon by \; to allow semicolon separated rows
  156. sArg = Replace( _
  157. Replace( _
  158. Replace( _
  159. Replace( _
  160. Replace(pvArg, &quot;\&quot;, &quot;\\&quot;) _
  161. , Chr(13), &quot;&quot;) _
  162. , Chr(10), &quot;\n&quot;) _
  163. , Chr(9), &quot;\t&quot;) _
  164. , &quot;;&quot;, &quot;\;&quot;)
  165. Case vbBoolean : sArg = Iif(pvArg, &quot;[TRUE]&quot;, &quot;[FALSE]&quot;)
  166. Case vbByte : sArg = Right(&quot;00&quot; &amp; Hex(pvArg), 2)
  167. Case vbSingle, vbDouble, vbCurrency
  168. sArg = Format(pvArg)
  169. If InStr(UCase(sArg), &quot;E&quot;) = 0 Then sArg = Format(pvArg, &quot;##0.0##&quot;)
  170. sArg = Replace(sArg, &quot;,&quot;, &quot;.&quot;)
  171. Case vbBigint : sArg = CStr(CLng(pvArg))
  172. Case vbDate : sArg = Year(pvArg) &amp; &quot;-&quot; &amp; Right(&quot;0&quot; &amp; Month(pvArg), 2) &amp; &quot;-&quot; &amp; Right(&quot;0&quot; &amp; Day(pvArg), 2) _
  173. &amp; &quot; &quot; &amp; Right(&quot;0&quot; &amp; Hour(pvArg), 2) &amp; &quot;:&quot; &amp; Right(&quot;0&quot; &amp; Minute(pvArg), 2) _
  174. &amp; &quot;:&quot; &amp; Right(&quot;0&quot; &amp; Second(pvArg), 2)
  175. Case Else : sArg = CStr(pvArg)
  176. End Select
  177. End If
  178. If pbShort And Len(sArg) &gt; cstLength Then
  179. sLength = &quot;(&quot; &amp; Len(sArg) &amp; &quot;)&quot;
  180. sArg = Left(sArg, cstLength - 5 - Len(slength)) &amp; &quot; ... &quot; &amp; sLength
  181. End If
  182. _CStr = sArg
  183. End Function &apos; CStr V0.9.5
  184. REM -----------------------------------------------------------------------------------------------------------------------
  185. Public Function _CVar(ByRef psArg As String, ByVal Optional pbStrDate As Boolean) As Variant
  186. &apos; psArg is presumed an output of _CStr (stored in the meantime in a text file f.i.)
  187. &apos; _CVar returns the corresponding original Variant variable or Null/Nothing if not possible
  188. &apos; Return values may of types Array, Long, Double, Date, Boolean, String, Null or Empty
  189. &apos; pbStrDate = True keeps dates as strings
  190. Dim cstEscape1 As String, cstEscape2 As String
  191. cstEscape1 = Chr(14) &apos; Form feed used as temporary escape character for \\
  192. cstEscape2 = Chr(27) &apos; ESC used as temporary escape character for \;
  193. _CVar = &quot;&quot;
  194. If Len(psArg) = 0 Then Exit Function
  195. Dim sArg As String, vArgs() As Variant, vVars() As Variant, i As Integer
  196. If IsMissing(pbStrDate) Then pbStrDate = False
  197. sArg = Replace( _
  198. Replace( _
  199. Replace( _
  200. Replace(psArg, &quot;\\&quot;, cstEscape1) _
  201. , &quot;\;&quot;, cstEscape2) _
  202. , &quot;\n&quot;, Chr(10)) _
  203. , &quot;\t&quot;, Chr(9))
  204. &apos; Semicolon separated string
  205. vArgs = Split(sArg, &quot;;&quot;)
  206. If UBound(vArgs) &gt; LBound(vArgs) Then &apos; Process each item recursively
  207. vVars = Array()
  208. Redim vVars(LBound(vArgs) To UBound(vArgs))
  209. For i = LBound(vVars) To UBound(vVars)
  210. vVars(i) = _CVar(vArgs(i), pbStrDate)
  211. Next i
  212. _CVar = vVars
  213. Exit Function
  214. End If
  215. &apos; Usual case
  216. Select Case True
  217. Case sArg = &quot;[EMPTY]&quot; : _CVar = EMPTY
  218. Case sArg = &quot;[NULL]&quot; Or sArg = &quot;[VARIANT]&quot; : _CVar = Null
  219. Case sArg = &quot;[OBJECT]&quot; : _CVar = Nothing
  220. Case sArg = &quot;[TRUE]&quot; : _CVar = True
  221. Case sArg = &quot;[FALSE]&quot; : _CVar = False
  222. Case IsDate(sArg)
  223. If pbStrDate Then _CVar = sArg Else _CVar = CDate(sArg)
  224. Case IsNumeric(sArg)
  225. If InStr(sArg, &quot;.&quot;) &gt; 0 Then
  226. _CVar = Val(sArg)
  227. Else
  228. _CVar = CLng(Val(sArg)) &apos; Val always returns a double
  229. End If
  230. Case _RegexSearch(sArg, &quot;^[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?$&quot;) &lt;&gt; &quot;&quot;
  231. _CVar = Val(sArg) &apos; Scientific notation
  232. Case Else : _CVar = Replace(Replace(sArg, cstEscape1, &quot;\&quot;), cstEscape2, &quot;;&quot;)
  233. End Select
  234. End Function &apos; CVar V1.7.0
  235. REM -----------------------------------------------------------------------------------------------------------------------
  236. Public Function _DecimalPoint() As String
  237. &apos;Return locale decimal point
  238. _DecimalPoint = Mid(Format(0, &quot;0.0&quot;), 2, 1)
  239. End Function
  240. REM -----------------------------------------------------------------------------------------------------------------------
  241. Private Function _ExtensionLocation() As String
  242. &apos; Return the URL pointing to the location where OO installed the Access2Base extension
  243. &apos; Adapted from https://wiki.documentfoundation.org/Documentation/DevGuide/Extensions#Location_of_Installed_Extensions
  244. Dim oPip As Object, sLocation As String
  245. Set oPip = GetDefaultContext.getByName(&quot;/singletons/com.sun.star.deployment.PackageInformationProvider&quot;)
  246. _ExtensionLocation = oPip.getPackageLocation(&quot;Access2Base&quot;)
  247. End Function &apos; ExtensionLocation
  248. REM -----------------------------------------------------------------------------------------------------------------------
  249. Private Function _GetDialogLib() As Object
  250. &apos; Return actual Access2Base dialogs library
  251. Dim oDialogLib As Object
  252. Set oDialogLib = DialogLibraries
  253. If oDialogLib.hasByName(&quot;Access2BaseDev&quot;) Then
  254. If Not oDialogLib.IsLibraryLoaded(&quot;Access2BaseDev&quot;) Then oDialogLib.loadLibrary(&quot;Access2BaseDev&quot;)
  255. Set _GetDialogLib = DialogLibraries.Access2BaseDev
  256. ElseIf oDialogLib.hasByName(&quot;Access2Base&quot;) Then
  257. If Not oDialogLib.IsLibraryLoaded(&quot;Access2Base&quot;) Then oDialogLib.loadLibrary(&quot;Access2Base&quot;)
  258. Set _GetDialogLib = DialogLibraries.Access2Base
  259. Else
  260. Set _GetDialogLib = Nothing
  261. EndIf
  262. End Function
  263. REM -----------------------------------------------------------------------------------------------------------------------
  264. Public Function _GetEventName(ByVal psProperty As String) As String
  265. &apos; Return the LO internal event name
  266. &apos; Corrects the typo on ErrorOccur(r?)ed
  267. _GetEventName = Replace(LCase(Mid(psProperty, 3, 1)) &amp; Right(psProperty, Len(psProperty) - 3), &quot;errorOccurred&quot;, &quot;errorOccured&quot;)
  268. End Function &apos; _GetEventName V1.7.0
  269. REM -----------------------------------------------------------------------------------------------------------------------
  270. Public Function _GetEventScriptCode(poObject As Object _
  271. , ByVal psEvent As String _
  272. , ByVal psName As String _
  273. , Optional ByVal pbExtendName As Boolean _
  274. ) As String
  275. &apos; Extract from the parent of poObject the macro linked to psEvent.
  276. &apos; psName is the name of the object
  277. Dim i As Integer, vEvents As Variant, sEvent As String, oParent As Object, iIndex As Integer, sName As String
  278. _GetEventScriptCode = &quot;&quot;
  279. If Not Utils._hasUNOMethod(poObject, &quot;getParent&quot;) Then Exit Function
  280. &apos; Find form index i.e. find control via getByIndex()
  281. If IsMissing(pbExtendName) Then pbExtendName = False
  282. Set oParent = poObject.getParent()
  283. iIndex = -1
  284. For i = 0 To oParent.getCount() - 1
  285. sName = oParent.getByIndex(i).Name
  286. If (sName = psName) Or (pbExtendName And (sName = &quot;MainForm&quot; Or sName = &quot;Form&quot;)) Then
  287. iIndex = i
  288. Exit For
  289. End If
  290. Next i
  291. If iIndex &lt; 0 Then Exit Function
  292. &apos; Find script event
  293. vEvents = oParent.getScriptEvents(iIndex) &apos; Returns an array
  294. sEvent = Utils._GetEventName(psEvent) &apos; Targeted event method
  295. For i = 0 To UBound(vEvents)
  296. If vEvents(i).EventMethod = sEvent Then
  297. _GetEventScriptCode = vEvents(i).ScriptCode
  298. Exit For
  299. End If
  300. Next i
  301. End Function &apos; _GetEventScriptCode V1.7.0
  302. REM -----------------------------------------------------------------------------------------------------------------------
  303. Private Function _GetResultSetColumnValue(poResultSet As Object _
  304. , ByVal piColIndex As Integer _
  305. , Optional ByVal pbReturnBinary As Boolean _
  306. ) As Variant
  307. REM Modified from Roberto Benitez&apos;s BaseTools
  308. REM get the data for the column specified by ColIndex
  309. REM If pbReturnBinary = False (default) then return length of binary field
  310. REM get type name from metadata
  311. Dim vValue As Variant, iType As Integer, vDateTime As Variant, oValue As Object
  312. Dim bNullable As Boolean, lSize As Long
  313. Const cstMaxTextLength = 65535
  314. Const cstMaxBinlength = 2 * 65535
  315. On Local Error Goto 0 &apos; Disable error handler
  316. vValue = Null &apos; Default value if error
  317. If IsMissing(pbReturnBinary) Then pbReturnBinary = False
  318. With com.sun.star.sdbc.DataType
  319. iType = poResultSet.MetaData.getColumnType(piColIndex)
  320. bNullable = ( poResultSet.MetaData.IsNullable(piColIndex) = com.sun.star.sdbc.ColumnValue.NULLABLE )
  321. Select Case iType
  322. Case .ARRAY : vValue = poResultSet.getArray(piColIndex)
  323. Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
  324. Set oValue = poResultSet.getBinaryStream(piColIndex)
  325. If bNullable Then
  326. If Not poResultSet.wasNull() Then
  327. If Not _hasUNOMethod(oValue, &quot;getLength&quot;) Then &apos; When no recordset
  328. lSize = cstMaxBinLength
  329. Else
  330. lSize = CLng(oValue.getLength())
  331. End If
  332. If lSize &lt;= cstMaxBinLength And pbReturnBinary Then
  333. vValue = Array()
  334. oValue.readBytes(vValue, lSize)
  335. Else &apos; Return length of field, not content
  336. vValue = lSize
  337. End If
  338. End If
  339. End If
  340. oValue.closeInput()
  341. Case .BIT, .BOOLEAN : vValue = poResultSet.getBoolean(piColIndex)
  342. Case .DATE : vDateTime = poResultSet.getDate(piColIndex)
  343. If Not poResultSet.wasNull() Then vValue = DateSerial(CInt(vDateTime.Year), CInt(vDateTime.Month), CInt(vDateTime.Day))
  344. Case .DISTINCT, .OBJECT, .OTHER, .STRUCT
  345. vValue = Null
  346. Case .DOUBLE, .REAL : vValue = poResultSet.getDouble(piColIndex)
  347. Case .FLOAT : vValue = poResultSet.getFloat(piColIndex)
  348. Case .INTEGER, .SMALLINT : vValue = poResultSet.getInt(piColIndex)
  349. Case .BIGINT : vValue = poResultSet.getLong(piColIndex)
  350. Case .DECIMAL, .NUMERIC : vValue = poResultSet.getDouble(piColIndex)
  351. Case .SQLNULL : vValue = poResultSet.getNull(piColIndex)
  352. Case .OBJECT, .OTHER, .STRUCT : vValue = Null
  353. Case .REF : vValue = poResultSet.getRef(piColIndex)
  354. Case .TINYINT : vValue = poResultSet.getShort(piColIndex)
  355. Case .CHAR, .VARCHAR : vValue = poResultSet.getString(piColIndex)
  356. Case .LONGVARCHAR, .CLOB
  357. Set oValue = poResultSet.getCharacterStream(piColIndex)
  358. If bNullable Then
  359. If Not poResultSet.wasNull() Then
  360. If Not _hasUNOMethod(oValue, &quot;getLength&quot;) Then &apos; When no recordset
  361. lSize = cstMaxTextLength
  362. Else
  363. lSize = CLng(oValue.getLength())
  364. End If
  365. oValue.closeInput()
  366. vValue = poResultSet.getString(piColIndex)
  367. End If
  368. Else
  369. oValue.closeInput()
  370. End If
  371. Case .TIME : vDateTime = poResultSet.getTime(piColIndex)
  372. If Not poResultSet.wasNull() Then vValue = TimeSerial(vDateTime.Hours, vDateTime.Minutes, vDateTime.Seconds)&apos;, vDateTime.HundredthSeconds)
  373. Case .TIMESTAMP : vDateTime = poResultSet.getTimeStamp(piColIndex)
  374. If Not poResultSet.wasNull() Then vValue = DateSerial(CInt(vDateTime.Year), CInt(vDateTime.Month), CInt(vDateTime.Day)) _
  375. + TimeSerial(vDateTime.Hours, vDateTime.Minutes, vDateTime.Seconds)&apos;, vDateTime.HundredthSeconds)
  376. Case Else
  377. vValue = poResultSet.getString(piColIndex) &apos;GIVE STRING A TRY
  378. If IsNumeric(vValue) Then vValue = Val(vValue) &apos;Required when type = &quot;&quot;, sometimes numeric fields are returned as strings (query/MSAccess)
  379. End Select
  380. If bNullable Then
  381. If poResultSet.wasNull() Then vValue = Null
  382. End If
  383. End With
  384. _GetResultSetColumnValue = vValue
  385. End Function &apos; GetResultSetColumnValue V 1.5.0
  386. REM -----------------------------------------------------------------------------------------------------------------------
  387. Public Function _FinalProperty(psShortcut As String) As String
  388. &apos; Return the final property of a shortcut
  389. Const cstEXCLAMATION = &quot;!&quot;
  390. Const cstDOT = &quot;.&quot;
  391. Dim iCurrentIndex As Integer, vCurrentObject As Variant, sCurrentProperty As String
  392. Dim sComponents() As String, sSubComponents() As String
  393. _FinalProperty = &quot;&quot;
  394. sComponents = Split(Trim(psShortcut), cstEXCLAMATION)
  395. If UBound(sComponents) = 0 Then Exit Function
  396. sSubComponents = Split(sComponents(UBound(sComponents)), cstDOT)
  397. Select Case UBound(sSubComponents)
  398. Case 1
  399. _FinalProperty = sSubComponents(1)
  400. Case Else
  401. Exit Function
  402. End Select
  403. End Function &apos; FinalProperty
  404. REM -----------------------------------------------------------------------------------------------------------------------
  405. Public Function _GetProductName(ByVal Optional psFlag As String) as String
  406. &apos;Return OO product (&quot;PRODUCT&quot;) and version numbers (&quot;VERSION&quot;)
  407. &apos;Derived from Tools library
  408. Dim oProdNameAccess as Object
  409. Dim sVersion as String
  410. Dim sProdName as String
  411. If IsMissing(psFlag) Then psFlag = &quot;ALL&quot;
  412. oProdNameAccess = _GetRegistryKeyContent(&quot;org.openoffice.Setup/Product&quot;)
  413. sProdName = oProdNameAccess.getByName(&quot;ooName&quot;)
  414. sVersion = oProdNameAccess.getByName(&quot;ooSetupVersionAboutBox&quot;)
  415. Select Case psFlag
  416. Case &quot;ALL&quot; : _GetProductName = sProdName &amp; &quot; &quot; &amp; sVersion
  417. Case &quot;PRODUCT&quot; : _GetProductName = sProdName
  418. Case &quot;VERSION&quot; : _GetProductName = sVersion
  419. End Select
  420. End Function &apos; GetProductName V1.0.0
  421. REM -----------------------------------------------------------------------------------------------------------------------
  422. Public Function _GetRandomFileName(ByVal psName As String) As String
  423. &apos; Return the full name of a random temporary file suffixed by psName
  424. Dim sRandom As String
  425. sRandom = Right(&quot;000000&quot; &amp; Int(999999 * Rnd), 6)
  426. _GetRandomFileName = Utils._getTempDirectoryURL() &amp; &quot;/&quot; &amp; &quot;A2B_TEMP_&quot; &amp; psName &amp; &quot;_&quot; &amp; sRandom
  427. End Function &apos; GetRandomFileName
  428. REM -----------------------------------------------------------------------------------------------------------------------
  429. Public Function _GetRegistryKeyContent(sKeyName as string, Optional bforUpdate as Boolean) As Variant
  430. &apos;Implement ConfigurationProvider service
  431. &apos;Derived from Tools library
  432. Dim oConfigProvider as Object
  433. Dim aNodePath(0) as new com.sun.star.beans.PropertyValue
  434. oConfigProvider = createUnoService(&quot;com.sun.star.configuration.ConfigurationProvider&quot;)
  435. aNodePath(0).Name = &quot;nodepath&quot;
  436. aNodePath(0).Value = sKeyName
  437. If IsMissing(bForUpdate) Then bForUpdate = False
  438. If bForUpdate Then
  439. _GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationUpdateAccess&quot;, aNodePath())
  440. Else
  441. _GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationAccess&quot;, aNodePath())
  442. End If
  443. End Function &apos; GetRegistryKeyContent V0.8.5
  444. REM -----------------------------------------------------------------------------------------------------------------------
  445. Public Function _getTempDirectoryURL() As String
  446. &apos; Return the temporary directory defined in the OO Options (Paths)
  447. Dim sDirectory As String, oSettings As Object, oPathSettings As Object
  448. If _ErrorHandler() Then On Local Error Goto Error_Function
  449. _getTempDirectoryURL = &quot;&quot;
  450. oPathSettings = createUnoService( &quot;com.sun.star.util.PathSettings&quot; )
  451. sDirectory = oPathSettings.GetPropertyValue( &quot;Temp&quot; )
  452. _getTempDirectoryURL = sDirectory
  453. Exit_Function:
  454. Exit Function
  455. Error_Function:
  456. TraceError(&quot;ERROR&quot;, Err, &quot;_getTempDirectoryURL&quot;, Erl)
  457. _getTempDirectoryURL = &quot;&quot;
  458. Goto Exit_Function
  459. End Function &apos; _getTempDirectoryURL V0.8.5
  460. REM -----------------------------------------------------------------------------------------------------------------------
  461. Public Function _getUNOTypeName(pvObject As Variant) As String
  462. &apos; Return the symbolic name of the pvObject (UNO-object) type
  463. &apos; Code-snippet from XRAY
  464. Dim oService As Object, vClass as Variant
  465. _getUNOTypeName = &quot;&quot;
  466. On Local Error Resume Next
  467. oService = CreateUnoService(&quot;com.sun.star.reflection.CoreReflection&quot;)
  468. vClass = oService.getType(pvObject)
  469. If vClass.TypeClass = com.sun.star.uno.TypeClass.STRUCT Then
  470. _getUNOTypeName = vClass.Name
  471. End If
  472. oService.Dispose()
  473. End Function &apos; getUNOTypeName
  474. REM -----------------------------------------------------------------------------------------------------------------------
  475. Public Function _hasUNOMethod(pvObject As Variant, psMethod As String) As Boolean
  476. &apos; Return true if pvObject has the (UNO) method psMethod
  477. &apos; Code-snippet found in Bernard Marcelly&apos;s XRAY
  478. Dim vInspect as Variant
  479. _hasUNOMethod = False
  480. If IsNull(pvObject) Then Exit Function
  481. On Local Error Resume Next
  482. vInspect = _A2B_.Introspection.Inspect(pvObject)
  483. _hasUNOMethod = vInspect.hasMethod(psMethod, com.sun.star.beans.MethodConcept.ALL)
  484. End Function &apos; hasUNOMethod V0.8.0
  485. REM -----------------------------------------------------------------------------------------------------------------------
  486. Public Function _hasUNOProperty(pvObject As Variant, psProperty As String) As Boolean
  487. &apos; Return true if pvObject has the (UNO) property psProperty
  488. &apos; Code-snippet found in Bernard Marcelly&apos;s XRAY
  489. Dim vInspect as Variant
  490. _hasUNOProperty = False
  491. If IsNull(pvObject) Then Exit Function
  492. On Local Error Resume Next
  493. vInspect = _A2B_.Introspection.Inspect(pvObject)
  494. _hasUNOProperty = vInspect.hasProperty(psProperty, com.sun.star.beans.PropertyConcept.ALL)
  495. End Function &apos; hasUNOProperty V0.8.0
  496. REM -----------------------------------------------------------------------------------------------------------------------
  497. Public Function _ImplementationName(pvObject As Variant) As String
  498. &apos; Use getImplementationName method or _getUNOTypeName function
  499. Dim sObjectType As String
  500. On Local Error Resume Next
  501. sObjectType = pvObject.getImplementationName()
  502. If sObjectType = &quot;&quot; Then sObjectType = _getUNOTypeName(pvObject)
  503. _ImplementationName = sObjectType
  504. End Function &apos; ImplementationName
  505. REM -----------------------------------------------------------------------------------------------------------------------
  506. Public Function _InList(ByVal pvItem As Variant, pvList As Variant, ByVal Optional pvReturnValue As Variant, Optional ByVal pbBinarySearch As Boolean) As Variant
  507. &apos; Return True if pvItem is present in the pvList array (case insensitive comparison)
  508. &apos; Return the value in pvList if pvReturnValue = True
  509. Dim i As Integer, bFound As Boolean, iListVarType As Integer, iItemVarType As Integer
  510. Dim iTop As Integer, iBottom As Integer, iFound As Integer
  511. iItemVarType = VarType(pvItem)
  512. If IsMissing(pvReturnValue) Then pvReturnValue = False
  513. If iItemVarType = vbNull Or IsNull(pvList) Then
  514. _InList = False
  515. ElseIf Not IsArray(pvList) Then
  516. If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList) ) Else bFound = ( pvItem = pvList )
  517. If Not pvReturnValue Then
  518. _InList = bFound
  519. Else
  520. If bFound Then _InList = pvList Else _InList = False
  521. End If
  522. ElseIf UBound(pvList) &lt; LBound(pvList) Then &apos; Array not initialized
  523. _InList = False
  524. Else
  525. bFound = False
  526. _InList = False
  527. iListVarType = VarType(pvList(LBound(pvList)))
  528. If iListVarType = iItemVarType _
  529. Or ( (iListVarType = vbInteger Or iListVarType = vbLong Or iListVarType = vbSingle Or iListVarType = vbDouble _
  530. Or iListVarType = vbCurrency Or iListVarType = vbBigint Or iListVarType = vbDecimal) _
  531. And (iItemVarType = vbInteger Or iItemVarType = vbLong Or iItemVarType = vbSingle Or iItemVarType = vbDouble _
  532. Or iItemVarType = vbCurrency Or iItemVarType = vbBigint Or iItemVarType = vbDecimal) _
  533. ) Then
  534. If IsMissing(pbBinarySearch) Then pbBinarySearch = False
  535. If Not pbBinarySearch Then &apos; Linear search
  536. For i = LBound(pvList) To UBound(pvList)
  537. If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList(i)) ) Else bFound = ( pvItem = pvList(i) )
  538. If bFound Then
  539. iFound = i
  540. Exit For
  541. End If
  542. Next i
  543. Else &apos; Binary search =&gt; array must be sorted
  544. iTop = UBound(pvList)
  545. iBottom = lBound(pvList)
  546. Do
  547. iFound = (iTop + iBottom) / 2
  548. If ( iItemVarType = vbString And UCase(pvItem) &gt; UCase(pvList(iFound)) ) Or ( iItemVarType &lt;&gt; vbString And pvItem &gt; pvList(iFound) ) Then
  549. iBottom = iFound + 1
  550. Else
  551. iTop = iFound - 1
  552. End If
  553. If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList(iFound)) ) Else bFound = ( pvItem = pvList(iFound) )
  554. Loop Until ( bFound ) Or ( iBottom &gt; iTop )
  555. End If
  556. If bFound Then
  557. If Not pvReturnValue Then _InList = True Else _InList = pvList(iFound)
  558. End If
  559. End If
  560. End If
  561. Exit Function
  562. End Function &apos; InList V1.1.0
  563. REM -----------------------------------------------------------------------------------------------------------------------
  564. Public Function _InspectPropertyType(poObject As Object, psProperty As String) As String
  565. &apos;Return type of property EVEN WHEN EMPTY ! (Used in date and time controls)
  566. Dim oInspect1 As Object, oInspect2 As Object, oInspect3 As Object
  567. &apos; On Local Error Resume Next
  568. _InspectPropertyType = &quot;&quot;
  569. Set oInspect1 = CreateUnoService(&quot;com.sun.star.script.Invocation&quot;)
  570. Set oInspect2 = oInspect1.createInstanceWithArguments(Array(poObject)).IntroSpection
  571. If Not IsNull(oInspect2) Then
  572. Set oInspect3 = oInspect2.getProperty(psProperty, com.sun.star.beans.PropertyConcept.ALL)
  573. If Not IsNull(oInspect3) Then _InspectPropertyType = oInspect3.Type.Name
  574. End If
  575. Set oInspect1 = Nothing : Set oInspect2 = Nothing : Set oInspect3 = Nothing
  576. End Function &apos; InspectPropertyType V1.0.0
  577. REM -----------------------------------------------------------------------------------------------------------------------
  578. Public Function _IsLeft(psString As String, psLeft As String) As Boolean
  579. &apos; Return True if left part of psString = psLeft
  580. Dim iLength As Integer
  581. iLength = Len(psLeft)
  582. _IsLeft = False
  583. If Len(psString) &gt;= iLength Then
  584. If Left(psString, iLength) = psLeft Then _IsLeft = True
  585. End If
  586. End Function
  587. REM -----------------------------------------------------------------------------------------------------------------------
  588. Public Function _IsBinaryType(ByVal lType As Long) As Boolean
  589. With com.sun.star.sdbc.DataType
  590. Select Case lType
  591. Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
  592. _IsBinaryType = True
  593. Case Else
  594. _IsBinaryType = False
  595. End Select
  596. End With
  597. End Function &apos; IsBinaryType V1.6.0
  598. REM -----------------------------------------------------------------------------------------------------------------------
  599. Public Function _IsPseudo(pvObject As Variant, ByVal pvType As Variant) As Boolean
  600. &apos; Test pvObject: does it exist ?
  601. &apos; is the _Type item = one of the proposed pvTypes ?
  602. &apos; does the pseudo-object refer to an existing object (e.g. does the form really exist in the db) ?
  603. Dim bIsPseudo As Boolean, bPseudoExists As Boolean, vObject As Variant
  604. If _ErrorHandler() Then On Local Error Goto Exit_False
  605. _IsPseudo = False
  606. bIsPseudo = False
  607. vObject = pvObject &apos; To avoid &quot;Object variable not set&quot; error message
  608. Select Case True
  609. Case IsEmpty(vObject)
  610. Case IsNull(vObject)
  611. Case VarType(vObject) &lt;&gt; vbObject
  612. Case Else
  613. With vObject
  614. Select Case True
  615. Case IsEmpty(._Type)
  616. Case IsNull(._Type)
  617. Case ._Type = &quot;&quot;
  618. Case Else
  619. bIsPseudo = _InList(._Type, pvType)
  620. If Not bIsPseudo Then &apos; If primary type did not succeed, give the subtype a chance
  621. If ._Type = OBJCONTROL Then bIsPseudo = _InList(._SubType, pvType)
  622. End If
  623. End Select
  624. End With
  625. End Select
  626. If Not bIsPseudo Then Goto Exit_Function
  627. Dim oDoc As Object, oForms As Variant
  628. Const cstSeparator = &quot;\;&quot;
  629. bPseudoExists = False
  630. With vObject
  631. Select Case ._Type
  632. Case OBJFORM
  633. If ._Name &lt;&gt; &quot;&quot; Then &apos; Check validity of form name
  634. Set oDoc = _A2B_.CurrentDocument()
  635. If oDoc.DbConnect = DBCONNECTFORM Then bPseudoExists = True Else bPseudoExists = _InList(._Name, Application._GetAllHierarchicalNames())
  636. End If
  637. Case OBJDATABASE
  638. If ._DbConnect = DBCONNECTFORM Then bPseudoExists = True Else bPseudoExists = Not IsNull(.Connection)
  639. Case OBJDIALOG
  640. If ._Name &lt;&gt; &quot;&quot; Then &apos; Check validity of dialog name
  641. bPseudoExists = ( _A2B_.hasItem(COLLALLDIALOGS, ._Name) )
  642. End If
  643. Case OBJCOLLECTION
  644. bPseudoExists = True
  645. Case OBJCONTROL
  646. If Not IsNull(.ControlModel) And ._Name &lt;&gt; &quot;&quot; Then &apos; Check validity of control
  647. Set oForms = .ControlModel.Parent
  648. bPseudoExists = ( oForms.hasByName(._Name) )
  649. End If
  650. Case OBJSUBFORM
  651. If Not IsNull(.DatabaseForm) And ._Name &lt;&gt; &quot;&quot; Then &apos; Check validity of subform
  652. If .DatabaseForm.ImplementationName = &quot;com.sun.star.comp.forms.ODatabaseForm&quot; Then
  653. Set oForms = .DatabaseForm.Parent
  654. bPseudoExists = ( oForms.hasByName(._Name) )
  655. End If
  656. End If
  657. Case OBJOPTIONGROUP
  658. bPseudoExists = ( .Count &gt; 0 )
  659. Case OBJCOMMANDBAR
  660. bPseudoExists = ( Not IsNull(._Window) )
  661. Case OBJCOMMANDBARCONTROL
  662. bPseudoExists = ( Not IsNull(._ParentCommandBar) )
  663. Case OBJEVENT
  664. bPseudoExists = ( Not IsNull(._EventSource) )
  665. Case OBJPROPERTY
  666. bPseudoExists = ( ._Name &lt;&gt; &quot;&quot; )
  667. Case OBJTABLEDEF
  668. bPseudoExists = ( ._Name &lt;&gt; &quot;&quot; And Not IsNull(.Table) )
  669. Case OBJQUERYDEF
  670. bPseudoExists = ( ._Name &lt;&gt; &quot;&quot; And Not IsNull(.Query) )
  671. Case OBJRECORDSET
  672. bPseudoExists = ( Not IsNull(.RowSet) )
  673. Case OBJFIELD
  674. bPseudoExists = ( ._Name &lt;&gt; &quot;&quot; And Not IsNull(.Column) )
  675. Case OBJTEMPVAR
  676. If ._Name &lt;&gt; &quot;&quot; Then &apos; Check validity of tempvar name
  677. bPseudoExists = ( _A2B_.hasItem(COLLTEMPVARS, ._Name) )
  678. End If
  679. Case Else
  680. End Select
  681. End With
  682. _IsPseudo = ( bIsPseudo And bPseudoExists )
  683. Exit_Function:
  684. Exit Function
  685. Exit_False:
  686. _IsPseudo = False
  687. Goto Exit_Function
  688. End Function &apos; IsPseudo V1.1.0
  689. REM -----------------------------------------------------------------------------------------------------------------------
  690. Private Function _IsScalar(ByVal pvArg As Variant, ByVal pvType As Variant, ByVal Optional pvValid As Variant) As Boolean
  691. &apos; Check type of pvArg and value in allowed pvValid list
  692. _IsScalar = False
  693. If IsArray(pvType) Then
  694. If Not _InList(VarType(pvArg), pvType) Then Exit Function
  695. ElseIf VarType(pvArg) &lt;&gt; pvType Then
  696. If pvType = vbBoolean And VarType(pvArg) = vbLong Then
  697. If pvArg &lt; -1 And pvArg &gt; 0 Then Exit Function &apos; Special boolean processing because the Not function returns a Long
  698. Else
  699. Exit Function
  700. End If
  701. End If
  702. If Not IsMissing(pvValid) Then
  703. If Not _InList(pvArg, pvValid) Then Exit Function
  704. End If
  705. _IsScalar = True
  706. Exit_Function:
  707. Exit Function
  708. End Function &apos; IsScalar V0.7.5
  709. REM -----------------------------------------------------------------------------------------------------------------------
  710. Public Function _PCase(ByVal psString As String) As String
  711. &apos; Return the proper case representation of argument
  712. Dim vSubStrings() As Variant, i As Integer, iLen As Integer
  713. vSubStrings = Split(psString, &quot; &quot;)
  714. For i = 0 To UBound(vSubStrings)
  715. iLen = Len(vSubStrings(i))
  716. If iLen &gt; 1 Then
  717. vSubStrings(i) = UCase(Left(vSubStrings(i), 1)) &amp; LCase(Right(vSubStrings(i), iLen - 1))
  718. ElseIf iLen = 1 Then
  719. vSubStrings(i) = UCase(vSubStrings(i))
  720. End If
  721. Next i
  722. _PCase = Join(vSubStrings, &quot; &quot;)
  723. End Function &apos; PCase V0.9.0
  724. REM -----------------------------------------------------------------------------------------------------------------------
  725. Private Function _PercentEncode(ByVal psChar As String) As String
  726. &apos; Percent encoding of single psChar character
  727. &apos; https://en.wikipedia.org/wiki/UTF-8
  728. Dim lChar As Long, sByte1 As String, sByte2 As String, sByte3 As String
  729. lChar = Asc(psChar)
  730. Select Case lChar
  731. Case 48 To 57, 65 To 90, 97 To 122 &apos; 0-9, A-Z, a-z
  732. _PercentEncode = psChar
  733. Case Asc(&quot;-&quot;), Asc(&quot;.&quot;), Asc(&quot;_&quot;), Asc(&quot;~&quot;)
  734. _PercentEncode = psChar
  735. Case Asc(&quot;!&quot;), Asc(&quot;$&quot;), Asc(&quot;&amp;&quot;), Asc(&quot;&apos;&quot;), Asc(&quot;(&quot;), Asc(&quot;)&quot;), Asc(&quot;*&quot;), Asc(&quot;+&quot;), Asc(&quot;,&quot;), Asc(&quot;;&quot;), Asc(&quot;=&quot;) &apos; Reserved characters used as delimiters in query strings
  736. _PercentEncode = psChar
  737. Case Asc(&quot; &quot;), Asc(&quot;%&quot;)
  738. _PercentEncode = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex(lChar), 2)
  739. Case 0 To 127
  740. _PercentEncode = psChar
  741. Case 128 To 2047
  742. sByte1 = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex(Int(lChar / 64) + 192), 2)
  743. sByte2 = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex((lChar Mod 64) + 128), 2)
  744. _PercentEncode = sByte1 &amp; sByte2
  745. Case 2048 To 65535
  746. sByte1 = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex(Int(lChar / 4096) + 224), 2)
  747. sByte2 = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex(Int(lChar - (4096 * Int(lChar / 4096))) /64 + 128), 2)
  748. sByte3 = &quot;%&quot; &amp; Right(&quot;00&quot; &amp; Hex((lChar Mod 64) + 128), 2)
  749. _PercentEncode = sByte1 &amp; sByte2 &amp; sByte3
  750. Case Else &apos; Not supported
  751. _PercentEncode = psChar
  752. End Select
  753. Exit Function
  754. End Function &apos; _PercentEncode V1.4.0
  755. REM -----------------------------------------------------------------------------------------------------------------------
  756. Public Function _ReadFileIntoArray(ByVal psFileName) As Variant
  757. &apos; Loads all lines of a text file into a Variant array
  758. &apos; Any error reduces output to an empty array
  759. &apos; Input file name presumed in URL form
  760. Dim vLines() As Variant, iFile As Integer, sLine As String, iCount1 As Integer, iCount2 As Integer
  761. Const cstMaxLines = 16000 &apos; +/- the limit of array sizes in Basic
  762. On Local Error GoTo Error_Function
  763. vLines = Array()
  764. _ReadFileIntoArray = Array()
  765. If psFileName = &quot;&quot; Then Exit Function
  766. iFile = FreeFile()
  767. Open psFileName For Input Access Read Shared As #iFile
  768. iCount1 = 0
  769. Do While Not Eof(iFile) And iCount1 &lt; cstMaxLines
  770. Line Input #iFile, sLine
  771. iCount1 = iCount1 + 1
  772. Loop
  773. Close #iFile
  774. ReDim vLines(0 To iCount1 - 1) &apos; Reading file twice preferred to ReDim Preserve for performance reasons
  775. iFile = FreeFile()
  776. Open psFileName For Input Access Read Shared As #iFile
  777. iCount2 = 0
  778. Do While Not Eof(iFile) And iCount2 &lt; iCount1
  779. Line Input #iFile, vLines(iCount2)
  780. iCount2 = iCount2 + 1
  781. Loop
  782. Close #iFile
  783. Exit_Function:
  784. _ReadFileIntoArray() = vLines()
  785. Exit Function
  786. Error_Function:
  787. vLines = Array()
  788. Resume Exit_Function
  789. End Function &apos; _ReadFileIntoArray V1.4.0
  790. REM -----------------------------------------------------------------------------------------------------------------------
  791. Public Function _RegexSearch(ByRef psString As String _
  792. , ByVal psRegex As String _
  793. , Optional ByRef plStart As Long _
  794. , Optional ByVal bForward As Boolean _
  795. ) As String
  796. &apos; Search is not case-sensitive
  797. &apos; Return &quot;&quot; if regex not found, otherwise returns the matching string
  798. &apos; plStart = start position of psString to search (starts at 1)
  799. &apos; In output plStart contains the first position of the matching string
  800. &apos; To search again the same or another pattern =&gt; plStart = plStart + Len(matching string)
  801. Dim oTextSearch As Object
  802. Dim vOptions As Variant &apos;com.sun.star.util.SearchOptions
  803. Dim lEnd As Long, vResult As Object
  804. _RegexSearch = &quot;&quot;
  805. Set oTextSearch = _A2B_.TextSearch &apos; UNO XTextSearch service
  806. vOptions = _A2B_.SearchOptions
  807. vOptions.searchString = psRegex &apos; Pattern to be searched
  808. oTextSearch.setOptions(vOptions)
  809. If IsMissing(plStart) Then plStart = 1
  810. If plStart &lt;= 0 Or plStart &gt; Len(psString) Then Exit Function
  811. If IsMissing(bForWard) Then bForward = True
  812. If bForward Then
  813. lEnd = Len(psString)
  814. vResult = oTextSearch.searchForward(psString, plStart - 1, lEnd)
  815. Else
  816. lEnd = 1
  817. vResult = oTextSearch.searchForward(psString, plStart, lEnd - 1)
  818. End If
  819. With vResult
  820. If .subRegExpressions &gt;= 1 Then
  821. &apos; http://www.openoffice.org/api/docs/common/ref/com/sun/star/util/SearchResult.html
  822. Select Case bForward
  823. Case True
  824. plStart = .startOffset(0) + 1
  825. lEnd = .endOffset(0) + 1
  826. Case False
  827. plStart = .endOffset(0) + 1
  828. lEnd = .startOffset(0)
  829. End Select
  830. _RegexSearch = Mid(psString, plStart, lEnd - plStart)
  831. Else
  832. plStart = 0
  833. End If
  834. End With
  835. End Function
  836. REM -----------------------------------------------------------------------------------------------------------------------
  837. Public Function _RegisterDialogEventScript(poObject As Object _
  838. , ByVal psEvent As String _
  839. , ByVal psListener As String _
  840. , ByVal psScriptCode As String _
  841. ) As Boolean
  842. &apos; Register a script event (psEvent) to poObject (Dialog or dialog Control)
  843. Dim oEvents As Object, sEvent As String, sEventName As String, oEvent As Object
  844. _RegisterDialogEventScript = False
  845. If Not _hasUNOMethod(poObject, &quot;getEvents&quot;) Then Exit Function
  846. &apos; Remove existing event, if any, then store new script code
  847. Set oEvents = poObject.getEvents()
  848. sEvent = Utils._GetEventName(psEvent)
  849. sEventName = &quot;com.sun.star.awt.&quot; &amp; psListener &amp; &quot;::&quot; &amp; sEvent
  850. If oEvents.hasByName(sEventName) Then oEvents.removeByName(sEventName)
  851. Set oEvent = CreateUnoStruct(&quot;com.sun.star.script.ScriptEventDescriptor&quot;)
  852. With oEvent
  853. .ListenerType = psListener
  854. .EventMethod = sEvent
  855. .ScriptType = &quot;Script&quot; &apos; Better than &quot;Basic&quot;
  856. .ScriptCode = psScriptCode
  857. End With
  858. oEvents.insertByName(sEventName, oEvent)
  859. _RegisterDialogEventScript = True
  860. End Function &apos; _RegisterDialogEventScript V1.8.0
  861. REM -----------------------------------------------------------------------------------------------------------------------
  862. Public Function _RegisterEventScript(poObject As Object _
  863. , ByVal psEvent As String _
  864. , ByVal psListener As String _
  865. , ByVal psScriptCode As String _
  866. , ByVal psName As String _
  867. , Optional ByVal pbExtendName As Boolean _
  868. ) As Boolean
  869. &apos; Register a script event (psEvent) to poObject (Form, SubForm or Control)
  870. Dim i As Integer, oEvent As Object, sEvent As String, oParent As Object, iIndex As Integer, sName As String
  871. _RegisterEventScript = False
  872. If Not _hasUNOMethod(poObject, &quot;getParent&quot;) Then Exit Function
  873. &apos; Find object internal index i.e. how to reach it via getByIndex()
  874. If IsMissing(pbExtendName) Then pbExtendName = False
  875. Set oParent = poObject.getParent()
  876. iIndex = -1
  877. For i = 0 To oParent.getCount() - 1
  878. sName = oParent.getByIndex(i).Name
  879. If (sName = psName) Or (pbExtendName And (sName = &quot;MainForm&quot; Or sName = &quot;Form&quot;)) Then
  880. iIndex = i
  881. Exit For
  882. End If
  883. Next i
  884. If iIndex &lt; 0 Then Exit Function
  885. sEvent = Utils._GetEventName(psEvent) &apos; Targeted event method
  886. If psScriptCode = &quot;&quot; Then
  887. oParent.revokeScriptEvent(iIndex, psListener, sEvent, &quot;&quot;)
  888. Else
  889. Set oEvent = CreateUnoStruct(&quot;com.sun.star.script.ScriptEventDescriptor&quot;)
  890. With oEvent
  891. .ListenerType = psListener
  892. .EventMethod = sEvent
  893. .ScriptType = &quot;Script&quot; &apos; Better than &quot;Basic&quot;
  894. .ScriptCode = psScriptCode
  895. End With
  896. oParent.registerScriptEvent(iIndex, oEvent)
  897. End If
  898. _RegisterEventScript = True
  899. End Function &apos; _RegisterEventScript V1.7.0
  900. REM -----------------------------------------------------------------------------------------------------------------------
  901. Public Sub _ResetCalledSub(ByVal psSub As String)
  902. &apos; Called in bottom of each public function. _A2B_.CalledSub variable is used for error handling
  903. &apos; Used to trace routine in/outs and to clarify error messages
  904. If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; Only when Utils module recompiled
  905. With _A2B_
  906. If .CalledSub = psSub Then .CalledSub = &quot;&quot;
  907. If .MinimalTraceLevel = 1 Then TraceLog(TRACEDEBUG, _GetLabel(&quot;Exiting&quot;) &amp; &quot; &quot; &amp; psSub &amp; &quot; ...&quot;, False)
  908. End With
  909. End Sub &apos; ResetCalledSub
  910. REM -----------------------------------------------------------------------------------------------------------------------
  911. Public Function _RunScript(ByVal psScript As String, Optional pvArgs() As Variant) As Boolean
  912. &apos; Execute a given script with pvArgs() array of arguments
  913. On Local Error Goto Error_Function
  914. _RunScript = False
  915. If IsNull(ThisComponent) Then Goto Exit_Function
  916. Dim oSCriptProvider As Object, oScript As Object, vResult As Variant
  917. Set oScriptProvider = ThisComponent.ScriptProvider()
  918. Set oScript = oScriptProvider.getScript(psScript)
  919. If IsMissing(pvArgs()) Then pvArgs() = Array()
  920. vResult = oScript.Invoke(pvArgs(), Array(), Array())
  921. _RunScript = True
  922. Exit_Function:
  923. Exit Function
  924. Error_Function:
  925. _RunScript = False
  926. Goto Exit_Function
  927. End Function
  928. REM -----------------------------------------------------------------------------------------------------------------------
  929. Public Sub _SetCalledSub(ByVal psSub As String)
  930. &apos; Called in top of each public function.
  931. &apos; Used to trace routine in/outs and to clarify error messages
  932. If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; First use of Access2Base in current LibO/AOO session
  933. With _A2B_
  934. If .CalledSub = &quot;&quot; Then
  935. .CalledSub = psSub
  936. .LastErrorCode = 0
  937. .LastErrorLevel = &quot;&quot;
  938. .ErrorText = &quot;&quot;
  939. .ErrorLongText = &quot;&quot;
  940. End If
  941. If .MinimalTraceLevel = 1 Then TraceLog(TRACEDEBUG, _GetLabel(&quot;Entering&quot;) &amp; &quot; &quot; &amp; psSub &amp; &quot; ...&quot;, False)
  942. End With
  943. End Sub &apos; SetCalledSub
  944. REM -----------------------------------------------------------------------------------------------------------------------
  945. Public Function _Surround(ByVal psName As String) As String
  946. &apos; Return [Name] if Name contains spaces
  947. &apos; Return [Name1].[Name2].[Name3] if Name1.Name2.Name3 contains dots
  948. Const cstSquareOpen = &quot;[&quot;
  949. Const cstSquareClose = &quot;]&quot;
  950. Const cstDot = &quot;.&quot;
  951. Dim sName As String
  952. If InStr(psName, &quot;.&quot;) &gt; 0 Then
  953. sName = Join(Split(psName, cstDot), cstSquareClose &amp; cstDot &amp; cstSquareOpen)
  954. _Surround = cstSquareOpen &amp; sName &amp; cstSquareClose
  955. ElseIf InStr(psName, &quot; &quot;) &gt; 0 Then
  956. _Surround = cstSquareOpen &amp; psName &amp; cstSquareClose
  957. Else
  958. _Surround = psName
  959. End If
  960. End Function &apos; Surround
  961. REM -----------------------------------------------------------------------------------------------------------------------
  962. Public Function _Trim(ByVal psString As String) As String
  963. &apos; Remove leading and trailing spaces, remove surrounding square brackets, replace tabs by spaces
  964. Const cstSquareOpen = &quot;[&quot;
  965. Const cstSquareClose = &quot;]&quot;
  966. Dim sTrim As String
  967. sTrim = Trim(Replace(psString, vbTab, &quot; &quot;))
  968. _Trim = sTrim
  969. If Len(sTrim) &lt;= 2 Then Exit Function
  970. If Left(sTrim, 1) = cstSquareOpen Then
  971. If Right(sTrim, 1) = cstSquareClose Then
  972. _Trim = Mid(sTrim, 2, Len(sTrim) - 2)
  973. End If
  974. End If
  975. End Function &apos; Trim V0.9.0
  976. REM -----------------------------------------------------------------------------------------------------------------------
  977. Public Function _TrimArray(pvArray As Variant) As Variant
  978. &apos; Remove empty strings from strings array
  979. Dim sTrim As String, vTrim() As Variant, i As Integer, j As Integer, iCount As Integer
  980. vTrim = Null
  981. If Not IsArray(pvArray) Then
  982. If Len(Trim(pvArray)) &gt; 0 Then vTrim = Array(pvArray) Else vTrim = Array()
  983. ElseIf UBound(pvArray) &lt; LBound(pvArray) Then &apos; Array empty
  984. vTrim = Array()
  985. Else
  986. iCount = 0
  987. For i = LBound(pvArray) To UBound(pvArray)
  988. If Len(Trim(pvArray(i))) = 0 Then iCount = iCount + 1
  989. Next i
  990. If iCount = 0 Then
  991. vTrim() = pvArray()
  992. ElseIf iCount = UBound(pvArray) - LBound(pvArray) + 1 Then &apos; Array empty or all blanks
  993. vTrim() = Array()
  994. Else
  995. ReDim vTrim(LBound(pvArray) To UBound(pvArray) - iCount)
  996. j = 0
  997. For i = LBound(pvArray) To UBound(pvArray)
  998. If Len(Trim(pvArray(i))) &gt; 0 Then
  999. vTrim(j) = pvArray(i)
  1000. j = j + 1
  1001. End If
  1002. Next i
  1003. End If
  1004. End If
  1005. _TrimArray() = vTrim()
  1006. End Function &apos; TrimArray V0.9.0
  1007. REM -----------------------------------------------------------------------------------------------------------------------
  1008. Private Function _UpdateResultSetColumnValue(piRDBMS As Integer _
  1009. , poResultSet As Object _
  1010. , ByVal piColIndex As Integer _
  1011. , ByVal pvValue As Variant _
  1012. ) As Boolean
  1013. REM store the pvValue for the column specified by ColIndex
  1014. REM get type name from metadata
  1015. Dim iType As Integer, vDateTime As Variant, oValue As Object
  1016. Dim bNullable As Boolean, lSize As Long, iValueType As Integer, sValueTypeName As String
  1017. Const cstMaxTextLength = 65535
  1018. Const cstMaxBinlength = 2 * 65535
  1019. On Local Error Goto 0 &apos; Disable error handler
  1020. _UpdateResultSetColumnValue = False
  1021. With com.sun.star.sdbc.DataType
  1022. iType = poResultSet.MetaData.getColumnType(piColIndex)
  1023. iValueType = VarType(pvValue)
  1024. sValueTypeName = UCase(poResultSet.MetaData.getColumnTypeName(piColIndex))
  1025. bNullable = ( poResultSet.MetaData.IsNullable(piColIndex) = com.sun.star.sdbc.ColumnValue.NULLABLE )
  1026. If bNullable And IsNull(pvValue) Then
  1027. poResultSet.updateNull(piColIndex)
  1028. Else
  1029. Select Case iType
  1030. Case .ARRAY, .DISTINCT, .OBJECT, .OTHER, .REF, .SQLNULL, .STRUCT
  1031. poResultSet.updateNull(piColIndex)
  1032. Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
  1033. poResultSet.updateBytes(piColIndex, pvValue)
  1034. Case .BIT, .BOOLEAN : poResultSet.updateBoolean(piColIndex, pvValue)
  1035. Case .DATE : vDateTime = CreateUnoStruct(&quot;com.sun.star.util.Date&quot;)
  1036. vDateTime.Year = Year(pvValue)
  1037. vDateTime.Month = Month(pvValue)
  1038. vDateTime.Day = Day(pvValue)
  1039. poResultSet.updateDate(piColIndex, vDateTime)
  1040. Case .DECIMAL, .NUMERIC : poResultSet.updateDouble(piColIndex, pvValue)
  1041. Case .DOUBLE, .REAL : poResultSet.updateDouble(piColIndex, pvValue)
  1042. Case .FLOAT : poResultSet.updateFloat(piColIndex, pvValue)
  1043. Case .INTEGER, .SMALLINT : poResultSet.updateInt(piColIndex, pvValue)
  1044. Case .BIGINT : poResultSet.updateLong(piColIndex, pvValue)
  1045. Case .DECIMAL, .NUMERIC : poResultSet.updateDouble(piColIndex, pvValue)
  1046. Case .TINYINT : poResultSet.updateShort(piColIndex, pvValue)
  1047. Case .CHAR, .VARCHAR, .LONGVARCHAR, .CLOB
  1048. If piRDBMS = DBMS_SQLITE And InStr(sValueTypeName, &quot;BINARY&quot;) &gt; 0 Then &apos; Sqlite exception ... !
  1049. poResultSet.updateBytes(piColIndex, pvValue)
  1050. Else
  1051. poResultSet.updateString(piColIndex, pvValue)
  1052. End If
  1053. Case .TIME : vDateTime = CreateUnoStruct(&quot;com.sun.star.util.Time&quot;)
  1054. vDateTime.Hours = Hour(pvValue)
  1055. vDateTime.Minutes = Minute(pvValue)
  1056. vDateTime.Seconds = Second(pvValue)
  1057. &apos;vDateTime.HundredthSeconds = 0
  1058. poResultSet.updateTime(piColIndex, vDateTime)
  1059. Case .TIMESTAMP : vDateTime = CreateUnoStruct(&quot;com.sun.star.util.DateTime&quot;)
  1060. vDateTime.Year = Year(pvValue)
  1061. vDateTime.Month = Month(pvValue)
  1062. vDateTime.Day = Day(pvValue)
  1063. vDateTime.Hours = Hour(pvValue)
  1064. vDateTime.Minutes = Minute(pvValue)
  1065. vDateTime.Seconds = Second(pvValue)
  1066. &apos;vDateTime.HundredthSeconds = 0
  1067. poResultSet.updateTimestamp(piColIndex, vDateTime)
  1068. Case Else
  1069. If bNullable Then poResultSet.updateNull(piColIndex)
  1070. End Select
  1071. End If
  1072. End With
  1073. _UpdateResultSetColumnValue = True
  1074. End Function &apos; UpdateResultSetColumnValue V 1.6.0
  1075. REM -----------------------------------------------------------------------------------------------------------------------
  1076. Private Function _URLEncode(ByVal psToEncode As String) As String
  1077. &apos; http://www.w3schools.com/tags/ref_urlencode.asp
  1078. &apos; http://xkr.us/articles/javascript/encode-compare/
  1079. &apos; http://tools.ietf.org/html/rfc3986
  1080. Dim sEncoded As String, sChar As String
  1081. Dim lCurrentChar As Long, bQuestionMark As Boolean
  1082. sEncoded = &quot;&quot;
  1083. bQuestionMark = False
  1084. For lCurrentChar = 1 To Len(psToEncode)
  1085. sChar = Mid(psToEncode, lCurrentChar, 1)
  1086. Select Case sChar
  1087. Case &quot; &quot;, &quot;%&quot;
  1088. sEncoded = sEncoded &amp; _PercentEncode(sChar)
  1089. Case &quot;?&quot; &apos; Is it the first &quot;?&quot; ?
  1090. If bQuestionMark Then &apos; &quot;?&quot; introduces in a URL the arguments part
  1091. sEncoded = sEncoded &amp; _PercentEncode(sChar)
  1092. Else
  1093. sEncoded = sEncoded &amp; sChar
  1094. bQuestionMark = True
  1095. End If
  1096. Case &quot;\&quot;
  1097. If bQuestionMark Then
  1098. sEncoded = sEncoded &amp; _PercentEncode(sChar)
  1099. Else
  1100. sEncoded = sEncoded &amp; &quot;/&quot; &apos; If Windows file naming ...
  1101. End If
  1102. Case Else
  1103. If bQuestionMark Then
  1104. sEncoded = sEncoded &amp; _PercentEncode(sChar)
  1105. Else
  1106. sEncoded = sEncoded &amp; _UTF8Encode(sChar) &apos; Because IE does not support %encoding in first part of URL
  1107. End If
  1108. End Select
  1109. Next lCurrentChar
  1110. _URLEncode = sEncoded
  1111. End Function &apos; _URLEncode V1.4.0
  1112. REM -----------------------------------------------------------------------------------------------------------------------
  1113. Private Function _UTF8Encode(ByVal psChar As String) As String
  1114. &apos; &amp;-encoding of single psChar character (e.g. &quot;é&quot; becomes &quot;&amp;eacute;&quot; or numeric equivalent
  1115. &apos; http://www.w3schools.com/charsets/ref_html_utf8.asp
  1116. Select Case psChar
  1117. Case &quot;&quot;&quot;&quot; : _UTF8Encode = &quot;&amp;quot;&quot;
  1118. Case &quot;&amp;&quot; : _UTF8Encode = &quot;&amp;amp;&quot;
  1119. Case &quot;&lt;&quot; : _UTF8Encode = &quot;&amp;lt;&quot;
  1120. Case &quot;&gt;&quot; : _UTF8Encode = &quot;&amp;gt;&quot;
  1121. Case &quot;&apos;&quot; : _UTF8Encode = &quot;&amp;apos;&quot;
  1122. Case &quot;:&quot;, &quot;/&quot;, &quot;?&quot;, &quot;#&quot;, &quot;[&quot;, &quot;]&quot;, &quot;@&quot; &apos; Reserved characters
  1123. _UTF8Encode = psChar
  1124. Case Chr(13) : _UTF8Encode = &quot;&quot; &apos; Carriage return
  1125. Case Chr(10) : _UTF8Encode = &quot;&lt;br&gt;&quot; &apos; Line Feed
  1126. Case &lt; Chr(126) : _UTF8Encode = psChar
  1127. Case &quot;€&quot; : _UTF8Encode = &quot;&amp;euro;&quot;
  1128. Case Else : _UTF8Encode = &quot;&amp;#&quot; &amp; Asc(psChar) &amp; &quot;;&quot;
  1129. End Select
  1130. Exit Function
  1131. End Function &apos; _UTF8Encode V1.4.0
  1132. </script:module>