summaryrefslogtreecommitdiffstats
path: root/src/kvilib/ext/kvi_stringconversion.cpp
blob: f316fc2ab7eaef2a70661cd43af5f490cb13a2f1 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//=============================================================================
//
//   File : kvi_stringconversion.cpp
//   Creation date : Thu Oct 20 2000 14:12:21 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVILIB__


#define _KVI_STRINGCONVERSION_CPP_
#include "kvi_stringconversion.h"

#include "kvi_qstring.h"
#include <stdio.h>

TQString g_szGlobalDir;
TQString g_szLocalDir;

namespace KviStringConversion
{

	void init(const TQString& szGlobalDir,const TQString& szLocalDir)
	{
		g_szGlobalDir=szGlobalDir;
		g_szLocalDir=szLocalDir;
	}

	void encodePath(TQString& buffer)
	{
		if(!buffer.isEmpty())
		{
			if(!g_szLocalDir.isEmpty())
			{
				if(KviTQString::find(buffer,g_szLocalDir)==0)
				{
					buffer.remove(0,g_szLocalDir.length());
					buffer.prepend("local://");
				}
			}
			if(!g_szGlobalDir.isEmpty())
			{
				if(KviTQString::find(buffer,g_szGlobalDir)==0)
				{
					buffer.remove(0,g_szGlobalDir.length());
					buffer.prepend("global://");
				}
			}
		}
	}

	void decodePath(TQString& buffer)
	{
		if(!buffer.isEmpty())
		{
			if(!g_szLocalDir.isEmpty())
			{
				if(KviTQString::find(buffer,"local://")==0)
				{
					buffer.remove(0,8);
					buffer.prepend(g_szLocalDir);
				}
			}
			if(!g_szGlobalDir.isEmpty())
			{
				if(KviTQString::find(buffer,"global://")==0)
				{
					buffer.remove(0,9);
					buffer.prepend(g_szGlobalDir);
				}
			}
		}
	}
	
	void encodePath(TQStringList& buffer)
	{
		for ( TQStringList::Iterator it = buffer.begin(); it != buffer.end(); ++it )
		{
			encodePath(*it);
		}
	}

	void decodePath(TQStringList& buffer)
	{
		for ( TQStringList::Iterator it = buffer.begin(); it != buffer.end(); ++it )
		{
			decodePath(*it);
		}
	}

	void toString(const bool bValue,TQString &buffer)
	{
		buffer = bValue ? '1' : '0';
	}
	
	bool fromString(const TQString & szValue,bool &buffer)
	{
		if(szValue.isEmpty())buffer = false;
		else buffer = !((KviTQString::equalCS(szValue,"0")) || (KviTQString::equalCI(szValue,"false")));
		return true;
	}

	void toString(const int iValue,TQString &buffer)
	{
		buffer.setNum(iValue);
	}
	
	bool fromString(const TQString &szValue,int &buffer)
	{
		bool bOk;
		buffer = szValue.toInt(&bOk);
		return bOk;
	}
	
	void toString(const unsigned int uValue,TQString &buffer)
	{
		buffer.setNum(uValue);
	}
	
	bool fromString(const TQString & szValue,unsigned int &buffer)
	{
		bool bOk;
		buffer= szValue.toUInt(&bOk);
		return bOk;
	}
	
	void toString(const TQRect &rValue,TQString &buffer)
	{
		buffer.sprintf("%d,%d,%d,%d",rValue.x(),rValue.y(),rValue.width(),rValue.height());
	}
	
	bool fromString(const TQString & szValue,TQRect &buffer)
	{
		KviTQCString tmp = KviTQString::toUtf8(szValue);
		const char * c = tmp.data();
		if(!c)return false;
		int l,t,w,h;
		if(sscanf(c,"%d,%d,%d,%d",&l,&t,&w,&h) != 4)return false;
		buffer.setRect(l,t,w,h);
		return true;
	}
	
