1 #ifndef myutils_h
2 #define myutils_h
3 
4 #define RCE_MALLOC	0
5 
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <string>
9 #include <string.h>
10 #include <memory.h>
11 #include <vector>
12 #include <math.h>
13 #include <stdarg.h>
14 #include <cstdlib>
15 #include <climits>
16 
17 #ifndef _MSC_VER
18 #include <inttypes.h>
19 #endif
20 
21 using namespace std;
22 
23 #ifdef _MSC_VER
24 #include <crtdbg.h>
25 #pragma warning(disable: 4996)	// deprecated functions
26 #define _CRT_SECURE_NO_DEPRECATE	1
27 #endif
28 
29 #if defined(_DEBUG) && !defined(DEBUG)
30 #define DEBUG	1
31 #endif
32 
33 #if defined(DEBUG) && !defined(_DEBUG)
34 #define _DEBUG	1
35 #endif
36 
37 #ifndef NDEBUG
38 #define	DEBUG	1
39 #define	_DEBUG	1
40 #endif
41 
42 typedef unsigned char byte;
43 typedef unsigned short uint16;
44 typedef unsigned uint32;
45 typedef int int32;
46 typedef double float32;
47 typedef signed char int8;
48 typedef unsigned char uint8;
49 
50 #ifdef _MSC_VER
51 
52 typedef __int64 int64;
53 typedef unsigned __int64 uint64;
54 
55 #define INT64_PRINTF		"lld"
56 #define UINT64_PRINTF		"llu"
57 
58 #define SIZE_T_PRINTF		"u"
59 #define OFF64_T_PRINTF		"lld"
60 
61 #define INT64_PRINTFX		"llx"
62 #define UINT64_PRINTFX		"llx"
63 
64 #define SIZE_T_PRINTFX		"x"
65 #define OFF64_T_PRINTFX		"llx"
66 
67 #elif defined(__x86_64__)
68 
69 typedef long int64;
70 typedef unsigned long uint64;
71 
72 #define INT64_PRINTF		"ld"
73 #define UINT64_PRINTF		"lu"
74 
75 #define SIZE_T_PRINTF		"lu"
76 #define OFF64_T_PRINTF		"ld"
77 
78 #define INT64_PRINTFX		"lx"
79 #define UINT64_PRINTFX		"lx"
80 
81 #define SIZE_T_PRINTFX		"lx"
82 #define OFF64_T_PRINTFX		"lx"
83 
84 #else
85 
86 typedef long long int64;
87 typedef unsigned long long uint64;
88 
89 #define INT64_PRINTF		"lld"
90 #define UINT64_PRINTF		"llu"
91 
92 #define SIZE_T_PRINTF		"u"
93 #define OFF64_T_PRINTF		"lld"
94 
95 #define INT64_PRINTFX		"llx"
96 #define UINT64_PRINTFX		"llx"
97 
98 #define SIZE_T_PRINTFX		"x"
99 #define OFF64_T_PRINTFX		"llx"
100 #endif
101 
102 #define d64		INT64_PRINTF
103 #define	u64		UINT64_PRINTF
104 #define	x64		UINT64_PRINTFX
105 
106 // const uint64 UINT64_MAX			= (~((uint64) 0));
107 
108 void myassertfail(const char *Exp, const char *File, unsigned Line);
109 #undef  assert
110 #ifdef  NDEBUG
111 #define assert(exp)     ((void)0)
112 #define myassert(exp)     ((void)0)
113 #else
114 #define assert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
115 #define myassert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
116 #endif
117 #define asserta(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
118 
119 #define ureturn(x)	return (x)
120 
121 #define NotUsed(v)	((void *) &v)
122 
123 // pom=plus or minus, tof=true or false
pom(bool Plus)124 static inline char pom(bool Plus)	{ return Plus ? '+' : '-'; }
tof(bool x)125 static inline char tof(bool x)		{ return x ? 'T' : 'F';	}
yon(bool x)126 static inline char yon(bool x)		{ return x ? 'Y' : 'N';	}
127 unsigned GetElapsedSecs();
128 
129 #if	RCE_MALLOC
130 
131 void *rce_malloc(unsigned bytes, const char *FileName, int Line);
132 void rce_free(void *p, const char *FileName, int LineNr);
133 void rce_chkmem();
134 
135 void rce_dumpmem_(const char *FileName, int LineNr);
136 #define rce_dumpmem()		rce_dumpmem_(__FILE__, __LINE__)
137 
138 void rce_assertvalidptr_(void *p, const char *FileName, int LineNr);
139 #define rce_assertvalidptr(p)	rce_assertvalidptr_(p, __FILE__, __LINE__)
140 
141 void rce_dumpptr_(void *p, const char *FileName, int LineNr);
142 #define rce_dumpptr(p)	rce_dumpptr_(p, __FILE__, __LINE__)
143 
144 #define mymalloc(n)		rce_malloc((n), __FILE__, __LINE__)
145 #define myfree(p)		rce_free(p, __FILE__, __LINE__)
146 #define myfree2(p,n)	rce_free(p, __FILE__, __LINE__)
147 #define myalloc(t, n)	(t *) rce_malloc((n)*sizeof(t), __FILE__, __LINE__)
148 
149 #else // RCE_MALLOC
150 void *mymalloc(unsigned bytes);
151 void myfree2(void *p, unsigned Bytes);
152 void myfree(void *p);
153 #define rce_chkmem()	/* empty */
154 #define myalloc(t, n)	(t *) mymalloc((n)*sizeof(t))
155 #endif // RCE_MALLOC
156 
157 #define SIZE(c)	unsigned((c).size())
158 
159 bool myisatty(int fd);
160 
161 #ifdef _MSC_VER
162 #define off_t	__int64
163 #endif
164 
165 FILE *OpenStdioFile(const string &FileName);
166 FILE *CreateStdioFile(const string &FileName);
167 bool CanSetStdioFilePos(FILE *f);
168 void CloseStdioFile(FILE *f);
169 void SetStdioFilePos(FILE *f, off_t Pos);
170 void ReadStdioFile(FILE *f, off_t Pos, void *Buffer, unsigned Bytes);
171 void ReadStdioFile(FILE *f, void *Buffer, unsigned Bytes);
172 void WriteStdioFile(FILE *f, off_t Pos, const void *Buffer, unsigned Bytes);
173 void WriteStdioFile(FILE *f, const void *Buffer, unsigned Bytes);
174 bool ReadLineStdioFile(FILE *f, char *Line, unsigned Bytes);
175 bool ReadLineStdioFile(FILE *f, string &Line);
176 byte *ReadAllStdioFile(FILE *f, off_t &FileSize);
177 byte *ReadAllStdioFile(const string &FileName, off_t &FileSize);
178 void AppendStdioFileToFile(FILE *fFrom, FILE *fTo);
179 void FlushStdioFile(FILE *f);
180 bool StdioFileExists(const string &FileName);
181 off_t GetStdioFilePos(FILE *f);
182 off_t GetStdioFileSize(FILE *f);
183 void LogStdioFileState(FILE *f);
184 void RenameStdioFile(const string &FileNameFrom, const string &FileNameTo);
185 void DeleteStdioFile(const string &FileName);
186 
187 void myvstrprintf(string &Str, const char *szFormat, va_list ArgList);
188 void myvstrprintf(string &Str, const char *szFormat, ...);
189 
190 void SetLogFileName(const string &FileName);
191 void Log(const char *szFormat, ...);
192 
193 void Die(const char *szFormat, ...);
194 void Warning(const char *szFormat, ...);
195 
196 void ProgressStep(unsigned i, unsigned N, const char *Format, ...);
197 void Progress(const char *szFormat, ...);
198 void Progress(const string &Str);
199 void ProgressLog(const char *szFormat, ...);
200 void ProgressExit();
201 
202 char *mystrsave(const char *s);
203 
204 double GetPeakMemUseBytes();
205 
206 // Are two floats equal to within epsilon?
207 const double epsilon = 0.01;
feq(double x,double y,double epsilon)208 inline bool feq(double x, double y, double epsilon)
209 	{
210 	if (fabs(x) > 10000)
211 		epsilon = fabs(x)/10000;
212 	if (fabs(x - y) > epsilon)
213 		return false;
214 	return true;
215 	}
216 
feq(double x,double y)217 inline bool feq(double x, double y)
218 	{
219 	if (x < -1e6 && y < -1e6)
220 		return true;
221 	double e = epsilon;
222 	if (fabs(x) > 10000)
223 		e = fabs(x)/10000;
224 	if (fabs(x - y) > e)
225 		return false;
226 	return true;
227 	}
228 
229 #define asserteq(x, y)	assert(feq(x, y))
230 #define assertaeq(x, y)	asserta(feq(x, y))
231 
232 #define	zero(a, n)	memset(a, 0, n*sizeof(a[0]))
233 
234 void InitRand();
235 unsigned randu32();
236 void Split(const string &Str, vector<string> &Fields, char Sep = 0);
237 double Pct(double x, double y);
238 double GetMemUseBytes();
239 const char *MemBytesToStr(double Bytes);
240 const char *IntToStr(unsigned i);
241 const char *FloatToStr(double d);
242 const char *SecsToStr(double Secs);
243 void Logu(unsigned u, unsigned w, unsigned prefixspaces = 2);
244 void Logf(float x, unsigned w, unsigned prefixspaces = 2);
245 const char *SecsToHHMMSS(int Secs);
246 
247 void MyCmdLine(int argc, char **argv);
248 void CmdLineErr(const char *Format, ...);
249 void Help();
250 void GetCmdLine(string &s);
251 
252 #define FLAG_OPT(LongName)						extern bool opt_##LongName; extern bool optset_##LongName;
253 #define TOG_OPT(LongName, Default)				extern bool opt_##LongName; extern bool optset_##LongName;
254 #define INT_OPT(LongName, Default, Min, Max)	extern int opt_##LongName; extern bool optset_##LongName;
255 #define UNS_OPT(LongName, Default, Min, Max)	extern unsigned opt_##LongName; extern bool optset_##LongName;
256 #define FLT_OPT(LongName, Default, Min, Max)	extern double opt_##LongName; extern bool optset_##LongName;
257 #define STR_OPT(LongName, Default)				extern string opt_##LongName; extern bool optset_##LongName;
258 #define ENUM_OPT(LongName, Default, Values)		extern int opt_##LongName; extern bool optset_##LongName;
259 #include "myopts.h"
260 #undef FLAG_OPT
261 #undef TOG_OPT
262 #undef INT_OPT
263 #undef UNS_OPT
264 #undef FLT_OPT
265 #undef STR_OPT
266 #undef ENUM_OPT
267 
268 extern const char *SVN_VERSION;
269 extern const char *SVN_MODS;
270 extern bool opt_quiet;
271 extern bool opt_version;
272 extern FILE *g_fLog;
273 
274 #endif	// myutils_h
275