1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-02 19:13:31 +02:00
VVVVVV/desktop_version/src/UtilityClass.cpp
Misa 769f99f590 Reduce dependency on libc functions
During 2.3 development, there's been a gradual shift to using SDL stdlib
functions instead of libc functions, but there are still some libc
functions (or the same libc function but from the STL) in the code.

Well, this patch replaces all the rest of them in one fell swoop.

SDL's stdlib can replace most of these, but its SDL_min() and SDL_max()
are inadequate - they aren't really functions, they're more like macros
with a nasty penchant for double-evaluation. So I just made my own
VVV_min() and VVV_max() functions and placed them in Maths.h instead,
then replaced all the previous usages of min(), max(), std::min(),
std::max(), SDL_min(), and SDL_max() with VVV_min() and VVV_max().

Additionally, there's no SDL_isxdigit(), so I just implemented my own
VVV_isxdigit().

SDL has SDL_malloc() and SDL_free(), but they have some refcounting
built in to them, so in order to use them with LodePNG, I have to
replace the malloc() and free() that LodePNG uses. Which isn't too hard,
I did it in a new file called ThirdPartyDeps.c, and LodePNG is now
compiled with the LODEPNG_NO_COMPILE_ALLOCATORS definition.

Lastly, I also refactored the awful strcpy() and strcat() usages in
PLATFORM_migrateSaveData() to use SDL_snprintf() instead. I know save
migration is getting axed in 2.4, but it still bothers me to have
something like that in the codebase otherwise.

Without further ado, here is the full list of functions that the
codebase now uses:

- SDL_strlcpy() instead of strcpy()
- SDL_strlcat() instead of strcat()
- SDL_snprintf() instead of sprintf(), strcpy(), or strcat() (see above)
- VVV_min() instead of min(), std::min(), or SDL_min()
- VVV_max() instead of max(), std::max(), or SDL_max()
- VVV_isxdigit() instead of isxdigit()
- SDL_strcmp() instead of strcmp()
- SDL_strcasecmp() instead of strcasecmp() or Win32 strcmpi()
- SDL_strstr() instead of strstr()
- SDL_strlen() instead of strlen()
- SDL_sscanf() instead of sscanf()
- SDL_getenv() instead of getenv()
- SDL_malloc() instead of malloc() (replacing in LodePNG as well)
- SDL_free() instead of free() (replacing in LodePNG as well)
2021-01-12 14:02:31 -05:00

272 lines
4.8 KiB
C++

