-
Notifications
You must be signed in to change notification settings - Fork 1
/
CBWString.CLW
389 lines (366 loc) · 15.4 KB
/
CBWString.CLW
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
MEMBER()
!--------------------------
! CBWStrFactoryClass
!
!--------------------------
! INCLUDE('EQUATES.CLW')
INCLUDE('CBWString.INC'),ONCE
CP_ACP equate(0) ! default to ANSI code page
MB_PRECOMPOSED equate(00000001h) ! use precomposed chars
MB_COMPOSITE equate(00000002h) ! use composite chars
MB_USEGLYPHCHARS equate(00000004h) ! use glyph chars, not ctrl chars
MB_ERR_INVALID_CHARS equate(00000008h) ! error for invalid chars
hProcessHeap LONG !Does not change so just get once
MAP
MODULE('Win32')
GetProcessHeap(),long,pascal,dll(1)
HeapAlloc(long hHeap, long dwFlags, long dwBytes),long,pascal,dll(1)
HeapFree(long hHeap, long dwFlags, long lpMem),bool,pascal,proc,dll(1)
MultiByteToWideChar(long CodePage, long dwFlags, long lpMultiByteStr, long cbMultiByte, |
long lpWideCharStr, long cchWideCharStr),long,pascal,dll(1)
CBZeroMemory(LONG DestinationPtr, LONG dwLength),PASCAL,NAME('RtlZeroMemory'),DLL(1)
SysReAllocString(LONG BSTR_pbstr, LONG OLECHAR_psz),SIGNED,RAW,PASCAL,DLL(1)
GetLastError(),long,pascal,dll(1)
END
MODULE('RTL')
!04/27/22 out: LenFastClip PROCEDURE(CONST *STRING Text2Measure),LONG,NAME('Cla$FASTCLIP') ! LenFastClip=len(clip( C10
! In Release builds using this sometimes stuck a Null in byte 1 of the string (I think)
! It is an undocumted RTL function so best to avoid
END
END
!----------------------------------------
CBWStrFactoryClass.CONSTRUCT PROCEDURE()
!----------------------------------------
CODE
SELF.CodePage = CP_ACP
SELF.FlagsMBTWC = MB_PRECOMPOSED
IF (SELF.WStrQ &= NULL) THEN
SELF.WStrQ &= NEW CBWStrQueueType
END
SELF.hProcessHeap = GetProcessHeap()
? ASSERT(SELF.hProcessHeap)
RETURN
!---------------------------------------
CBWStrFactoryClass.DESTRUCT PROCEDURE()
!---------------------------------------
CODE
IF NOT (SELF.WStrQ &= NULL) THEN
! message('CBWStrFactoryClass.DESTRUCT frees ' & Records(SELF.WStrQ) )
SELF.WStrQ_FreeQ()
END
RETURN
!!-----------------------------------
!CBWStrFactoryClass.Init PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!
!!-----------------------------------
!CBWStrFactoryClass.Kill PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!---------------------------------------
CBWStrFactoryClass.WStrQ_FreeQ PROCEDURE()
!---------------------------------------
CODE
LOOP
GET(SELF.WStrQ, 1)
IF ERRORCODE() THEN BREAK.
SELF.WStrQ_DeleteQ()
END
FREE(SELF.WStrQ)
RETURN
!---------------------------------------
CBWStrFactoryClass.WStrQ_DeleteQ PROCEDURE()
CODE !DISPOSE of References in this Q (If are any)
IF SELF.WStrQ.WStrPtr THEN
IF ~HeapFree(SELF.hProcessHeap, 0, SELF.WStrQ.WStrPtr) THEN . !0=Error
SELF.WStrQ.WStrPtr = 0
END
DELETE(SELF.WStrQ)
RETURN
!---------------------------------------
CBWStrFactoryClass.WStrQ_AddQ PROCEDURE(LONG pWStrPtr, LONG pWStrBytes)
CODE
SELF.WStrQ.WStrPtr = pWStrPtr
SELF.WStrQ.WStrBytes = pWStrBytes
IF pWStrPtr THEN
ADD(SELF.WStrQ, SELF.WStrQ.WStrPtr)
ELSE
? ASSERT(0=SELF.Asserts)
END
RETURN
!---------------------------------------
CBWStrFactoryClass.LenBytes PROCEDURE(<LONG pWStrPtr>) !Bytes = 2x WChar including <0,0>. OMIT for last
CODE
IF OMITTED(pWStrPtr) THEN RETURN(SELF.WStrQ.WStrBytes). !Omit for Last Make
IF pWStrPtr=0 THEN RETURN(0). !Omit for Last Make
IF SELF.WStrQ.WStrPtr <> pWStrPtr THEN
SELF.WStrQ.WStrPtr = pWStrPtr ; GET(SELF.WStrQ, SELF.WStrQ.WStrPtr) ; IF ERRORCODE() THEN CLEAR(SELF.WStrQ).
END
RETURN SELF.WStrQ.WStrBytes
!---------------------------
CBWStrFactoryClass.LenWChar PROCEDURE(<LONG pWStrPtr>) !WChar including <0,0> = Bytes/2. OMIT for last
CODE
IF OMITTED(pWStrPtr) THEN RETURN(SELF.WStrQ.WStrBytes / 2).
RETURN SELF.LenBytes() / 2
!---------------------------------------
!-----------------------------------
CBWStrFactoryClass.Proc_1 PROCEDURE()
!-----------------------------------
CODE
MESSAGE('CBWStrFactoryClass.Proc_1 GetProcessHeap=' & GetProcessHeap() &'|SELF.hProcessHeap=' & SELF.hProcessHeap )
RETURN
!!-----------------------------------
!CBWStrFactoryClass.Proc_2 PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!!-----------------------------------
!CBWStrFactoryClass.Proc_3 PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!!-----------------------------------
!CBWStrFactoryClass.Proc_4 PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!!-----------------------------------
!CBWStrFactoryClass.Proc_5 PROCEDURE()
!!-----------------------------------
! CODE
! RETURN
!---------------------------------------------------------------------
CBWStrFactoryClass.Make1 procedure(*cstring sz)!,LONG !Returns Address
dwWideChrs long,auto
dwBytes long,auto
ptrWideStr long
code
!TODO Check for Blank and ?
!TODO: Web says "Don't use MB_PRECOMPOSED" also that it is ignored
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sz), -1, 0, 0) !-1=CHR(0) terminated
IF dwWideChrs THEN
dwBytes = dwWideChrs * 2
ptrWideStr = HeapAlloc(SELF.hProcessHeap, 0, dwBytes)
IF ptrWideStr THEN
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sz), -1, ptrWideStr, dwWideChrs)
IF dwWideChrs THEN
SELF.WStrQ_AddQ(ptrWideStr,dwBytes) !We worked so add to queue to dispose
ELSE
? IF SELF.Asserts THEN ASSERT(dwWideChrs,'Make1 Cstr MultiByteToWideChar #2 err ' & GetLastError()).
IF ~HeapFree(SELF.hProcessHeap, 0, ptrWideStr) THEN
? IF SELF.Asserts THEN ASSERT(0,'Make1 Cstr HeapFree err ' & GetLastError()).
END
ptrWideStr = 0
END
ELSE
? IF SELF.Asserts THEN ASSERT(ptrWideStr,'Make1 Cstr HeapAlloc err ' & GetLastError()).
END
ELSE
? IF SELF.Asserts THEN ASSERT(dwWideChrs,'Make1 Cstr MultiByteToWideChar #1 err ' & GetLastError()).
END
IF ~ptrWideStr THEN CLEAR(SELF.WStrQ).
RETURN ptrWideStr
!---------------------------------------------------------------------
CBWStrFactoryClass.Make1 PROCEDURE(*String str, BYTE bClip=1)!,LONG !Returns Address
sRef &STRING
sSpace STRING(1) !(' ')
sLen long,auto
dwBytes long
dwWideChrs long,auto
ptrWideStr long
CODE
sLen=CHOOSE(~bClip,SIZE(str),LEN(CLIP(str))) !04/27/22 was: sLen=CHOOSE(~bClip,size(str),LenFastClip(str))
IF ~sLen THEN
IF bClip<>2 THEN RETURN 0. !Pass 2 for a ' ' string always
sRef &= sSpace ; sLen=1 !If Length passed is Zero below MB2WC fails so assure at least a space
ELSE
sRef &= str
END
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sRef), sLen, 0, 0) !will NOT include room for <0,0>
IF dwWideChrs THEN
dwBytes = dwWideChrs * 2 + 2 !add 2 to allow room to add <0,0>
ptrWideStr = HeapAlloc(SELF.hProcessHeap, 0, dwBytes) !assert(self.pWideStr)
! stop('1 str=' & str & '<13,10>sLen=' & sLen & ' dwWideChrs=' & dwWideChrs & ' dwBytes=' & dwBytes & ' pWideStr=' & ptrWideStr)
IF ptrWideStr THEN
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sRef), sLen, ptrWideStr, dwWideChrs)
IF dwWideChrs THEN !We WORKED and have a WideStr
CBZeroMemory(ptrWideStr+dwBytes-2,2) !must <0,0> after end because converted STRING. Could have used HEAP_ZERO_MEMORY Equate(8)
SELF.WStrQ_AddQ(ptrWideStr,dwBytes) !We worked so add to queue to dispose
! stop('2 str=' & str & '<13,10>sLen=' & sLen & ' dwWideChrs=' & dwWideChrs & ' dwBytes=' & dwBytes & ' pWideStr=' & ptrWideStr)
ELSE
? IF SELF.Asserts THEN ASSERT(dwWideChrs,'Make1 Str MultiByteToWideChar #2 err ' & GetLastError()).
IF ~HeapFree(SELF.hProcessHeap, 0, ptrWideStr) THEN
? IF SELF.Asserts THEN ASSERT(0,'Make1 Str HeapFree err ' & GetLastError()).
END
ptrWideStr = 0
END
ELSE
? IF SELF.Asserts THEN ASSERT(ptrWideStr,'Make1 Str HeapAlloc err ' & GetLastError()).
END
ELSE
? IF SELF.Asserts THEN ASSERT(dwWideChrs,'Make1 Str MultiByteToWideChar #1 err ' & GetLastError()).
END
IF ~ptrWideStr THEN CLEAR(SELF.WStrQ).
RETURN ptrWideStr
!---------------------------------------------------------------------
CBWStrFactoryClass.Make1s2c PROCEDURE(*String str, BYTE bClip=1)!,LONG !Returns Address
sz &cstring
sLen LONG,AUTO
ptrWideStr long
CODE
!Thought about calling MultiByteToWideChar() here with the STRING but then WStr would not have ending <0,0> w/o adding it
!probably worth considering since there are so many STRINGs. Try it with my little class first
sLen=CHOOSE(~bClip,SIZE(str),LEN(CLIP(str))) !04/27/22 was: sLen=CHOOSE(~bClip,size(str),LenFastClip(str))
IF sLen=0 THEN RETURN 0. !Blank input returns 0 null pointer
sz &= new cstring(sLen + 1)
? assert(~sz &= null)
IF ~sz &= null THEN
sz = str[1 : sLen]
ptrWideStr = SELF.Make1(sz)
DISPOSE(sz)
END
RETURN ptrWideStr
!---------------------------------------------------------------------
CBWStrFactoryClass.Make1 PROCEDURE(STRING sstr, BYTE bClip=0)
CODE
RETURN SELF.Make1(sstr,bClip)
!---------------------------------------------------------------------
CBWStrFactoryClass.Replace1 PROCEDURE(*LONG InOutPtrWideStr, *String InStr, BYTE bClip=1)
CODE
IF InOutPtrWideStr THEN
SELF.WStrQ.WStrPtr = InOutPtrWideStr
GET(SELF.WStrQ,SELF.WStrQ.WStrPtr)
IF ~ERRORCODE() THEN SELF.WStrQ_DeleteQ().
END
InOutPtrWideStr = SELF.Make1(InStr, bClip)
RETURN
OMIT('**END**')
!end of OMIT('**END**')
!================= Light Weight WideStr ==============================================
!Should I use a BSTRING for Unicode convert? Peek(address(Bstr), GiveAddrWideStr )
CBWideStrClass.Construct procedure
CODE
IF ~hProcessHeap THEN hProcessHeap = GetProcessHeap().
SELF.CodePage = CP_ACP ; SELF.FlagsMBTWC = MB_PRECOMPOSED
return
CBWideStrClass.Destruct procedure
code
if self.bSelfCleaning then self.Release().
return
!--------------
CBWideStrClass.Init procedure(*cstring sz, byte bSelfCleaning)
dwWideChrs long,auto
dwbytes long,auto
code
self.bSelfCleaning = bSelfCleaning
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sz), -1, 0, 0) !ACP,PRECOMPOSED
if dwWideChrs then
dwBytes = dwWideChrs * 2
self.pWideStr = HeapAlloc(hProcessHeap, 0, dwBytes) !assert(self.pWideStr)
if self.pWideStr then
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sz), -1, self.pWideStr, dwWideChrs)
if dwWideChrs then
self.dwBytes = dwBytes
else
SELF.Release() !HeapFree(GetProcess Heap(), 0, self.pWideStr) ; self.pWideStr = 0
end
end
end
return self.dwBytes
!---------------
CBWideStrClass.InitS2C Procedure(*String str, Byte bSelfCleaning=true, Byte bClip=1)
sz &cstring
sLen long,auto
dwBytes long
CODE
sLen=CHOOSE(~bClip,SIZE(str),LEN(CLIP(str))) !04/27/22 was: sLen=CHOOSE(~bClip,size(str),LenFastClip(str))
IF ~sLen THEN
IF bClip=2 AND SIZE(str) THEN sLen=1 ELSE RETURN 0. !Pass 2 for a ' ' string always
END
! IF ~sLen AND bClip=2 THEN sLen=1. !Pass 2 for a ' ' string always
! IF sLen=0 THEN RETURN 0. !Blank input returns 0 null pointer
sz &= new cstring(sLen + 1)
? IF SELF.Asserts THEN Assert(~sz &= null).
IF ~sz &= null THEN
? IF SELF.Asserts THEN Assert(sLen>0 AND sLen<=size(str)).
sz = str[1 : sLen] !Carl was--> sz = clip(str) <--chg to save sLen above so avoid clip here
dwBytes = self.Init(sz, bSelfCleaning)
DISPOSE(sz)
END
RETURN dwBytes
!---------------
CBWideStrClass.Init Procedure(String str, Byte bSelfCleaning=true, Byte bClip=0)
CODE
return SELF.init(str,bSelfCleaning,bClip)
!---------------
!Make the WSTR direct from the STRING and save the move to CString. That would have cleaned <0>
CBWideStrClass.Init Procedure(*String str, Byte bSelfCleaning=true, Byte bClip=1)
sRef &STRING
sSpace STRING(1) !(' ')
sLen long,auto
dwBytes long
dwWideChrs long,auto
CODE
sLen=CHOOSE(~bClip,SIZE(str),LEN(CLIP(str))) !04/27/22 was: sLen=CHOOSE(~bClip,size(str),LenFastClip(str))
IF ~sLen THEN
IF bClip<>2 THEN RETURN 0. !Pass 2 for a ' ' string always
sRef &= sSpace ; sLen=1 !If Length passed is Zero below MB2WC fails so assure at least a space
ELSE
sRef &= str
END
self.bSelfCleaning = bSelfCleaning
!c dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sz), -1, 0, 0) !ACP,PRECOMPOSED
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sRef), sLen, 0, 0) !will NOT include room for <0,0>
if dwWideChrs then
dwBytes = dwWideChrs * 2 + 2 !add 2 to allow room to add <0,0>
self.pWideStr = HeapAlloc(hProcessHeap, 0, dwBytes) !assert(self.pWideStr)
! stop('1 str=' & str & '<13,10>sLen=' & sLen & ' dwWideChrs=' & dwWideChrs & ' dwBytes=' & dwBytes & ' pWideStr=' & SELF.pWideStr)
if self.pWideStr then
dwWideChrs = MultiByteToWideChar(SELF.CodePage, SELF.FlagsMBTWC, address(sRef), sLen, self.pWideStr, dwWideChrs)
if dwWideChrs then
self.dwBytes = dwBytes
CBZeroMemory(self.pWideStr+dwBytes-2,2) !must <0,0> after end because converted STRING. Could have used HEAP_ZERO_MEMORY Equate(8)
! stop('2 str=' & str & '<13,10>sLen=' & sLen & ' dwWideChrs=' & dwWideChrs & ' dwBytes=' & dwBytes & ' pWideStr=' & SELF.pWideStr)
else
SELF.Release() !HeapFree(GetProcess Heap(), 0, self.pWideStr) ; self.pWideStr = 0
end
end
end
return self.dwBytes
!---------------
CBWideStrClass.IsInitialised procedure
code
return choose(self.pWideStr)
CBWideStrClass.GetWideStr procedure
code
return self.pWideStr
CBWideStrClass.Len procedure
code
return self.dwBytes
CBWideStrClass.Release procedure
code
if self.pWideStr then
HeapFree(hProcessHeap, 0, self.pWideStr) !assert(
self.dwBytes = 0 ; self.pWideStr = 0
end
return
!==========================================================================================
CBBStringClass.GetWideStr PROCEDURE(*BSTRING BS, BYTE NullIfBlank=0, BYTE NullIfLenZero=0)!,LONG
PWStr LONG,OVER(BS)
CODE
IF PWStr=0 THEN !empty, fall thru to return 0
ELSIF NullIfLenZero AND LEN(BS)=0 THEN RETURN 0
ELSIF NullIfBlank AND ~CLIP(BS) THEN RETURN 0
END
RETURN PWStr
!----------------------------------------------------
CBBStringClass.BStrFromPWStr PROCEDURE(*BSTRING OutBS, LONG InPWStr)!,BOOL !Returns BSTR Set = PWSTR (a copy of it). Returns False if failed
bWorked BOOL
CODE
IF InPWStr<>OutBS AND (InPWStr<0 OR InPWStr>0FFFFh) THEN !Reallocates a previously allocated string to be the size
bWorked=SysReAllocString(ADDRESS(OutBS), InPWStr ) !of a second string and copies the second string into the reallocated memory.
END
RETURN bWorked