]> git.donarmstrong.com Git - mothur.git/blob - myutils.h
6374c8bee360033277aba81f73e836ef9a86cc13
[mothur.git] / myutils.h
1 //uchime by Robert C. Edgar http://drive5.com/uchime This code is donated to the public domain.\r
2 \r
3 #ifndef myutils_h\r
4 #define myutils_h\r
5 \r
6 #define RCE_MALLOC      0\r
7 \r
8 #include <stdio.h>\r
9 #include <sys/types.h>\r
10 #include <string>\r
11 #include <string.h>\r
12 #include <memory.h>\r
13 #include <vector>\r
14 #include <math.h>\r
15 #include <stdarg.h>\r
16 #include <cstdlib>\r
17 #include <climits>\r
18 \r
19 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
20 #else\r
21 #include <inttypes.h>\r
22 #endif\r
23 \r
24 using namespace std;\r
25 \r
26 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
27 #else\r
28 //#include <crtdbg.h>\r
29 #pragma warning(disable: 4996)  // deprecated functions\r
30 #define _CRT_SECURE_NO_DEPRECATE        1\r
31 #endif\r
32 \r
33 //#if defined(_DEBUG) && !defined(DEBUG)\r
34 #define DEBUG   1\r
35 //#endif\r
36 \r
37 //#if defined(DEBUG) && !defined(_DEBUG)\r
38 #define _DEBUG  1\r
39 //#endif\r
40 \r
41 //#ifndef NDEBUG\r
42 #define DEBUG   1\r
43 #define _DEBUG  1\r
44 //#endif\r
45 \r
46 typedef unsigned char byte;\r
47 typedef unsigned short uint16;\r
48 typedef unsigned uint32;\r
49 typedef int int32;\r
50 typedef double float32;\r
51 typedef signed char int8;\r
52 typedef unsigned char uint8;\r
53 \r
54 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
55 \r
56 typedef long long int64;\r
57 typedef unsigned long long uint64;\r
58 \r
59 #define INT64_PRINTF            "lld"\r
60 #define UINT64_PRINTF           "llu"\r
61 \r
62 #define SIZE_T_PRINTF           "u"\r
63 #define OFF64_T_PRINTF          "lld"\r
64 \r
65 #define INT64_PRINTFX           "llx"\r
66 #define UINT64_PRINTFX          "llx"\r
67 \r
68 #define SIZE_T_PRINTFX          "x"\r
69 #define OFF64_T_PRINTFX         "llx"\r
70 \r
71 \r
72 #elif defined(__x86_64__)\r
73 \r
74 typedef long int64;\r
75 typedef unsigned long uint64;\r
76 \r
77 #define INT64_PRINTF            "ld"\r
78 #define UINT64_PRINTF           "lu"\r
79 \r
80 #define SIZE_T_PRINTF           "lu"\r
81 #define OFF64_T_PRINTF          "ld"\r
82 \r
83 #define INT64_PRINTFX           "lx"\r
84 #define UINT64_PRINTFX          "lx"\r
85 \r
86 #define SIZE_T_PRINTFX          "lx"\r
87 #define OFF64_T_PRINTFX         "lx"\r
88 \r
89 #else\r
90 \r
91 typedef __int64 int64;\r
92 typedef unsigned __int64 uint64;\r
93 \r
94 #define INT64_PRINTF            "lld"\r
95 #define UINT64_PRINTF           "llu"\r
96 \r
97 #define SIZE_T_PRINTF           "u"\r
98 #define OFF64_T_PRINTF          "lld"\r
99 \r
100 #define INT64_PRINTFX           "llx"\r
101 #define UINT64_PRINTFX          "llx"\r
102 \r
103 #define SIZE_T_PRINTFX          "x"\r
104 #define OFF64_T_PRINTFX         "llx"\r
105 \r
106 #endif\r
107 \r
108 #define d64             INT64_PRINTF\r
109 #define u64             UINT64_PRINTF\r
110 #define x64             UINT64_PRINTFX\r
111 \r
112 // const uint64 UINT64_MAX                      = (~((uint64) 0));\r
113 \r
114 void myassertfail(const char *Exp, const char *File, unsigned Line);\r
115 #undef  assert\r
116 #ifdef  NDEBUG\r
117 #define assert(exp)     ((void)0)\r
118 #define myassert(exp)     ((void)0)\r
119 #else\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
122 #endif\r
123 #define asserta(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )\r
124 \r
125 #define ureturn(x)      return (x)\r
126 \r
127 #define NotUsed(v)      ((void *) &v)\r
128 \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
134 \r
135 #if     RCE_MALLOC\r
136 \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
139 void rce_chkmem();\r
140 \r
141 void rce_dumpmem_(const char *FileName, int LineNr);\r
142 #define rce_dumpmem()           rce_dumpmem_(__FILE__, __LINE__)\r
143 \r
144 void rce_assertvalidptr_(void *p, const char *FileName, int LineNr);\r
145 #define rce_assertvalidptr(p)   rce_assertvalidptr_(p, __FILE__, __LINE__)\r
146 \r
147 void rce_dumpptr_(void *p, const char *FileName, int LineNr);\r
148 #define rce_dumpptr(p)  rce_dumpptr_(p, __FILE__, __LINE__)\r
149 \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
154 \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
162 \r
163 #define SIZE(c) unsigned((c).size())\r
164 \r
165 bool myisatty(int fd);\r
166 \r
167 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
168 #else\r
169 #define off_t   __int64\r
170 #endif\r
171 \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
193 \r
194 void myvstrprintf(string &Str, const char *szFormat, va_list ArgList);\r
195 void myvstrprintf(string &Str, const char *szFormat, ...);\r
196 \r
197 void SetLogFileName(const string &FileName);\r
198 void Log(const char *szFormat, ...);\r
199 \r
200 void Die(const char *szFormat, ...);\r
201 void Warning(const char *szFormat, ...);\r
202 \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
208 \r
209 char *mystrsave(const char *s);\r
210 \r
211 double GetPeakMemUseBytes();\r
212 \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
216         {\r
217         if (fabs(x) > 10000)\r
218                 epsilon = fabs(x)/10000;\r
219         if (fabs(x - y) > epsilon)\r
220                 return false;\r
221         return true;\r
222         }\r
223 \r
224 inline bool feq(double x, double y)\r
225         {\r
226         if (x < -1e6 && y < -1e6)\r
227                 return true;\r
228         double e = epsilon;\r
229         if (fabs(x) > 10000)\r
230                 e = fabs(x)/10000;\r
231         if (fabs(x - y) > e)\r
232                 return false;\r
233         return true;\r
234         }\r
235 \r
236 #define asserteq(x, y)  assert(feq(x, y))\r
237 #define assertaeq(x, y) asserta(feq(x, y))\r
238 \r
239 #define zero(a, n)      memset(a, 0, n*sizeof(a[0]))\r
240 \r
241 void InitRand();\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
253 \r
254 void MyCmdLine(int argc, char **argv);\r
255 void CmdLineErr(const char *Format, ...);\r
256 void Help();\r
257 void GetCmdLine(string &s);\r
258 \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
267 #undef FLAG_OPT\r
268 #undef TOG_OPT\r
269 #undef INT_OPT\r
270 #undef UNS_OPT\r
271 #undef FLT_OPT\r
272 #undef STR_OPT\r
273 #undef ENUM_OPT\r
274 \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
280 \r
281 #endif  // myutils_h\r