Mercurial > repo
comparison interps/sadol/pch.hpp @ 996:859f9b4339e6
<Gregor> tar xf egobot.tar.xz
author | HackBot |
---|---|
date | Sun, 09 Dec 2012 19:30:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
995:6883f5911eb7 | 996:859f9b4339e6 |
---|---|
1 /* | |
2 * BDSM2 | |
3 * Author: Adam Sawicki | |
4 * http://www.regedit.risp.pl | |
5 * mailto:regedit@risp.pl | |
6 */ | |
7 /* Precompiled header */ | |
8 #ifndef PCH_H_ | |
9 #define PCH_H_ | |
10 | |
11 #include <cstdio> | |
12 #include <cmath> | |
13 #include <cstdlib> // for exit, atoi, atof etc. | |
14 #include <string> | |
15 #include <vector> | |
16 #include <algorithm> // for swap | |
17 | |
18 using std::string; | |
19 | |
20 // 'function': was declared deprecated | |
21 #pragma warning(disable: 4996) | |
22 | |
23 const size_t BUF_SIZE = 1024; | |
24 | |
25 enum ERR_PLACE { | |
26 ERR_GENERAL = 1, | |
27 ERR_PARSE = 2, | |
28 ERR_OPTIMIZE = 3, | |
29 ERR_EXECUTE = 4, | |
30 ERR_IO = 5, | |
31 }; | |
32 | |
33 inline void ReverseString(string *s) | |
34 { | |
35 size_t i = 0; | |
36 size_t j = s->length()-1; | |
37 while (i < j) std::swap( (*s)[i++], (*s)[j--] ); | |
38 } | |
39 | |
40 // convert number to digit in any base ('0'..'9', 'A'..'Z', 'a'..'z') | |
41 template <typename T> | |
42 char DigitToChar(T Digit, bool UpperCase = true) | |
43 { | |
44 if (Digit < 10) return static_cast<char>(Digit+'0'); | |
45 else { | |
46 if (UpperCase) return static_cast<char>(Digit-10+'A'); | |
47 else return static_cast<char>(Digit-10+'a'); | |
48 } | |
49 } | |
50 | |
51 // Convert unsigned number to string | |
52 template <typename T> | |
53 void UintToStr(T Number, string *str, int Base = 10, bool UpperCase = true) | |
54 { | |
55 T BaseT = static_cast<T>(Base); | |
56 str->clear(); | |
57 char ch; | |
58 while (Number != 0) { | |
59 ch = DigitToChar<T>(Number % BaseT, UpperCase); | |
60 Number /= BaseT; | |
61 *str += ch; | |
62 } | |
63 if (str->empty()) *str = '0'; | |
64 else ReverseString(str); | |
65 } | |
66 | |
67 // convert signed number to string | |
68 template <typename T> | |
69 void IntToStr(T Number, string *str, unsigned Base = 10, bool UpperCase = true) | |
70 { | |
71 T BaseT = static_cast<T>(Base); | |
72 int Sign = ( Number >= T() ? +1 : -1 ); | |
73 if (Sign == -1) Number = -Number; | |
74 str->clear(); | |
75 char ch; | |
76 while (Number != 0) { | |
77 ch = DigitToChar<T>(Number % BaseT, UpperCase); | |
78 Number /= BaseT; | |
79 *str += ch; | |
80 } | |
81 if (str->empty()) *str = '0'; | |
82 if (Sign < 0) *str += '-'; | |
83 ReverseString(str); | |
84 } | |
85 | |
86 // Convert unsigned number to string, return it | |
87 inline string IntToStr(unsigned x, int base = 10, bool UpperCase = true) | |
88 { | |
89 string r; | |
90 UintToStr(x, &r, base, UpperCase); | |
91 return r; | |
92 } | |
93 | |
94 // Convert signed number to string, return it | |
95 inline string IntToStr(int x, int base = 10, bool UpperCase = true) | |
96 { | |
97 string r; | |
98 IntToStr(x, &r, base, UpperCase); | |
99 return r; | |
100 } | |
101 | |
102 // Convert size to string | |
103 inline void Size_tToStr(size_t x, string *str, int base = 10, bool UpperCase = true) | |
104 { | |
105 typedef unsigned int DestType; | |
106 UintToStr<DestType>(static_cast<DestType>(x), str, base, UpperCase); | |
107 } | |
108 | |
109 /// Convert size to string, return it | |
110 inline string Size_tToStr(size_t x, int base = 10) | |
111 { | |
112 string r; | |
113 Size_tToStr(x, &r, base); | |
114 return r; | |
115 } | |
116 | |
117 // Convert double precision floating point number to string | |
118 // Mode: | |
119 // - 'e' : -0.12345e-001 | |
120 // - 'E' : -0.12345E-001 | |
121 // - 'f' : -0.12345 | |
122 // - 'g' : optimum ('e', 'f') | |
123 // - 'G' : optimum ('E', 'f') | |
124 inline string DoubleToStr(double x, char mode = 'f', int precision = 6) | |
125 { | |
126 char sz[256]; | |
127 sprintf(sz, string("%." + IntToStr(precision) + mode).c_str(), x); | |
128 return string(sz); | |
129 } | |
130 | |
131 inline string CharToStr(char x) | |
132 { | |
133 return string() + x; | |
134 } | |
135 | |
136 inline double CharToDouble(char ch) | |
137 { | |
138 return (double)(unsigned int)(unsigned char)ch; | |
139 } | |
140 | |
141 // Round a number instead of truncating it | |
142 inline int Round(double x) | |
143 { | |
144 return static_cast<int>(floor(x+0.5)); | |
145 } | |
146 | |
147 // Print error message and terminate process | |
148 void Error(const string &Msg, ERR_PLACE Place = ERR_GENERAL); | |
149 | |
150 #endif |