// examples/files translated by mycpp_main // BEGIN mycpp output #include "mycpp/runtime.h" namespace files { // forward declare } GLOBAL_STR(S_zFn, "stdout\n"); namespace files { // declare void run_tests(); void run_benchmarks(); } // declare namespace files namespace files { // define void run_tests() { mylib::BufWriter* f = nullptr; BigStr* contents = nullptr; mylib::Writer* f2 = nullptr; StackRoot _root0(&f); StackRoot _root1(&contents); StackRoot _root2(&f2); f = Alloc(); for (int i = 0; i < 30; ++i) { f->write(chr((i + 65))); } contents = f->getvalue(); mylib::print_stderr(StrFormat("Wrote %d bytes to StringIO", len(contents))); if (1) { mylib::print_stderr(StrFormat("contents = %s ... %s", contents->slice(0, 10), contents->slice(-10))); } f2 = mylib::Stdout(); f2->write(S_zFn); } void run_benchmarks() { int n; int result; int i; mylib::BufWriter* f = nullptr; StackRoot _root0(&f); n = 10000; result = 0; i = 0; while (i < n) { f = Alloc(); for (int j = 0; j < 30; ++j) { f->write(chr((j + 65))); } result += len(f->getvalue()); mylib::MaybeCollect(); i += 1; } mylib::print_stderr(StrFormat("Ran %d iterations", n)); mylib::print_stderr(StrFormat("result = %d", result)); } } // define namespace files int main(int argc, char **argv) { gHeap.Init(); char* b = getenv("BENCHMARK"); if (b && strlen(b)) { // match Python's logic fprintf(stderr, "Benchmarking...\n"); files::run_benchmarks(); } else { files::run_tests(); } gHeap.CleanProcessExit(); }