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

« back to all changes in this revision

Viewing changes to src/kvirc/kvi_alias.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_alias.cpp (/usr/build/NEW_kvirc/kvirc/src/kvirc/kvi_alias.cpp)
3
 
//   Last major modification : Sat Mar 6 1999 20:50:53 by Szymon Stefanek
4
 
//
5
 
//   This file is part of the KVirc irc client distribution
 
1
// =============================================================================
 
2
//
 
3
//      --- kvi_alias.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
24
#define _KVI_DEBUG_CHECK_RANGE_
23
 
#include "kvi_locale.h"
 
25
#define _KVI_DEBUG_CLASS_NAME_ "KviAlias"
 
26
 
 
27
#include <qfile.h>
 
28
 
 
29
#include "kvi_alias.h"
24
30
#include "kvi_debug.h"
25
 
#include "kvi_alias.h"
26
31
#include "kvi_defines.h"
 
32
#include "kvi_locale.h"
27
33
 
28
34
KviAliasManager::KviAliasManager()
29
35
{
30
 
        m_pAliasList = new QList<KviAlias>;
 
36
        m_pAliasList = new QPtrList<KviAlias>;
31
37
        m_pAliasList->setAutoDelete(true);
32
38
}
33
39
 
34
40
KviAliasManager::~KviAliasManager()
35
41
{
36
 
        delete m_pAliasList;
 
42
        if( m_pAliasList ) {
 
43
                delete m_pAliasList;
 
44
                m_pAliasList = 0;
 
45
        }
37
46
}
38
47
 
39
 
KviAlias * KviAliasManager::findAlias(const char *name)
 
