1 | ' com/bstring.ab
|
---|
2 |
|
---|
3 | #require <ole2.ab>
|
---|
4 | #require <oleauto.ab>
|
---|
5 |
|
---|
6 | Namespace ActiveBasic
|
---|
7 | Namespace COM
|
---|
8 |
|
---|
9 | Class BString
|
---|
10 | Sub Init(s As PCSTR, len As DWord)
|
---|
11 | Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
|
---|
12 | bs = SysAllocStringLen(0, lenBS)
|
---|
13 | MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
|
---|
14 | End Sub
|
---|
15 | Public
|
---|
16 | Sub BString()
|
---|
17 | bs = 0
|
---|
18 | End Sub
|
---|
19 |
|
---|
20 | Sub BString(len As DWord)
|
---|
21 | bs = SysAllocStringLen(0, len)
|
---|
22 | End Sub
|
---|
23 |
|
---|
24 | Sub BString(ByRef s As BString)
|
---|
25 | Init(s.bs, s.Length)
|
---|
26 | End Sub
|
---|
27 |
|
---|
28 | Sub BString(s As LPCOLESTR)
|
---|
29 | bs = SysAllocString(s)
|
---|
30 | End Sub
|
---|
31 |
|
---|
32 | Sub BString(s As LPCOLESTR, len As DWord)
|
---|
33 | bs = SysAllocStringLen(s, len)
|
---|
34 | End Sub
|
---|
35 |
|
---|
36 | Sub BString(s As PCSTR)
|
---|
37 | Init(s, lstrlenA(s))
|
---|
38 | End Sub
|
---|
39 |
|
---|
40 | Sub BString(s As PCSTR, len As DWord)
|
---|
41 | Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
|
---|
42 | bs = SysAllocStringLen(0, lenBS)
|
---|
43 | MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
|
---|
44 | End Sub
|
---|
45 |
|
---|
46 | Sub BString(ByRef s As String)
|
---|
47 | Init(s.StrPtr, s.Length As DWord)
|
---|
48 | End Sub
|
---|
49 |
|
---|
50 | Sub ~BString()
|
---|
51 | Clear()
|
---|
52 | End Sub
|
---|
53 |
|
---|
54 | Sub Assign(ByRef bstr As BString)
|
---|
55 | Clear()
|
---|
56 | Init(bstr, bstr.Length)
|
---|
57 | End Sub
|
---|
58 |
|
---|
59 | Sub Assign(s As LPCOLESTR)
|
---|
60 | Clear()
|
---|
61 | Init(s, lstrlenW(s))
|
---|
62 | End Sub
|
---|
63 |
|
---|
64 | Sub AssignFromBStr(bstr As BSTR)
|
---|
65 | Clear()
|
---|
66 | String.Copy(bs, bstr)
|
---|
67 | End Sub
|
---|
68 |
|
---|
69 | Const Function Copy() As BSTR
|
---|
70 | BString.Copy(Copy, bs)
|
---|
71 | End Function
|
---|
72 |
|
---|
73 | Sub Clear()
|
---|
74 | If bs <> 0 Then
|
---|
75 | SysFreeString(bs)
|
---|
76 | bs = 0
|
---|
77 | End If
|
---|
78 | End Sub
|
---|
79 |
|
---|
80 | Sub Attach(ByRef bstr As BSTR)
|
---|
81 | Clear()
|
---|
82 | BString.Move(bs, bstr)
|
---|
83 | End Sub
|
---|
84 |
|
---|
85 | Function Detach() As BSTR
|
---|
86 | BString.Move(Detach, bs)
|
---|
87 | End Function
|
---|
88 |
|
---|
89 | Function BStr() As BSTR
|
---|
90 | BStr = bs
|
---|
91 | End Function
|
---|
92 | /*
|
---|
93 | Static Function Assgin(bs As BSTR) As BString
|
---|
94 | Assgin = New BString
|
---|
95 | Assgin.Assgin(bs)
|
---|
96 | End Function
|
---|
97 |
|
---|
98 | Static Function Attach(bs As BSTR) As BString
|
---|
99 | Attach = New BString
|
---|
100 | Attach.Attach(bs)
|
---|
101 | End Function
|
---|
102 | */
|
---|
103 | Const Function Length() As DWord
|
---|
104 | Length = SysStringLen(bs)
|
---|
105 | End Function
|
---|
106 |
|
---|
107 | Const Function Operator [](i As SIZE_T) As OLECHAR
|
---|
108 | #ifdef _DEBUG
|
---|
109 | If i > Length Then
|
---|
110 | 'Throw OutOfRangeException
|
---|
111 | End If
|
---|
112 | #endif
|
---|
113 | Return bs[i]
|
---|
114 | End Function
|
---|
115 |
|
---|
116 | Sub Operator []=(i As SIZE_T, c As OLECHAR)
|
---|
117 | #ifdef _DEBUG
|
---|
118 | If i > Length Then
|
---|
119 | 'Throw OutOfRangeException
|
---|
120 | End If
|
---|
121 | #endif
|
---|
122 | bs[i] = c
|
---|
123 | End Sub
|
---|
124 |
|
---|
125 | Override Function ToString() As String
|
---|
126 | Return New String(bs As PCWSTR, Length As Long)
|
---|
127 | End Function
|
---|
128 |
|
---|
129 | Override Function GetHashCode() As Long
|
---|
130 | Return _System_GetHashFromWordArray(bs, Length)
|
---|
131 | End Function
|
---|
132 |
|
---|
133 | Private
|
---|
134 | bs As BSTR
|
---|
135 |
|
---|
136 | Static Sub Copy(ByRef dst As BSTR, ByVal src As BSTR)
|
---|
137 | dst = SysAllocStringLen(src, SysStringLen(src))
|
---|
138 | End Sub
|
---|
139 |
|
---|
140 | Static Sub Move(ByRef dst As BSTR, ByRef src As BSTR)
|
---|
141 | dst = src
|
---|
142 | src = 0
|
---|
143 | End Sub
|
---|
144 | End Class
|
---|
145 |
|
---|
146 | End Namespace 'COM
|
---|
147 | End Namespace 'ActiveBasic
|
---|