	void toString(const TQString &szValue,TQString &buffer)
	{
		buffer = szValue;
	}
	
	bool fromString(const TQString & szValue,TQString &buffer)
	{
		buffer = szValue;
		return true;
	}
	
	void toString(const KviPixmap &pValue,TQString &buffer)
	{
		buffer=pValue.path();
		encodePath(buffer);
	}
	
	bool fromString(const TQString & szValue,KviPixmap &buffer)
	{
		TQString szPath(szValue);
		decodePath(szPath);
		if(szPath.isEmpty()) {
			buffer.setNull();
			return true;
		} else {
			return buffer.load(szPath);
		}
	}
	
	void toString(const KviMsgType &mValue,TQString &buffer)
	{
		buffer.sprintf("%d,%u,%u,%d,%d",mValue.m_iPixId,mValue.m_cForeColor,mValue.m_cBackColor,mValue.m_bLogEnabled,mValue.m_iLevel);
	}
	
	bool fromString(const TQString & szValue,KviMsgType &buffer)
	{
		int iId,iLog,iLevel;
		unsigned int uFore,uBack;
		KviTQCString tmp = KviTQString::toUtf8(szValue);
		char * cx = tmp.data();
		if(!cx)return false;
		if(sscanf(cx,"%d,%u,%u,%d,%d",&iId,&uFore,&uBack,&iLog,&iLevel) != 5)return false;
		buffer = KviMsgType(buffer.m_szType,iId,uFore,uBack,iLog,iLevel);
		return true;
	}
	
	void toString(const TQColor &cValue,TQString &buffer)
	{
		buffer = cValue.name();
	}
	
	bool fromString(const TQString & szValue,TQColor &buffer)
	{
		buffer.setNamedColor(szValue); return true;
	}
	
	void toString(const TQFont &fValue,TQString &buffer)
	{
		TQString family(fValue.family());
		buffer.sprintf("%s,%d,%d,%d",KviTQString::toUtf8(family).data(),fValue.pointSize(),fValue.styleHint(),fValue.weight());
		TQString options;
		if(fValue.bold())options.append('b');
		if(fValue.italic())options.append('i');
		if(fValue.underline())options.append('u');
		if(fValue.strikeOut())options.append('s');
		if(fValue.fixedPitch())options.append('f');
	
		if(!options.isEmpty())
		{
			buffer.append(',');
			buffer.append(options);
		}
	}
	
	bool fromString(const TQString & szValue,TQFont &buffer)
	{
		KviStr str = szValue;
		KviStr family,pointSize,styleHint,weight,options;
		str.getToken(family,',');
		str.getToken(pointSize,',');
		str.getToken(styleHint,',');
		str.getToken(weight,','); 
		if(!family.isEmpty())buffer.setFamily(family.ptr());
		int i;
		bool bOk;
		i = pointSize.toInt(&bOk);
		if(bOk && (i > 0))buffer.setPointSize(i);
		i = styleHint.toInt(&bOk);
		if(bOk && (i >= 0))buffer.setStyleHint((TQFont::StyleHint)i);
		i = weight.toInt(&bOk);
		if(bOk && (i >= 0))buffer.setWeight(i);
		if(!str.isEmpty())
		{
			buffer.setBold(str.contains("b"));
			buffer.setItalic(str.contains("i"));
			buffer.setUnderline(str.contains("u"));
			buffer.setStrikeOut(str.contains("s"));
			buffer.setFixedPitch(str.contains("f"));
		}
		return true;
	}
	
	void toString(const TQStringList &sValue,TQString &buffer)
	{
		buffer = sValue.join(",");
	}
	
	bool fromString(const TQString & szValue,TQStringList &buffer)
	{
#ifdef COMPILE_USE_QT4
		buffer = szValue.split(",");
#else
		buffer = TQStringList::split(",",szValue);
#endif
		return true;
	}

}