summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--cssprops.h81
-rw-r--r--cssprops.txt10
-rw-r--r--gencssprops.pl68
-rw-r--r--gentags.pl6
-rw-r--r--tags.h2
6 files changed, 169 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 660c67f..3579e82 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC = cc
CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -g -O0
LDFLAGS =
-HDRS = mem.h vec.h parse.h tags.h dom.h
+HDRS = mem.h vec.h parse.h tags.h dom.h cssprops.h
SRCS = vec.c parse.c dom.c main.c
OBJS = $(SRCS:.c=.o)
TARGET = glacier
@@ -12,7 +12,10 @@ all: $(TARGET)
tags.h: tags.txt gentags.pl
perl gentags.pl < tags.txt > tags.h
-$(OBJS): tags.h
+cssprops.h: cssprops.txt gencssprops.pl
+ perl gencssprops.pl < cssprops.txt > cssprops.h
+
+$(OBJS): tags.h cssprops.h
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $(TARGET) $(LDFLAGS)
diff --git a/cssprops.h b/cssprops.h
new file mode 100644
index 0000000..266d990
--- /dev/null
+++ b/cssprops.h
@@ -0,0 +1,81 @@
+/* Generated file, do not edit */
+
+#ifndef CSSPROPS_H
+#define CSSPROPS_H
+
+#include <string.h>
+
+typedef enum {
+ CSS_PROP_UNKNOWN,
+ CSS_PROP_COLOR,
+ CSS_PROP_DISPLAY,
+ CSS_PROP_FONT_FAMILY,
+ CSS_PROP_FONT_SIZE,
+ CSS_PROP_FONT_WEIGHT,
+ CSS_PROP_LINE_HEIGHT,
+ CSS_PROP_LIST_STYLE_TYPE,
+ CSS_PROP_MARGIN,
+ CSS_PROP_PADDING_LEFT,
+ CSS_PROP_TEXT_DECORATION,
+} css_prop;
+
+static const struct {
+ const char *name;
+ css_prop prop;
+} css_prop_map[] = {
+ { "color", CSS_PROP_COLOR },
+ { "display", CSS_PROP_DISPLAY },
+ { "font-family", CSS_PROP_FONT_FAMILY },
+ { "font-size", CSS_PROP_FONT_SIZE },
+ { "font-weight", CSS_PROP_FONT_WEIGHT },
+ { "line-height", CSS_PROP_LINE_HEIGHT },
+ { "list-style-type", CSS_PROP_LIST_STYLE_TYPE },
+ { "margin", CSS_PROP_MARGIN },
+ { "padding-left", CSS_PROP_PADDING_LEFT },
+ { "text-decoration", CSS_PROP_TEXT_DECORATION },
+};
+
+static inline css_prop
+str_to_css_prop(const char *name, size_t len)
+{
+ int lo, hi, mid, cmp;
+
+ lo = 0;
+ hi = 10 - 1;
+
+ while (lo <= hi) {
+ mid = (lo + hi) / 2;
+ cmp = strncmp(name, css_prop_map[mid].name, len);
+ if (cmp == 0) {
+ if (css_prop_map[mid].name[len] == '\0')
+ return css_prop_map[mid].prop;
+ hi = mid - 1;
+ } else if (cmp < 0)
+ hi = mid - 1;
+ else
+ lo = mid + 1;
+ }
+
+ return CSS_PROP_UNKNOWN;
+}
+
+static inline const char *
+css_prop_to_str(css_prop prop)
+{
+ switch (prop) {
+ case CSS_PROP_UNKNOWN: return "unknown";
+ case CSS_PROP_COLOR: return "color";
+ case CSS_PROP_DISPLAY: return "display";
+ case CSS_PROP_FONT_FAMILY: return "font-family";
+ case CSS_PROP_FONT_SIZE: return "font-size";
+ case CSS_PROP_FONT_WEIGHT: return "font-weight";
+ case CSS_PROP_LINE_HEIGHT: return "line-height";
+ case CSS_PROP_LIST_STYLE_TYPE: return "list-style-type";
+ case CSS_PROP_MARGIN: return "margin";
+ case CSS_PROP_PADDING_LEFT: return "padding-left";
+ case CSS_PROP_TEXT_DECORATION: return "text-decoration";
+ default: return "unknown";
+ }
+}
+
+#endif /* CSSPROPS_H */
diff --git a/cssprops.txt b/cssprops.txt
new file mode 100644
index 0000000..2f1ccd0
--- /dev/null
+++ b/cssprops.txt
@@ -0,0 +1,10 @@
+color
+display
+font-family
+font-size
+font-weight
+line-height
+list-style-type
+margin
+padding-left
+text-decoration
diff --git a/gencssprops.pl b/gencssprops.pl
new file mode 100644
index 0000000..be0caea
--- /dev/null
+++ b/gencssprops.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my @props = sort grep { /\S/ } map { chomp; $_ } <STDIN>;
+
+my $maxlen = (sort { $b <=> $a } map { length($_) } @props)[0];
+
+print "/* Generated file, do not edit */\n\n";
+print "#ifndef CSSPROPS_H\n";
+print "#define CSSPROPS_H\n\n";
+print "#include <string.h>\n\n";
+
+print "typedef enum {\n";
+print "\tCSS_PROP_UNKNOWN,\n";
+for my $prop (@props) {
+ (my $enum = uc($prop)) =~ s/-/_/g;
+ printf "\tCSS_PROP_%s,\n", $enum;
+}
+print "} css_prop;\n\n";
+
+print "static const struct {\n";
+print "\tconst char *name;\n";
+print "\tcss_prop prop;\n";
+print "} css_prop_map[] = {\n";
+for my $prop (@props) {
+ (my $enum = uc($prop)) =~ s/-/_/g;
+ printf "\t{ \"%-*s\tCSS_PROP_%s },\n", $maxlen + 2, "$prop\",", $enum;
+}
+print "};\n\n";
+
+my $n = scalar @props;
+
+print "static inline css_prop\n";
+print "str_to_css_prop(const char *name, size_t len)\n";
+print "{\n";
+print "\tint lo, hi, mid, cmp;\n\n";
+print "\tlo = 0;\n";
+print "\thi = $n - 1;\n\n";
+print "\twhile (lo <= hi) {\n";
+print "\t\tmid = (lo + hi) / 2;\n";
+print "\t\tcmp = strncmp(name, css_prop_map[mid].name, len);\n";
+print "\t\tif (cmp == 0) {\n";
+print "\t\t\tif (css_prop_map[mid].name[len] == '\\0')\n";
+print "\t\t\t\treturn css_prop_map[mid].prop;\n";
+print "\t\t\thi = mid - 1;\n";
+print "\t\t} else if (cmp < 0)\n";
+print "\t\t\thi = mid - 1;\n";
+print "\t\telse\n";
+print "\t\t\tlo = mid + 1;\n";
+print "\t}\n\n";
+print "\treturn CSS_PROP_UNKNOWN;\n";
+print "}\n\n";
+
+print "static inline const char *\n";
+print "css_prop_to_str(css_prop prop)\n";
+print "{\n";
+print "\tswitch (prop) {\n";
+print "\tcase CSS_PROP_UNKNOWN: return \"unknown\";\n";
+for my $prop (@props) {
+ (my $enum = uc($prop)) =~ s/-/_/g;
+ printf "\tcase CSS_PROP_%s: return \"%s\";\n", $enum, $prop;
+}
+print "\tdefault: return \"unknown\";\n";
+print "\t}\n";
+print "}\n\n";
+
+print "#endif /* CSSPROPS_H */\n";
diff --git a/gentags.pl b/gentags.pl
index 6c5b58b..0dfa283 100644
--- a/gentags.pl
+++ b/gentags.pl
@@ -3,7 +3,7 @@ use strict;
use warnings;
my @tags = sort grep { /\S/ } map { chomp; $_ } <STDIN>;
-
+my $n = scalar @tags;
my $maxlen = (sort { $b <=> $a } map { length($_) } @tags)[0];
print "/* Generated file, do not edit */\n\n";
@@ -19,6 +19,8 @@ for my $tag (@tags) {
}
print "} tag_type;\n\n";
+printf "#define TAGLEN %d\n\n", $n + 2;
+
print "static const struct {\n";
print "\tconst char *name;\n";
print "\ttag_type tag;\n";
@@ -28,8 +30,6 @@ for my $tag (@tags) {
}
print "};\n\n";
-my $n = scalar @tags;
-
print "static inline tag_type\n";
print "str_to_tag(const char *name, size_t len)\n";
print "{\n";
diff --git a/tags.h b/tags.h
index 4889686..b140ef3 100644
--- a/tags.h
+++ b/tags.h
@@ -26,6 +26,8 @@ typedef enum {
TAG_UL,
} tag_type;
+#define TAGLEN 18
+
static const struct {
const char *name;
tag_type tag;