summaryrefslogtreecommitdiff
path: root/fluid/io/String_Writer.cxx
diff options
context:
space:
mode:
authorMatthias Melcher <github@matthiasm.com>2025-03-16 17:16:12 -0400
committerGitHub <noreply@github.com>2025-03-16 17:16:12 -0400
commit51a55bc73660f64e8f4b32b8b4d3858f2a786f7b (patch)
tree122ad9f838fcf8f61ed7cf5fa031e8ed69817e10 /fluid/io/String_Writer.cxx
parent13a7073a1e007ce5b71ef70bced1a9b15158820d (diff)
Fluid: restructuring and rejuvenation of the source code.
* Add classes for application and project * Removed all globals from Fluid.h * Extracting args and project history into their own classes * Moving globals into Application class * Initialize values inside headers for some classes. * Undo functionality wrapped in a class inside Project. * File reader and writer are now linked to a project. * Avoid global project access * Nodes (former Types) will be managed by a new Tree class. * Removed static members (hidden globals) form Node/Fl_Type. * Adding Tree iterator. * Use nullptr instead of 0, NULL, or 0L * Renamed Fl_..._Type to ..._Node, FL_OVERRIDE -> override * Renaming ..._type to ...::prototype * Splitting Widget Panel into multiple files. * Moved callback code into widget panel file. * Cleaning up Fluid_Image -> Image_asset * Moving Fd_Snap_Action into new namespace fld::app::Snap_Action etc. * Moved mergeback into proj folder. * `enum ID` is now `enum class Type`.
Diffstat (limited to 'fluid/io/String_Writer.cxx')
-rw-r--r--fluid/io/String_Writer.cxx142
1 files changed, 142 insertions, 0 deletions
diff --git a/fluid/io/String_Writer.cxx b/fluid/io/String_Writer.cxx
new file mode 100644
index 000000000..6b70ec1f3
--- /dev/null
+++ b/fluid/io/String_Writer.cxx
@@ -0,0 +1,142 @@
+//
+// String File Writer code for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2025 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// https://www.fltk.org/COPYING.php
+//
+// Please see the following page on how to report bugs and issues:
+//
+// https://www.fltk.org/bugs.php
+//
+
+#include "io/String_Writer.h"
+
+#include "Fluid.h"
+#include "Project.h"
+#include "nodes/Window_Node.h"
+#include "nodes/Function_Node.h"
+
+using namespace fld;
+using namespace fld::io;
+
+/**
+ Write a string to a file, replacing all non-ASCII characters with octal codes.
+ \param[in] out output file
+ \param[in] text write this NUL terminated utf-8 string
+ \return EOF if any of the file access calls failed, 0 if OK
+ */
+static int write_escaped_strings(FILE *out, const char *text) {
+ int ret = 0;
+ const unsigned char *utf8_text = (const unsigned char *)text;
+ for (const unsigned char *s = utf8_text; *s; ++s) {
+ unsigned char c = *s;
+ // escape control characters, delete, all utf-8, and the double quotes
+ // note: we should have an option in the project settings to allow utf-8
+ // characters in the output text and not escape them
+ if (c < 32 || c > 126 || c == '\"') {
+ if (c == '\r') {
+ ret = fputs("\\r", out);
+ } else if (c == '\n') {
+ ret = fputs("\\n", out);
+ } else {
+ ret = fprintf(out, "\\%03o", c);
+ }
+ } else {
+ ret = putc((int)c, out);
+ }
+ }
+ return ret;
+}
+
+/**
+ Write a file that contains all label and tooltip strings for internationalization.
+ The user is responsible to set the right file name extension. The file format
+ is determined by `proj_.i18n_type`.
+ \param[in] filename file path and name to a file that will hold the strings
+ \return 1 if the file could not be opened for writing, or the result of `fclose`.
+ */
+int fld::io::write_strings(Project &proj, const std::string &filename) {
+ Node *p;
+ Widget_Node *w;
+ int i;
+
+ FILE *fp = fl_fopen(filename.c_str(), "wb");
+ if (!fp) return 1;
+
+ switch (proj.i18n_type) {
+ case fld::I18n_Type::NONE : /* None, just put static text out */
+ fprintf(fp, "# generated by Fast Light User Interface Designer (fluid) version %.4f\n",
+ FL_VERSION);
+ for (auto w: proj.tree.all_widgets()) {
+ if (w->label()) {
+ write_escaped_strings(fp, w->label());
+ putc('\n', fp);
+ }
+ if (w->tooltip()) {
+ write_escaped_strings(fp, w->tooltip());
+ putc('\n', fp);
+ }
+ }
+ break;
+ case fld::I18n_Type::GNU : /* GNU gettext, put a .po file out */
+ fprintf(fp, "# generated by Fast Light User Interface Designer (fluid) version %.4f\n",
+ FL_VERSION);
+ for (p = proj.tree.first; p; p = p->next) {
+ if (p->is_widget()) {
+ w = (Widget_Node *)p;
+
+ if (w->label()) {
+ fputs("msgid \"", fp);
+ write_escaped_strings(fp, w->label());
+ fputs("\"\n", fp);
+
+ fputs("msgstr \"", fp);
+ write_escaped_strings(fp, w->label());
+ fputs("\"\n", fp);
+ }
+
+ if (w->tooltip()) {
+ fputs("msgid \"", fp);
+ write_escaped_strings(fp, w->tooltip());
+ fputs("\"\n", fp);
+
+ fputs("msgstr \"", fp);
+ write_escaped_strings(fp, w->tooltip());
+ fputs("\"\n", fp);
+ }
+ }
+ }
+ break;
+ case fld::I18n_Type::POSIX : /* POSIX catgets, put a .msg file out */
+ fprintf(fp, "$ generated by Fast Light User Interface Designer (fluid) version %.4f\n",
+ FL_VERSION);
+ fprintf(fp, "$set %s\n", proj.i18n_pos_set.c_str());
+ fputs("$quote \"\n", fp);
+
+ for (i = 1, p = proj.tree.first; p; p = p->next) {
+ if (p->is_widget()) {
+ w = (Widget_Node *)p;
+
+ if (w->label()) {
+ fprintf(fp, "%d \"", i ++);
+ write_escaped_strings(fp, w->label());
+ fputs("\"\n", fp);
+ }
+
+ if (w->tooltip()) {
+ fprintf(fp, "%d \"", i ++);
+ write_escaped_strings(fp, w->tooltip());
+ fputs("\"\n", fp);
+ }
+ }
+ }
+ break;
+ }
+
+ return fclose(fp);
+}