FEMM/compat/afx.h

108 lines
4.4 KiB
C++

// portable umbrella for the MFC surface FEMM math files touch: BOOL/TRUE/FALSE, CString, _strnicmp, DeleteFile.
#pragma once
#include <string>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <strings.h>
#define __AFXWIN_H__
#ifndef BOOL
typedef int BOOL;
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
// MFC-flavored thin wrapper around std::string.
class CString {
std::string s;
public:
CString() = default;
CString(const char* p) : s(p ? p : "") {}
CString(const std::string& x) : s(x) {}
operator const char*() const { return s.c_str(); }
const char* c_str() const { return s.c_str(); }
const char* GetString() const { return s.c_str(); }
const std::string& Std() const { return s; }
std::string& Std() { return s; }
int GetLength() const { return (int)s.size(); }
bool IsEmpty() const { return s.empty(); }
void Empty() { s.clear(); }
char operator[](int i) const { return s[(size_t)i]; }
CString& operator=(const char* p) { s = p ? p : ""; return *this; }
CString& operator=(const std::string& x) { s = x; return *this; }
CString& operator+=(const CString& o) { s += o.s; return *this; }
CString& operator+=(const char* p) { s += p ? p : ""; return *this; }
CString& operator+=(char c) { s += c; return *this; }
CString operator+(const CString& o) const { CString r(*this); r += o; return r; }
CString operator+(const char* p) const { CString r(*this); r += p; return r; }
bool operator==(const CString& o) const { return s == o.s; }
bool operator==(const char* p) const { return s == (p ? p : ""); }
bool operator!=(const CString& o) const { return !(*this == o); }
bool operator!=(const char* p) const { return !(*this == p); }
bool operator<(const CString& o) const { return s < o.s; }
CString Mid(int start) const { return CString(s.substr((size_t)start)); }
CString Mid(int start, int n) const { return CString(s.substr((size_t)start, (size_t)n)); }
CString Left(int n) const { return CString(s.substr(0, (size_t)n)); }
CString Right(int n) const { int sz = (int)s.size(); return CString(s.substr((size_t)(sz > n ? sz - n : 0))); }
int Find(char c) const { auto p = s.find(c); return p == std::string::npos ? -1 : (int)p; }
int Find(const char* p) const { auto i = s.find(p ? p : ""); return i == std::string::npos ? -1 : (int)i; }
int ReverseFind(char c) const { auto p = s.rfind(c); return p == std::string::npos ? -1 : (int)p; }
void MakeLower() { for (auto& c : s) c = (char)std::tolower((unsigned char)c); }
void MakeUpper() { for (auto& c : s) c = (char)std::toupper((unsigned char)c); }
void TrimLeft() { while (!s.empty() && std::isspace((unsigned char)s.front())) s.erase(s.begin()); }
void TrimRight() { while (!s.empty() && std::isspace((unsigned char)s.back())) s.pop_back(); }
int Format(const char* fmt, ...) {
va_list ap; va_start(ap, fmt);
int n = vsnprintf(nullptr, 0, fmt, ap); va_end(ap);
if (n <= 0) { s.clear(); return 0; }
s.resize((size_t)n);
va_start(ap, fmt);
vsnprintf(&s[0], (size_t)n + 1, fmt, ap);
va_end(ap);
return n;
}
};
// concatenation of CString and a C string literal on either side.
inline CString operator+(const char* p, const CString& r) { return CString(p) + r; }
// Win32 stand-ins used by FEMM math files.
inline int DeleteFile(const char* path) { return std::remove(path) == 0; }
inline int _strnicmp(const char* a, const char* b, size_t n) { return strncasecmp(a, b, n); }
inline int _stricmp (const char* a, const char* b) { return strcasecmp(a, b); }
inline void Sleep(unsigned int /*ms*/) {}
// MSVC math-intrinsic macros.
#ifndef __min
#define __min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef __max
#define __max(a, b) ((a) > (b) ? (a) : (b))
#endif
// forward decls for MFC dialog/binding types femm/ headers reference.
class CDataExchange;
class CWnd;
class CDialog;
class CFile;
class CFileException;
class CArchive;
class CObject;
class CRuntimeClass;
#include "resource.h"