1 //uchime by Robert C. Edgar http://drive5.com/uchime This code is donated to the public domain.
\r
9 #include <sys/types.h>
\r
19 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
21 #include <inttypes.h>
\r
24 using namespace std;
\r
26 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
28 //#include <crtdbg.h>
\r
29 #pragma warning(disable: 4996) // deprecated functions
\r
30 #define _CRT_SECURE_NO_DEPRECATE 1
\r
33 //#if defined(_DEBUG) && !defined(DEBUG)
\r
37 //#if defined(DEBUG) && !defined(_DEBUG)
\r
46 typedef unsigned char byte;
\r
47 typedef unsigned short uint16;
\r
48 typedef unsigned uint32;
\r
50 typedef double float32;
\r
51 typedef signed char int8;
\r
52 typedef unsigned char uint8;
\r
54 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
56 typedef long long int64;
\r
57 typedef unsigned long long uint64;
\r
59 #define INT64_PRINTF "lld"
\r
60 #define UINT64_PRINTF "llu"
\r
62 #define SIZE_T_PRINTF "u"
\r
63 #define OFF64_T_PRINTF "lld"
\r
65 #define INT64_PRINTFX "llx"
\r
66 #define UINT64_PRINTFX "llx"
\r
68 #define SIZE_T_PRINTFX "x"
\r
69 #define OFF64_T_PRINTFX "llx"
\r
72 #elif defined(__x86_64__)
\r
75 typedef unsigned long uint64;
\r
77 #define INT64_PRINTF "ld"
\r
78 #define UINT64_PRINTF "lu"
\r
80 #define SIZE_T_PRINTF "lu"
\r
81 #define OFF64_T_PRINTF "ld"
\r
83 #define INT64_PRINTFX "lx"
\r
84 #define UINT64_PRINTFX "lx"
\r
86 #define SIZE_T_PRINTFX "lx"
\r
87 #define OFF64_T_PRINTFX "lx"
\r
91 typedef __int64 int64;
\r
92 typedef unsigned __int64 uint64;
\r
94 #define INT64_PRINTF "lld"
\r
95 #define UINT64_PRINTF "llu"
\r
97 #define SIZE_T_PRINTF "u"
\r
98 #define OFF64_T_PRINTF "lld"
\r
100 #define INT64_PRINTFX "llx"
\r
101 #define UINT64_PRINTFX "llx"
\r
103 #define SIZE_T_PRINTFX "x"
\r
104 #define OFF64_T_PRINTFX "llx"
\r
108 #define d64 INT64_PRINTF
\r
109 #define u64 UINT64_PRINTF
\r
110 #define x64 UINT64_PRINTFX
\r
112 // const uint64 UINT64_MAX = (~((uint64) 0));
\r
114 void myassertfail(const char *Exp, const char *File, unsigned Line);
\r
117 #define assert(exp) ((void)0)
\r
118 #define myassert(exp) ((void)0)
\r
120 #define assert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
\r
121 #define myassert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
\r
123 #define asserta(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
\r
125 #define ureturn(x) return (x)
\r
127 #define NotUsed(v) ((void *) &v)
\r
129 // pom=plus or minus, tof=true or false
\r
130 static inline char pom(bool Plus) { return Plus ? '+' : '-'; }
\r
131 static inline char tof(bool x) { return x ? 'T' : 'F'; }
\r
132 static inline char yon(bool x) { return x ? 'Y' : 'N'; }
\r
133 unsigned GetElapsedSecs();
\r
137 void *rce_malloc(unsigned bytes, const char *FileName, int Line);
\r
138 void rce_free(void *p, const char *FileName, int LineNr);
\r
141 void rce_dumpmem_(const char *FileName, int LineNr);
\r
142 #define rce_dumpmem() rce_dumpmem_(__FILE__, __LINE__)
\r
144 void rce_assertvalidptr_(void *p, const char *FileName, int LineNr);
\r
145 #define rce_assertvalidptr(p) rce_assertvalidptr_(p, __FILE__, __LINE__)
\r
147 void rce_dumpptr_(void *p, const char *FileName, int LineNr);
\r
148 #define rce_dumpptr(p) rce_dumpptr_(p, __FILE__, __LINE__)
\r
150 #define mymalloc(n) rce_malloc((n), __FILE__, __LINE__)
\r
151 #define myfree(p) rce_free(p, __FILE__, __LINE__)
\r
152 #define myfree2(p,n) rce_free(p, __FILE__, __LINE__)
\r
153 #define myalloc(t, n) (t *) rce_malloc((n)*sizeof(t), __FILE__, __LINE__)
\r
155 #else // RCE_MALLOC
\r
156 void *mymalloc(unsigned bytes);
\r
157 void myfree2(void *p, unsigned Bytes);
\r
158 void myfree(void *p);
\r
159 #define rce_chkmem() /* empty */
\r
160 #define myalloc(t, n) (t *) mymalloc((n)*sizeof(t))
\r
161 #endif // RCE_MALLOC
\r
163 #define SIZE(c) unsigned((c).size())
\r
165 bool myisatty(int fd);
\r
167 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
169 #define off_t __int64
\r
172 FILE *OpenStdioFile(const string &FileName);
\r
173 FILE *CreateStdioFile(const string &FileName);
\r
174 bool CanSetStdioFilePos(FILE *f);
\r
175 void CloseStdioFile(FILE *f);
\r
176 void SetStdioFilePos(FILE *f, off_t Pos);
\r
177 void ReadStdioFile(FILE *f, off_t Pos, void *Buffer, unsigned Bytes);
\r
178 void ReadStdioFile(FILE *f, void *Buffer, unsigned Bytes);
\r
179 void WriteStdioFile(FILE *f, off_t Pos, const void *Buffer, unsigned Bytes);
\r
180 void WriteStdioFile(FILE *f, const void *Buffer, unsigned Bytes);
\r
181 bool ReadLineStdioFile(FILE *f, char *Line, unsigned Bytes);
\r
182 bool ReadLineStdioFile(FILE *f, string &Line);
\r
183 byte *ReadAllStdioFile(FILE *f, off_t &FileSize);
\r
184 byte *ReadAllStdioFile(const string &FileName, off_t &FileSize);
\r
185 void AppendStdioFileToFile(FILE *fFrom, FILE *fTo);
\r
186 void FlushStdioFile(FILE *f);
\r
187 bool StdioFileExists(const string &FileName);
\r
188 off_t GetStdioFilePos(FILE *f);
\r
189 off_t GetStdioFileSize(FILE *f);
\r
190 void LogStdioFileState(FILE *f);
\r
191 void RenameStdioFile(const string &FileNameFrom, const string &FileNameTo);
\r
192 void DeleteStdioFile(const string &FileName);
\r
194 void myvstrprintf(string &Str, const char *szFormat, va_list ArgList);
\r
195 void myvstrprintf(string &Str, const char *szFormat, ...);
\r
197 void SetLogFileName(const string &FileName);
\r
198 void Log(const char *szFormat, ...);
\r
200 void Die(const char *szFormat, ...);
\r
201 void Warning(const char *szFormat, ...);
\r
203 void ProgressStep(unsigned i, unsigned N, const char *Format, ...);
\r
204 void Progress(const char *szFormat, ...);
\r
205 void Progress(const string &Str);
\r
206 void ProgressLog(const char *szFormat, ...);
\r
207 void ProgressExit();
\r
209 char *mystrsave(const char *s);
\r
211 double GetPeakMemUseBytes();
\r
213 // Are two floats equal to within epsilon?
\r
214 const double epsilon = 0.01;
\r
215 inline bool feq(double x, double y, double epsilon)
\r
217 if (fabs(x) > 10000)
\r
218 epsilon = fabs(x)/10000;
\r
219 if (fabs(x - y) > epsilon)
\r
224 inline bool feq(double x, double y)
\r
226 if (x < -1e6 && y < -1e6)
\r
228 double e = epsilon;
\r
229 if (fabs(x) > 10000)
\r
231 if (fabs(x - y) > e)
\r
236 #define asserteq(x, y) assert(feq(x, y))
\r
237 #define assertaeq(x, y) asserta(feq(x, y))
\r
239 #define zero(a, n) memset(a, 0, n*sizeof(a[0]))
\r
242 unsigned randu32();
\r
243 void Split(const string &Str, vector<string> &Fields, char Sep = 0);
\r
244 double Pct(double x, double y);
\r
245 double GetMemUseBytes();
\r
246 const char *MemBytesToStr(double Bytes);
\r
247 const char *IntToStr(unsigned i);
\r
248 const char *FloatToStr(double d);
\r
249 const char *SecsToStr(double Secs);
\r
250 void Logu(unsigned u, unsigned w, unsigned prefixspaces = 2);
\r
251 void Logf(float x, unsigned w, unsigned prefixspaces = 2);
\r
252 const char *SecsToHHMMSS(int Secs);
\r
254 void MyCmdLine(int argc, char **argv);
\r
255 void CmdLineErr(const char *Format, ...);
\r
257 void GetCmdLine(string &s);
\r
259 #define FLAG_OPT(LongName) extern bool opt_##LongName; extern bool optset_##LongName;
\r
260 #define TOG_OPT(LongName, Default) extern bool opt_##LongName; extern bool optset_##LongName;
\r
261 #define INT_OPT(LongName, Default, Min, Max) extern int opt_##LongName; extern bool optset_##LongName;
\r
262 #define UNS_OPT(LongName, Default, Min, Max) extern unsigned opt_##LongName; extern bool optset_##LongName;
\r
263 #define FLT_OPT(LongName, Default, Min, Max) extern double opt_##LongName; extern bool optset_##LongName;
\r
264 #define STR_OPT(LongName, Default) extern string opt_##LongName; extern bool optset_##LongName;
\r
265 #define ENUM_OPT(LongName, Default, Values) extern int opt_##LongName; extern bool optset_##LongName;
\r
266 #include "myopts.h"
\r
275 extern const char *SVN_VERSION;
\r
276 extern const char *SVN_MODS;
\r
277 extern bool opt_quiet;
\r
278 extern bool opt_version;
\r
279 extern FILE *g_fLog;
\r
281 #endif // myutils_h
\r