forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Macro.pb
123 lines (92 loc) · 2.85 KB
/
Macro.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
CompilerIf #PB_Compiler_Unicode = 0
; We could use PeekS(Memory, -1, #PB_Ascii) for both code, but it will have a performance hit
;
Macro PeekAscii(Memory)
PeekS(Memory)
EndMacro
Macro PeekUnicode(Memory)
PeekS(Memory, -1, #PB_Unicode)
EndMacro
Macro PeekAsciiLength(Memory, Length)
PeekS(Memory, Length)
EndMacro
Macro PokeAscii(Memory, Text)
PokeS(Memory, Text)
EndMacro
Macro PokeUnicode(Memory, Text)
PokeS(Memory, Text, -1, #PB_Unicode)
EndMacro
Macro ReadAsciiString(File)
ReadString(File)
EndMacro
Macro MemoryAsciiLength(Memory)
MemoryStringLength(Memory)
EndMacro
Macro ToAscii(String)
@String
EndMacro
CompilerElse
; not really a macro, but this is a macro in non-unicode mode, that's why it is here
Procedure ToAscii(String$)
Static *Buffer
If *Buffer
FreeMemory(*Buffer)
EndIf
*Buffer = AllocateMemory(StringByteLength(String$, #PB_Ascii) + 1)
PokeS(*Buffer, String$, -1, #PB_Ascii)
ProcedureReturn *Buffer
EndProcedure
Procedure ToUTF8(String$)
Static *Buffer
If *Buffer
FreeMemory(*Buffer)
EndIf
*Buffer = AllocateMemory(StringByteLength(String$, #PB_UTF8) + 1)
PokeS(*Buffer, String$, -1, #PB_UTF8)
ProcedureReturn *Buffer
EndProcedure
Macro PeekAscii(Memory)
PeekS(Memory, -1, #PB_Ascii)
EndMacro
Macro PeekUnicode(Memory)
PeekS(Memory)
EndMacro
Macro PeekAsciiLength(Memory, Length)
PeekS(Memory, Length, #PB_Ascii)
EndMacro
Macro PokeAscii(Memory, Text)
PokeS(Memory, Text, -1, #PB_Ascii)
EndMacro
Macro PokeUnicode(Memory, Text)
PokeS(Memory, Text)
EndMacro
Macro ReadAsciiString(File)
ReadString(File, #PB_Ascii)
EndMacro
Macro MemoryAsciiLength(Memory)
MemoryStringLength(Memory, #PB_Ascii)
EndMacro
CompilerEndIf
; this is the only way to have it work in all cases, including unicode on PPC (no quad!)
Macro AsciiConst(a, b, c, d)
((a) << 24 | (b) << 16 | (c) << 8 | (d))
EndMacro
Macro AsciiConst3(a, b, c)
((a) << 16 | (b) << 8 | (c))
EndMacro
Macro MemoryStringLengthBytes(Memory)
(MemoryStringLength(Memory)*#CharSize)
EndMacro
; If we interpret a UTF8 string as Ascii, we get the size in bytes which is perfect
Macro MemoryUTF8LengthBytes(Memory)
MemoryStringLength(Memory, #PB_Ascii)
EndMacro
; This is for debugging mostly
Macro DebugPointer(Ptr)
RSet(Hex(Ptr, #PB_Integer), SizeOf(INTEGER)*2, "0")
EndMacro