17 #define MAX_PATH 260
\r
20 static char g_strListFileName[MAX_PATH];
\r
21 static bool g_bListFileAppend = false;
\r
23 static SEQWEIGHT g_SeqWeight = SEQWEIGHT_Undefined;
\r
25 void SetSeqWeightMethod(SEQWEIGHT Method)
\r
27 g_SeqWeight = Method;
\r
30 SEQWEIGHT GetSeqWeightMethod()
\r
35 void SetListFileName(const char *ptrListFileName, bool bAppend)
\r
37 assert(strlen(ptrListFileName) < MAX_PATH);
\r
38 strcpy(g_strListFileName, ptrListFileName);
\r
39 g_bListFileAppend = bAppend;
\r
42 void Log(const char szFormat[], ...)
\r
44 if (0 == g_strListFileName[0])
\r
47 static FILE *f = NULL;
\r
49 if (g_bListFileAppend)
\r
54 f = _fsopen(g_strListFileName, mode, _SH_DENYNO);
\r
57 perror(g_strListFileName);
\r
58 exit(EXIT_NotStarted);
\r
63 va_start(ArgList, szFormat);
\r
64 vsprintf(szStr, szFormat, ArgList);
\r
65 fprintf(f, "%s", szStr);
\r
69 const char *GetTimeAsStr()
\r
71 static char szStr[32];
\r
74 struct tm *ptmCurrentTime = localtime(&t);
\r
75 strcpy(szStr, asctime(ptmCurrentTime));
\r
76 assert('\n' == szStr[24]);
\r
81 // Exit immediately with error message, printf-style.
\r
82 void Quit(const char szFormat[], ...)
\r
87 va_start(ArgList, szFormat);
\r
88 vsprintf(szStr, szFormat, ArgList);
\r
90 fprintf(stderr, "\n*** ERROR *** %s\n", szStr);
\r
92 Log("\n*** FATAL ERROR *** ");
\r
94 Log("Stopped %s\n", GetTimeAsStr());
\r
97 if (IsDebuggerPresent())
\r
99 int iBtn = MessageBox(NULL, szStr, "muscle", MB_ICONERROR | MB_OKCANCEL);
\r
100 if (IDCANCEL == iBtn)
\r
104 exit(EXIT_FatalError);
\r
107 void Warning(const char szFormat[], ...)
\r
112 va_start(ArgList, szFormat);
\r
113 vsprintf(szStr, szFormat, ArgList);
\r
115 fprintf(stderr, "\n*** WARNING *** %s\n", szStr);
\r
116 Log("\n*** WARNING *** %s\n", szStr);
\r
119 // Remove leading and trailing blanks from string
\r
120 void TrimBlanks(char szStr[])
\r
122 TrimLeadingBlanks(szStr);
\r
123 TrimTrailingBlanks(szStr);
\r
126 void TrimLeadingBlanks(char szStr[])
\r
128 size_t n = strlen(szStr);
\r
129 while (szStr[0] == ' ')
\r
131 memmove(szStr, szStr+1, n);
\r
136 void TrimTrailingBlanks(char szStr[])
\r
138 size_t n = strlen(szStr);
\r
139 while (n > 0 && szStr[n-1] == ' ')
\r
148 SCORE StrToScore(const char *pszStr)
\r
150 return (SCORE) atof(pszStr);
\r
153 void StripWhitespace(char szStr[])
\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
163 void StripGaps(char szStr[])
\r
165 unsigned uOutPos = 0;
\r
166 unsigned uInPos = 0;
\r
167 while (char c = szStr[uInPos++])
\r
169 szStr[uOutPos++] = c;
\r
170 szStr[uOutPos] = 0;
\r
173 bool IsValidSignedInteger(const char *Str)
\r
175 if (0 == strlen(Str))
\r
177 if ('+' == *Str || '-' == *Str)
\r
179 while (char c = *Str++)
\r
185 bool IsValidInteger(const char *Str)
\r
187 if (0 == strlen(Str))
\r
189 while (char c = *Str++)
\r
195 // Is c valid as first character in an identifier?
\r
196 bool isidentf(char c)
\r
198 return isalpha(c) || '_' == c;
\r
201 // Is c valid character in an identifier?
\r
202 bool isident(char c)
\r
204 return isalpha(c) || isdigit(c) || '_' == c;
\r
207 bool IsValidIdentifier(const char *Str)
\r
209 if (!isidentf(Str[0]))
\r
211 while (char c = *Str++)
\r
219 const char *strFileName = ValueOpt("loga");
\r
220 if (0 != strFileName)
\r
221 g_bListFileAppend = true;
\r
223 strFileName = ValueOpt("log");
\r
224 if (0 == strFileName)
\r
226 strcpy(g_strListFileName, strFileName);
\r
229 // Get filename, stripping any extension and directory parts.
\r
230 void NameFromPath(const char szPath[], char szName[], unsigned uBytes)
\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
248 char *strsave(const char *s)
\r
250 char *ptrCopy = strdup(s);
\r
252 Quit("Out of memory");
\r
256 bool IsValidFloatChar(char c)
\r
258 return isdigit(c) || '.' == c || 'e' == c || 'E' == c || 'd' == c ||
\r
259 'D' == c || '.' == c || '+' == c || '-' == c;
\r
262 void Call_MY_ASSERT(const char *file, int line, bool b, const char *msg)
\r
266 Quit("%s(%d): MY_ASSERT(%s)", file, line, msg);
\r
269 static size_t g_MemTotal;
\r
271 void MemPlus(size_t Bytes, char *Where)
\r
273 g_MemTotal += Bytes;
\r
274 Log("+%10u %6u %6u %s\n",
\r
276 (unsigned) GetMemUseMB(),
\r
277 (unsigned) (g_MemTotal/1000000),
\r
281 void MemMinus(size_t Bytes, char *Where)
\r
283 g_MemTotal -= Bytes;
\r
284 Log("-%10u %6u %6u %s\n",
\r
286 (unsigned) GetMemUseMB(),
\r
287 (unsigned) (g_MemTotal/1000000),
\r