X-Git-Url: https://git.draconx.ca/gitweb/dxcommon.git/blobdiff_plain/b84052135e522d00278d33e0b3d0afbb46e03f51..d4f7fab64222a7d3ce15e86c5c7b32f7cdd3ce93:/scripts/gen-strtab.awk diff --git a/scripts/gen-strtab.awk b/scripts/gen-strtab.awk index 7e3de12..2265bb5 100755 --- a/scripts/gen-strtab.awk +++ b/scripts/gen-strtab.awk @@ -23,6 +23,10 @@ # @nozero # All strings will have a non-zero offset in the strtab. # +# @macro +# Instead of a variable declaration, the generated header will define an +# object-like macro that can be used as the initializer for a char array. +# # A string is defined by beginning a line with one or two & characters, which # must be immediately followed by a C identifier. Two & characters indicates # a string that should not be translated, as described below. A nonempty @@ -43,9 +47,12 @@ # - All other backslashes are deleted. This can be used to prevent special # handling of whitespace, # or & characters at the beginning of a line. # -# The output defines a variable, strtab, which contains all of the strings, -# and each identifier in the input is declared as an emumeration constant -# whose value is the offset of the associated string within strtab. +# Unless the @macro option is specified, the output defines a variable, +# strtab, which contains all of the strings, and each identifier in the input +# is declared as an emumeration constant whose value is the offset of the +# associated string within strtab. Otherwise, if the @macro option is +# specified, no variables are defined and STRTAB_INITIALIZER object-like macro +# may be used to initialize a char array with static storage duration. # # Normally, the generated source code wraps strings using the identity macro # N_(x), which has no effect on the resulting data structures but enables tools @@ -74,6 +81,7 @@ END { BEGIN { opts["zero"] = 1 + opts["macro"] = 0 collected = ident = "" startline = endline = 0 num_vars = 0 @@ -131,7 +139,7 @@ END { } END { - strtab = "" + strtab = cont = "" strtab_len = 0 count = bucketsort(sorted_strings, strings) max = 0 @@ -140,15 +148,20 @@ END { print "#ifndef N_" print "# define N_(x) x" print "#endif" - print "\nstatic const char strtab[] =" + if (opts["macro"]) { + cont = " \\"; + print "\n#define STRTAB_INITIALIZER" cont; + } else { + print "\nstatic const char strtab[] ="; + } for (i = 0; i < count; i++) { s = sorted_strings[i] - gsub(/\\\\/, "\2", s) + gsub(/\\\\/, "\2\2", s) if ((n = index(strtab "\1", s "\1")) > 0) { offsets[sorted_strings[i]] = real_length(substr(strtab, 1, n-1)); if (!(sorted_strings[i] in nol10n)) - print "\tSTR_L10N_(N_(\"" sorted_strings[i] "\"))"; + print "\tSTR_L10N_(N_(\"" sorted_strings[i] "\"))" cont; } else if (strtab) { strtab = strtab "\1" s offsets[sorted_strings[i]] = strtab_len + 1 @@ -160,18 +173,18 @@ END { } } - gsub(/\2/, "\\\\", strtab); + gsub(/\2/, "\\", strtab); n = split(strtab, split_strtab, "\1"); for (i = 1; i <= n; i++) { printf("\t%4s ", i > !!opts["zero"] ? "\"\\0\"" : ""); if (split_strtab[i] in nol10n) { - print "\"" split_strtab[i] "\""; + print "\"" split_strtab[i] "\"" cont; } else { - print "N_(\"" split_strtab[i] "\")"; + print "N_(\"" split_strtab[i] "\")" cont; } } - print "\t\"\";"; + print "\t\"\"" substr(";", 1, !opts["macro"]); print "enum {" for (i = 0; i < num_vars; i++) { @@ -187,13 +200,13 @@ END { print "\n#define STRTAB_MAX_OFFSET " max } -# finish_input_string(strings, ident, val) +# finish_string_input(strings, ident, val) # # Deal with backslash-escapes and special characters in val, then set # strings[ident] = val. function finish_string_input(strings, ident, val, n, tmpval) { - gsub(/\\\\/, "\1", val) + gsub(/\\\\/, "\1\1", val) val = val (endline > startline ? "\n" : "") gsub(/\\\n/, "", val) @@ -208,7 +221,7 @@ function finish_string_input(strings, ident, val, n, tmpval) gsub(/"/, "\\\"", tmpval) gsub(/\t/, "\\t", tmpval) gsub(/\n/, "\\n", tmpval) - gsub(/\1/, "\\\\", tmpval) + gsub(/\1/, "\\", tmpval) strings[ident] = tmpval if (!current_l10n) { @@ -219,7 +232,7 @@ function finish_string_input(strings, ident, val, n, tmpval) function real_length(s, t) { t = length(s) - return t - gsub(/\\./, "&", s) + return t - gsub(/\\.|\2\2/, "&", s) } # bucketsort(dst, src)