~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvirc/kvi_rawevent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//   File : kvi_rawevent.cpp
3
 
//   Last major modification : Sat Feb 30 2000 17:16:14 by Szymon Stefanek
4
 
//
5
 
//   This file is part of the KVirc irc client distribution
 
1
// =============================================================================
 
2
//
 
3
//      --- kvi_rawevent.cpp ---
 
4
//
 
5
//   This file is part of the KVIrc IRC client distribution
6
6
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
7
7
//
8
8
//   This program is FREE software. You can redistribute it and/or
17
17
//
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.
21
21
//
22
 
 
23
 
 
 
22
// =============================================================================
 
23
 
 
24
#define _KVI_DEBUG_CHECK_RANGE_
 
25
#define _KVI_DEBUG_CLASS_NAME_ "KviRawEvent"
24
26
 
25
27
#include <qfile.h>
 
28
 
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"
30
33
 
31
34
KviRawEventManager::KviRawEventManager()
32
35
{
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;
35
39
}
36
40
 
37
41
KviRawEventManager::~KviRawEventManager()
42
46
bool KviRawEventManager::save(const char *filename)
43
47
{
44
48
        QFile f(filename);
45
 
        if(!f.open(IO_WriteOnly | IO_Truncate))return false;
46
 
 
47
 
        f.writeBlock(KVI_MAGIC_STRING,KVI_MAGIC_STRING_LENGTH);
48
 
 
49
 
        for(int i = 0;i< 1000;i++){
50
 
                if(m_numericEvents[i])
51
 
                {
52
 
                        for(KviRawEvent * e = m_numericEvents[i]->first();e;e= m_numericEvents[i]->next())
53
 
                        {
54
 
                                f.writeBlock("[RAWEVENT ",10);
55
 
                                KviStr tmp(KviStr::Format,"%d",i);
56
 
                                f.writeBlock(tmp.ptr(),tmp.len());
57
 
                                f.writeBlock(" ",1);
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) )
 
50
                return false;
 
51
 
 
52
        f.writeBlock(KVI_MAGIC_STRING, KVI_MAGIC_STRING_LENGTH);
 
53
 
 
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());
 
60
                                f.writeBlock(" ", 1);
 
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);
65
68
                        }
66
69
                }
67
70
        }
68
71
 
69
 
        if(m_pLiteralEventsDict)
70
 
        {
 
72
        if( m_pLiteralEventsDict ) {
71
73
                QAsciiDictIterator<KviRawEventList> it(*m_pLiteralEventsDict);
72
 
                QList<KviRawEvent> * l = 0;
73
 
                while((l = it.current()))
74
 
                {
75
 
                        for(KviRawEvent * e = l->first();e;e= l->next())
76
 
                        {
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());
80
 
                                f.writeBlock(" ",1);
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);
88
 
                        }                       
 
79
                                f.writeBlock(tmp.ptr(), tmp.len());
 
80
                                f.writeBlock(" ", 1);
 
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);
 
88
                        }
89
89
                        ++it;
90
90
                }
91
91
        }
94
94
        return true;
95
95
}
96
96
 
 
97
KviRawEventListPtr KviRawEventManager::numericEvent(int i)
 
98
{
 
99
        return m_numericEvents[i];
 
100
}
 
101
 
 
102
QAsciiDict<KviRawEventList> *KviRawEventManager::literalEventsDict()
 
103
{
 
104
        return m_pLiteralEventsDict;
 
105
}
 
106
 
97
107
void KviRawEventManager::cleanup()
98
108
{
99
 
        if(m_pLiteralEventsDict)delete m_pLiteralEventsDict;
100
 
        m_pLiteralEventsDict = 0;
101
 
        for(int i=0;i<1000;i++)
102
 
        {
103
 
                if(m_numericEvents[i])
104
 
                {
 
109
        if( m_pLiteralEventsDict ) {
 
110
                delete m_pLiteralEventsDict;
 
111
                m_pLiteralEventsDict = 0;
 
112
        }
 
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;
107
117
                }
108
118
        }
109
119
}
110
120
 
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)
112
122
{
113
123
        KviStr cmd(command);
114
124
        cmd.stripWhiteSpace();
115
 
        if(cmd.isEmpty())return;
 
125
        if( cmd.isEmpty() )
 
126
                return;
116
127
 
117
128
        bool bOk = false;
118
 
        int num = cmd.toInt(&bOk);
 
129
        int  num = cmd.toInt(&bOk);
119
130
 
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() )
 
135
                e->szSource = "*";
124
136
        e->szBuffer = buffer;
125
137
 
126
 
        if(bOk)
127
 
        {
128
 
                if((num >= 0) && (num < 1000))
129
 
                {
130
 
                        if(!m_numericEvents[num])
131
 
                        {
132
 
                                m_numericEvents[num] = new QList<KviRawEvent>;
 
138
        if( bOk ) {
 
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);
134
143
                        }
135
144
                        m_numericEvents[num]->append(e);
137
146
                }
138
147
        }
139
148
 
140
 
        if(!m_pLiteralEventsDict)
141
 
        {
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);
144
152
        }
