Add cmd/puffs-gen-c/base-impl.h.

Also rename preamble.h to base-header.h.
This commit is contained in:
Nigel Tao committed 2017-06-01 11:02:43 +10:00
1 parent 1841ed8a36
commit 930ff53ff6
7 files changed
+67 -38

No files matched your search

@@ -1,7 +1,7 @@
// After editing this file, run "go generate" in this directory.
#ifndef PUFFS_BASE_H
#define PUFFS_BASE_H
#ifndef PUFFS_BASE_HEADER_H
#define PUFFS_BASE_HEADER_H
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
@@ -26,7 +26,7 @@
//
// For now, the API and ABI are simply unstable and can change at any time.
//
// TODO: don't hard code this in preamble.h.
// TODO: don't hard code this in base-header.h.
#define PUFFS_VERSION (0x00001)
#endif // PUFFS_BASE_H
#endif // PUFFS_BASE_HEADER_H
+8
View File
@@ -0,0 +1,8 @@
// After editing this file, run "go generate" in this directory.
#ifndef PUFFS_BASE_IMPL_H
#define PUFFS_BASE_IMPL_H
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
#endif // PUFFS_BASE_IMPL_H
+7 -3
View File
@@ -5,7 +5,11 @@
package main
const preamble = "" +
"#ifndef PUFFS_BASE_H\n#define PUFFS_BASE_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in preamble.h.\n#define PUFFS_VERSION (0x00001)\n\n#endif // PUFF" +
"S_BASE_H\n" +
const baseHeader = "" +
"#ifndef PUFFS_BASE_HEADER_H\n#define PUFFS_BASE_HEADER_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define PUFFS_VERSION (0x00001)" +
"\n\n#endif // PUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#endif // PUFFS_BASE_IMPL_H\n" +
""
+30 -21
View File
@@ -5,7 +5,7 @@
package main
// gen.go converts preamble.h to data.go.
// gen.go converts base.* to data.go.
//
// Invoke it via "go generate".
@@ -27,18 +27,11 @@ func main() {
}
func main1() error {
const inFilename = "preamble.h"
in, err := ioutil.ReadFile(inFilename)
if err != nil {
return err
}
const afterEditing = "// After editing this file,"
if !bytes.HasPrefix(in, []byte(afterEditing)) {
return fmt.Errorf("%s's contents do not start with %q", inFilename, afterEditing)
}
if i := bytes.Index(in, []byte("\n\n")); i >= 0 {
in = in[i+2:]
files := []struct {
filename, varname string
}{
{"base-header.h", "baseHeader"},
{"base-impl.h", "baseImpl"},
}
out := &bytes.Buffer{}
@@ -49,16 +42,32 @@ func main1() error {
out.WriteString("\n")
out.WriteString("package main\n")
out.WriteString("\n")
out.WriteString("const preamble = \"\" +\n")
for len(in) > 0 {
s := in
if len(s) > columns {
s = s[:columns]
for _, f := range files {
in, err := ioutil.ReadFile(f.filename)
if err != nil {
return err
}
in = in[len(s):]
fmt.Fprintf(out, "%q +\n", s)
const afterEditing = "// After editing this file,"
if !bytes.HasPrefix(in, []byte(afterEditing)) {
return fmt.Errorf("%s's contents do not start with %q", f.filename, afterEditing)
}
if i := bytes.Index(in, []byte("\n\n")); i >= 0 {
in = in[i+2:]
}
fmt.Fprintf(out, "const %s = \"\" +\n", f.varname)
for len(in) > 0 {
s := in
if len(s) > columns {
s = s[:columns]
}
in = in[len(s):]
fmt.Fprintf(out, "%q +\n", s)
}
out.WriteString("\"\"\n\n")
}
out.WriteString("\"\"\n")
formatted, err := format.Source(out.Bytes())
if err != nil {
+3 -2
View File
@@ -45,9 +45,8 @@ func (gen) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error)
includeGuard := "PUFFS_" + strings.ToUpper(pkgName) + "_H"
fmt.Fprintf(b, "#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
// Write preamble.
fmt.Fprintf(b, "// Code generated by puffs-gen-c. DO NOT EDIT.\n\n")
b.WriteString(preamble)
b.WriteString(baseHeader)
b.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
b.WriteString("// ---------------- Status Codes\n\n")
@@ -82,6 +81,8 @@ func (gen) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error)
b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n")
fmt.Fprintf(b, "#endif // %s\n\n", includeGuard)
b.WriteString("// C HEADER ENDS HERE.\n\n")
b.WriteString(baseImpl)
b.WriteString("\n")
b.WriteString("// ---------------- Private Structs\n\n")
if err := forEachStruct(b, pkgName, m, files, priOnly, writeStruct); err != nil {
+11 -4
View File
@@ -3,8 +3,8 @@
// Code generated by puffs-gen-c. DO NOT EDIT.
#ifndef PUFFS_BASE_H
#define PUFFS_BASE_H
#ifndef PUFFS_BASE_HEADER_H
#define PUFFS_BASE_HEADER_H
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
@@ -29,10 +29,10 @@
//
// For now, the API and ABI are simply unstable and can change at any time.
//
// TODO: don't hard code this in preamble.h.
// TODO: don't hard code this in base-header.h.
#define PUFFS_VERSION (0x00001)
#endif // PUFFS_BASE_H
#endif // PUFFS_BASE_HEADER_H
#ifdef __cplusplus
extern "C" {
@@ -88,6 +88,13 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self);
// C HEADER ENDS HERE.
#ifndef PUFFS_BASE_IMPL_H
#define PUFFS_BASE_IMPL_H
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
#endif // PUFFS_BASE_IMPL_H
// ---------------- Private Structs
// ---------------- Private Constructor and Destructor Prototypes
+4 -4
View File
@@ -3,8 +3,8 @@
// Code generated by puffs-gen-c. DO NOT EDIT.
#ifndef PUFFS_BASE_H
#define PUFFS_BASE_H
#ifndef PUFFS_BASE_HEADER_H
#define PUFFS_BASE_HEADER_H
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
@@ -29,10 +29,10 @@
//
// For now, the API and ABI are simply unstable and can change at any time.
//
// TODO: don't hard code this in preamble.h.
// TODO: don't hard code this in base-header.h.
#define PUFFS_VERSION (0x00001)
#endif // PUFFS_BASE_H
#endif // PUFFS_BASE_HEADER_H
#ifdef __cplusplus
extern "C" {