diff options
| author | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-06 04:21:03 +0500 |
|---|---|---|
| committer | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-06 04:21:03 +0500 |
| commit | 4810610dafb8ed93dd3672e32256997e22eca950 (patch) | |
| tree | e0db960825627acbe94a71f773ca3ac36617869f /util | |
| parent | f93978aba486bf0fea8d9ee857d014a02f3f7d96 (diff) | |
wip
Diffstat (limited to 'util')
| -rw-r--r-- | util/code_snapshot.cxx | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/util/code_snapshot.cxx b/util/code_snapshot.cxx index 907c6ae37..652892763 100644 --- a/util/code_snapshot.cxx +++ b/util/code_snapshot.cxx @@ -45,12 +45,13 @@ #include "../fluid/widgets/Style_Parser.h" #include <stdio.h> -#include <algorithm> +#include <stdlib.h> +#include <string.h> -Fl_Window* window = nullptr; -Fl_Group* group = nullptr; -fld::widget::Code_Viewer* code_viewer = nullptr; +Fl_Window* window = 0; +Fl_Group* group = 0; +fld::widget::Code_Viewer* code_viewer = 0; int line_height = 10; void create_window() { @@ -71,11 +72,16 @@ void create_window() { // Make sure the display is opened. Fl_Display_Device::display_device(); + int h; line_height = fl_height(code_viewer->textfont(), code_viewer->textsize()); - line_height = std::max(line_height, fl_height(FL_COURIER, code_viewer->textsize())); - line_height = std::max(line_height, fl_height(FL_COURIER_BOLD, code_viewer->textsize())); - line_height = std::max(line_height, fl_height(FL_COURIER_ITALIC, code_viewer->textsize())); - line_height = std::max(line_height, fl_height(FL_COURIER_BOLD_ITALIC, code_viewer->textsize())); + h = fl_height(FL_COURIER, code_viewer->textsize()); + if (h > line_height) line_height = h; + h = fl_height(FL_COURIER_BOLD, code_viewer->textsize()); + if (h > line_height) line_height = h; + h = fl_height(FL_COURIER_ITALIC, code_viewer->textsize()); + if (h > line_height) line_height = h; + h = fl_height(FL_COURIER_BOLD_ITALIC, code_viewer->textsize()); + if (h > line_height) line_height = h; } void save_snapshot(const char* code, const char* filename) { @@ -143,34 +149,49 @@ int main(int argc, char *argv[]) { // fprintf(stderr, "Reading \"%s\".\n", argv[i]); - std::string code; - std::string filename; + char *code = 0; + size_t code_len = 0; + size_t code_alloc = 0; + char filename[FL_PATH_MAX]; bool in_code_block = false; + int j; for (;;) { fgets(line, 1023, f); if (feof(f)) break; if (in_code_block) { if (strstr(line, "\\endcode_international")) { - if (!code.empty()) { - code.resize(code.size() - 1); - save_snapshot(code.c_str(), filename.c_str()); + if (code_len > 0) { + code[code_len - 1] = '\0'; // remove trailing newline + save_snapshot(code, filename); } in_code_block = false; - code = ""; + code_len = 0; } else { - code += line; + size_t linelen = strlen(line); + if (code_len + linelen >= code_alloc) { + code_alloc = (code_alloc == 0) ? 4096 : code_alloc * 2; + code = (char *)realloc(code, code_alloc); + } + memcpy(code + code_len, line, linelen); + code_len += linelen; + code[code_len] = '\0'; } } else { if (strstr(line, "\\code_international")) { const char* fn_start = strstr(line, "{\""); const char* fn_end = strstr(line, "\"}"); if (fn_start && fn_end && (fn_end > fn_start)) { - filename = std::string(fn_start+2, fn_end-fn_start-2); + int len = (int)(fn_end - fn_start - 2); + if (len >= FL_PATH_MAX) len = FL_PATH_MAX - 1; + for (j = 0; j < len; j++) + filename[j] = fn_start[2 + j]; + filename[len] = '\0'; in_code_block = true; } } } } + if (code) free(code); fclose(f); } |
