Next version of JABA
[jabaws.git] / binaries / src / muscle / globals.cpp
1 #if     WIN32\r
2 #include <windows.h>\r
3 #include <share.h>\r
4 #endif\r
5 \r
6 #include "muscle.h"\r
7 #include <stdio.h>\r
8 #include <stdlib.h>\r
9 #include <stdarg.h>\r
10 #include <string.h>\r
11 #include <math.h>\r
12 #include <assert.h>\r
13 #include <time.h>\r
14 #include <errno.h>\r
15 \r
16 #ifndef MAX_PATH\r
17 #define MAX_PATH        260\r
18 #endif\r
19 \r
20 static char g_strListFileName[MAX_PATH];\r
21 static bool g_bListFileAppend = false;\r
22 \r
23 static SEQWEIGHT g_SeqWeight = SEQWEIGHT_Undefined;\r
24 \r
25 void SetSeqWeightMethod(SEQWEIGHT Method)\r
26         {\r
27         g_SeqWeight = Method;\r
28         }\r
29 \r
30 SEQWEIGHT GetSeqWeightMethod()\r
31         {\r
32         return g_SeqWeight;\r
33         }\r
34 \r
35 void SetListFileName(const char *ptrListFileName, bool bAppend)\r
36         {\r
37         assert(strlen(ptrListFileName) < MAX_PATH);\r
38         strcpy(g_strListFileName, ptrListFileName);\r
39         g_bListFileAppend = bAppend;\r
40         }\r
41 \r
42 void Log(const char szFormat[], ...)\r
43         {\r
44         if (0 == g_strListFileName[0])\r
45                 return;\r
46 \r
47         static FILE *f = NULL;\r
48         char *mode;\r
49         if (g_bListFileAppend)\r
50                 mode = "a";\r
51         else\r
52                 mode = "w";\r
53         if (NULL == f)\r
54                 f = _fsopen(g_strListFileName, mode, _SH_DENYNO);\r
55         if (NULL == f)\r
56                 {\r
57                 perror(g_strListFileName);\r
58                 exit(EXIT_NotStarted);\r
59                 }\r
60 \r
61         char szStr[4096];\r
62         va_list ArgList;\r
63         va_start(ArgList, szFormat);\r
64         vsprintf(szStr, szFormat, ArgList);\r
65         fprintf(f, "%s", szStr);\r
66         fflush(f);\r
67         }\r
68 \r
69 const char *GetTimeAsStr()\r
70         {\r
71         static char szStr[32];\r
72         time_t t;\r
73         time(&t);\r
74         struct tm *ptmCurrentTime = localtime(&t);\r
75         strcpy(szStr, asctime(ptmCurrentTime));\r
76         assert('\n' == szStr[24]);\r
77         szStr[24] = 0;\r
78         return szStr;\r
79         }\r
80 \r
81 // Exit immediately with error message, printf-style.\r
82 void Quit(const char szFormat[], ...)\r
83         {\r
84         va_list ArgList;\r
85         char szStr[4096];\r
86 \r
87         va_start(ArgList, szFormat);\r
88         vsprintf(szStr, szFormat, ArgList);\r
89 \r
90         fprintf(stderr, "\n*** ERROR ***  %s\n", szStr);\r
91 \r
92         Log("\n*** FATAL ERROR ***  ");\r
93         Log("%s\n", szStr);\r
94         Log("Stopped %s\n", GetTimeAsStr());\r
95 \r
96 #ifdef WIN32\r
97         if (IsDebuggerPresent())\r
98                 {\r
99                 int iBtn = MessageBox(NULL, szStr, "muscle", MB_ICONERROR | MB_OKCANCEL);\r
100                 if (IDCANCEL == iBtn)\r
101                         Break();\r
102                 }\r
103 #endif\r
104         exit(EXIT_FatalError);\r
105         }\r
106 \r
107 void Warning(const char szFormat[], ...)\r
108         {\r
109         va_list ArgList;\r
110         char szStr[4096];\r
111 \r
112         va_start(ArgList, szFormat);\r
113         vsprintf(szStr, szFormat, ArgList);\r
114 \r
115         fprintf(stderr, "\n*** WARNING *** %s\n", szStr);\r
116         Log("\n*** WARNING ***  %s\n", szStr);\r
117         }\r
118 \r
119 // Remove leading and trailing blanks from string\r
120 void TrimBlanks(char szStr[])\r
121         {\r
122         TrimLeadingBlanks(szStr);\r
123         TrimTrailingBlanks(szStr);\r
124         }\r
125 \r
126 void TrimLeadingBlanks(char szStr[])\r
127         {\r
128         size_t n = strlen(szStr);\r
129         while (szStr[0] == ' ')\r
130                 {\r
131                 memmove(szStr, szStr+1, n);\r
132                 szStr[--n] = 0;\r
133                 }\r
134         }\r
135 \r
136 void TrimTrailingBlanks(char szStr[])\r
137         {\r
138         size_t n = strlen(szStr);\r
139         while (n > 0 && szStr[n-1] == ' ')\r
140                 szStr[--n] = 0;\r
141         }\r
142 \r
143 bool Verbose()\r
144         {\r
145         return true;\r
146         }\r
147 \r
148 SCORE StrToScore(const char *pszStr)\r
149         {\r
150         return (SCORE) atof(pszStr);\r
151         }\r
152 \r
153 void StripWhitespace(char szStr[])\r
154         {\r
155         unsigned uOutPos = 0;\r
156         unsigned uInPos = 0;\r
157         while (char c = szStr[uInPos++])\r
158                 if (' ' != c && '\t' != c && '\n' != c && '\r' != c)\r
159                         szStr[uOutPos++] = c;\r
160         szStr[uOutPos] = 0;\r
161         }\r
162 \r
163 void StripGaps(char szStr[])\r
164         {\r
165         unsigned uOutPos = 0;\r
166         unsigned uInPos = 0;\r
167         while (char c = szStr[uInPos++])\r
168                 if ('-' != c)\r
169                         szStr[uOutPos++] = c;\r
170         szStr[uOutPos] = 0;\r
171         }\r
172 \r
173 bool IsValidSignedInteger(const char *Str)\r
174         {\r
175         if (0 == strlen(Str))\r
176                 return false;\r
177         if ('+' == *Str || '-' == *Str)\r
178                 ++Str;\r
179         while (char c = *Str++)\r
180                 if (!isdigit(c))\r
181                         return false;\r
182         return true;\r
183         }\r
184 \r
185 bool IsValidInteger(const char *Str)\r
186         {\r
187         if (0 == strlen(Str))\r
188                 return false;\r
189         while (char c = *Str++)\r
190                 if (!isdigit(c))\r
191                         return false;\r
192         return true;\r
193         }\r
194 \r
195 // Is c valid as first character in an identifier?\r
196 bool isidentf(char c)\r
197         {\r
198         return isalpha(c) || '_' == c;\r
199         }\r
200 \r
201 // Is c valid character in an identifier?\r
202 bool isident(char c)\r
203         {\r
204         return isalpha(c) || isdigit(c) || '_' == c;\r
205         }\r
206 \r
207 bool IsValidIdentifier(const char *Str)\r
208         {\r
209         if (!isidentf(Str[0]))\r
210                 return false;\r
211         while (char c = *Str++)\r
212                 if (!isident(c))\r
213                         return false;\r
214         return true;\r
215         }\r
216 \r
217 void SetLogFile()\r
218         {\r
219         const char *strFileName = ValueOpt("loga");\r
220         if (0 != strFileName)\r
221                 g_bListFileAppend = true;\r
222         else\r
223                 strFileName = ValueOpt("log");\r
224         if (0 == strFileName)\r
225                 return;\r
226         strcpy(g_strListFileName, strFileName);\r
227         }\r
228 \r
229 // Get filename, stripping any extension and directory parts.\r
230 void NameFromPath(const char szPath[], char szName[], unsigned uBytes)\r
231         {\r
232         if (0 == uBytes)\r
233                 return;\r
234         const char *pstrLastSlash = strrchr(szPath, '/');\r
235         const char *pstrLastBackslash = strrchr(szPath, '\\');\r
236         const char *pstrLastDot = strrchr(szPath, '.');\r
237         const char *pstrLastSep = pstrLastSlash > pstrLastBackslash ?\r
238           pstrLastSlash : pstrLastBackslash;\r
239         const char *pstrBegin = pstrLastSep ? pstrLastSep + 1 : szPath;\r
240         const char *pstrEnd = pstrLastDot ? pstrLastDot - 1 : szPath + strlen(szPath);\r
241         unsigned uNameLength = (unsigned) (pstrEnd - pstrBegin + 1);\r
242         if (uNameLength > uBytes - 1)\r
243                 uNameLength = uBytes - 1;\r
244         memcpy(szName, pstrBegin, uNameLength);\r
245         szName[uNameLength] = 0;\r
246         }\r
247 \r
248 char *strsave(const char *s)\r
249         {\r
250         char *ptrCopy = strdup(s);\r
251         if (0 == ptrCopy)\r
252                 Quit("Out of memory");\r
253         return ptrCopy;\r
254         }\r
255 \r
256 bool IsValidFloatChar(char c)\r
257         {\r
258         return isdigit(c) || '.' == c || 'e' == c || 'E' == c || 'd' == c ||\r
259           'D' == c || '.' == c || '+' == c || '-' == c;\r
260         }\r
261 \r
262 void Call_MY_ASSERT(const char *file, int line, bool b, const char *msg)\r
263         {\r
264         if (b)\r
265                 return;\r
266         Quit("%s(%d): MY_ASSERT(%s)", file, line, msg);\r
267         }\r
268 \r
269 static size_t g_MemTotal;\r
270 \r
271 void MemPlus(size_t Bytes, char *Where)\r
272         {\r
273         g_MemTotal += Bytes;\r
274         Log("+%10u  %6u  %6u  %s\n",\r
275           (unsigned) Bytes,\r
276           (unsigned) GetMemUseMB(),\r
277           (unsigned) (g_MemTotal/1000000),\r
278           Where);\r
279         }\r
280 \r
281 void MemMinus(size_t Bytes, char *Where)\r
282         {\r
283         g_MemTotal -= Bytes;\r
284         Log("-%10u  %6u  %6u  %s\n",\r
285           (unsigned) Bytes,\r
286           (unsigned) GetMemUseMB(),\r
287           (unsigned) (g_MemTotal/1000000),\r
288           Where);\r
289         }\r