Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.5 KiB

  1. Attribute VB_Name = "RutinasLib"
  2. '===========================================================================================
  3. ' Rutinas de Libreria
  4. '
  5. '===========================================================================================
  6. ' SubRoutine : vntGetTok
  7. ' Author : Pierre Jacomet
  8. ' Version : 1.1
  9. '
  10. ' Description : Devuelve un Token tipado extrayendo el mismo del String de origen
  11. ' sobre la base de un separador de Tokens pasado como argumento.
  12. '
  13. ' Called by : Anyone, utility
  14. '
  15. ' Environment data:
  16. ' Files that it uses (Specify if they are inherited in open state): NONE
  17. ' Parameters (Command Line) and usage mode {I,I/O,O}:
  18. ' Parameters (inherited from environment) :
  19. ' Public Variables created:
  20. ' Environment Variables (Public or Module Level) modified:
  21. ' Environment Variables used in coupling with other routines:
  22. ' Local variables :
  23. ' Problems detected :
  24. ' Request for Modifications:
  25. ' History:
  26. ' 1999-08-01 Added some routines for Skipping Whites, and File Management
  27. '===========================================================================================
  28. Option Explicit
  29. Public Function vntGetTok(ByRef sTokStrIO As String, Optional ByVal iTipDatoIN = vbString, Optional ByVal sTokSepIN As String = ":") As Variant
  30. Dim iPosSep As Integer
  31. sTokStrIO = Trim$(sTokStrIO)
  32. If Len(sTokStrIO) > 0 Then
  33. iPosSep = InStr(1, sTokStrIO, sTokSepIN, 0)
  34. Select Case iTipDatoIN
  35. Case vbInteger To vbDouble, vbCurrency, vbDecimal
  36. vntGetTok = IIf(iPosSep > 0, Val(SubStr(sTokStrIO, 1, iPosSep - 1)), _
  37. Val(sTokStrIO))
  38. Case vbString
  39. vntGetTok = IIf(iPosSep > 0, SubStr(sTokStrIO, 1, iPosSep - 1), sTokStrIO)
  40. Case vbBoolean
  41. vntGetTok = IIf(iPosSep > 0, SubStr(sTokStrIO, 1, iPosSep - 1) = "S", _
  42. sTokStrIO = "S")
  43. End Select
  44. If iPosSep > 0 Then
  45. sTokStrIO = SubStr(sTokStrIO, iPosSep + Len(sTokSepIN))
  46. Else
  47. sTokStrIO = ""
  48. End If
  49. Else
  50. Select Case iTipDatoIN
  51. Case vbInteger
  52. vntGetTok = 0
  53. Case vbString
  54. vntGetTok = ""
  55. Case vbBoolean
  56. vntGetTok = False
  57. End Select
  58. End If
  59. End Function
  60. Function SubStr(ByVal sStrIN As String, ByVal iPosIn As Integer, _
  61. Optional ByVal iPosFin As Integer = -1) As String
  62. On Local Error GoTo SubstrErrHandler
  63. If iPosFin = -1 Then iPosFin = Len(sStrIN)
  64. SubStr = Mid$(sStrIN, iPosIn, iPosFin - iPosIn + 1)
  65. Exit Function
  66. SubstrErrHandler:
  67. SubStr = vbNullString
  68. Resume Next
  69. End Function
  70. Public Function SkipWhite(ByRef strIn As String)
  71. While " " = Left$(strIn, 1)
  72. strIn = Right$(strIn, Len(strIn) - 1)
  73. Wend
  74. SkipWhite = strIn
  75. End Function
  76. ' Aun no anda, el Dir no funciona sobre shares pareciera.
  77. Public Function bIsDirectory(ByVal sDirIN As String) As Boolean
  78. On Local Error GoTo ErrHandler
  79. bIsDirectory = True
  80. Dir sDirIN
  81. Exit Function
  82. ErrHandler:
  83. bIsDirectory = False
  84. Resume Next
  85. End Function
  86. Function FileExists(strPath) As Boolean
  87. Dim Msg As String
  88. ' Turn on error trapping so error handler responds
  89. ' if any error is detected.
  90. On Error GoTo CheckError
  91. FileExists = False
  92. If "" = strPath Then Exit Function
  93. FileExists = (Dir(strPath) <> "")
  94. ' Avoid executing error handler if no error
  95. ' occurs.
  96. Exit Function
  97. CheckError: ' Branch here if error occurs.
  98. ' Define constants to represent intrinsic Visual
  99. ' Basic error codes.
  100. Const mnErrDiskNotReady = 71, _
  101. mnErrDeviceUnavailable = 68
  102. ' vbExclamation, vbOK, vbCancel, vbCritical, and
  103. ' vbOKCancel are constants defined in the VBA type
  104. ' library.
  105. If (Err.Number = mnErrDiskNotReady) Then
  106. Msg = "Put a floppy disk in the drive "
  107. Msg = Msg & "and close the door."
  108. ' Display message box with an exclamation mark
  109. ' icon and with OK and Cancel buttons.
  110. If MsgBox(Msg, vbExclamation & vbOKCancel) = _
  111. vbOK Then
  112. Resume
  113. Else
  114. Resume Next
  115. End If
  116. ElseIf Err.Number = mnErrDeviceUnavailable Then
  117. Msg = "This drive or path does not exist: "
  118. Msg = Msg & strPath
  119. MsgBox Msg, vbExclamation
  120. Resume Next
  121. Else
  122. Msg = "Unexpected error #" & Str(Err.Number)
  123. Msg = Msg & " occurred: " & Err.Description
  124. ' Display message box with Stop sign icon and
  125. ' OK button.
  126. MsgBox Msg, vbCritical
  127. Stop
  128. End If
  129. Resume
  130. End Function
  131. Function Max(ByVal f1 As Double, ByVal f2 As Double) As Double
  132. Max = IIf(f1 > f2, f1, f2)
  133. End Function
  134. ' dirname: Returns the Parent Directory Pathname of a Pathname
  135. Public Function Dirname(ByVal sPath As String) As String
  136. Dirname = ""
  137. If "" = sPath Then Exit Function
  138. Dim bDQ As Boolean
  139. bDQ = (Left$(sPath, 1) = Chr(34))
  140. Dim iDirWack As Long
  141. iDirWack = InStrRev(sPath, "\")
  142. iDirWack = Max(iDirWack, InStrRev(sPath, "/"))
  143. If iDirWack = 0 Then Exit Function
  144. Dirname = Left$(sPath, iDirWack - 1) & IIf(bDQ, Chr(34), "")
  145. End Function
  146. ' Basename: Returns only the FileName Entry component of a Pathname
  147. Public Function Basename(ByVal sPath As String) As String
  148. Basename = sPath
  149. If "" = sPath Then Exit Function
  150. Dim bDQ As Boolean
  151. bDQ = (Left$(sPath, 1) = Chr(34))
  152. Dim iDirWack As Long
  153. iDirWack = InStrRev(sPath, "\")
  154. If iDirWack = 0 Then iDirWack = InStrRev(sPath, "/")
  155. If iDirWack = 0 Then Exit Function
  156. Basename = IIf(bDQ, Chr(34), "") & Right$(sPath, Len(sPath) - iDirWack)
  157. End Function
  158. Public Function FilenameNoExt(ByVal sPath As String) As String
  159. FilenameNoExt = sPath
  160. If "" = sPath Then Exit Function
  161. Dim bDQ As Boolean
  162. bDQ = (Left$(sPath, 1) = Chr(34))
  163. Dim iDot As Long
  164. iDot = InStrRev(sPath, ".")
  165. If iDot > 0 Then
  166. FilenameNoExt = Left$(sPath, iDot - 1) & IIf(bDQ, Chr(34), "")
  167. End If
  168. End Function
  169. Public Function FileExtension(ByVal sPath As String) As String
  170. FileExtension = ""
  171. If "" = sPath Then Exit Function
  172. Dim bDQ As Boolean
  173. bDQ = (Right$(sPath, Len(sPath) - 1) = Chr(34))
  174. If bDQ Then sPath = Left$(sPath, Len(sPath) - 1)
  175. Dim iDot As Long
  176. iDot = InStrRev(sPath, ".")
  177. If iDot > 0 Then
  178. FileExtension = UCase$(Right$(sPath, Len(sPath) - iDot))
  179. End If
  180. End Function
  181. Public Function Rel2AbsPathName(ByVal sPath As String) As String
  182. Rel2AbsPathName = sPath
  183. If "" = sPath Then Exit Function
  184. sPath = Trim$(sPath)
  185. If sPath = Basename(sPath) Then
  186. Rel2AbsPathName = CurDir() + "\" + sPath
  187. ElseIf Left$(sPath, 2) = ".\" Then
  188. Rel2AbsPathName = CurDir() + Mid$(sPath, 2, Len(sPath) - 1)
  189. ElseIf Left$(sPath, 3) = """.\" Then
  190. Rel2AbsPathName = """" + CurDir() + Mid$(sPath, 3, Len(sPath) - 2)
  191. End If
  192. End Function
  193. Public Function UnQuotedPath(sPath As String, Optional bIsQuoted As Boolean = False) As String
  194. UnQuotedPath = Trim$(sPath)
  195. If "" = UnQuotedPath Then Exit Function
  196. If Left$(UnQuotedPath, 1) = """" Then
  197. bIsQuoted = True
  198. UnQuotedPath = Mid$(UnQuotedPath, 2, Len(UnQuotedPath) - 1)
  199. If Right$(UnQuotedPath, 1) = """" Then
  200. UnQuotedPath = Left$(UnQuotedPath, Len(UnQuotedPath) - 1)
  201. End If
  202. End If
  203. End Function
  204. Public Function QuotedPath(sPath As String) As String
  205. QuotedPath = """" + Trim$(sPath) + """"
  206. End Function
  207. Public Function ChangeFileExt(sPath As String, sExt As String) As String
  208. Dim bIsQuoted As Boolean
  209. bIsQuoted = False
  210. ChangeFileExt = UnQuotedPath(Trim$(sPath), bIsQuoted)
  211. If "" = ChangeFileExt Then Exit Function
  212. If (bIsQuoted) Then
  213. ChangeFileExt = QuotedPath(FilenameNoExt(ChangeFileExt) + sExt)
  214. Else
  215. ChangeFileExt = FilenameNoExt(ChangeFileExt) + sExt
  216. End If
  217. End Function
  218. Public Function IsFullPathname(ByVal strPath As String) As Boolean
  219. strPath = Trim$(strPath)
  220. IsFullPathname = (Left$(strPath, 2) = "\\" Or Mid$(strPath, 2, 2) = ":\")
  221. End Function
  222. #If NEEDED Then
  223. Sub DumpError(ByVal strFunction As String, ByVal strErrMsg As String)
  224. MsgBox strFunction & " - Failed " & strErrMsg & vbCrLf & _
  225. "Error Number = " & Err.Number & " - " & Err.Description, _
  226. vbCritical, "Error"
  227. ' Err.Clear
  228. End Sub
  229. #End If
  230. Function Capitalize(strIn As String) As String
  231. Capitalize = UCase$(Left$(strIn, 1)) + Mid$(strIn, 2)
  232. End Function
  233. Function KindOfPrintf(ByVal strFormat, ByVal args As Variant) As String
  234. If (Not IsArray(args)) Then
  235. args = Array(args)
  236. End If
  237. Dim iX As Integer, iPos As Integer
  238. KindOfPrintf = ""
  239. For iX = 0 To UBound(args)
  240. iPos = InStr(strFormat, "%s")
  241. If (iPos = 0) Then Exit For
  242. strFormat = Mid$(strFormat, 1, iPos - 1) & args(iX) & Mid$(strFormat, iPos + 2)
  243. Next iX
  244. KindOfPrintf = strFormat
  245. End Function
  246. Function ShowAsHex(ByVal strHex As String) As String
  247. Dim iX As Integer
  248. Dim byteRep() As Byte
  249. byteRep = strHex
  250. ShowAsHex = "0x"
  251. For iX = 1 To UBound(byteRep)
  252. ShowAsHex = ShowAsHex + Hex(byteRep(iX))
  253. Next iX
  254. End Function
  255. Function Null2EmptyString(ByVal vntIn) As String
  256. If (IsNull(vntIn)) Then
  257. Null2EmptyString = vbNullString
  258. Else
  259. Null2EmptyString = vntIn
  260. End If
  261. End Function
  262. Function Null2Number(ByVal vntIn) As Long
  263. If (IsNull(vntIn)) Then
  264. Null2Number = 0
  265. Else
  266. Null2Number = vntIn
  267. End If
  268. End Function