48
KviAlias *KviAliasManager::findAlias(const char *name)
40
49
{
41
50
        int r;
42
 
        for(KviAlias * a=m_pAliasList->first();a;a=m_pAliasList->next()){
43
 
                r = kvi_strcmpCI(a->szName.ptr(),name);
44
 
                if(r == 0)return a; //equal
45
 
                if(r < 0)return 0; //found a greater one
 
51
        for( KviAlias *a = m_pAliasList->first(); a; a = m_pAliasList->next() ) {
 
52
                r = kvi_strcmpCI(a->szName.ptr(), name);
 
53
                if( r == 0 ) return a; // Equal
 
54
                if( r <  0 ) return 0; // Found a greater one
46
55
        }
47
56
        return 0;
48
57
}
53
62
        s->bLocked = false;
54
63
 
55
64
        KviAlias *a = findAlias(s->szName.ptr());
56
 
        if(a){
57
 
                if(a->bLocked){
58
 
                        debug("Attempt to change a locked alias : %s",a->szName.ptr());
59
 
                        return false; //It is locked!
 
65
        if( a ) {
 
66
                if( a->bLocked ) {
 
67
                        debug("Attempt to change a locked alias : %s", a->szName.ptr());
 
68
                        return false; // It is locked!
60
69
                }
61
70
                m_pAliasList->removeRef(a);
62
71
        }
63
72
        int idx = 0;
64
 
        for(a=m_pAliasList->first();a;a=m_pAliasList->next()){
65
 
                if(kvi_strcmpCI(a->szName.ptr(),s->szName.ptr())<0){
66
 
                        //found a greater one
67
 
                        m_pAliasList->insert(idx,s);
 
73
        for( a = m_pAliasList->first(); a; a = m_pAliasList->next() ) {
 
74
                if( kvi_strcmpCI(a->szName.ptr(), s->szName.ptr()) < 0 ) {
 
75
                        // Found a greater one
 
76
                        m_pAliasList->insert(idx, s);
68
77
                        return true;
69
78
                }
70
79
                idx++;
71
80
        }
72
 
        // found no greater aliases
 
81
        // Found no greater aliases
73
82
        m_pAliasList->append(s);
74
83
        return true;
75
84
}
76
85
 
77
86
void KviAliasManager::clearAll()
78
87
{
79
 
        delete m_pAliasList;
80
 
        m_pAliasList = new QList<KviAlias>;
 
88
        if( m_pAliasList ) delete m_pAliasList;
 
89
        m_pAliasList = new QPtrList<KviAlias>;
81
90
        m_pAliasList->setAutoDelete(true);
82
91
}
83
92
 
 
93
int KviAliasManager::count()
 
94
{
 
95
        return m_pAliasList->count();
 
96
}
 
97
 
84
98
void KviAliasManager::removeAlias(KviAlias *s)
85
99
{
86
100
        __range_valid(s);
90
104
bool KviAliasManager::removeAlias(const char *name)
91
105
{
92
106
        KviAlias *a = findAlias(name);
93
 
        if(a){
94
 
                if(a->bLocked)debug("Attempt to remove a locked alias : %s",a->szName.ptr());
 
107
        if( a ) {
 
108
                if( a->bLocked )
 
109
                        debug("Attempt to remove a locked alias : %s", a->szName.ptr());
95
110
                else {
96
111
                        m_pAliasList->removeRef(a);
97
112
                        return true;
100
115
        return false;
101
116
}
102
117
 
103
 
bool KviAliasManager::load(const char *filename,KviStr &error)
 
118
bool KviAliasManager::load(const char *filename, KviStr &error)
104
119
{
105
120
        KviStr buffer;
106
 
        if(!kvi_loadFile(filename,buffer)){
107
 
                error = __tr("Unable to load file");
 
121
        if( !kvi_loadFile(filename, buffer) ) {
 
122
                error = _i18n_("Unable to load file");
108
123
                return false;
109
124
        }
110
 
        if(!kvi_strEqualCSN(KVI_MAGIC_STRING,buffer.ptr(),KVI_MAGIC_STRING_LENGTH)){
111
 
                error = __tr("Invalid magic");
112
 
                return false; //not a kvirc file
 
125
        if( !kvi_strEqualCSN(KVI_MAGIC_STRING, buffer.ptr(), KVI_MAGIC_STRING_LENGTH) ) {
 
126
                error = _i18n_("Invalid magic");
 
127
                return false; // Not a KVIrc file
113
128
        }
114
129
        buffer.cutLeft(KVI_MAGIC_STRING_LENGTH);
115
 
        buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
116
 
        if(buffer.isEmpty())return true; //done
 
130
        buffer.stripLeftWhiteSpace(); // Be flexible... allow some spaces... for manual editing by experts :)
 
131
        if( buffer.isEmpty() )
 
132
                return true; // Done
117
133
 
118
 
        while(buffer.hasData()){
119
 
                //[ALIAS <aliasname> bytes <numofbytes>]\n
120
 
                //...
121
 
                //...
122
 
                //...
123
 
                //\n[ENDALIAS]\n
124
 
                if(!kvi_strEqualCIN("[ALIAS ",buffer.ptr(),7)){
125
 
                        error = __tr("Unrecognized [ALIAS] label.");
 
134
        while( buffer.hasData() ) {
 
135
                // [ALIAS <aliasname> bytes <numofbytes>]\n
 
136
                // ...
 
137
                // ...
 
138
                // ...
 
139
                // \n[ENDALIAS]\n
 
140
                if( !kvi_strEqualCIN("[ALIAS ", buffer.ptr(), 7) ) {
 
141
                        error = _i18n_("Unrecognized [ALIAS] label.");
126
142
                        return false;
127
143
                }
128
144
                buffer.cutLeft(7);
129
145
 
130
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
 
146
                buffer.stripLeftWhiteSpace(); // Be flexible... allow some spaces... for manual editing by experts :)
131
147
 
132
148
                KviAlias *a = new KviAlias;
133
149
 
134
 
                if(!buffer.getToken(a->szName,' ')){
135
 
                        error = __tr("Syntax error in [ALIAS] label");
136
 
                        delete a;
137
 
                        return false;
138
 
                } //there must be at least another token
139
 
                if(a->szName.isEmpty()){
140
 
                        error = __tr("Empty alias name?");
141
 
                        delete a;
142
 
                        return false;
143
 
                } //syntax error somewhere
144
 
 
145
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
146
 
 
147
 
                if(!kvi_strEqualCIN("bytes ",buffer.ptr(),6)){
148
 
                        error = __tr("Syntax error in [ALIAS] label (alias length)");
149
 
                        delete a;
 
150
                if( !buffer.getToken(a->szName, ' ') ) {
 
151
                        // There must be at least another token
 
152
                        error = _i18n_("Syntax error in [ALIAS] label");
 
153
                        if( a ) delete a;
 
154
                        return false;
 
155
                }
 
156
                if( a->szName.isEmpty() ) {
 
157
                        // Syntax error somewhere
 
158
                        error = _i18n_("Empty alias name?");
 
159
                        if( a ) delete a;
 
160
                        return false;
 
161
                }
 
162
 
 
163
                buffer.stripLeftWhiteSpace(); // Be flexible... allow some spaces... for manual editing by experts :)
 
164
 
 
165
                if( !kvi_strEqualCIN("bytes ", buffer.ptr(), 6) ) {
 
166
                        error = _i18n_("Syntax error in [ALIAS] label (alias length)");
 
167
                        if( a ) delete a;
150
168
                        return false;
151
169
                }
152
170
                buffer.cutLeft(6);
153
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
154
 
 
155
 
 
156
 
                if(!isdigit(*(buffer.ptr()))){
157
 
                        error = __tr("Syntax error in [ALIAS] label (digit expected)");
158
 
                        delete a;
 
171
                buffer.stripLeftWhiteSpace(); // Be flexible... allow some spaces... for manual editing by experts :)
 
172
 
 
173
                if( !isdigit(*(buffer.ptr())) ) {
 
174
                        error = _i18n_("Syntax error in [ALIAS] label (digit expected)");
 
175
                        if( a ) delete a;
159
176
                        return false;
160
177
                }
161
178
 
162
179
                KviStr numBytes;
163
 
                if(!buffer.getToken(numBytes,']')){
164
 
                        error = __tr("Syntax error in [ALIAS] label (can not extract alias length)");
165
 
                        delete a;
 
180
                if( !buffer.getToken(numBytes, ']') ) {
 
181
                        error = _i18n_("Syntax error in [ALIAS] label (cannot extract alias length)");
 
182
                        if( a ) delete a;
166
183
                        return false;
167
184
                }
168
185
 
169
186
                bool bOk = false;
170
187
                int bytes = numBytes.toInt(&bOk);
171
 
                if(!bOk){
172
 
                        error = __tr("Unrecogized token ");
173
 
                        error +=numBytes;
174
 
                        delete a;
175
 
                        return false;
176
 
                } //syntax error again...someone messed with the file
177
 
                buffer.cutLeft(1); //cut the \n
178
 
                if(buffer.len() < bytes){
179
 
                        error = __tr("Alias buffer smaller than declared length (");
180
 
                        error += numBytes;
181
 
                        error +=")";
182
 
                        delete a;
183
 
                        return false;
184
 
                }
185
 
                a->szBuffer.setStr(buffer.ptr(),bytes);
 
188
                if( !bOk ) {
 
189
                        // Syntax error again... someone messed with the file
 
190
                        error = _i18n_("Unrecogized token ");
 
191
                        error += numBytes;
 
192
                        if( a ) delete a;
 
193
                        return false;
 
194
                }
 
195
                buffer.cutLeft(1); // Cut the \n
 
196
                if( buffer.len() < bytes ) {
 
197
                        error = _i18n_("Alias buffer smaller than declared length (");
 
198
                        error += numBytes;
 
199
                        error += ")";
 
200
                        if( a ) delete a;
 
201
                        return false;
 
202
                }
 
203
                a->szBuffer.setStr(buffer.ptr(), bytes);
186
204
                buffer.cutLeft(bytes);
187
205
 
188
 
                if(!kvi_strEqualCIN("\n[ENDALIAS]",buffer.ptr(),11)){
189
 
                        error = __tr("Unrecognized [ENDALIAS] label");
190
 
                        delete a;
 
206
                if( !kvi_strEqualCIN("\n[ENDALIAS]", buffer.ptr(), 11) ) {
 
207
                        error = _i18n_("Unrecognized [ENDALIAS] label");
 
208
                        if( a ) delete a;
191
209
                        return false;
192
210
                }
193
211
                buffer.cutLeft(11);
194
212
 
195
213
                addAlias(a);
196
214
 
197
 
                buffer.stripLeftWhiteSpace(); //be flexible...allow some spaces...for manual editing by experts :)
 
215
                buffer.stripLeftWhiteSpace(); // Be flexible... Allow some spaces... For manual editing by experts :)
198
216
        }
199
217
        return true;
200
218
}
202
220
bool KviAliasManager::save(const char *filename)
203
221
{
204
222
        QFile f(filename);
205
 
        if(!f.open(IO_WriteOnly | IO_Truncate))return false;
206
 
 
207
 
        f.writeBlock(KVI_MAGIC_STRING,KVI_MAGIC_STRING_LENGTH);
208
 
 
209
 
        for(KviAlias *a=m_pAliasList->first();a;a=m_pAliasList->next()){
210
 
                f.writeBlock("[ALIAS ",7);
211
 
                f.writeBlock(a->szName.ptr(),a->szName.len());
212
 
                f.writeBlock(" bytes ",7);
213
 
                KviStr szNBytes(KviStr::Format,"%d",a->szBuffer.len());
214
 
                f.writeBlock(szNBytes.ptr(),szNBytes.len());
215
 
                f.writeBlock("]\n",2);
216
 
                f.writeBlock(a->szBuffer.ptr(),a->szBuffer.len());
217
 
                f.writeBlock("\n[ENDALIAS]\n",12);
 
223
        if( !f.open(IO_WriteOnly | IO_Truncate) )
 
224
                return false;
 
225
 
 
226
        f.writeBlock(KVI_MAGIC_STRING, KVI_MAGIC_STRING_LENGTH);
 
227
 
 
228
        for( KviAlias *a = m_pAliasList->first(); a; a = m_pAliasList->next() ) {
 
229
                f.writeBlock("[ALIAS ", 7);
 
230
                f.writeBlock(a->szName.ptr(), a->szName.len());
 
231
                f.writeBlock(" bytes ", 7);
 
232
                KviStr szNBytes(KviStr::Format, "%d", a->szBuffer.len());
 
233
                f.writeBlock(szNBytes.ptr(), szNBytes.len());
 
234
                f.writeBlock("]\n", 2);
 
235
                f.writeBlock(a->szBuffer.ptr(), a->szBuffer.len());
 
236
                f.writeBlock("\n[ENDALIAS]\n", 12);
218
237
        }
219
238
 
220
239
        f.close();
221
240
        return true;
222
241
}
 
242
 
 
243
QPtrList<KviAlias> *KviAliasManager::aliasList()
 
244
{
 
245
        return m_pAliasList;
 
246
}