aboutsummaryrefslogtreecommitdiff
path: root/tiff-tables.awk
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-12-16 02:11:07 +0100
committerPřemysl Eric Janouch <p@janouch.name>2022-06-10 02:18:14 +0200
commit81145064de936e9ec401a50deca315891c6068f0 (patch)
treed0060c36073a6ee79e8f8c6dcdb21c0fa8c1f444 /tiff-tables.awk
parent60a8ee7a80063eaf620eaa727af0f484fdb91bff (diff)
downloadfiv-81145064de936e9ec401a50deca315891c6068f0.tar.gz
fiv-81145064de936e9ec401a50deca315891c6068f0.tar.xz
fiv-81145064de936e9ec401a50deca315891c6068f0.zip
Generate TIFF structs/enums from a text file
This is to make the tables much easier to maintain.
Diffstat (limited to 'tiff-tables.awk')
-rwxr-xr-xtiff-tables.awk94
1 files changed, 94 insertions, 0 deletions
diff --git a/tiff-tables.awk b/tiff-tables.awk
new file mode 100755
index 0000000..2d93c36
--- /dev/null
+++ b/tiff-tables.awk
@@ -0,0 +1,94 @@
+#!/usr/bin/awk -f
+BEGIN {
+ FS = ", *"
+ print "// Generated by tiff-tables.awk. DO NOT MODIFY."
+}
+
+{
+ # Remember and strip consecutive comments.
+ if (match($0, /#/))
+ comment[++comments] = substr($0, RSTART + 1)
+ else if (!/[[:space:]]/)
+ comments = 0
+
+ sub(/#.*$/, "")
+ sub(/[[:space:]]*$/, "")
+}
+
+# Converts arbitrary strings to C identifiers (when running in the C locale).
+function identifize(s) {
+ # Regard parenthesised text as comments.
+ while (match(s, /[[:space:]]\([^)]+\)/)) {
+ comment[++comments] = substr(s, RSTART, RLENGTH)
+ s = substr(s, 1, RSTART - 1) substr(s, RSTART + RLENGTH)
+ }
+
+ # Capitalize words (toupper is POSIX), removing spaces and dashes between.
+ while (match(s, /[-[:space:]]./)) {
+ s = substr(s, 1, RSTART - 1) \
+ toupper(substr(s, RSTART + 1, 1)) \
+ substr(s, RSTART + RLENGTH)
+ }
+
+ # Replace any remaining non-identifier characters with underscores.
+ gsub(/[^[:alnum:]]/, "_", s)
+ return s
+}
+
+function flushcomments(prefix, i, acc) {
+ for (i = 1; i <= comments; i++)
+ acc = acc prefix comment[i] "\n"
+ comments = 0
+ return acc
+}
+
+function flushvalues() {
+ if (values) {
+ allvalues = allvalues "enum " fieldname " {\n" values "};\n\n"
+ values = ""
+ fields = fields "\n\t\t{}\n\t}},"
+ } else if (fields) {
+ fields = fields " NULL},"
+ }
+}
+
+function flushsection() {
+ if (section) {
+ flushvalues()
+ print "};\n\n" allvalues "static struct tiff_entry " \
+ sectionsnakecase "_entries[] = {" fields "\n\t{}\n};"
+ }
+}
+
+# Section marker
+/^= / {
+ flushsection()
+ section = identifize(substr($0, 3))
+ sectionsnakecase = tolower(substr($0, 3))
+ gsub(/[^[:alnum:]]/, "_", sectionsnakecase)
+ fields = ""
+ allvalues = ""
+ print "\n" flushcomments("//") "enum {"
+}
+
+# Field
+section && /^[^\t=]/ {
+ flushvalues()
+ fieldname = section "_" identifize($2)
+ fields = fields "\n\t{\"" $2 "\", " fieldname ","
+ print flushcomments("\t//") "\t" fieldname " = " $1 ","
+}
+
+# Value
+section && /^\t/ {
+ sub(/^\t*/, "")
+ valuename = fieldname "_" identifize($2)
+ if (!values)
+ fields = fields " (struct tiff_value[]) {"
+ values = values flushcomments("\t//") "\t" valuename " = " $1 ",\n"
+ fields = fields "\n\t\t{\"" $2 "\", " valuename "},"
+}
+
+END {
+ flushsection()
+}