Branch: refs/heads/master Author: Colomban Wendling ban@herbesfolles.org Committer: Colomban Wendling ban@herbesfolles.org Date: Tue, 12 Dec 2023 23:36:00 UTC Commit: 3a019a43e96b8df423ce7af2d6a74231bbac0205 https://github.com/geany/geany/commit/3a019a43e96b8df423ce7af2d6a74231bbac02...
Log Message: ----------- encodings: Add a flag for whether the encoding is supported
This adds support for checking whether an encoding is actually supported on the system, as some are commonly not supported everywhere, e.g. ISO-IR-111, ISO-8859-8-I or HZ.
Here we assume that a conversion to UTF-8 means it also works the other way around (e.g. from UTF-8, assuming the data can be represented, of course). This is a valid assumption if GNU libc or GNU libiconv is used, and might or might not hold for other implementations. This assumption however makes the code a fair bit simpler, and should have little impact in reality as this flag is mostly a suggestion that can be used to hide UI elements or skip attempts, and that in most situations it doesn't make much sense for us to be able to *write* to an encoding we cannot read.
As for startup performances, checking all encodings takes about 10ms on my machine, which sounds Good Enoughâ„¢.
This is currently unused, but subsequent commits will start using this.
Modified Paths: -------------- src/encodings.c src/encodingsprivate.h
Modified: src/encodings.c 27 lines changed, 26 insertions(+), 1 deletions(-) =================================================================== @@ -59,12 +59,24 @@ static gboolean pregs_loaded = FALSE; GeanyEncoding encodings[GEANY_ENCODINGS_MAX];
+static gboolean conversion_supported(const gchar *to, const gchar *from) +{ + GIConv conv = g_iconv_open(to, from); + if (conv == (GIConv) -1) + return FALSE; + + g_iconv_close(conv); + return TRUE; +} + + #define fill(Order, Group, Idx, Charset, Name) \ encodings[Idx].idx = Idx; \ encodings[Idx].order = Order; \ encodings[Idx].group = Group; \ encodings[Idx].charset = Charset; \ - encodings[Idx].name = Name; + encodings[Idx].name = Name; \ + encodings[Idx].supported = FALSE;
static void init_encodings(void) { @@ -140,6 +152,19 @@ static void init_encodings(void) fill(14, EASTASIAN, GEANY_ENCODING_UHC, "UHC", _("Korean"));
fill(0, NONE, GEANY_ENCODING_NONE, "None", _("Without encoding")); + + /* fill the flags member */ + for (guint i = 0; i < G_N_ELEMENTS(encodings); i++) + { + if (i == GEANY_ENCODING_NONE || conversion_supported("UTF-8", encodings[i].charset)) + encodings[i].supported = TRUE; + else + { + /* geany_debug() doesn't really work at this point, unless G_MESSAGES_DEBUG + * is set explicitly by the caller, but that's better than nothing */ + geany_debug("Encoding %s is not supported by the system", encodings[i].charset); + } + } }
Modified: src/encodingsprivate.h 1 lines changed, 1 insertions(+), 0 deletions(-) =================================================================== @@ -46,6 +46,7 @@ typedef struct GeanyEncoding GeanyEncodingGroup group; /* Internally used member for grouping */ const gchar *charset; /* String representation of the encoding, e.g. "ISO-8859-3" */ const gchar *name; /* Translatable and descriptive name of the encoding, e.g. "South European" */ + gboolean supported; /* Whether this encoding is supported on the system */ } GeanyEncoding;
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).