citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 7f4b2b7b6c1b74e25cb59b1fac0d4941de35f7d1
parent b5e289854f4c1c7d46ce8cbd9e205b85543c3b9a
Author: Kyle Milz <kyle@0x30.net>
Date:   Wed, 13 Jul 2016 22:52:32 -0600

src: move tab lookup to inside gl buffer

Diffstat:
Msrc/gl_buffer.cc | 14++++++++++++++
Msrc/gl_runtime_conn.cc | 55++++++++++++++++++++++++++++++++-----------------------
2 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/src/gl_buffer.cc b/src/gl_buffer.cc @@ -107,6 +107,8 @@ demo_buffer_add_text (demo_buffer_t *buffer, glyphy_point_t top_left = buffer->cursor; buffer->cursor.y += font_size /* * font->ascent */; unsigned int unicode; + unsigned int col = 0; + for (const unsigned char *p = (const unsigned char *) utf8; *p; p++) { if (*p < 128) { unicode = *p; @@ -132,6 +134,7 @@ demo_buffer_add_text (demo_buffer_t *buffer, if (unicode == '\n') { buffer->cursor.y += font_size; buffer->cursor.x = top_left.x; + col = 0; continue; } @@ -139,6 +142,14 @@ demo_buffer_add_text (demo_buffer_t *buffer, glyph_info_t gi; demo_font_lookup_glyph (font, glyph_index, &gi); + /* Let tab operate like it does in editors, 8 spaces. */ + if (unicode == '\t') { + int nspaces = 8 - (col % 8); + buffer->cursor.x += font_size * gi.advance * nspaces; + col += nspaces; + continue; + } + /* Update ink extents */ glyphy_extents_t ink_extents; demo_shader_add_glyph_vertices (buffer->cursor, font_size, &gi, buffer->vertices, &ink_extents); @@ -154,6 +165,9 @@ demo_buffer_add_text (demo_buffer_t *buffer, glyphy_extents_add (&buffer->logical_extents, &corner); buffer->cursor.x += font_size * gi.advance; + + /* Hack; Not all characters are a single column wide. */ + col++; } buffer->dirty = true; diff --git a/src/gl_runtime_conn.cc b/src/gl_runtime_conn.cc @@ -45,7 +45,7 @@ RuntimeProcess::RuntimeProcess(af_unix *sock, demo_buffer_t *buf, demo_font_t *f top_left.x += 50; socket->read_all(current_unit.num_lines); - current_unit.execution_counts.resize(current_unit.num_lines); + current_unit.execution_counts.resize(current_unit.num_lines, 0); socket->read_all(current_unit.inst_sites); } @@ -57,26 +57,28 @@ void RuntimeProcess::read_file(std::string file_name, glyphy_point_t top_left) { std::string line; - std::ifstream file_stream(file_name); - - if (file_stream.is_open() == 0) - errx(1, "ifstream.open()"); - - while (std::getline(file_stream, line)) { - size_t tab_pos = 0; - // Find and replace replace all tabs with spaces - while ((tab_pos = line.find('\t')) != std::string::npos) { - int rem = tab_pos % 8; - line.erase(tab_pos, 1); - line.insert(tab_pos, std::string(8 - rem, ' ')); - } - - demo_buffer_move_to(buffer, &top_left); - demo_buffer_add_text(buffer, line.c_str(), font, 1); - ++top_left.y; - } + std::ifstream src_file(file_name, std::ios::binary); + + if (! src_file) + errx(1, "src_file.open()"); + + src_file.seekg(0, src_file.end); + int length = src_file.tellg(); + src_file.seekg(0, src_file.beg); + + char *src_buffer = new char [length + 1]; + + src_file.read(src_buffer, length); + src_buffer[length] = '\0'; + + if (! src_file) + errx(1, "src_file.read()"); + src_file.close(); - file_stream.close(); + demo_buffer_move_to(buffer, &top_left); + demo_buffer_add_text(buffer, src_buffer, font, 1); + + delete[] src_buffer; } void @@ -87,9 +89,16 @@ RuntimeProcess::draw() void RuntimeProcess::idle() { - for (auto &trans_unit : translation_units) { - size_t bytes_total = trans_unit.num_lines * sizeof(uint64_t); - assert(socket->read_all((uint8_t *)&trans_unit.execution_counts[0], bytes_total) == bytes_total); + for (auto &t : translation_units) { + size_t bytes_total = t.num_lines * sizeof(uint64_t); + + socket->read_all((uint8_t *)&t.execution_counts[0], bytes_total); + + int execs = 0; + for (int i = 0; i < t.num_lines; i++) + execs += t.execution_counts[i]; + + std::cout << t.file_name << ": " << execs << std::endl; } // Send response back