-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcessAsciiGraphicsCommand.bas
More file actions
114 lines (95 loc) · 6.82 KB
/
ProcessAsciiGraphicsCommand.bas
File metadata and controls
114 lines (95 loc) · 6.82 KB
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
#include once "ProcessAsciiGraphicsCommand.bi"
#include once "CharConstants.bi"
Sub ProcessAsciiGraphicsCommand( _
ByVal pBot As IrcBot Ptr, _
ByVal User As WString Ptr, _
ByVal Channel As WString Ptr, _
ByVal MessageText As WString Ptr _
)
Dim wSpace1 As WString Ptr = StrChr(MessageText, WhiteSpaceChar)
If wSpace1 = 0 Then
Exit Sub
End If
Dim AsciiPictureName As WString Ptr = wSpace1 + 1
If lstrlen(AsciiPictureName) = 0 Then
Exit Sub
End If
Dim hFile As HANDLE = CreateFile(@pBot->AsciiFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile = INVALID_HANDLE_VALUE Then
Exit Sub
End If
Const MaxBufferLength As Integer = 635
' Буфер для хранения данных чтения
Dim Buffer As ZString * (MaxBufferLength + SizeOf(WString)) = Any
' Читаем данные файла
Dim ReadBytesCount As DWORD = Any
If ReadFile(hFile, @Buffer, MaxBufferLength, @ReadBytesCount, 0) <> 0 Then
' Ставим нулевой символ, чтобы строка была валидной
Buffer[ReadBytesCount] = 0
Buffer[ReadBytesCount + 1] = 0
' Будем считать, что кодировка текста UTF-16 с меткой BOM
If ReadBytesCount > 2 Then
If Buffer[0] = 255 AndAlso Buffer[1] = 254 Then
Dim wLine As WString Ptr = CPtr(WString Ptr, @Buffer[2])
Dim ReadedBytesCount As Integer = 0
Dim Result2 As Integer = 1
' Флаг того, что фразу нашли
Dim FindFlag As Boolean = False
Do
' Найти в буфере CrLf
Dim wCrLf As WString Ptr = StrStr(wLine, @vbCrLf)
Do While wCrLf = NULL
' Проверить буфер на переполнение
If ReadedBytesCount >= MaxBufferLength Then
' Буфер заполнен, будем читать данные в следующий раз
Buffer[MaxBufferLength] = 0
Buffer[MaxBufferLength + 1] = 0
Exit Do
End If
' Если CrLf в буфере нет, то читать данные с файла
Result2 = ReadFile(hFile, @Buffer + ReadedBytesCount, MaxBufferLength - ReadedBytesCount, @ReadBytesCount, 0)
If Result2 = 0 OrElse ReadBytesCount = 0 Then
' Ошибка или данные прочитаны, выйти
Exit Do
End If
' Прочитанный байт всего
ReadedBytesCount += ReadBytesCount
' Ставим нулевой символ, чтобы строка была валидной
Buffer[ReadBytesCount] = 0
Buffer[ReadBytesCount + 1] = 0
' Искать CrLf заново
wCrLf = StrStr(wLine, @vbCrLf)
Loop
' CrLf найдено
If wCrLf <> 0 Then
wCrLf[0] = 0
End If
If FindFlag Then
' Если пустая строка, то выйти из цикла
If lstrlen(wLine) = 0 Then
Exit Do
End If
' Отправить строку в чат
pBot->SayWithTimeOut(Channel, wLine)
End If
' Сравнить со строкой
If lstrcmp(AsciiPictureName, wLine) = 0 Then
' Найдено, теперь нужно отобразить в чат
FindFlag = True
End If
' Переместить правее CrLf
If wCrLf <> 0 Then
wLine = wCrLf + 2
' Передвинуть данные в буфере влево
Dim tmpBuffer As ZString * (MaxBufferLength + SizeOf(WString)) = Any
lstrcpy(CPtr(WString Ptr, @tmpBuffer), wLine)
lstrcpy(CPtr(WString Ptr, @Buffer), CPtr(WString Ptr, @tmpBuffer))
wLine = CPtr(WString Ptr, @Buffer)
End If
ReadedBytesCount = 0
Loop While Result2 <> 0 And ReadBytesCount <> 0
End If
End If
End If
CloseHandle(hFile)
End Sub