145
 
        QList<KviRawEvent> * l = m_pLiteralEventsDict->find(command);
146
 
        if(!l)
147
 
        {
148
 
                l = new QList<KviRawEvent>;
 
153
        QPtrList<KviRawEvent> *l = m_pLiteralEventsDict->find(command);
 
154
        if( !l ) {
 
155
                l = new QPtrList<KviRawEvent>;
149
156
                l->setAutoDelete(true);
150
 
                m_pLiteralEventsDict->insert(command,l);
 
157
                m_pLiteralEventsDict->insert(command, l);
151
158
        }
152
159
 
153
160
        l->append(e);
154
161
}
155
162
 
156
 
bool KviRawEventManager::load(const char *filename,KviStr &error)
 
163
bool KviRawEventManager::load(const char *filename, KviStr &error)
157
164
{
158
165
        cleanup();
159
166
        KviStr buffer;
160
 
        if(!kvi_loadFile(filename,buffer)){
 
167
        if( !kvi_loadFile(filename, buffer) ) {
161
168
                error = _i18n_("Unable to load file");
162
169
                return false;
163
170
        }
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
167
174
        }
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() )
 
178
                return true; // Done
171
179
 
172
 
        while(buffer.hasData()){
173
 
                //[RAWEVENT command bytes <numofbytes>]\n
174
 
                //...
175
 
                //...
176
 
                //...
177
 
                //\n[ENDRAWEVENT]\n
 
180
        while( buffer.hasData() ) {
 
181
                // [RAWEVENT command bytes <numofbytes>]\n
 
182
                // ...
 
183
                // ...
 
184
                // ...
 
185
                // \n[ENDRAWEVENT]\n
178
186
 
179
187
                // Event label
180
 
                if(!kvi_strEqualCIN("[RAWEVENT ",buffer.ptr(),10)){
 
188
                if( !kvi_strEqualCIN("[RAWEVENT ", buffer.ptr(), 10) ) {
181
189
                        error = _i18n_("Unrecognized [RAWEVENT] label.");
182
190
                        return false;
183
191
                }
184
192
 
185
193
                buffer.cutLeft(10);
186
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
 
194
                buffer.stripLeftWhiteSpace();
187
195
 
188
196
                // Event name
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");
192
200
                        return false;
193
 
                } //there must be at least another token
194
 
 
195
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
 
201
                }
 
202
                buffer.stripLeftWhiteSpace();
196
203
 
197
204
                KviStr eventSource;
198
 
                if(!buffer.getToken(eventSource,' '))
199
 
                {
 
205
                if( !buffer.getToken(eventSource, ' ') ) {
200
206
                        error = _i18n_("Missing event source in [RAWEVENT] label");
201
207
                        return false;
202
208
                }
203
 
 
204
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
205
 
 
206
 
                if(!kvi_strEqualCIN("bytes ",buffer.ptr(),6)){
207
 
                        error = _i18n_("Syntax error in [RAWEVENT] label (event length)");
 
209
                buffer.stripLeftWhiteSpace();
 
210
 
 
211
                if( !kvi_strEqualCIN("bytes ", buffer.ptr(), 6) ) {
 
212
                        error = _i18n_("Syntax error in [RAWEVENT] label: event length");
208
213
                        return false;
209
214
                }
210
215
                buffer.cutLeft(6);
211
 
 
212
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
213
 
 
214
 
                if(!isdigit(*(buffer.ptr()))){
215
 
                        error = _i18n_("Syntax error in [RAWEVENT] label (digit expected)");
 
216
                buffer.stripLeftWhiteSpace();
 
217
 
 
218
                if( !isdigit(*(buffer.ptr())) ) {
 
219
                        error = _i18n_("Syntax error in [RAWEVENT] label: digit expected");
216
220
                        return false;
217
221
                }
218
222
 
219
223
                KviStr numBytes;
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");
222
226
                        return false;
223
227
                }
224
228
 
225
 
                bool bOk = false;
 
229
                bool bOk  = false;
226
230
                int bytes = numBytes.toInt(&bOk);
227
 
                if(!bOk){
228
 
                        error = _i18n_("Unrecogized token ");
229
 
                        error +=numBytes;
 
231
                if( !bOk ) {
 
232
                        error  = _i18n_("Unrecogized token ");
 
233
                        error += numBytes;
230
234
                        return false;
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;
236
 
                        error +=")";
 
240
                        error += ")";
237
241
                        return false;
238
242
                }
239
243
 
240
 
                KviStr eventBuffer(buffer.ptr(),bytes);
241
 
 
242
 
                addHandler(eventName.ptr(),eventSource.ptr(),eventBuffer.ptr());
243
 
 
 
244
                KviStr eventBuffer(buffer.ptr(), bytes);
 
245
                addHandler(eventName.ptr(), eventSource.ptr(), eventBuffer.ptr());
244
246
                buffer.cutLeft(bytes);
245
247
 
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");
248
250
                        return false;
249
251
                }
250
252
                buffer.cutLeft(14);
251
 
 
252
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
 
253
                buffer.stripLeftWhiteSpace();
253
254
        }
254
255
        return true;
255
256
}
256
 
 
257
 
 
258