Changeset 478 for trunk/Include/com/bstring.ab
- Timestamp:
- Mar 13, 2008, 9:06:43 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Include/com/bstring.ab
r335 r478 1 1 ' com/bstring.ab 2 3 '#require <ole2.ab>4 '#require <oleauto.ab>5 2 6 3 Namespace ActiveBasic … … 8 5 9 6 Class BString 10 'Inherits System.IDisposable, System.ICloneable7 Implements System.IDisposable ', System.ICloneable 11 8 Public 12 9 Sub BString() … … 19 16 20 17 Sub BString(s As BString) 21 BString.Copy(This.bs, s.bs) 22 End Sub 23 24 Sub BString(s As LPCOLESTR) 25 bs = SysAllocString(s) 18 If Not IsNothing(s) Then 19 bs = copy(s.bs) 20 End If 26 21 End Sub 27 22 28 23 Sub BString(s As LPCOLESTR, len As DWord) 29 bs = SysAllocStringLen(s, len) 30 End Sub 31 32 Sub BString(s As PCSTR) 33 Init(s, lstrlenA(s)) 34 End Sub 35 36 Sub BString(s As PCSTR, len As DWord) 37 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0) 38 bs = SysAllocStringLen(0, lenBS) 39 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS) 24 If s <> 0 Then 25 bs = SysAllocStringLen(s, len) 26 End If 40 27 End Sub 41 28 42 29 Sub BString(s As String) 43 Init(s.StrPtr, s.Length As DWord) 44 End Sub 30 If Not IsNothing(s) Then 31 Init(s.StrPtr, s.Length As DWord) 32 End If 33 End Sub 34 35 Static Function FromBStr(bs As BSTR) As BString 36 FromBStr = New BString(bs, SysStringLen(bs)) 37 End Function 38 39 Static Function FromCStr(s As PCWSTR) As BString 40 If s <> 0 Then 41 FromCStr = New BString(s, lstrlenW(s)) 42 Else 43 FromCStr = New BString 44 End If 45 End Function 46 47 Static Function FromCStr(s As PCWSTR, len As DWord) As BString 48 If s <> 0 Then 49 FromCStr = New BString(s, len) 50 Else 51 FromCStr = New BString 52 End If 53 End Function 54 55 Static Function FromCStr(s As PCSTR) As BString 56 Dim dst As PCWSTR 57 Dim lenW = GetStr(s, dst) 58 FromCStr = FromCStr(s, lenW) 59 End Function 60 61 Static Function FromCStr(s As PCSTR, len As DWord) As BString 62 Dim dst As PCWSTR 63 Dim lenW = GetStr(s, len, dst) 64 FromCStr = FromCStr(s, lenW) 65 End Function 45 66 46 67 Sub ~BString() … … 48 69 End Sub 49 70 50 Sub Assign(bstr As BString)51 Clear()52 BString.Copy(This.bs, bstr.bs)53 End Sub54 55 Sub Assign(s As LPCOLESTR)56 Clear()57 s = SysAllocString(s)58 End Sub59 60 Sub AssignFromBStr(bstr As BSTR)61 Clear()62 BString.Copy(bs, bstr)63 End Sub64 65 71 Const Function Copy() As BSTR 66 BString.Copy(Copy,bs)72 Copy = copy(bs) 67 73 End Function 68 74 … … 76 82 77 83 Sub Clear() 78 If bs <> 0 Then 79 SysFreeString(bs) 80 bs = 0 81 End If 84 reset(0) 82 85 End Sub 83 86 84 87 Sub Attach(ByRef bstr As BSTR) 85 Clear() 86 BString.Move(bs, bstr) 88 reset(move(bstr)) 87 89 End Sub 88 90 89 91 Function Detach() As BSTR 90 BString.Move(Detach,bs)92 Detach = move(bs) 91 93 End Function 92 94 93 95 Function BStr() As BSTR 94 96 BStr = bs 95 End Function96 /*97 Static Function Assgin(bs As BSTR) As BString98 Assgin = New BString99 Assgin.Assgin(bs)100 97 End Function 101 98 … … 104 101 Attach.Attach(bs) 105 102 End Function 106 */ 103 107 104 Const Function Length() As DWord 108 Length = SysStringLen(bs)105 Length = GetDWord(bs As VoidPtr - SizeOf (DWord)) 'SysStringLen(bs) 109 106 End Function 110 107 111 108 Const Function Operator [](i As SIZE_T) As OLECHAR 112 #ifdef _DEBUG113 109 If i > Length Then 114 'Throw OutOfRangeException 115 End If 116 #endif 110 Throw New ArgumentOutOfRangeException("i") 111 End If 117 112 Return bs[i] 118 113 End Function 119 114 120 115 Sub Operator []=(i As SIZE_T, c As OLECHAR) 121 #ifdef _DEBUG122 116 If i > Length Then 123 'Throw OutOfRangeException 124 End If 125 #endif 117 Throw New ArgumentOutOfRangeException("i") 118 End If 126 119 bs[i] = c 127 120 End Sub … … 135 128 End Function 136 129 130 Override Function Equals(o As Object) As Boolean 131 If Not IsNothing(o) Then 132 If This.GetType().Equals(o.GetType()) Then 133 Equals(o As BString) 134 End If 135 End If 136 End Function 137 138 Const Function Equals(s As BString) As Boolean 139 Equals = Compare(This, s) = 0 140 End Function 141 142 Static Function Compare(l As BString, r As BString) As Long 143 If IsNullOrEmpty(l) Then 144 If IsNullOrEmpty(r) Then 145 Compare = 0 146 Else 147 Compare = -1 148 End If 149 Else 150 If IsNullOrEmpty(bsr) Then 151 Compare = 1 152 Else 153 Compare = Strings.ChrCmp(l.bs, l.Length As SIZE_T, r.bs, r.Length As SIZE_T) 154 End If 155 End If 156 End Function 157 158 Static Function IsNullOrEmpty(s As BString) 159 If IsNothing(s) Then 160 IsNullOrEmpty = True 161 ElseIf s.bs = 0 Then 162 IsNullOrEmpty = True 163 ElseIf s.Length = 0 Then 164 IsNullOrEmpty = True 165 Else 166 IsNullOrEmpty = False 167 End If 168 End Function 169 170 Function Operator ==(s As BString) As Boolean 171 Return Compare(This, s) = 0 172 End Function 173 174 Function Operator <>(s As BString) As Boolean 175 Return Compare(This, s) <> 0 176 End Function 177 178 Function Operator <(s As BString) As Boolean 179 Return Compare(This, s) < 0 180 End Function 181 182 Function Operator <=(s As BString) As Boolean 183 Return Compare(This, s) <= 0 184 End Function 185 186 Function Operator >(s As BString) As Boolean 187 Return Compare(This, s) > 0 188 End Function 189 190 Function Operator >=(s As BString) As Boolean 191 Return Compare(This, s) >= 0 192 End Function 193 137 194 Private 138 195 bs As BSTR 139 196 140 Sub Init(s As PCSTR, len As DWord) 141 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0) 142 bs = SysAllocStringLen(0, lenBS) 143 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS) 144 End Sub 145 146 Static Sub Copy(ByRef dst As BSTR, ByVal src As BSTR) 147 dst = SysAllocStringLen(src, SysStringLen(src)) 148 End Sub 149 150 Static Sub Move(ByRef dst As BSTR, ByRef src As BSTR) 151 dst = src 152 src = 0 153 End Sub 197 Sub init(s As PCSTR, len As DWord) 198 If <> 0 Then 199 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0) 200 bs = SysAllocStringLen(0, lenBS) 201 If bs <> 0 Then 202 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS) 203 End If 204 End If 205 End Sub 206 207 Sub reset(newBS As BSTR) 208 Dim old = InterlockedExchangePointer(bs, newBS) 209 SysFreeString(old) 210 End Sub 211 212 Static Function copy(src As BSTR) As BSTR 213 copy = SysAllocStringLen(src, SysStringLen(src)) 214 End Function 215 216 Static Function move(ByRef src As BSTR) As BSTR 217 move = InterlockedExchangePointer(src, 0) 218 End Function 154 219 End Class 155 220
Note:
See TracChangeset
for help on using the changeset viewer.