summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/util/Misc.cpp
blob: cb2efe259db6f2568c3dbb1ed27100891229852b (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
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 *
 * Distributable under the terms of either the Apache License (Version 2.0) or 
 * the GNU Lesser General Public License, as specified in the COPYING file.
 *
 * Changes are Copyright(C) 2007, 2008 by Nokia Corporation and/or its subsidiary(-ies), all rights reserved.
*/
#include "CLucene/StdHeader.h"
#include "Misc.h"

#ifdef _CL_TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if defined(_CL_HAVE_SYS_TIME_H)
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#ifdef _CL_HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif

CL_NS_DEF(util)

uint64_t Misc::currentTimeMillis()
{
#if defined(_CLCOMPILER_MSVC) || defined(__MINGW32__) || defined(__BORLANDC__)
    struct _timeb tstruct;
    _ftime(&tstruct);

    return (((uint64_t) tstruct.time) * 1000) + tstruct.millitm;
#else
    struct timeval tstruct;
    if (gettimeofday(&tstruct, NULL) < 0) {
        _CLTHROWA(CL_ERR_Runtime,"Error in gettimeofday call.");
    }

    return (((uint64_t) tstruct.tv_sec) * 1000) + tstruct.tv_usec / 1000;
#endif
}

// #pragma mark -- char related utils

size_t Misc::ahashCode(const char* str)
{
	// Compute the hash code using a local variable to be reentrant.
	size_t hashCode = 0;
	while (*str != 0)
		hashCode = hashCode * 31 + *str++;
	return hashCode;
}

size_t Misc::ahashCode(const char* str, size_t len)
{
	// Compute the hash code using a local variable to be reentrant.
	size_t count = len;
    size_t hashCode = 0;
	for (size_t i = 0; i < count; i++)
		hashCode = hashCode * 31 + *str++;
	return hashCode;
}

char* Misc::ajoin(const char* a, const char* b, const char* c, const char* d,
    const char* e, const char* f)
{
#define aLEN(x) (x == NULL ? 0 : strlen(x))
    const size_t totalLen = aLEN(a) + aLEN(b) + aLEN(c) + aLEN(d) + aLEN(e)
        + aLEN(f) + sizeof(char); /* Space for terminator. */

    char* buf = _CL_NEWARRAY(char, totalLen);
    buf[0] = 0;
    if (a != NULL)
        strcat(buf, a);

    if (b != NULL)
        strcat(buf, b);

    if (c != NULL)
        strcat(buf, c);

    if (d != NULL)
        strcat(buf, d);

    if (e != NULL)
        strcat(buf, e);

    if (f != NULL)
        strcat(buf, f);

    return buf;
}

char* Misc::segmentname(const char* segment, const char* ext, int32_t x)
{
	CND_PRECONDITION(ext != NULL, "ext is NULL");

	char* buf = _CL_NEWARRAY(char, CL_MAX_PATH);
	if (x == -1)
		_snprintf(buf, CL_MAX_PATH, "%s%s", segment, ext);
	else
		_snprintf(buf, CL_MAX_PATH, "%s%s%d", segment, ext, x);
	return buf;
}

void Misc::segmentname(char* buffer, int32_t bufferLen, const char* segment,
    const char* ext, int32_t x)
{
	CND_PRECONDITION(buffer  != NULL, "buffer is NULL");
	CND_PRECONDITION(segment != NULL, "segment is NULL");
	CND_PRECONDITION(ext     != NULL, "extention is NULL");

	if (x == -1)
		_snprintf(buffer, bufferLen, "%s%s", segment, ext);
	else
		_snprintf(buffer, bufferLen, "%s%s%d", segment, ext, x);
}

// #pragma mark -- qt related utils

size_t Misc::qhashCode(const QString& str)
{
	size_t hashCode = 0;
	for (int i = 0; i < str.count(); ++i)
		hashCode = hashCode * 31 + str.at(i).unicode();
	return hashCode;
}

size_t Misc::qhashCode(const QString& str, size_t len)
{
    size_t count = len;
	size_t hashCode = 0;
	for (size_t i = 0; i < count; ++i)
		hashCode = hashCode * 31 + str.at(i).unicode();
	return hashCode;
}

QString Misc::qjoin(const QString &a, const QString &b, const QString &c,
    const QString &d, const QString &e, const QString &f)
{
    QString buffer;

    if (!a.isNull() && !a.isEmpty())
        buffer.append(a);

    if (!b.isNull() && !b.isEmpty())
        buffer.append(b);

    if (!c.isNull() && !c.isEmpty())
        buffer.append(c);

    if (!d.isNull() && !d.isEmpty())
        buffer.append(d);

    if (!e.isNull() && !e.isEmpty())
        buffer.append(e);

    if (!f.isNull() && !f.isEmpty())
        buffer.append(f);

    return buffer;
}

QString Misc::segmentname(const QString& segment, const QString& ext, int32_t x)
{
	CND_PRECONDITION(!ext.isEmpty(), "ext is NULL");
	
    if (x == -1)
        return QString(segment + ext);
	
    QString buf(QLatin1String("%1%2%3"));
	return buf.arg(segment).arg(ext).arg(x);
}

void Misc::segmentname(QString& buffer, int32_t bufferLen,
    const QString& segment, const QString& ext, int32_t x)
{
	CND_PRECONDITION(!segment.isEmpty(), "segment is NULL");
	CND_PRECONDITION(!ext.isEmpty(), "extention is NULL");

    buffer = segment + ext;
    if (x != -1)
        buffer += QString::number(x);
}

// #pragma mark -- TCHAR related utils

int32_t Misc::stringDifference(const TCHAR* s1, int32_t len1, const TCHAR* s2,
    int32_t len2)
{
	int32_t len = len1 < len2 ? len1 : len2;
	for (int32_t i = 0; i < len; i++)
		if (s1[i] != s2[i])
			return i;
	return len;
}

/* DSR:CL_BUG: (See comment for join method in Misc.h): */
TCHAR* Misc::join (const TCHAR* a, const TCHAR* b, const TCHAR* c,
                   const TCHAR* d, const TCHAR* e, const TCHAR* f)
{
#define LEN(x) (x == NULL ? 0 : _tcslen(x))
    const size_t totalLen = LEN(a) + LEN(b) + LEN(c) + LEN(d) + LEN(e) + LEN(f)
        + sizeof(TCHAR); /* Space for terminator. */

    TCHAR* buf = _CL_NEWARRAY(TCHAR, totalLen);
    buf[0] = 0;
    if (a != NULL)
        _tcscat(buf, a);

    if (b != NULL)
        _tcscat(buf, b);

    if (c != NULL)
        _tcscat(buf, c);
    
    if (d != NULL)
        _tcscat(buf, d);

    if (e != NULL)
        _tcscat(buf, e);

    if (f != NULL)
        _tcscat(buf, f);

    return buf;
}

#ifdef _UCS2

size_t Misc::whashCode(const wchar_t* str)
{
	// Compute the hash code using a local variable to be reentrant.
	size_t hashCode = 0;
	while (*str != 0)
		hashCode = hashCode * 31 + *str++;
	return hashCode;
}

size_t Misc::whashCode(const wchar_t* str, size_t len)
{
	// Compute the hash code using a local variable to be reentrant.
    size_t count = len;
	size_t hashCode = 0;
	for (size_t i = 0; i < count; i++)
		hashCode = hashCode * 31 + *str++;
	return hashCode;
}

char* Misc::_wideToChar(const wchar_t* s CL_FILELINEPARAM)
{
   size_t len = _tcslen(s);
   char* msg = _CL_NEWARRAY(char, len + 1);
   _cpywideToChar(s, msg, len + 1);
   return msg;
}

void Misc::_cpywideToChar(const wchar_t* s, char* d, size_t len)
{
    size_t sLen = wcslen(s);
    for (uint32_t i = 0; i < len && i < sLen + 1; i++)
        d[i] = LUCENE_OOR_CHAR(s[i]);
}

wchar_t* Misc::_charToWide(const char* s CL_FILELINEPARAM)
{
   size_t len = strlen(s);
   wchar_t* msg = _CL_NEWARRAY(wchar_t, len + 1);
   _cpycharToWide(s, msg, len + 1);
   return msg;
}

void Misc::_cpycharToWide(const char* s, wchar_t* d, size_t len)
{
    size_t sLen = strlen(s);
    for (uint32_t i = 0; i < len && i < sLen + 1; i++)
      d[i] = s[i];
}

#endif

CL_NS_END