1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
// Hyperbolic Rogue -- HyperRogue streams
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
/** \file hprint.cpp
* \brief Routines related to displaying various things, and writing them to console and files. Nicer than the standard C++ streams.
*/
#include "hyper.h"
namespace hr {
EX FILE *debugfile;
#if HDR
#define DF_INIT 1 // always display these
#define DF_MSG 2 // always display these
#define DF_WARN 4 // always display these
#define DF_ERROR 8 // always display these
#define DF_STEAM 16
#define DF_GRAPH 32
#define DF_TURN 64
#define DF_FIELD 128
#define DF_GEOM 256
#define DF_MEMORY 512
#define DF_TIME 1024 // a flag to display timestamps
#define DF_GP 2048
#define DF_POLY 4096
#define DF_LOG 8192
#define DF_VERTEX 16384
#define DF_KEYS "imwesxufgbtoplv"
#endif
EX int debugflags = DF_INIT | DF_ERROR | DF_WARN | DF_MSG | DF_TIME | DF_LOG;
EX string s0;
EX string its(int i) { char buf[64]; sprintf(buf, "%d", i); return buf; }
EX string itsh8(int i) {static char buf[16]; sprintf(buf, "%08X", i); return buf; }
EX string fts(ld x, int prec IS(6)) {
std::stringstream ss;
ss.precision(prec);
ss << x;
return ss.str();
}
EX map<void*, int> pointer_indices;
EX string index_pointer(void *v) {
if(v == nullptr) return "0";
if(!pointer_indices.count(v)) {
int s = isize(pointer_indices);
pointer_indices[v] = s;
}
int i = pointer_indices[v];
string res;
while(true) { res += ('A' + (i % 26)); i /= 26; if(!i) break; i--; }
return res;
}
EX int stamplen = 6;
EX string get_stamp() {
if(stamplen == 0) return "";
int t = SDL_GetTicks();
int pow10 = 1;
for(int i=0; i<stamplen; i++) pow10 *= 10;
if(t < 0) t = pow10 - 1;
t %= pow10;
string s = its(t);
while(isize(s) < stamplen) s = "0" + s;
return s;
}
#if HDR
inline string ONOFF(bool b) { return b ? XLAT("ON") : XLAT("OFF"); }
struct hstream {
color_t vernum;
virtual void write_char(char c) = 0;
virtual void write_chars(const char* c, size_t q) { while(q--) write_char(*(c++)); }
virtual char read_char() = 0;
virtual void read_chars(char* c, size_t q) { while(q--) *(c++) = read_char(); }
virtual color_t get_vernum() { return vernum; }
virtual void flush() {}
hstream() { vernum = VERNUM_HEX; }
template<class T> void write(const T& t) { hwrite(*this, t); }
template<class T> void read(T& t) { hread(*this, t); }
template<class T> T get() { T t; hread(*this, t); return t; }
template<class T> T get_raw() { T t; hread_raw(*this, t); return t; }
};
template<class T> void hwrite_raw(hstream& hs, const T& c) { hs.write_chars((char*) &c, sizeof(T)); }
template<class T> void hread_raw(hstream& hs, T& c) { hs.read_chars((char*) &c, sizeof(T)); }
template<class T, typename = typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value>::type> void hwrite(hstream& hs, const T& c) { hwrite_raw(hs, c); }
template<class T, typename = typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value>::type> void hread(hstream& hs, T& c) { hread_raw(hs, c); }
inline void hwrite(hstream& hs, const string& s) {
if(isize(s) >= 255) {
hs.write_char((char)255);
hs.write<int>(isize(s));
}
else
hs.write_char(isize(s));
for(char c: s) hs.write_char(c);
}
inline void hread(hstream& hs, string& s) {
s = ""; int l = (unsigned char) hs.read_char();
if(l == 255) l = hs.get<int>();
for(int i=0; i<l; i++) s += hs.read_char();
}
inline void hwrite(hstream& hs, const ld& h) { double d = h; hs.write_chars((char*) &d, sizeof(double)); }
inline void hread(hstream& hs, ld& h) { double d; hs.read_chars((char*) &d, sizeof(double)); h = d; }
template<class T, size_t X> void hwrite(hstream& hs, const array<T, X>& a) { for(auto &ae: a) hwrite(hs, ae); }
template<class T, size_t X> void hread(hstream& hs, array<T, X>& a) { for(auto &ae: a) hread(hs, ae); }
inline void hread(hstream& hs, hyperpoint& h) { for(int i=0; i<MDIM; i++) hread(hs, h[i]); }
inline void hwrite(hstream& hs, hyperpoint h) { for(int i=0; i<MDIM; i++) hwrite(hs, h[i]); }
template<class T> void hwrite(hstream& hs, const vector<T>& a) { hwrite<int>(hs, isize(a)); for(auto &ae: a) hwrite(hs, ae); }
template<class T> void hread(hstream& hs, vector<T>& a) { a.resize(hs.get<int>()); for(auto &ae: a) hread(hs, ae); }
template<class T, class U> void hwrite(hstream& hs, const map<T,U>& a) {
hwrite<int>(hs, isize(a)); for(auto &ae: a) hwrite(hs, ae.first, ae.second);
}
template<class T, class U> void hread(hstream& hs, map<T,U>& a) {
a.clear();
int N = hs.get<int>();
for(int i=0; i<N; i++) {
T key; hread(hs, key);
hread(hs, a[key]);
}
}
template<class C, class C1, class... CS> void hwrite(hstream& hs, const C& c, const C1& c1, const CS&... cs) { hwrite(hs, c); hwrite(hs, c1, cs...); }
template<class C, class C1, class... CS> void hread(hstream& hs, C& c, C1& c1, CS&... cs) { hread(hs, c); hread(hs, c1, cs...); }
struct hstream_exception : hr_exception { hstream_exception() {} };
struct fhstream : hstream {
FILE *f;
explicit fhstream() { f = NULL; }
explicit fhstream(const string pathname, const char *mode) { f = fopen(pathname.c_str(), mode); vernum = VERNUM_HEX; }
~fhstream() { if(f) fclose(f); }
void write_char(char c) override { write_chars(&c, 1); }
void write_chars(const char* c, size_t i) override { if(fwrite(c, i, 1, f) != 1) throw hstream_exception(); }
void read_chars(char* c, size_t i) override { if(fread(c, i, 1, f) != 1) throw hstream_exception(); }
char read_char() override { char c; read_chars(&c, 1); return c; }
virtual void flush() override { fflush(f); }
};
struct shstream : hstream {
string s;
int pos;
explicit shstream(const string& t = "") : s(t) { pos = 0; vernum = VERNUM_HEX; }
void write_char(char c) override { s += c; }
char read_char() override { if(pos == isize(s)) throw hstream_exception(); return s[pos++]; }
};
inline void print(hstream& hs) {}
template<class... CS> string sprint(const CS&... cs) { shstream hs; print(hs, cs...); return hs.s; }
template<class C, class C1, class... CS> void print(hstream& hs, const C& c, const C1& c1, const CS&... cs) { print(hs, c); print(hs, c1, cs...); }
template<class... CS> void println(hstream& hs, const CS&... cs) { print(hs, cs...); hs.write_char('\n'); }
inline string spaced(int i) { return its(i); }
inline string spaced(color_t col) { return itsh8(col); }
inline string spaced(const string& s) { return s; }
inline string spaced(ld x) { return fts(x, 10); }
template<class T> string spaced_of(T a[], int q) { string s = spaced(a[0]); for(int i=1; i<q; i++) s += ' ', s += spaced(a[i]); return s; }
template<class T, int i> string spaced(const array<T,i>& a) { return spaced_of(&a[0], isize(a)); }
template<class C, class C1, class... CS> string spaced(const C& c, const C1& c1, const CS&... cs) { return spaced(c) + " " + spaced(c1, cs...); }
bool scan(fhstream& hs, int&);
bool scan(fhstream& hs, ld&);
bool scan(fhstream& hs, string&);
bool scan(fhstream& hs, color_t& c);
template<class C, class C1, class... CS> bool scan(fhstream& hs, C& c, C1& c1, CS&... cs) { return scan(hs, c) && scan(hs, c1, cs...); }
string scanline(fhstream& hs);
template<class T> T scan(fhstream& hs) { T t {}; scan(hs, t); return t; }
// copied from: https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element
namespace detail
{
template<int... Is>
struct seq { };
template<int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template<int... Is>
struct gen_seq<0, Is...> : seq<Is...> { };
template<typename T, typename F, int... Is>
void for_each(T&& t, F f, seq<Is...>)
{
auto l = { (f(std::get<Is>(t)), 0)... }; ignore(l);
}
}
template<typename... Ts, typename F>
void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
{
detail::for_each(t, f, detail::gen_seq<sizeof...(Ts)>());
}
inline void print(hstream& hs, const string& s) { hs.write_chars(s.c_str(), isize(s)); }
inline void print(hstream& hs, int i) { print(hs, its(i)); }
inline void print(hstream& hs, ld x) { print(hs, fts(x, 6)); }
inline void print(hstream& hs, color_t col) { print(hs, itsh8(col)); }
template<class T> void print(hstream& hs, const walker<T>& w) { print(hs, "[", w.at, "/", w.spin, "/", w.mirrored, "]"); }
struct comma_printer {
bool first;
hstream& hs;
template<class T> void operator() (const T& t) { if(first) first = false; else print(hs, ","); print(hs, t); }
comma_printer(hstream& hs) : first(true), hs(hs) {}
};
template<class T, size_t X> void print(hstream& hs, const array<T, X>& a) { print(hs, "("); comma_printer c(hs); for(const T& t: a) c(t); print(hs, ")"); }
template<class T> void print(hstream& hs, const vector<T>& a) { print(hs, "("); comma_printer c(hs); for(const T& t: a) c(t); print(hs, ")"); }
template<class T, class U> void print(hstream& hs, const map<T,U>& a) { print(hs, "("); comma_printer c(hs); for(auto& t: a) c(t); print(hs, ")"); }
template<class T> void print(hstream& hs, const set<T>& a) { print(hs, "("); comma_printer c(hs); for(auto& t: a) c(t); print(hs, ")"); }
template<class T> string separated(string separator, const vector<T>& a) {
shstream ss;
bool first = true;
for(auto& v: a) { if(first) first = false; else print(ss, separator); print(ss, v); }
return ss.s;
}
inline void print(hstream& hs, const hyperpoint h) { print(hs, "("); for(int i=0; i<MDIM; i++) { if(i) print(hs, ","); print(hs, h[i]); } print(hs, ")"); } // (const array<ld, MAXMDIM>&)h); }
inline void print(hstream& hs, const transmatrix T) {
print(hs, "("); comma_printer c(hs);
for(int i=0; i<MDIM; i++)
for(int j=0; j<MDIM; j++) c(T[i][j]);
print(hs, ")"); }
inline void print(hstream& hs, const shiftpoint h) { print(hs, h.h, "@", h.shift); }
inline void print(hstream& hs, const shiftmatrix T) { print(hs, T.T, "@", T.shift); }
template<class T, class U> void print(hstream& hs, const pair<T, U> & t) { print(hs, "(", t.first, ",", t.second, ")"); }
template<class... T> void print(hstream& hs, const tuple<T...> & t) {
print(hs, "(");
comma_printer p(hs);
for_each_in_tuple(t, p);
print(hs, ")");
}
#ifndef SPECIAL_LOGGER
inline void special_log(char c) { if(debugfile) fputc(c, debugfile); putchar(c); }
#endif
#if !CAP_SDL && CAP_TIMEOFDAY
int SDL_GetTicks();
#endif
struct logger : hstream {
int indentation;
bool doindent;
explicit logger() { doindent = false; }
void write_char(char c) override;
char read_char() override { throw hstream_exception(); }
void flush() override { fflush(stdout); }
};
extern logger hlog;
template<class... T> void println_log(T... t) { println(hlog, t...); }
template<class... T> void print_log(T... t) { print(hlog, t...); }
#ifdef __GNUC__
__attribute__((__format__ (__printf__, 1, 2)))
#endif
inline string format(const char *fmt, ...) {
char buf[1000];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, 1000, fmt, ap);
va_end(ap);
return buf;
}
inline void print(hstream& hs, heptagon* h) { print(hs, "H", index_pointer(h)); }
inline void print(hstream& hs, cell* h) { print(hs, "C", index_pointer(h)); }
inline void print(hstream& hs, hrmap* h) { print(hs, "M", index_pointer(h)); }
inline void print(hstream& hs, cellwalker cw) {
if(cw.at) print(hs, "[", cw.at, "/", cw.at->type, ":", cw.spin, ":", cw.mirrored, "]");
else print(hs, "[NULL]");
}
struct indenter {
dynamicval<int> ind;
explicit indenter(int i = 2) : ind(hlog.indentation, hlog.indentation + (i)) {}
};
struct indenter_finish : indenter {
explicit indenter_finish(bool b = true): indenter(b ? 2:0) {}
explicit indenter_finish(string s): indenter(2) {
indenter tmp(-2);
println(hlog, s);
}
~indenter_finish() { if(hlog.indentation != ind.backup) println(hlog, "(done)"); }
};
#endif
void logger::write_char(char c) {
if(doindent) {
doindent = false;
if(debugflags & DF_TIME) {
string s = get_stamp();
if(s != "") { for(char c: s) special_log(c); special_log(' '); }
}
for(int i=0; i<indentation; i++) special_log(' ');
}
special_log(c);
if(c == 10) {
doindent = true;
if(debugfile) fflush(debugfile);
}
}
EX void print(hstream& hs, cld x) {
int parts = 0;
if(kz(real(x))) {
print(hs, real(x));
parts++;
}
if(kz(imag(x))) {
if(parts && imag(x) > 0) print(hs, "+");
parts++;
print(hs, imag(x), "i");
}
if(!parts) print(hs, 0);
}
EX string fts_fixed(ld x, int prec IS(6)) {
std::stringstream ss;
ss.precision(prec);
ss << std::fixed << x;
return ss.str();
}
bool scan(fhstream& hs, int& i) { return fscanf(hs.f, "%d", &i) == 1; }
bool scan(fhstream& hs, color_t& c) { return fscanf(hs.f, "%x", &c) == 1; }
bool scan(fhstream& hs, ld& x) { return fscanf(hs.f, "%lf", &x) == 1; }
bool scan(fhstream& hs, string& s) { char t[10000]; t[0] = 0; int err = fscanf(hs.f, "%9500s", t); s = t; return err == 1 && t[0]; }
string scanline(fhstream& hs) { char buf[10000]; buf[0] = 0; ignore(fgets(buf, 10000, hs.f)); return buf; }
EX string scanline_noblank(fhstream& hs) {
string s = scanline(hs);
while(s.size() && among(s.back(), '\t', '\r', ' ', '\n')) s.pop_back();
while(s.size() && among(s[0], '\t', '\r', ' ', '\n')) s = s.substr(1);
return s;
}
/*
string fts_smartdisplay(ld x, int maxdisplay) {
string rv;
if(x > 1e9 || x < -1e9) retrun fts(x);
if(x<0) { rv = "-"; x = -x; }
int i = int(x);
rv += its(i);
x -= i;
bool nonzero = i;
if(x == 0) return rv;
if(x < 1e-9 && nonzero) return rv;
rv += ".";
while(maxdisplay > 0) {
x *= 10;
rv += '0' + int(x);
if(int(x)) nonzero = true;
x -= int(x);
if(x == 0) return rv;
if(x < 1e-9 && nonzero) return rv;
maxdisplay--;
}
} */
EX string cts(char c) { char buf[8]; buf[0] = c; buf[1] = 0; return buf; }
EX string llts(long long i) {
// sprintf does not work on Windows IIRC
if(i < 0) return "-" + llts(-i);
if(i < 10) return its((int) i);
return llts(i/10) + its(i%10);
}
EX string itsh(unsigned int i) {static char buf[16]; sprintf(buf, "%03X", i); return buf; }
EX string itsh(int i) {static char buf[16]; sprintf(buf, "%03X", i); return buf; }
EX string itsh2(int i) {static char buf[16]; sprintf(buf, "%02X", i); return buf; }
EX string itsh(unsigned long long i) {
int i0 = int(i);
int i1 = int(i >> 32);
if(i1) return itsh(i1) + itsh8(i0);
else return itsh(i0);
}
EX logger hlog;
// kz: utility for printing
// if it is close to 0, assume it is floating errors
EX ld kz(ld x) {
if(abs(x) < 1e-6) return 0;
return x;
}
EX hyperpoint kz(hyperpoint h) {
for(int d=0; d<MAXMDIM; d++) h[d] = kz(h[d]);
return h;
}
EX transmatrix kz(transmatrix h) {
for(int d=0; d<MAXMDIM; d++)
for(int e=0; e<MAXMDIM; e++)
h[d][e] = kz(h[d][e]);
return h;
}
EX shiftmatrix kz(shiftmatrix h) {
h.T = kz(h.T);
return h;
}
EX shiftpoint kz(shiftpoint h) {
h.h = kz(h.h);
return h;
}
#if HDR
template<class T> vector<T> kz(vector<T> v) {
for(auto& el: v) el = kz(el);
return v;
}
#endif
EX string pick123() { return cts('1' + rand() % 3); }
EX string pick12() { return cts('1' + rand() % 2); }
#if HDR
template<class T> string serialize(const T& data) {
shstream shs;
hwrite(shs, data);
return shs.s;
}
template<class T> T deserialize(const string& s) {
shstream shs;
shs.s = s;
T data;
hread(shs, data);
return data;
}
#endif
EX string as_hexstring(string o) {
string res;
for(char x: o) {
char buf[4];
sprintf(buf, "%02X", (unsigned char)(x));
res += buf;
}
return res;
}
EX string from_hexstring(string o) {
string res;
for(int i=0; i<isize(o); i+=2) {
char buf[4];
buf[0] = o[i];
buf[1] = o[i+1];
buf[2] = 0;
unsigned x;
sscanf(buf, "%02X", &x);
res += char(x);
}
return res;
}
EX string as_cstring(string o) {
string s = "string(\"";
for(char c: o)
s += format("\\x%02x", (unsigned char) c);
s += format("\", %d)", isize(o));
return s;
}
EX string as_nice_cstring(string o) {
string s = "\"";
for(char c: o)
if(c >= 32 && c < 126)
s += c;
else if(c == 10)
s += "\\n";
else
s += format("\\x%02x", (unsigned char) c);
s += "\"";
return s;
}
#if HDR
#if ISANDROID
#define DEBB(r,x)
#define DEBB0(r,x)
#define DEBBI(r,x)
#else
#define DEBB(r,x) { if(debugflags & (r)) { println_log x; } }
#define DEBB0(r,x) { if(debugflags & (r)) { print_log x; } }
#define DEBBI(r,x) { if(debugflags & (r)) { println_log x; } } indenter_finish _debbi(debugflags & (r));
#endif
#endif
#if CAP_GMP
EX string its(mpq_class x) { std::stringstream ss; ss << x; return ss.str(); }
EX void print(hstream& hs, const mpq_class& x) {
std::stringstream ss; ss << x; print(hs, ss.str());
}
#endif
#if HDR
template<class... T> string lalign(int len, T... t) {
shstream hs;
print(hs, t...);
while(isize(hs.s) < len) hs.s += " ";
return hs.s;
}
#endif
}
|