source: trunk/ab5.0/ablib/src/system/string.sbp

Last change on this file was 697, checked in by イグトランス (egtra), 15 years ago

string.sbpの変換ルーチンで、ヌル終端無し文字列から有り文字列への変換でヌルを付け忘れるバグのため、常にヌル終端を付加する前の方式に戻す。GetWCStr, GetMBStr, GetTCStrを削除。

File size: 6.0 KB
Line 
1'string.sbp
2'文字列変数の操作用
3
4/*!
5@brief Stringが内部で保持しているポインタを返す。
6@param[in] s 文字列。
7@return sの内部バッファへのポインタ、ただしsがNothingならNULL。
8*/
9Function StrPtr(s As String) As *Char
10 If Not ActiveBasic.IsNothing(s) Then
11 StrPtr = s.StrPtr
12 End If
13End Function
14'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている。
15
16/*!
17@brief 指定した長さの空文字(\0)を持つ文字列を作成する。
18@param[in] length 長さ
19@return Legnth = lengthとなっている文字列。
20*/
21Function ZeroString(length As Long) As String
22 ZeroString = New String(0 As Char, length)
23End Function
24
25Function MakeStr(psz As PSTR) As String
26 Return New String(psz)
27End Function
28
29Function MakeStr(psz As PWSTR) As String
30 Return New String(psz)
31End Function
32
33Dim _System_AllocForConvertedString As *Function(size As SIZE_T) As VoidPtr
34_System_AllocForConvertedString = AddressOf (GC_malloc_atomic)
35
36Namespace Detail
37 Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
38 Dim lenWCS = MultiByteToWideChar(CP_ACP, 0, mbsSrc, (len As DWord) As Long, 0, 0)
39 wcsDst = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
40 GetWCStr = MultiByteToWideChar(CP_ACP, 0, mbsSrc, (len As DWord) As Long, wcsDst, lenWCS)
41 wcsDst[lenWCS] = 0
42 End Function
43
44 Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
45 wcsDst = wcsSrc
46 GetWCStr = len
47 End Function
48
49 Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
50 Dim lenMBS = WideCharToMultiByte(CP_ACP, 0, wcsSrc, (len As DWord) As Long, 0, 0, 0, 0)
51 mbsDst = _System_AllocForConvertedString(SizeOf (CHAR) * (lenMBS + 1)) As PSTR
52 GetMBStr = WideCharToMultiByte(CP_ACP, 0, wcsSrc, (len As DWord) As Long, mbsDst, lenMBS, 0, 0) As SIZE_T
53 mbsDst[lenMBS] = 0
54 End Function
55
56 Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
57 mbsDst = mbsSrc
58 GetMBStr = len
59 End Function
60
61 ' ToTCStrの補助
62 Sub GetStrNT(mbszSrc As PSTR, ByRef mbszDst As PSTR)
63 mbszDst = mbszSrc
64 End Sub
65
66 Sub GetStrNT(mbszSrc As PSTR, ByRef wcszDst As PWSTR)
67 GetStr(mbszSrc, wcszDst)
68 End Sub
69
70 Sub GetStrNT(wcszSrc As PWSTR, ByRef mbszDst As PSTR)
71 GetStr(wcszSrc, mbszDst)
72 End Sub
73
74 Sub GetStrNT(wcszSrc As PWSTR, ByRef wcszDst As PWSTR)
75 wcszDst = wcszSrc
76 End Sub
77
78End Namespace
79
80/*
81変換の組み合わせは、
82入力引数: wcsz, wcs + len, mbsz, mbs + len, str
83出力関数: wcs(z)出力GetStr, mbs(z)出力GetStr, ToWCStr, ToMBStr, ToTCStr,
84で、5 * 5 = 25通り。
85*/
86
87Function GetStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
88 If mbszSrc = 0 Then
89 wcsDst = 0
90 Return 0
91 Else
92 Return Detail.GetWCStr(mbszSrc, lstrlenA(mbszSrc) As SIZE_T, wcsDst)
93 End If
94End Function
95
96Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
97 If mbsSrc = 0 Then
98 wcsDst = 0
99 Return 0
100 Else
101 Return Detail.GetWCStr(mbsSrc, len, wcsDst)
102 End If
103End Function
104
105Function GetStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
106 If wcszSrc = 0 Then
107 wcsDst = 0
108 Return 0
109 Else
110 wcsDst = wcszSrc
111 Return lstrlenW(wcszSrc) As SIZE_T + 1
112 End If
113End Function
114
115Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
116 If wcsSrc = 0 Then
117 wcsDst = 0
118 Return 0
119 Else
120 wcsDst = wcsSrc
121 Return len
122 End If
123End Function
124
125Function GetStr(wcszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
126 If wcszSrc = 0 Then
127 mbsDst = 0
128 Return 0
129 Else
130 Return Detail.GetMBStr(wcszSrc, lstrlenW(wcszSrc) As SIZE_T, mbsDst)
131 End If
132End Function
133
134Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
135 If wcsSrc = 0 Then
136 mbsDst = 0
137 Return 0
138 Else
139 Return Detail.GetMBStr(wcsSrc, len As SIZE_T, mbsDst)
140 End If
141End Function
142
143Function GetStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
144 If mbszSrc = 0 Then
145 mbsDst = 0
146 Return 0
147 Else
148 mbsDst = mbszSrc
149 Return lstrlenA(mbszSrc) As SIZE_T + 1
150 End If
151End Function
152
153Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
154 If mbsSrc = 0 Then
155 mbsDst = 0
156 Return len
157 Else
158 mbsDst = mbsSrc
159 Return 0
160 End If
161End Function
162
163Function GetStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
164 If ActiveBasic.IsNothing(strSrc) Then
165 wcsDst = 0
166 Return 0
167 Else
168 Return Detail.GetWCStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
169 End If
170End Function
171
172Function GetStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
173 If ActiveBasic.IsNothing(strSrc) Then
174 mbsDst = 0
175 Return 0
176 Else
177 Return Detail.GetMBStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
178 End If
179End Function
180
181Function ToWCStr(mbsz As PSTR) As PWSTR
182 Detail.GetStrNT(mbsz, ToWCStr)
183End Function
184
185Function ToWCStr(mbs As PSTR, len As SIZE_T) As PWSTR
186 GetStr(mbs, len, ToWCStr)
187End Function
188
189Function ToWCStr(wcsz As PWSTR) As PWSTR
190 ToWCStr = wcsz
191End Function
192
193Function ToWCStr(wcs As PWSTR, len As SIZE_T) As PWSTR
194 ToWCStr = wcs
195End Function
196
197Function ToWCStr(s As String) As PWSTR
198 GetStr(s, ToWCStr)
199End Function
200
201Function ToMBStr(mbsz As PSTR) As PSTR
202 ToMBStr = mbsz
203End Function
204
205Function ToMBStr(mbs As PSTR, len As SIZE_T) As PSTR
206 ToMBStr = mbs
207End Function
208
209Function ToMBStr(wcsz As PWSTR) As PSTR
210 Detail.GetStrNT(wcsz, ToMBStr)
211End Function
212
213Function ToMBStr(wcs As PWSTR, len As SIZE_T) As PSTR
214 GetStr(wcs, len, ToMBStr)
215End Function
216
217Function ToMBStr(s As String) As PSTR
218 GetStr(s, ToMBStr)
219End Function
220
221Function ToTCStr(mbsz As PSTR) As PCTSTR
222 Detail.GetStrNT(mbsz, ToTCStr)
223End Function
224
225Function ToTCStr(mbs As PSTR, len As SIZE_T) As PCTSTR
226 GetStr(mbs, len, ToTCStr)
227End Function
228
229Function ToTCStr(wcsz As PWSTR) As PCTSTR
230 Detail.GetStrNT(wcsz, ToTCStr)
231End Function
232
233Function ToTCStr(wcs As PWSTR, len As SIZE_T) As PCTSTR
234 GetStr(wcs, len, ToTCStr)
235End Function
236
237Function ToTCStr(s As String) As PCTSTR
238 Return StrPtr(s)
239End Function
240
241#ifndef UNICODE
242TypeDef BoxedStrChar = System.SByte
243#else
244TypeDef BoxedStrChar = System.UInt16
245#endif
Note: See TracBrowser for help on using the repository browser.