view interps/sadol/pch.hpp @ 12518:2d8fe55c6e65 draft default tip

<int-e> learn The password of the month is release incident pilot.
author HackEso <hackeso@esolangs.org>
date Sun, 03 Nov 2024 00:31:02 +0000
parents 859f9b4339e6
children
line wrap: on
line source

/*
 * BDSM2
 * Author: Adam Sawicki
 * http://www.regedit.risp.pl
 * mailto:regedit@risp.pl
 */
/* Precompiled header */
#ifndef PCH_H_
#define PCH_H_

#include <cstdio>
#include <cmath>
#include <cstdlib> // for exit, atoi, atof etc.
#include <string>
#include <vector>
#include <algorithm> // for swap

using std::string;

// 'function': was declared deprecated
#pragma warning(disable: 4996)

const size_t BUF_SIZE = 1024;

enum ERR_PLACE {
	ERR_GENERAL = 1,
	ERR_PARSE = 2,
	ERR_OPTIMIZE = 3,
	ERR_EXECUTE = 4,
	ERR_IO = 5,
};

inline void ReverseString(string *s)
{
	size_t i = 0;
	size_t j = s->length()-1;
	while (i < j) std::swap( (*s)[i++], (*s)[j--] );
}

// convert number to digit in any base ('0'..'9', 'A'..'Z', 'a'..'z')
template <typename T>
char DigitToChar(T Digit, bool UpperCase = true)
{
	if (Digit < 10) return static_cast<char>(Digit+'0');
	else {
		if (UpperCase) return static_cast<char>(Digit-10+'A');
		else           return static_cast<char>(Digit-10+'a');
	}
}

// Convert unsigned number to string
template <typename T>
void UintToStr(T Number, string *str, int Base = 10, bool UpperCase = true)
{
	T BaseT = static_cast<T>(Base);
	str->clear();
	char ch;
	while (Number != 0) {
		ch = DigitToChar<T>(Number % BaseT, UpperCase);
		Number /= BaseT;
		*str += ch;
	}
	if (str->empty()) *str = '0';
	else ReverseString(str);
}

// convert signed number to string
template <typename T>
void IntToStr(T Number, string *str, unsigned Base = 10, bool UpperCase = true)
{
	T BaseT = static_cast<T>(Base);
	int Sign = ( Number >= T() ? +1 : -1 );
	if (Sign == -1) Number = -Number;
	str->clear();
	char ch;
	while (Number != 0) {
		ch = DigitToChar<T>(Number % BaseT, UpperCase);
		Number /= BaseT;
		*str += ch;
	}
	if (str->empty()) *str = '0';
	if (Sign < 0) *str += '-';
	ReverseString(str);
}

// Convert unsigned number to string, return it
inline string IntToStr(unsigned x, int base = 10, bool UpperCase = true)
{
	string r;
	UintToStr(x, &r, base, UpperCase);
	return r;
}

// Convert signed number to string, return it
inline string IntToStr(int x, int base = 10, bool UpperCase = true)
{
	string r;
	IntToStr(x, &r, base, UpperCase);
	return r;
}

// Convert size to string
inline void Size_tToStr(size_t x, string *str, int base = 10, bool UpperCase = true)
{
	typedef unsigned int DestType;
	UintToStr<DestType>(static_cast<DestType>(x), str, base, UpperCase);
}

/// Convert size to string, return it
inline string Size_tToStr(size_t x, int base = 10)
{
	string r;
	Size_tToStr(x, &r, base);
	return r;
}

// Convert double precision floating point number to string
// Mode:
// - 'e' : -0.12345e-001
// - 'E' : -0.12345E-001
// - 'f' : -0.12345
// - 'g' : optimum ('e', 'f')
// - 'G' : optimum ('E', 'f')
inline string DoubleToStr(double x, char mode = 'f', int precision = 6)
{
	char sz[256];
	sprintf(sz, string("%." + IntToStr(precision) + mode).c_str(), x);
	return string(sz);
}

inline string CharToStr(char x)
{
	return string() + x;
}

inline double CharToDouble(char ch)
{
	return (double)(unsigned int)(unsigned char)ch;
}

// Round a number instead of truncating it
inline int Round(double x)
{
	return static_cast<int>(floor(x+0.5));
}

// Print error message and terminate process
void Error(const string &Msg, ERR_PLACE Place = ERR_GENERAL);

#endif