-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.h
executable file
·155 lines (124 loc) · 3.35 KB
/
Utility.h
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
/*
* Utility.h - Useful functions
* General utility
* Wilcox Technologies
*
* Copyright (c) 2011 Wilcox Technologies. All rights reserved.
*/
#ifndef __WT_GENERAL_UTILITY_H_
#define __WT_GENERAL_UTILITY_H_
#include <stdio.h> // fprintf, stdout
#include <string.h> // strlen
#include <ctype.h> // isalnum, isspace
#include <stdlib.h> // calloc
#include <time.h>
#define alloc_error(name,len) { fprintf(stdout, "PANIC: couldn't allocate %ld bytes for %s\n", static_cast<long int>(len), name); abort(); }
#define fatal_error(errstr) { fprintf(stdout, "PANIC: %s\n", errstr); abort(); }
#define nonfatal_error(errstr) { fprintf(stdout, "ERROR: %s\n", errstr); }
#define warning_error(errstr) { fprintf(stdout, "WARNING: %s\n", errstr); }
#ifdef __GNUC__
# define UNUSED __attribute__((unused))
# define UNIMPLEMENTED fprintf(stderr, "WARNING: function %s is unimplemented! (file %s line %d)\n", __func__, __FILE__, __LINE__);
#else
# define UNUSED
# define UNIMPLEMENTED fprintf(stderr, "WARNING: function is unimplemented! (file %s line %d)\n", __FILE__, __LINE__);
#endif
#ifdef _WIN32
# define libAPI __declspec(dllexport)
# define strcasecmp _stricmp
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
#else
# define libAPI
#endif
#if defined(__MWERKS__) || defined(_AIX)
#include <stdarg.h>
static int asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
/* Initialise, like BSD */
*strp = NULL;
va_start(ap, fmt);
int len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if(len > -1)
{
*strp = static_cast<char *>(calloc(len + 1, sizeof(char)));
if(*strp == NULL)
alloc_error("asprintf buffer", len + 1);
va_start(ap, fmt);
len = vsnprintf(*strp, len + 1, fmt, ap);
va_end(ap);
if(len < 0)
{
free(*strp);
*strp = NULL;
return -1;
}
}
return len;
}
#endif
#if defined(_WIN32) || defined(__sun) || defined(__MWERKS__)
inline time_t timegm(struct tm *stupid_time)
{
time_t secs, gmt_secs;
time(&secs);
//local_secs = mktime(stupid_time);
stupid_time = gmtime(&secs);
gmt_secs = mktime(stupid_time);
return gmt_secs;
}
#endif
// originally from: http://www.codeproject.com/KB/string/urlencode.aspx
// Written horribly MFC-specific by rkrakowiak in 2001
// made portable to excess by awilcox in 2011
inline char toHex(const char &x)
{
return x > 9 ? x + 55: x + 48;
}
inline char *URLEncode(const char *sIn, const char *charsToIgnore = "\x5f\x2e\x2d")
{
char *sOut;
if(sIn == NULL) return NULL;
int k;
const int nLen = strlen(sIn) + 1;
register char *pOutTmp = NULL;
register const char *pInTmp = NULL;
//count not alphanumeric characters
k = 0;
pInTmp = sIn;
while(*pInTmp)
{
if (!isalnum(*pInTmp) && (!charsToIgnore || strchr(charsToIgnore, *pInTmp) == NULL))
k++;
pInTmp++;
}
//alloc out buffer
sOut = static_cast<char *> (calloc(nLen + (2 * k), sizeof(char)));
if(sOut)
{
pInTmp = sIn;
pOutTmp = sOut;
// do encoding
while (*pInTmp)
{
if(isalnum(*pInTmp) || (charsToIgnore && strchr(charsToIgnore, *pInTmp) != NULL))
*pOutTmp++ = *pInTmp;
else
{
*pOutTmp++ = '%';
*pOutTmp++ = toHex(*pInTmp>>4);
*pOutTmp++ = toHex(*pInTmp%16);
}
pInTmp++;
}
*pOutTmp = '\0';
}
return sOut;
}
#endif/*!__WT_GENERAL_UTILITY_H_*/