Add project files.

This commit is contained in:
2018-06-25 21:48:45 -04:00
parent b04a25689b
commit 3c1b7d28e8
425 changed files with 35333 additions and 0 deletions
@@ -0,0 +1,37 @@
#include "msunix.hpp"
int vasprintf(char ** ret, const char * format, va_list ap)
{
int len;
char *buffer;
len = _vscprintf(format, ap) + 1;
buffer = (char *) malloc(len * sizeof(char));
if (!buffer) return 0;
vsprintf_s(buffer, len, format, ap);
*ret = buffer;
return len -1;
}
/*int snprintf(char * str, size_t size, const char * format, ...)
{
va_list args;
size_t len;
va_start(args, format);
len = _vscprintf(format, args) + 1;
if (len > size) len = size;
vsprintf_s(str, len, format, args);
return len - 1;
}*/
int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
@@ -0,0 +1,14 @@
#ifndef __MSUNIX__
#define __MSUNIX__
#define _MSUNIX_VERSION "1.0.0"
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
int vasprintf(char ** ret, const char * format, va_list ap);
//int snprintf(char * str, size_t size, const char * format, ...);
int setenv(const char *name, const char *value, int overwrite);
#endif // __MSUNIX__
@@ -0,0 +1,423 @@
#include "cString.hpp"
/*** ANSI C Header Files ***/
#include <stdlib.h> /* strtol */
#ifdef _MSC_VER
// disable _s warnings
# define _CRT_SECURE_NO_WARNINGS
// disable pragma warnings
# pragma warning( disable : 4068 )
# pragma warning( disable : 4996 )
# include "MSUNIX/msunix.hpp"
#endif
using UtilityEngine::cString;
cString::cString()
{
clear();
}
cString::cString(const char* s)
{
copy_str(s);
}
cString::cString(const cString& copy)
{
copy_str(copy);
}
cString::~cString()
{
clear();
}
const char* cString::alloc_str(size_t sz)
{
if (mp_str)
clear();
m_len = (sz > __cString__MAX_LEN) ? __cString__MAX_LEN : sz;
mp_str = (char *)calloc(1, m_len + 1);
return mp_str;
}
void cString::clear()
{
if (mp_str)
free((void *)mp_str);
mp_str = nullptr;
m_len = 0;
}
const char* cString::c_str() const
{
return mp_str;
}
const char* cString::copy_str(const char* copy)
{
if (copy) {
size_t len = strnlen(copy, __cString__MAX_LEN);
alloc_str(len);
strncpy((char *)mp_str, copy, len);
m_len = len;
}
return mp_str;
}
bool cString::have_value() const
{
if (mp_str)
return true;
else
return false;
}
size_t cString::length() const
{
return m_len;
}
size_t cString::size() const
{
return m_len;
}
// string format
cString& cString::format(const char* format, ...)
{
char * buffer;
va_list args;
va_start(args, format);
vasprintf(&buffer, format, args);
copy_str(buffer);
free(buffer);
return *this;
}
// trim leading and trailing spaces
cString& cString::trim()
{
const static char * whitespace = "\x20\x1b\t\r\n\v\b\f\a";
if (!have_value())
return *this; // make sure we have a string
size_t begin = 0;
size_t end = length() - 1;
for (begin = 0; begin <= end; ++begin) {
if (strchr(whitespace, mp_str[begin]) == nullptr) {
break;
}
}
for (; end > begin; --end) {
if (strchr(whitespace, mp_str[end]) == nullptr) {
break;
}
else {
mp_str[end] = '\0';
}
}
if (begin) {
for (size_t i = 0; mp_str[i]; ++i) {
mp_str[i] = mp_str[begin++];
}
}
m_len = strlen(mp_str);
return *this;
}
cString cString::lower() const
{
cString rs = *this;
for (size_t i = 0; rs.mp_str[i]; ++i) {
rs.mp_str[i] = (char)tolower(rs.mp_str[i]);
}
return rs;
}
cString cString::upper() const
{
cString rs = *this;
for (size_t i = 0; rs.mp_str[i]; ++i) {
rs.mp_str[i] = (char)toupper(rs.mp_str[i]);
}
return rs;
}
const char& cString::first_char() const
{
return mp_str[0];
}
const char& cString::last_char() const
{
return mp_str[length() - 1];
}
// non-destructive split
const std::vector<cString> cString::split(const char match) const
{
const char match_s[2] = { match, 0 };
return split(match_s, -1);
}
const std::vector<cString> cString::split(const char* match) const
{
return split(match, -1);
}
const std::vector<cString> cString::split(const char* match, int max_split) const
{
std::vector<cString> rtn;
if (length() < 1)
return rtn;
size_t match_len = strnlen(match, __cString__MAX_LEN);
if (match_len >= __cString__MAX_LEN)
return rtn;
char* mi; // match index
char* pstr = mp_str; // string pointer
while ((mi = strstr(pstr, match)) && (max_split < 0 || --max_split)) {
if (mi != pstr) {
size_t lhsz = mi - pstr;
char* cslhs = (char *)malloc(lhsz + 1);
cslhs[lhsz] = '\0'; // strncpy doesn't terminate it
rtn.emplace_back(strncpy(cslhs, pstr, lhsz)); // calls cString copy ctor
pstr += lhsz;
free(cslhs);
}
pstr += match_len;
}
if (*pstr != '\0') {
rtn.emplace_back(pstr);
}
return rtn;
}
const cString& cString::char_repl(const char& match, const char& repl)
{
for (size_t i = 0; mp_str[i]; ++i) {
if (mp_str[i] == match) mp_str[i] = repl;
}
return *this;
}
long int cString::char_find(const char& match) const
{
for (size_t i = 0; mp_str[i]; ++i) {
if (mp_str[i] == match)
return i;
}
return -1;
}
cString cString::substr(size_t start, size_t length) const
{
cString rs;
char * buf;
if ((length + 1) > __cString__MAX_LEN)
return rs;
if ((start + length) > __cString__MAX_LEN)
return rs;
if (length > m_len - start)
return rs;
if (!mp_str)
return rs;
buf = (char *)calloc(sizeof(char), length + 1);
memcpy(buf, mp_str + start, length);
rs = buf;
return rs;
}
long int cString::find(const cString& match)
{
char * pos = strstr(mp_str, match.c_str());
if (pos)
return (long)(pos - mp_str);
else
return -1;
}
const cString cString::replace(const cString& match, const cString& repl)
{
cString rs;
long f1 = find(match);
if (f1 >= 0) {
size_t pos1 = (size_t)f1;
size_t pos2 = pos1 + match.length();
cString s1 = pos1 > 0 ? substr(0, pos1) : "";
cString s2 = substr(pos2, length() - pos2);
rs = s1 + repl + s2;
}
return rs;
}
const bool cString::IsInt() const
{
bool rtn = false;
cString tmp = c_str();
tmp.trim();
switch (tmp.first_char())
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
rtn = true;
break;
default:
rtn = false;
break;
}
return rtn;
}
const long int cString::ToInt() const
{
return strtol(mp_str, NULL, 10);
}
cString& cString::operator = (const char* rhs)
{
copy_str(rhs);
return *this;
}
cString& cString::operator = (const cString& rhs)
{
copy_str(rhs.c_str());
return *this;
}
cString& cString::operator += (const char rhs)
{
operator+=(&rhs);
return *this;
}
cString& cString::operator += (const char* rhs)
{
if (rhs) {
size_t newlen = m_len + strnlen(rhs, __cString__MAX_LEN);
if (newlen > __cString__MAX_LEN)
newlen = __cString__MAX_LEN;
char * buf = (char *)calloc(1, newlen + 1);
if (!buf)
return *this;
if (mp_str && m_len)
strncpy(buf, mp_str, m_len);
strncpy(buf + m_len, rhs, newlen - m_len);
buf[newlen] = '\0';
copy_str(buf);
free(buf);
}
return *this;
}
cString& cString::operator += (const cString& rhs)
{
operator+=(rhs.c_str());
return *this;
}
bool cString::operator == (const char* rhs) const
{
if (std::strncmp(this->c_str(), rhs, __cString__MAX_LEN) == 0)
return true;
else
return false;
}
bool cString::operator == (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) == 0)
return true;
else
return false;
}
bool cString::operator != (const char* rhs) const
{
if (std::strncmp(this->c_str(), rhs, __cString__MAX_LEN) != 0)
return true;
else
return false;
}
bool cString::operator != (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) != 0)
return true;
else
return false;
}
bool cString::operator > (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) > 0)
return true;
else
return false;
}
bool cString::operator < (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) < 0)
return true;
else
return false;
}
bool cString::operator >= (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) >= 0)
return true;
else
return false;
}
bool cString::operator <= (const cString& rhs) const
{
if (std::strncmp(this->c_str(), rhs.c_str(), __cString__MAX_LEN) <= 0)
return true;
else return false;
}
cString::operator const char* () const
{
return c_str();
}
cString::operator std::string () const
{
return std::string(c_str());
}
cString operator + (const cString& lhs, const cString& rhs)
{
cString rs = lhs;
rs += rhs;
return rs;
}
@@ -0,0 +1,85 @@
#ifndef _CSTRING_HPP_
#define _CSTRING_HPP_
/*** C++ STL Files ***/
#include <string>
#include <vector>
/*** SDL Header Files ***/
#include <SDL.h>
/*** DLL Header File ***/
#include "dllExport.h"
/*#pragma warning (disable : 4231)*/
EXPIMP_TEMPLATE template class EXPORT_FROM_MYDLL std::allocator<char>;
EXPIMP_TEMPLATE template class EXPORT_FROM_MYDLL std::basic_string<char>;
/*#pragma warning (default : 4231)*/
#define __cString__MAX_LEN 65535
namespace UtilityEngine {
class EXPORT_FROM_MYDLL cString
{
public:
cString(); // default constructor
cString(const char* s);
cString(const cString& copy); // copy constructor
~cString();
const char* alloc_str(size_t sz); // smart alloc string
void clear(); // frees the string
const char* c_str() const; // getter
const char* copy_str(const char* copy); // alloc & copy
// utility methods
bool have_value() const;
size_t length() const;
size_t size() const;
cString& format(const char* format, ...);
cString& trim();
cString lower() const;
cString upper() const;
const char& first_char() const;
const char& last_char() const;
const std::vector<cString> split(const char match) const;
const std::vector<cString> split(const char* match) const;
const std::vector<cString> split(const char* match, int max_split) const;
long int char_find(const char& match) const;
const cString& char_repl(const char& match, const char& repl);
cString substr(size_t start, size_t length) const;
long int find(const cString& match);
const cString replace(const cString& match, const cString& repl);
const bool IsInt() const;
const long int ToInt() const;
// operators
cString& operator = (const char* rhs); // assignment operator
cString& operator = (const cString& rhs); // assignment operator
cString& operator += (const char rhs);
cString& operator += (const char* rhs); // concatenation operator
cString& operator += (const cString& rhs); // concatenation operator
bool operator == (const char* rhs) const; // comparisons
bool operator == (const cString& rhs) const;
bool operator != (const char* rhs) const;
bool operator != (const cString& rhs) const;
bool operator > (const cString& rhs) const;
bool operator < (const cString& rhs) const;
bool operator >= (const cString& rhs) const;
bool operator <= (const cString& rhs) const;
// conversion operators
operator const char* () const; // c-string type
operator std::string () const; // c++ string class
private:
char* mp_str = nullptr;
size_t m_len = 0;
};/// END CLASS DEFINITION cString
// function overloads
}/// END NAMESPACE DEFINITION UtilityEngine
UtilityEngine::cString operator + (const UtilityEngine::cString& lhs, const UtilityEngine::cString& rhs);
#endif/// END IFNDEF _CSTRING_HPP_
@@ -0,0 +1,132 @@
#include "cUtility.hpp"
/*** SDL Header Files ***/
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_net.h>
using UtilityEngine::cUtility;
/*static*/ cUtility* cUtility::sp_inst = nullptr;
//private:
cUtility::cUtility()
{}
cUtility::~cUtility()
{}
//public:
// cUtility& cUtility::operator=( const cUtility& copy )
// {
// if (this != &copy)
// {
// }
//
// return *this;
// }
///Functions
/*static*/ cUtility& cUtility::Inst()
{
if (sp_inst == nullptr)
sp_inst = new cUtility();
return *sp_inst;
}
/*static*/ void cUtility::Delete()
{
delete sp_inst;
sp_inst = nullptr;
}
void cUtility::Message( const cString& msg, const cString& debugInfo /*= ""*/, const eTypeSDL typeSDL /*= eTypeSDL::None*/ ) const
{
cString sdlerror;
switch (typeSDL)
{
case NONE:
break;
case SDL:
sdlerror = SDL_GetError();
break;
case TTF:
sdlerror = TTF_GetError();
break;
case IMAGE:
sdlerror = IMG_GetError();
break;
case MIXER:
sdlerror = Mix_GetError();
break;
case NET:
sdlerror = SDLNet_GetError();
break;
case GFX:
sdlerror = SDL_GetError();
break;
default:
break;
}
SDL_ClearError();
if (debugInfo.size() > 0)
fprintf(stderr, "Error in %s %s %s \n", debugInfo.substr((1 + debugInfo.char_find('\\')), debugInfo.length()).c_str(), msg.c_str(), sdlerror.c_str());
else
fprintf(stderr, "%s\n", msg.c_str());
}
void cUtility::PrintVersion( const eTypeSDL typeSDL /*= eTypeSDL::ALL*/ ) const
{
SDL_version compiled_version;
SDL_version running_version;
cString type = "";
switch (typeSDL)
{
case ALL:
for ( int num = SDL; num <= NET; num++)
PrintVersion((eTypeSDL)num);
break;
case SDL:
SDL_VERSION(&compiled_version);
SDL_GetVersion(&running_version);
type = "SDL";
break;
case TTF:
SDL_TTF_VERSION(&compiled_version);
running_version = *TTF_Linked_Version();
type = "SDL_TTF";
break;
case IMAGE:
SDL_IMAGE_VERSION(&compiled_version);
running_version = *IMG_Linked_Version();
type = "SDL_IMAGE";
break;
case MIXER:
SDL_MIXER_VERSION(&compiled_version);
running_version = *Mix_Linked_Version();
type = "SDL_MIXER";
break;
case NET:
SDL_NET_VERSION(&compiled_version);
running_version = *SDLNet_Linked_Version();
type = "SDL_NET";
break;
default:
break;
}
if (typeSDL != ALL) {
VersionParser("Compiled", compiled_version, type);
VersionParser("Running", running_version, type);
}
}
///Private:
///Functions
void cUtility::VersionParser( const cString& runCompiled, const SDL_version& version, const cString& type ) const
{
fprintf(stderr, "%s with %s version: %u.%u.%u \n", runCompiled.c_str(), type.c_str(), version.major, version.minor, version.patch);
}
@@ -0,0 +1,67 @@
#ifndef _CUTILITY_HPP_
#define _CUTILITY_HPP_
/*** ANSI C Header Files ***/
#include <stdio.h> //for __FUNCTION__
#ifndef __FUNCTION__
# define __FUNCTION__ __func__
#endif
#define __STRINGIFY__(x) #x
#define __TOSTRING__(x) __STRINGIFY__(x)
#define __AT__ __FILE__ ":" __TOSTRING__(__LINE__) ":(" __FUNCTION__ ")"
/*** DLL Header File ***/
#include "dllExport.h"
/*** Custom Header Files ***/
#include "cString.hpp"
using UtilityEngine::cString;
namespace UtilityEngine {
/* Singleton */
class EXPORT_FROM_MYDLL cUtility
{
public:
enum eTypeSDL : unsigned int
{
NONE = 0,
ALL = 0,
SDL,
TTF,
IMAGE,
MIXER,
GFX,
NET,
};
private:
cUtility();
~cUtility();
public:
///Functions
static cUtility& Inst();
static void Delete();
void Message( const cString& msg, const cString& funcName = "", const eTypeSDL typeSDL = eTypeSDL::NONE ) const;
void PrintVersion( const eTypeSDL typeSDL = eTypeSDL::ALL ) const;
private:
///Functions
void VersionParser( const cString& runCompiled, const SDL_version& version, const cString& type ) const;
private:
///Variables
static cUtility* sp_inst;// = nullptr
};/// END CLASS DEFINITION cUtility
}/// END NAMESPACE DEFINITION UtilityEngine
#endif/// END IFNDEF _CUTILITY_HPP_