#define HELP_DEFINITION
#include "UtilityClass.h"
#include <cctype>
#include <SDL.h>
#include <sstream>
/* Used by UtilityClass::GCString to generate a button list */
static const char *GCChar(SDL_GameControllerButton button)
{
if (button == SDL_CONTROLLER_BUTTON_A)
{
return "A";
}
else if (button == SDL_CONTROLLER_BUTTON_B)
{
return "B";
}
else if (button == SDL_CONTROLLER_BUTTON_X)
{
return "X";
}
else if (button == SDL_CONTROLLER_BUTTON_Y)
{
return "Y";
}
else if (button == SDL_CONTROLLER_BUTTON_BACK)
{
return "BACK";
}
else if (button == SDL_CONTROLLER_BUTTON_GUIDE)
{
return "GUIDE";
}
else if (button == SDL_CONTROLLER_BUTTON_START)
{
return "START";
}
else if (button == SDL_CONTROLLER_BUTTON_LEFTSTICK)
{
return "L3";
}
else if (button == SDL_CONTROLLER_BUTTON_RIGHTSTICK)
{
return "R3";
}
else if (button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER)
{
return "LB";
}
else if (button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)
{
return "RB";
}
SDL_assert(0 && "Unhandled button!");
return NULL;
}
int ss_toi( std::string _s )
{
std::istringstream i(_s);
int x = 0;
i >> x;
return x;
}
std::vector<std::string> split( const std::string &s, char delim, std::vector<std::string> &elems )
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split( const std::string &s, char delim )
{
std::vector<std::string> elems;
return split(s, delim, elems);
}
UtilityClass::UtilityClass() :
glow(0),
glowdir(0)
{
for (size_t i = 0; i < SDL_arraysize(splitseconds); i++)
{
splitseconds[i] = (i * 100) / 30;
}
slowsine = 0;
}
std::string UtilityClass::String( int _v )
{
std::ostringstream os;
os << _v;
return(os.str());
}
int UtilityClass::Int(const char* str, int fallback /*= 0*/)
{
if (!is_number(str))
{
return fallback;
}
return (int) SDL_strtol(str, NULL, 0);
}
std::string UtilityClass::GCString(std::vector<SDL_GameControllerButton> buttons)
{
std::string retval = "";
for (size_t i = 0; i < buttons.size(); i += 1)
{
retval += GCChar(buttons[i]);
if ((i + 1) < buttons.size())
{
retval += ",";
}
}
return retval;
}
std::string UtilityClass::twodigits( int t )
{
if (t < 10)
{
return "0" + String(t);
}
if (t >= 100)
{
return "??";
}
return String(t);
}
std::string UtilityClass::timestring( int t )
{
//given a time t in frames, return a time in seconds
std::string tempstring = "";
int temp = (t - (t % 30)) / 30;
if (temp < 60) //less than one minute
{
t = t % 30;
tempstring = String(temp) + ":" + twodigits(splitseconds[t]);
}
else
{
int temp2 = (temp - (temp % 60)) / 60;
temp = temp % 60;
t = t % 30;
tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(splitseconds[t]);
}
return tempstring;
}
std::string UtilityClass::number( int _t )
{
static const std::string ones_place[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
static const std::string tens_place[] = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
static const std::string teens[] = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
if (_t < 0)
{
return "???";
}
else if (_t > 100)
{
return "Lots";
}
else if (_t == 0)
{
return "Zero";
}
else if (_t == 100)
{
return "One Hundred";
}
else if (_t >= 1 && _t <= 9)
{
return ones_place[_t-1];
}
else if (_t >= 11 && _t <= 19)
{
return teens[_t-11];
}
else if (_t % 10 == 0)
{
return tens_place[(_t/10)-1];
}
else
{
return tens_place[(_t/10)-1] + " " + ones_place[(_t%10)-1];
}
}
bool UtilityClass::intersects( SDL_Rect A, SDL_Rect B )
{
return (SDL_HasIntersection(&A, &B) == SDL_TRUE);
}
void UtilityClass::updateglow()
{
slowsine++;
if (slowsine >= 64) slowsine = 0;
if (glowdir == 0) {
glow+=2;
if (glow >= 62) glowdir = 1;
}else {
glow-=2;
if (glow < 2) glowdir = 0;
}
}
bool is_number(const char* str)
{
for (int i = 0; str[i] != '\0'; i++)
{
if (!SDL_isdigit(static_cast<unsigned char>(str[i])) && (i != 0 || str[0] != '-'))
{
return false;
}
}
return true;
}
static bool VVV_isxdigit(const unsigned char digit)
{
return (digit >= 'a' && digit <= 'z')
|| (digit >= 'A' && digit <= 'Z')
|| SDL_isdigit(digit);
}
bool is_positive_num(const std::string& str, bool hex)
{
for (size_t i = 0; i < str.length(); i++)
{
if (hex)
{
if (!VVV_isxdigit(static_cast<unsigned char>(str[i])))
{
return false;
}
}
else
{
if (!SDL_isdigit(static_cast<unsigned char>(str[i])))
{
return false;
}
}
}
return true;
}
bool endsWith(const std::string& str, const std::string& suffix)
{
if (str.size() < suffix.size())
{
return false;
}
return str.compare(
str.size() - suffix.size(),
suffix.size(),
suffix
) == 0;
}