// examples/files translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "Wrote %d bytes to StringIO"); GLOBAL_STR(str1, "contents = %s ... %s"); GLOBAL_STR(str2, "contents = %s ... %s"); GLOBAL_STR(str3, "stdout\n"); GLOBAL_STR(str4, "Ran %d iterations"); GLOBAL_STR(str5, "result = %d"); namespace files { // forward declare } // forward declare namespace files 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<mylib::BufWriter>(); 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(str3); } 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<mylib::BufWriter>(); 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(); }