home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 3_2004-2005.ISO / Data / Zips / BAS_Module18049510142004.psc / ModuleMania / Common.bas < prev   
Encoding:
BASIC Source File  |  2004-10-14  |  12.2 KB  |  313 lines

  1. Attribute VB_Name = "Common"
  2. Option Explicit
  3.  
  4. ' Note - Use error handlers in procedures that call these functions
  5.  
  6. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  7.           (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  8.  
  9. Public Declare Function AllocStrB Lib "oleaut32" Alias "SysAllocStringByteLen" _
  10.           (ByVal lpszStr As Long, ByVal lLenB As Long) As Long
  11.  
  12. Public Declare Function OSWinHelp Lib "user32" Alias "WinHelpA" _
  13.           (ByVal hWnd As Long, ByVal lpHelpFile As String, _
  14.            ByVal wCommand As Long, ByVal dwData As Long) As Long
  15.  
  16. Public Const ZERO As Long = 0
  17. Public Const ONE As Long = 1
  18. Public Const TWO As Long = 2
  19. Public Const THREE As Long = 3
  20. Public Const FOUR As Long = 4
  21. Public Const FIVE As Long = 5
  22.  
  23. Private Const INVALID_ARG As Long = FIVE
  24.  
  25. Public Enum HelpTypeFlags
  26.     HelpContents = THREE
  27.     HowToUseHelp = FOUR
  28.     SearchForHelpOn = 261
  29. End Enum
  30.  
  31. ' The return value is the sum of the attribute values
  32. Public Declare Function GetAttributes Lib "kernel32" Alias "GetFileAttributesA" _
  33.     (ByVal lpSpec As String) As Long
  34.  
  35. ' Sets the Attributes argument whose sum specifies file attributes
  36. ' An error occurs if you try to set the attributes of an open file
  37. Public Declare Function SetAttributes Lib "kernel32" Alias "SetFileAttributesA" _
  38.     (ByVal lpSpec As String, ByVal dwAttributes As Long) As Long
  39.  
  40. Private Const INVALID_FILE_ATTRIBUTES As Long = &HFFFFFFFF ' -1
  41.  
  42. Public Enum vbFileAttributes
  43.     vbInvalidFile = -1   ' Returned INVALID_FILE_ATTRIBUTES
  44.     vbNormal = 0         ' Normal (default for SetAttributes)
  45.     vbReadOnly = 1       ' Read-only
  46.     vbHidden = 2         ' Hidden
  47.     vbSystem = 4         ' System file
  48.     vbVolume = 8         ' Volume label
  49.     vbDirectory = 16     ' Directory or folder
  50.     vbArchive = 32       ' File has changed since last backup
  51.     vbTemporary = &H100  ' 256
  52.     vbCompressed = &H800 ' 2048
  53. End Enum
  54.  
  55. Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
  56.     (ByVal lpszShortPath As String, ByVal lpszLongPath As String, _
  57.      ByVal cchBuffer As Long) As Long
  58.  
  59. Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
  60.     (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
  61.      ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, _
  62.      Arguments As Long) As Long
  63.  
  64. Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000&
  65. Private Const sEMPTY_STR = "Empty string passed."
  66.  
  67. ' Maximum allowed path length including path, filename,
  68. ' and command line arguments for NT (Intel) and Win95
  69. Public Const MAX_PATH As Long = 260
  70. Public Const DIR_SEP As String = "\"
  71.  
  72. ' PI is: 3.1415926535897932384626433832795028841971694
  73. Public Const PI As Double = 3.14159265358979 'PI = 4 * Atn(1)
  74.  
  75. Public Function RandomColor() As Long 'Returns a random RBG color
  76.     Dim lRed As Long ' ⌐Jay
  77.     Dim lGreen As Long
  78.     Dim lBlue As Long
  79.     
  80.     Call Randomize(Timer)
  81.     lRed = Random(ONE, 255)
  82.     lGreen = Random(ONE, 255)
  83.     lBlue = Random(ONE, 255)
  84.     RandomColor = RGB(lRed, lGreen, lBlue)
  85. End Function
  86.  
  87. ' Bounced off Paul Catons InIDE
  88. Public Function InTheIDE() As Boolean
  89.     Debug.Assert True Xor DebugOnly(InTheIDE)
  90. End Function
  91.  
  92. Private Function DebugOnly(fInIDE As Boolean) As Boolean
  93.     fInIDE = True
  94. End Function
  95.  
  96. Public Function GetAttrib(sFileSpec As String, ByVal Attrib As vbFileAttributes) As Boolean
  97.     ' Returns True if the specified attribute(s) is currently set.
  98.     If LenB(sFileSpec) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  99.     GetAttrib = (GetAttributes(sFileSpec) And Attrib) = Attrib
  100. End Function
  101.  
  102. Public Sub SetAttrib(sFileSpec As String, ByVal Attrib As vbFileAttributes, Optional fTurnOff As Boolean)
  103.     ' Sets/clears the specified attribute(s) without affecting other attributes. You
  104.     ' do not need to know the current state of an attribute to set it to on or off.
  105.     If LenB(sFileSpec) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  106.     Dim Attribs As Long: Attribs = GetAttributes(sFileSpec)
  107.     If fTurnOff Then
  108.         SetAttributes sFileSpec, Attribs And (Not Attrib)
  109.     Else
  110.         SetAttributes sFileSpec, Attribs Or Attrib
  111.     End If
  112. End Sub
  113.  
  114. Public Function Exists(sFileSpec As String, Optional fIsDir As Boolean, Optional fCreateIfNot As Boolean) As Boolean
  115.     If LenB(sFileSpec) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  116.     If fIsDir Then
  117.         Exists = DirExists(sFileSpec, fCreateIfNot)
  118.     Else
  119.         Exists = FileExists(sFileSpec, fCreateIfNot)
  120.     End If
  121. End Function
  122.  
  123. Public Function FileExists(sFileSpec As String, Optional fCreateIfNot As Boolean) As Boolean
  124.     If LenB(sFileSpec) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  125.     Dim Attribs As Long: Attribs = GetAttributes(sFileSpec)
  126.     If (Attribs <> INVALID_FILE_ATTRIBUTES) Then
  127.         FileExists = ((Attribs And vbDirectory) <> vbDirectory)
  128.     End If
  129.     If (FileExists = False) Then
  130.         If fCreateIfNot Then FileExists = CreateFile(sFileSpec)
  131.     End If
  132. End Function
  133.  
  134. Public Function DirExists(sPath As String, Optional fCreateIfNot As Boolean) As Boolean
  135.     If LenB(sPath) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  136.     Dim Attribs As Long: Attribs = GetAttributes(sPath)
  137.     If (Attribs <> INVALID_FILE_ATTRIBUTES) Then
  138.         DirExists = ((Attribs And vbDirectory) = vbDirectory)
  139.     End If
  140.     If (DirExists = False) Then
  141.         If fCreateIfNot Then DirExists = CreatePath(sPath)
  142.     End If
  143. End Function
  144.  
  145. '-----------------------------------------------------------
  146. ' Creates the specified file sFileSpec.
  147. ' Returns: 2 if created, 1 if existed, 0 if error.
  148. '-----------------------------------------------------------
  149. Public Function CreateFile(sFileSpec As String) As Long
  150.     If LenB(sFileSpec) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  151.     Dim iFile As Integer, Idx As Integer, sFile As String
  152.  
  153.     On Error GoTo FailedCreateFile
  154.     If FileExists(sFileSpec) Then
  155.         CreateFile = ONE
  156.     Else
  157.         sFile = LongPathName(sFileSpec)
  158.         Idx = InStrR(sFile, DIR_SEP)
  159.         If (Idx > ZERO) And (Idx < Len(sFile)) Then
  160.             If CreatePath(Left$(sFile, Idx)) Then
  161.                 iFile = FreeFile
  162.                 Open sFile For Output As #iFile
  163.                 Close #iFile
  164.                 CreateFile = TWO
  165.     End If: End If: End If
  166. FailedCreateFile:
  167. End Function
  168.  
  169. '-----------------------------------------------------------
  170. ' Creates the specified directory sPath.
  171. ' Returns: 2 if created, 1 if existed, 0 if error.
  172. '-----------------------------------------------------------
  173. Public Function CreatePath(sPath As String) As Long
  174.     If LenB(sPath) = ZERO Then Err.Raise INVALID_ARG, , sEMPTY_STR
  175.     Dim sDir As String, sTemp As String, Idx As Integer
  176.  
  177.     On Error GoTo FailedCreatePath
  178.     If (DirExists(sPath)) Then
  179.         CreatePath = ONE
  180.     Else
  181.         ' Add trailing backslash if missing
  182.         sDir = AddBackslash(LongPathName(sPath))
  183.  
  184.         ' Set Idx to the first backslash
  185.         Idx = InStr(ONE, sDir, DIR_SEP)
  186.  
  187.         Do ' Loop and make each subdir of the path separately
  188.             Idx = InStr(Idx + ONE, sDir, DIR_SEP)
  189.             If (Idx <> ZERO) Then
  190.                 sTemp = Left$(sDir, Idx - ONE)
  191.                 ' Determine if this directory already exists
  192.                 If (DirExists(sTemp) = False) Then
  193.                     ' We must create this directory
  194.                     MkDir sTemp
  195.                     CreatePath = TWO
  196.                 End If
  197.             End If
  198.         Loop Until Idx = ZERO
  199.     End If
  200. FailedCreatePath:
  201. End Function
  202.  
  203. Public Function LongPathName(sPathName As String) As String
  204.     If LenB(sPathName) = 0 Then Exit Function
  205.     LongPathName = sPathName ' Default to the passed name
  206.     On Error GoTo GetFailed
  207.     Dim sPath As String, lResult As Long
  208.     sPath = String$(MAX_PATH, vbNullChar)
  209.     lResult = GetLongPathName(sPathName, sPath, MAX_PATH)
  210.     If (lResult) Then LongPathName = TrimZ(sPath)
  211. GetFailed:
  212. End Function
  213.  
  214. Public Function TrimZ(sNullTerminated As String) As String
  215.     If (LenB(sNullTerminated) = ZERO) Then Err.Raise INVALID_ARG, , sEMPTY_STR
  216.     Dim Idx As Integer: Idx = InStr(sNullTerminated, vbNullChar)
  217.     If (Idx <> ZERO) Then
  218.         TrimZ = Left$(sNullTerminated, Idx - ONE)
  219.     Else
  220.         TrimZ = Trim$(sNullTerminated)
  221.     End If
  222. End Function
  223.  
  224. Public Function InStrR(sSrc As String, sTerm As String, _
  225.                        Optional ByVal lLeftBound As Long = ONE, _
  226.                        Optional ByVal lRightBound As Long, _
  227.                        Optional CaseSensative As Boolean) As Long
  228.     If (LenB(sSrc) = ZERO) Or (LenB(sTerm) = ZERO) Then Err.Raise INVALID_ARG
  229.     Dim lPos As Long, lTerm As Long
  230.     If lRightBound = ZERO Then lRightBound = Len(sSrc)
  231.     lTerm = Len(sTerm)
  232.     lRightBound = (lRightBound - lTerm) + ONE
  233.     If CaseSensative Then
  234.         lLeftBound = InStr(sSrc, sTerm)
  235.         If lLeftBound = ZERO Then Exit Function
  236.         For lPos = lRightBound To lLeftBound Step -ONE
  237.             If (Mid$(sSrc, lPos, lTerm) = sTerm) Then
  238.                 InStrR = lPos
  239.                 Exit Function
  240.             End If
  241.         Next lPos
  242.     Else
  243.         Dim sText As String, sFind As String
  244.         sText = LCase$(sSrc): sFind = LCase$(sTerm)
  245.         lLeftBound = InStr(sText, sFind)
  246.         If lLeftBound = ZERO Then Exit Function
  247.         For lPos = lRightBound To lLeftBound Step -ONE
  248.             If (Mid$(sText, lPos, lTerm) = sFind) Then
  249.                 InStrR = lPos
  250.                 Exit Function
  251.             End If
  252.         Next lPos
  253.     End If
  254. End Function
  255.  
  256. '-BuildStr-----------------------------------------------
  257. '  This function can replace vb's string & concatenation.
  258. '  The speed is exactly the same for simple appends:
  259. '     sResult = sResult & "text"
  260. '     sResult = BuildStr(sResult, "text")
  261. '  But for more substrings this function is much faster
  262. '  because vb's multiple appending is very slow:
  263. '     sResult = sResult & "some" & "more" & "text"
  264. '     sResult = BuildStr(sResult, "some", "more", "text")
  265. '  Notice you can safely pass as an argument the variable
  266. '  that the function is assigning back to (compiler safe).
  267. '  You can also specify the delimiter character(s) to
  268. '  insert between the appended substrings, and will work
  269. '  correctly if an argument is omitted or passed empty:
  270. '     sMsg = BuildStr("s1", , "s2", "s3", vbCrLf)
  271. '     MsgBox BuildStr("", sMsg, , "s4", vbCrLf)
  272. '--------------------------------------------------------
  273. Public Function BuildStr(Str1 As String, Optional Str2 As String, Optional Str3 As String, Optional Str4 As String, Optional Delim As String) As String
  274.     Dim LenWrk As Long, LenAll As Long
  275.     Dim LenDlm As Long, CntDlm As Long
  276.     Dim Len1 As Long, Len2 As Long
  277.     Dim Len3 As Long, Len4 As Long
  278.     Dim lpStr As Long
  279.     Len1 = LenB(Str1): Len2 = LenB(Str2)
  280.     Len3 = LenB(Str3): Len4 = LenB(Str4)
  281.     LenDlm = LenB(Delim)
  282.     If (LenDlm <> ZERO) Then
  283.         CntDlm = -LenDlm
  284.         If (Len1 <> ZERO) Then CntDlm = ZERO
  285.         If (Len2 <> ZERO) Then CntDlm = CntDlm + LenDlm
  286.         If (Len3 <> ZERO) Then CntDlm = CntDlm + LenDlm
  287.         If (Len4 <> ZERO) Then CntDlm = CntDlm + LenDlm
  288.     End If
  289.     LenAll = Len1 + Len2 + Len3 + Len4 + CntDlm
  290.     If (LenAll > ZERO) Then
  291.         lpStr = AllocStrB(0&, LenAll)
  292.  
  293.         ' Preserve Unicode by passing StrPtr and byte count
  294.         If (Len1 <> ZERO) Then
  295.             CopyMemory ByVal lpStr, ByVal StrPtr(Str1), Len1
  296.             LenWrk = Len1
  297.         End If
  298.         
  299.         If (Len2 <> ZERO) Then
  300.             If (LenDlm <> ZERO) Then If (LenWrk <> ZERO) Then GoSub InsDelim
  301.             CopyMemory ByVal lpStr + LenWrk, ByVal StrPtr(Str2), Len2
  302.             LenWrk = LenWrk + Len2
  303.         End If
  304.         
  305.         If (Len3 <> ZERO) Then
  306.             If (LenDlm <> ZERO) Then If (LenWrk <> ZERO) Then GoSub InsDelim
  307.             CopyMemory ByVal lpStr + LenWrk, ByVal StrPtr(Str3), Len3
  308.             LenWrk = LenWrk + Len3
  309.         End If
  310.         
  311.         If (Len4 <> ZERO) Then
  312.             If (LenDlm <> ZERO) Then If (LenWrk <> ZERO) Then GoSub InsDelim
  313.             CopyMemory ByVal lpStr + LenWrk, ByVal StrPtr(Str4     EstnRaise INVALID_ARG
  314.     i As L4