-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_MailslotEx.ahk
149 lines (128 loc) · 3.15 KB
/
class_MailslotEx.ahk
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
#Include %A_LineFile%\..\Mailslot.ahk
class MailslotEx extends Mailslot
{
class Server extends Mailslot.Server
{
FStream(encoding:="CP0")
{
return new MailslotEx.Reader(this, encoding)
}
}
class Client extends Mailslot.Client
{
FStream(encoding:="CP0")
{
return new MailslotEx.Writer(this, encoding)
}
}
class Reader
{
__New(slot, encoding:="CP0")
{
ObjSetCapacity(this, "_", bytes := slot._GetInfo("Size"))
addr := ObjGetAddress(this, "_")
if !(read := slot.FRead(addr + 0, bytes))
return
this.__Slot := &slot
this.__Ptr := addr + 0
this.Pos := addr + 0
this.Size := read + 0
this.Encoding := RTrim(encoding, "-RAWraw") ;// StrGet doesn't seem to recognize the 'RAW' suffix
}
__Get(key:="", args*)
{
if !key
return ObjGetAddress(this, "_")
}
__Call(method, args*)
{
if (method ~= "i)^Read(U?(Int|Short|Char|Ptr)|Int64|Double|Float)$")
return this.ReadNum(SubStr(method, 5))
}
Read(chars:=-1)
{
enc := this.Encoding
BytesPerChar := (enc = "UTF-16" || enc = "CP1200") ? 2 : 1
bytes_left := (this.__Ptr + this.Size) - this.Pos
chars_left := bytes_left // BytesPerChar
if (chars < 0 || chars > chars_left) ;// read rest of buffer from current pos
chars := chars_left
str := StrGet(this.Pos, chars, enc)
this.Seek((StrPut(str, enc) - 1) * BytesPerChar, 1)
return str
}
ReadLine()
{
enc := this.Encoding
BytesPerChar := (enc = "UTF-16" || enc = "CP1200") ? 2 : 1
length := ((this.__Ptr + this.Size) - this.Pos) // BytesPerChar
str := StrGet(this.Pos, length, enc)
ln := SubStr(str, 1, (p := InStr(str, "`n")) ? p : StrLen(str))
this.Seek((StrPut(str, enc) - 1) * BytesPerChar, 1)
return ln
}
RawRead(ByRef buf, bytes)
{
VarSetCapacity(buf, bytes)
DllCall("RtlMoveMemory", "Ptr", &buf, "Ptr", this.Pos, "UPtr", bytes)
}
Seek(distance, origin:=0)
{
if (distance < 0 && origin != 2)
origin := 2
start := origin == 0 ? this.__Ptr
: origin == 1 ? this.Pos
: origin == 2 ? this.__Ptr + this.Size
: 0
return start ? this.Pos := start + distance : 0
}
ReadNum(NumType:="UPtr")
{
static sizeof := { "Char": 1, "Short": 2, "Int": 4, "Int64": 8
, "Float": 4, "Double": 8, "Ptr": A_PtrSize }
ptr := this.Pos, this.Seek(sizeof[ LTrim(NumType, "Uu") ], 1)
return NumGet(ptr + 0, NumType)
}
}
class Writer
{
__New(slot, encoding:="CP0")
{
if !(fSlot := FileOpen(slot.Filename, "w"))
return
this.__Slot := &slot
this.__Stream := fSlot
this.Encoding := encoding ;// set encoding after opening the file to avoid BOM
}
__Delete()
{
this.__Stream.Close()
}
__Call(method, args*)
{
if (method ~= "i)^Write(Line|U?(Int|Short|Char)|Int64|Double|Float)?$")
{
fSlot := this.__Stream
return fSlot[method](args*)
}
}
RawWrite(ByRef buf, bytes)
{
fSlot := this.__Stream
return fSlot.RawWrite(buf, bytes)
}
__Handle {
get {
return this.__Stream.__Handle
}
}
Encoding {
get {
return this.__Stream.Encoding
}
set {
return this.__Stream.Encoding := value
}
}
}
}