1/*
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 *
7 * Changes are Copyright(C) 2007, 2008 by Nokia Corporation and/or its subsidiary(-ies), all rights reserved.
8*/
9#include "CLucene/StdHeader.h"
10#include "Misc.h"
11
12#ifdef _CL_TIME_WITH_SYS_TIME
13# include <sys/time.h>
14# include <time.h>
15#else
16# if defined(_CL_HAVE_SYS_TIME_H)
17# include <sys/time.h>
18# else
19# include <time.h>
20# endif
21#endif
22
23#ifdef _CL_HAVE_SYS_TIMEB_H
24# include <sys/timeb.h>
25#endif
26
27#ifdef UNDER_CE
28#include <QTime>
29#endif
30
31CL_NS_DEF(util)
32
33uint64_t Misc::currentTimeMillis()
34{
35#ifndef UNDER_CE
36#if defined(_CLCOMPILER_MSVC) || defined(__MINGW32__) || defined(__BORLANDC__)
37 struct _timeb tstruct;
38 _ftime(&tstruct);
39
40 return (((uint64_t) tstruct.time) * 1000) + tstruct.millitm;
41#else
42 struct timeval tstruct;
43 if (gettimeofday(&tstruct, NULL) < 0) {
44 _CLTHROWA(CL_ERR_Runtime,"Error in gettimeofday call.");
45 }
46
47 return (((uint64_t) tstruct.tv_sec) * 1000) + tstruct.tv_usec / 1000;
48#endif
49#else //UNDER_CE
50 QT_USE_NAMESPACE
51 QTime t = QTime::currentTime();
52 return t.second() * 1000 + t.msec();
53#endif //UNDER_CE
54}
55
56// #pragma mark -- char related utils
57
58size_t Misc::ahashCode(const char* str)
59{
60 // Compute the hash code using a local variable to be reentrant.
61 size_t hashCode = 0;
62 while (*str != 0)
63 hashCode = hashCode * 31 + *str++;
64 return hashCode;
65}
66
67size_t Misc::ahashCode(const char* str, size_t len)
68{
69 // Compute the hash code using a local variable to be reentrant.
70 size_t count = len;
71 size_t hashCode = 0;
72 for (size_t i = 0; i < count; i++)
73 hashCode = hashCode * 31 + *str++;
74 return hashCode;
75}
76
77char* Misc::ajoin(const char* a, const char* b, const char* c, const char* d,
78 const char* e, const char* f)
79{
80#define aLEN(x) (x == NULL ? 0 : strlen(x))
81 const size_t totalLen = aLEN(a) + aLEN(b) + aLEN(c) + aLEN(d) + aLEN(e)
82 + aLEN(f) + sizeof(char); /* Space for terminator. */
83
84 char* buf = _CL_NEWARRAY(char, totalLen);
85 buf[0] = 0;
86 if (a != NULL)
87 strcat(buf, a);
88
89 if (b != NULL)
90 strcat(buf, b);
91
92 if (c != NULL)
93 strcat(buf, c);
94
95 if (d != NULL)
96 strcat(buf, d);
97
98 if (e != NULL)
99 strcat(buf, e);
100
101 if (f != NULL)
102 strcat(buf, f);
103
104 return buf;
105}
106
107char* Misc::segmentname(const char* segment, const char* ext, int32_t x)
108{
109 CND_PRECONDITION(ext != NULL, "ext is NULL");
110
111 char* buf = _CL_NEWARRAY(char, CL_MAX_PATH);
112 if (x == -1)
113 _snprintf(buf, CL_MAX_PATH, "%s%s", segment, ext);
114 else
115 _snprintf(buf, CL_MAX_PATH, "%s%s%d", segment, ext, x);
116 return buf;
117}
118
119void Misc::segmentname(char* buffer, int32_t bufferLen, const char* segment,
120 const char* ext, int32_t x)
121{
122 CND_PRECONDITION(buffer != NULL, "buffer is NULL");
123 CND_PRECONDITION(segment != NULL, "segment is NULL");
124 CND_PRECONDITION(ext != NULL, "extention is NULL");
125
126 if (x == -1)
127 _snprintf(buffer, bufferLen, "%s%s", segment, ext);
128 else
129 _snprintf(buffer, bufferLen, "%s%s%d", segment, ext, x);
130}
131
132// #pragma mark -- qt related utils
133
134size_t Misc::qhashCode(const QString& str)
135{
136 size_t hashCode = 0;
137 for (int i = 0; i < str.count(); ++i)
138 hashCode = hashCode * 31 + str.at(i).unicode();
139 return hashCode;
140}
141
142size_t Misc::qhashCode(const QString& str, size_t len)
143{
144 size_t count = len;
145 size_t hashCode = 0;
146 for (size_t i = 0; i < count; ++i)
147 hashCode = hashCode * 31 + str.at(i).unicode();
148 return hashCode;
149}
150
151QString Misc::qjoin(const QString &a, const QString &b, const QString &c,
152 const QString &d, const QString &e, const QString &f)
153{
154 QString buffer;
155
156 if (!a.isNull() && !a.isEmpty())
157 buffer.append(a);
158
159 if (!b.isNull() && !b.isEmpty())
160 buffer.append(b);
161
162 if (!c.isNull() && !c.isEmpty())
163 buffer.append(c);
164
165 if (!d.isNull() && !d.isEmpty())
166 buffer.append(d);
167
168 if (!e.isNull() && !e.isEmpty())
169 buffer.append(e);
170
171 if (!f.isNull() && !f.isEmpty())
172 buffer.append(f);
173
174 return buffer;
175}
176
177QString Misc::segmentname(const QString& segment, const QString& ext, int32_t x)
178{
179 CND_PRECONDITION(!ext.isEmpty(), "ext is NULL");
180
181 if (x == -1)
182 return QString(segment + ext);
183
184 QString buf(QLatin1String("%1%2%3"));
185 return buf.arg(segment).arg(ext).arg(x);
186}
187
188void Misc::segmentname(QString& buffer, int32_t bufferLen,
189 const QString& segment, const QString& ext, int32_t x)
190{
191 CND_PRECONDITION(!segment.isEmpty(), "segment is NULL");
192 CND_PRECONDITION(!ext.isEmpty(), "extention is NULL");
193
194 buffer = segment + ext;
195 if (x != -1)
196 buffer += QString::number(x);
197}
198
199// #pragma mark -- TCHAR related utils
200
201int32_t Misc::stringDifference(const TCHAR* s1, int32_t len1, const TCHAR* s2,
202 int32_t len2)
203{
204 int32_t len = len1 < len2 ? len1 : len2;
205 for (int32_t i = 0; i < len; i++)
206 if (s1[i] != s2[i])
207 return i;
208 return len;
209}
210
211/* DSR:CL_BUG: (See comment for join method in Misc.h): */
212TCHAR* Misc::join (const TCHAR* a, const TCHAR* b, const TCHAR* c,
213 const TCHAR* d, const TCHAR* e, const TCHAR* f)
214{
215#define LEN(x) (x == NULL ? 0 : _tcslen(x))
216 const size_t totalLen = LEN(a) + LEN(b) + LEN(c) + LEN(d) + LEN(e) + LEN(f)
217 + sizeof(TCHAR); /* Space for terminator. */
218
219 TCHAR* buf = _CL_NEWARRAY(TCHAR, totalLen);
220 buf[0] = 0;
221 if (a != NULL)
222 _tcscat(buf, a);
223
224 if (b != NULL)
225 _tcscat(buf, b);
226
227 if (c != NULL)
228 _tcscat(buf, c);
229
230 if (d != NULL)
231 _tcscat(buf, d);
232
233 if (e != NULL)
234 _tcscat(buf, e);
235
236 if (f != NULL)
237 _tcscat(buf, f);
238
239 return buf;
240}
241
242#ifdef _UCS2
243
244size_t Misc::whashCode(const wchar_t* str)
245{
246 // Compute the hash code using a local variable to be reentrant.
247 size_t hashCode = 0;
248 while (*str != 0)
249 hashCode = hashCode * 31 + *str++;
250 return hashCode;
251}
252
253size_t Misc::whashCode(const wchar_t* str, size_t len)
254{
255 // Compute the hash code using a local variable to be reentrant.
256 size_t count = len;
257 size_t hashCode = 0;
258 for (size_t i = 0; i < count; i++)
259 hashCode = hashCode * 31 + *str++;
260 return hashCode;
261}
262
263char* Misc::_wideToChar(const wchar_t* s CL_FILELINEPARAM)
264{
265 size_t len = _tcslen(s);
266 char* msg = _CL_NEWARRAY(char, len + 1);
267 _cpywideToChar(s, msg, len + 1);
268 return msg;
269}
270
271void Misc::_cpywideToChar(const wchar_t* s, char* d, size_t len)
272{
273 size_t sLen = wcslen(s);
274 for (uint32_t i = 0; i < len && i < sLen + 1; i++)
275 d[i] = LUCENE_OOR_CHAR(s[i]);
276}
277
278wchar_t* Misc::_charToWide(const char* s CL_FILELINEPARAM)
279{
280 size_t len = strlen(s);
281 wchar_t* msg = _CL_NEWARRAY(wchar_t, len + 1);
282 _cpycharToWide(s, msg, len + 1);
283 return msg;
284}
285
286void Misc::_cpycharToWide(const char* s, wchar_t* d, size_t len)
287{
288 size_t sLen = strlen(s);
289 for (uint32_t i = 0; i < len && i < sLen + 1; i++)
290 d[i] = s[i];
291}
292
293#endif
294
295CL_NS_END
296