18
18
// You should have received a copy of the GNU General Public License
19
19
// along with this program. If not, write to the Free Software Foundation,
20
// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
// Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
// =============================================================================
24
#define _KVI_DEBUG_CHECK_RANGE_
25
#define _KVI_DEBUG_CLASS_NAME_ "KviRawEvent"
26
29
#include "kvi_defines.h"
30
#include "kvi_fileutils.h"
27
31
#include "kvi_locale.h"
28
32
#include "kvi_rawevent.h"
29
#include "kvi_fileutils.h"
31
34
KviRawEventManager::KviRawEventManager()
33
36
m_pLiteralEventsDict = 0;
34
for(int i=0;i<1000;i++)m_numericEvents[i] = 0;
37
for( int i = 0; i < 1000; i++ )
38
m_numericEvents[i] = 0;
37
41
KviRawEventManager::~KviRawEventManager()
42
46
bool KviRawEventManager::save(const char *filename)
45
if(!f.open(IO_WriteOnly | IO_Truncate))return false;
47
f.writeBlock(KVI_MAGIC_STRING,KVI_MAGIC_STRING_LENGTH);
49
for(int i = 0;i< 1000;i++){
50
if(m_numericEvents[i])
52
for(KviRawEvent * e = m_numericEvents[i]->first();e;e= m_numericEvents[i]->next())
54
f.writeBlock("[RAWEVENT ",10);
55
KviStr tmp(KviStr::Format,"%d",i);
56
f.writeBlock(tmp.ptr(),tmp.len());
58
f.writeBlock(e->szSource.ptr(),e->szSource.len());
59
f.writeBlock(" bytes ",7);
60
KviStr szNBytes(KviStr::Format,"%d",e->szBuffer.len());
61
f.writeBlock(szNBytes.ptr(),szNBytes.len());
62
f.writeBlock("]\n",2);
63
f.writeBlock(e->szBuffer.ptr(),e->szBuffer.len());
64
f.writeBlock("\n[ENDRAWEVENT]\n",15);
49
if( !f.open(IO_WriteOnly | IO_Truncate) )
52
f.writeBlock(KVI_MAGIC_STRING, KVI_MAGIC_STRING_LENGTH);
54
for( int i = 0; i < 1000; i++ ) {
55
if( m_numericEvents[i] ) {
56
for( KviRawEvent *e = m_numericEvents[i]->first(); e; e = m_numericEvents[i]->next() ) {
57
f.writeBlock("[RAWEVENT ", 10);
58
KviStr tmp(KviStr::Format, "%d", i);
59
f.writeBlock(tmp.ptr(), tmp.len());
61
f.writeBlock(e->szSource.ptr(), e->szSource.len());
62
f.writeBlock(" bytes ", 7);
63
KviStr szNBytes(KviStr::Format, "%d", e->szBuffer.len());
64
f.writeBlock(szNBytes.ptr(), szNBytes.len());
65
f.writeBlock("]\n", 2);
66
f.writeBlock(e->szBuffer.ptr(), e->szBuffer.len());
67
f.writeBlock("\n[ENDRAWEVENT]\n", 15);
69
if(m_pLiteralEventsDict)
72
if( m_pLiteralEventsDict ) {
71
73
QAsciiDictIterator<KviRawEventList> it(*m_pLiteralEventsDict);
72
QList<KviRawEvent> * l = 0;
73
while((l = it.current()))
75
for(KviRawEvent * e = l->first();e;e= l->next())
77
f.writeBlock("[RAWEVENT ",10);
74
QPtrList<KviRawEvent> *l = 0;
75
while( (l = it.current()) ) {
76
for( KviRawEvent *e = l->first(); e; e = l->next() ) {
77
f.writeBlock("[RAWEVENT ", 10);
78
78
KviStr tmp(it.currentKey());
79
f.writeBlock(tmp.ptr(),tmp.len());
81
f.writeBlock(e->szSource.ptr(),e->szSource.len());
82
f.writeBlock(" bytes ",7);
83
KviStr szNBytes(KviStr::Format,"%d",e->szBuffer.len());
84
f.writeBlock(szNBytes.ptr(),szNBytes.len());
85
f.writeBlock("]\n",2);
86
f.writeBlock(e->szBuffer.ptr(),e->szBuffer.len());
87
f.writeBlock("\n[ENDRAWEVENT]\n",15);
79
f.writeBlock(tmp.ptr(), tmp.len());
81
f.writeBlock(e->szSource.ptr(), e->szSource.len());
82
f.writeBlock(" bytes ", 7);
83
KviStr szNBytes(KviStr::Format, "%d", e->szBuffer.len());
84
f.writeBlock(szNBytes.ptr(), szNBytes.len());
85
f.writeBlock("]\n", 2);
86
f.writeBlock(e->szBuffer.ptr(), e->szBuffer.len());
87
f.writeBlock("\n[ENDRAWEVENT]\n", 15);
97
KviRawEventListPtr KviRawEventManager::numericEvent(int i)
99
return m_numericEvents[i];
102
QAsciiDict<KviRawEventList> *KviRawEventManager::literalEventsDict()
104
return m_pLiteralEventsDict;
97
107
void KviRawEventManager::cleanup()
99
if(m_pLiteralEventsDict)delete m_pLiteralEventsDict;
100
m_pLiteralEventsDict = 0;
101
for(int i=0;i<1000;i++)
103
if(m_numericEvents[i])
109
if( m_pLiteralEventsDict ) {
110
delete m_pLiteralEventsDict;
111
m_pLiteralEventsDict = 0;
113
for( int i = 0; i < 1000; i++ ) {
114
if( m_numericEvents[i] ) {
105
115
delete m_numericEvents[i];
106
116
m_numericEvents[i] = 0;
111
void KviRawEventManager::addHandler(const char * command,const char * source,const char * buffer)
121
void KviRawEventManager::addHandler(const char *command, const char *source, const char *buffer)
113
123
KviStr cmd(command);
114
124
cmd.stripWhiteSpace();
115
if(cmd.isEmpty())return;
117
128
bool bOk = false;
118
int num = cmd.toInt(&bOk);
129
int num = cmd.toInt(&bOk);
120
KviRawEvent * e = new KviRawEvent;
131
KviRawEvent *e = new KviRawEvent();
121
132
e->szSource = source;
122
133
e->szSource.stripWhiteSpace();
123
if(e->szSource.isEmpty())e->szSource = "*";
134
if( e->szSource.isEmpty() )
124
136
e->szBuffer = buffer;
128
if((num >= 0) && (num < 1000))
130
if(!m_numericEvents[num])
132
m_numericEvents[num] = new QList<KviRawEvent>;
139
if( (num >= 0) && (num < 1000) ) {
140
if( !m_numericEvents[num] ) {
141
m_numericEvents[num] = new QPtrList<KviRawEvent>;
133
142
m_numericEvents[num]->setAutoDelete(true);
135
144
m_numericEvents[num]->append(e);
140
if(!m_pLiteralEventsDict)
142
m_pLiteralEventsDict = new QAsciiDict<KviRawEventList>(17,false);
149
if( !m_pLiteralEventsDict ) {
150
m_pLiteralEventsDict = new QAsciiDict<KviRawEventList>(17, false);
143
151
m_pLiteralEventsDict->setAutoDelete(true);
145
QList<KviRawEvent> * l = m_pLiteralEventsDict->find(command);
148
l = new QList<KviRawEvent>;
153
QPtrList<KviRawEvent> *l = m_pLiteralEventsDict->find(command);
155
l = new QPtrList<KviRawEvent>;
149
156
l->setAutoDelete(true);
150
m_pLiteralEventsDict->insert(command,l);
157
m_pLiteralEventsDict->insert(command, l);
156
bool KviRawEventManager::load(const char *filename,KviStr &error)
163
bool KviRawEventManager::load(const char *filename, KviStr &error)
160
if(!kvi_loadFile(filename,buffer)){
167
if( !kvi_loadFile(filename, buffer) ) {
161
168
error = _i18n_("Unable to load file");
164
if(!kvi_strEqualCSN(KVI_MAGIC_STRING,buffer.ptr(),KVI_MAGIC_STRING_LENGTH)){
171
if( !kvi_strEqualCSN(KVI_MAGIC_STRING, buffer.ptr(), KVI_MAGIC_STRING_LENGTH) ) {
165
172
error = _i18n_("Invalid magic");
166
return false; //not a kvirc file
173
return false; // Not a KVIrc file
168
175
buffer.cutLeft(KVI_MAGIC_STRING_LENGTH);
169
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
170
if(buffer.isEmpty())return true; //done
176
buffer.stripLeftWhiteSpace(); // Be flexible... allow some spaces... for manual editing by experts :)
177
if( buffer.isEmpty() )
172
while(buffer.hasData()){
173
//[RAWEVENT command bytes <numofbytes>]\n
180
while( buffer.hasData() ) {
181
// [RAWEVENT command bytes <numofbytes>]\n
180
if(!kvi_strEqualCIN("[RAWEVENT ",buffer.ptr(),10)){
188
if( !kvi_strEqualCIN("[RAWEVENT ", buffer.ptr(), 10) ) {
181
189
error = _i18n_("Unrecognized [RAWEVENT] label.");
185
193
buffer.cutLeft(10);
186
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
194
buffer.stripLeftWhiteSpace();
189
197
KviStr eventName;
190
if(!buffer.getToken(eventName,' ')){
198
if( !buffer.getToken(eventName, ' ') ) { // There must be at least another token
191
199
error = _i18n_("Syntax error in [RAWEVENT] label");
193
} //there must be at least another token
195
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
202
buffer.stripLeftWhiteSpace();
197
204
KviStr eventSource;
198
if(!buffer.getToken(eventSource,' '))
205
if( !buffer.getToken(eventSource, ' ') ) {
200
206
error = _i18n_("Missing event source in [RAWEVENT] label");
204
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
206
if(!kvi_strEqualCIN("bytes ",buffer.ptr(),6)){
207
error = _i18n_("Syntax error in [RAWEVENT] label (event length)");
209
buffer.stripLeftWhiteSpace();
211
if( !kvi_strEqualCIN("bytes ", buffer.ptr(), 6) ) {
212
error = _i18n_("Syntax error in [RAWEVENT] label: event length");
210
215
buffer.cutLeft(6);
212
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
214
if(!isdigit(*(buffer.ptr()))){
215
error = _i18n_("Syntax error in [RAWEVENT] label (digit expected)");
216
buffer.stripLeftWhiteSpace();
218
if( !isdigit(*(buffer.ptr())) ) {
219
error = _i18n_("Syntax error in [RAWEVENT] label: digit expected");
220
if(!buffer.getToken(numBytes,']')){
221
error = _i18n_("Syntax error in [RAWEVENT] label (can not extract alias length)");
224
if(!buffer.getToken(numBytes, ']')){
225
error = _i18n_("Syntax error in [RAWEVENT] label: cannot extract alias length");
226
230
int bytes = numBytes.toInt(&bOk);
228
error = _i18n_("Unrecogized token ");
232
error = _i18n_("Unrecogized token ");
231
} //syntax error again...someone messed with the file
232
buffer.cutLeft(1); //cut the \n
233
if(buffer.len() < bytes){
234
error = _i18n_("Raw event buffer smaller than declared length (");
235
} // Syntax error... someone messed with the file
236
buffer.cutLeft(1); // Cut the newline
237
if( buffer.len() < bytes ) {
238
error = _i18n_("Raw event buffer smaller than declared length (");
235
239
error += numBytes;
240
KviStr eventBuffer(buffer.ptr(),bytes);
242
addHandler(eventName.ptr(),eventSource.ptr(),eventBuffer.ptr());
244
KviStr eventBuffer(buffer.ptr(), bytes);
245
addHandler(eventName.ptr(), eventSource.ptr(), eventBuffer.ptr());
244
246
buffer.cutLeft(bytes);
246
if(!kvi_strEqualCIN("\n[ENDRAWEVENT]",buffer.ptr(),14)){
248
if( !kvi_strEqualCIN("\n[ENDRAWEVENT]", buffer.ptr(), 14) ) {
247
249
error = _i18n_("Unrecognized [ENDRAWEVENT] label");
250
252
buffer.cutLeft(14);
252
buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
253
buffer.stripLeftWhiteSpace();