Implement INSERT wuffs_base__status__string data

This commit is contained in:
Nigel Tao committed 2018-06-10 20:34:34 +10:00
1 parent 35af4c4841
commit 2ac2424e77
4 files changed
+98 -5

No files matched your search

+2 -1
View File
@@ -19,5 +19,6 @@
// !! INSERT wuffs_base__status__string data.
const char* wuffs_base__status__string(wuffs_base__status s) {
return "unknown status";
uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];
return o ? wuffs_base__status__string_data + o : "unknown status";
}
+35 -2
View File
@@ -83,8 +83,11 @@ func Do(args []string) error {
if err := expandBangBangInsert(&buf, baseImplC, map[string]func(*buffer) error{
"// !! INSERT base-public.h.\n": insertBasePublicH,
"// !! INSERT wuffs_base__status__string data.\n": func(b *buffer) error {
// TODO.
return nil
messages := [256]string{}
for _, z := range builtin.StatusList {
messages[uint8(z.Value)] = z.Message
}
return genStatusStringData(b, "wuffs_base__", &messages)
},
}); err != nil {
return nil, err
@@ -234,6 +237,36 @@ func insertBasePublicH(buf *buffer) error {
return nil
}
func genStatusStringData(b *buffer, pkgPrefix string, ss *[256]string) error {
data := []byte{0x00}
offsets := [256]uint16{}
for i, s := range ss {
if s == "" {
continue
}
if len(data) > 0xFFFF {
return errors.New("status string messages are too long")
}
offsets[i] = uint16(len(data))
data = append(data, s...)
data = append(data, 0x00)
}
b.printf("static const char %sstatus__string_data[] = {\n", pkgPrefix)
for _, x := range data {
b.printf("0x%02X,", x)
}
b.writes("};\n\n")
b.printf("static const uint16_t %sstatus__string_offsets[] = {\n", pkgPrefix)
for _, x := range offsets {
b.printf("0x%04X,", x)
}
b.writes("};\n\n")
return nil
}
type gen struct {
PKGPREFIX string // e.g. "WUFFS_JPEG__"
pkgPrefix string // e.g. "wuffs_jpeg__"
+1 -1
View File
@@ -123,7 +123,7 @@ const basePrivateH = "" +
""
const baseImplC = "" +
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! INSERT base-public.h.\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* wuffs_base__status__string(wuffs_base__status s) {\n return \"unknown status\";\n}\n" +
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! INSERT base-public.h.\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* wuffs_base__status__string(wuffs_base__status s) {\n uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];\n return o ? wuffs_base__status__string_data + o : \"unknown status\";\n}\n" +
""
type template_args_short_read struct {