mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Generate C status code methods.
This commit is contained in:
1 parent
c5530859b9
commit
e87cdaa9f5
4 files changed
+90
No files matched your search
@@ -118,6 +118,8 @@ func (g *gen) generate() error {
|
||||
g.printf("puffs_%s_%s = %d%s,\n", g.pkgName, s, -2*i, nudge)
|
||||
}
|
||||
g.printf("} puffs_%s_status;\n\n", g.pkgName)
|
||||
g.printf("bool puffs_%s_status_is_error(puffs_%s_status s);\n\n", g.pkgName, g.pkgName)
|
||||
g.printf("const char* puffs_%s_status_string(puffs_%s_status s);\n\n", g.pkgName, g.pkgName)
|
||||
|
||||
g.writes("// ---------------- Public Structs\n\n")
|
||||
if err := g.forEachStruct(pubOnly, (*gen).writeStruct); err != nil {
|
||||
@@ -141,6 +143,27 @@ func (g *gen) generate() error {
|
||||
g.writes(baseImpl)
|
||||
g.writes("\n")
|
||||
|
||||
g.writes("// ---------------- Status Codes Implementations\n\n")
|
||||
g.printf("bool puffs_%s_status_is_error(puffs_%s_status s) {"+
|
||||
"return s & 1; }\n\n", g.pkgName, g.pkgName)
|
||||
g.printf("const char* puffs_%s_status_strings[%d] = {\n", g.pkgName, len(builtInStatuses))
|
||||
for _, s := range builtInStatuses {
|
||||
if strings.HasPrefix(s, "status_") {
|
||||
s = s[len("status_"):]
|
||||
} else if strings.HasPrefix(s, "error_") {
|
||||
s = s[len("error_"):]
|
||||
}
|
||||
s = g.pkgName + ": " + strings.Replace(s, "_", " ", -1)
|
||||
g.printf("%q,", s)
|
||||
}
|
||||
g.writes("};\n\n")
|
||||
g.printf("const char* puffs_%s_status_string(puffs_%s_status s) {\n", g.pkgName, g.pkgName)
|
||||
g.writes("s = -(s >> 1);")
|
||||
g.printf("if ((0 <= s) && (s < %d)) { return puffs_%s_status_strings[s]; }\n",
|
||||
len(builtInStatuses), g.pkgName)
|
||||
g.writes("return \"\";\n")
|
||||
g.writes("}\n\n")
|
||||
|
||||
g.writes("// ---------------- Private Structs\n\n")
|
||||
if err := g.forEachStruct(priOnly, (*gen).writeStruct); err != nil {
|
||||
return err
|
||||
|
||||
+28
@@ -64,6 +64,10 @@ typedef enum {
|
||||
puffs_gif_status_short_src = -12,
|
||||
} puffs_gif_status;
|
||||
|
||||
bool puffs_gif_status_is_error(puffs_gif_status s);
|
||||
|
||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||
|
||||
// ---------------- Public Structs
|
||||
|
||||
typedef struct {
|
||||
@@ -110,6 +114,30 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
|
||||
// ---------------- Status Codes Implementations
|
||||
|
||||
bool puffs_gif_status_is_error(puffs_gif_status s) {
|
||||
return s & 1;
|
||||
}
|
||||
|
||||
const char* puffs_gif_status_strings[7] = {
|
||||
"gif: ok",
|
||||
"gif: bad version",
|
||||
"gif: bad receiver",
|
||||
"gif: bad argument",
|
||||
"gif: constructor not called",
|
||||
"gif: short dst",
|
||||
"gif: short src",
|
||||
};
|
||||
|
||||
const char* puffs_gif_status_string(puffs_gif_status s) {
|
||||
s = -(s >> 1);
|
||||
if ((0 <= s) && (s < 7)) {
|
||||
return puffs_gif_status_strings[s];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// ---------------- Private Structs
|
||||
|
||||
// ---------------- Private Constructor and Destructor Prototypes
|
||||
|
||||
@@ -64,6 +64,10 @@ typedef enum {
|
||||
puffs_gif_status_short_src = -12,
|
||||
} puffs_gif_status;
|
||||
|
||||
bool puffs_gif_status_is_error(puffs_gif_status s);
|
||||
|
||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||
|
||||
// ---------------- Public Structs
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -80,6 +80,39 @@ cleanup0:
|
||||
puffs_gif_lzw_decoder_destructor(&dec);
|
||||
}
|
||||
|
||||
void test_status_is_error() {
|
||||
if (puffs_gif_status_is_error(puffs_gif_status_ok)) {
|
||||
FAIL("test_status_is_error: is_error(ok) returned true");
|
||||
return;
|
||||
}
|
||||
if (!puffs_gif_status_is_error(puffs_gif_error_bad_version)) {
|
||||
FAIL("test_status_is_error: is_error(bad_version) returned false");
|
||||
return;
|
||||
}
|
||||
if (puffs_gif_status_is_error(puffs_gif_status_short_dst)) {
|
||||
FAIL("test_status_is_error: is_error(short_dst) returned true");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void test_status_strings() {
|
||||
const char* s0 = puffs_gif_status_string(puffs_gif_status_ok);
|
||||
if (strcmp(s0, "gif: ok")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: ok\"", s0);
|
||||
return;
|
||||
}
|
||||
const char* s1 = puffs_gif_status_string(puffs_gif_error_bad_version);
|
||||
if (strcmp(s1, "gif: bad version")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: bad version\"", s1);
|
||||
return;
|
||||
}
|
||||
const char* s2 = puffs_gif_status_string(puffs_gif_status_short_dst);
|
||||
if (strcmp(s2, "gif: short dst")) {
|
||||
FAIL("test_status_strings: got \"%s\", want \"gif: short dst\"", s2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The empty comments forces clang-format to place one element per line.
|
||||
test tests[] = {
|
||||
test_constructor_not_called, //
|
||||
@@ -87,5 +120,7 @@ test tests[] = {
|
||||
test_bad_receiver, //
|
||||
test_puffs_version_bad, //
|
||||
test_puffs_version_good, //
|
||||
test_status_is_error, //
|
||||
test_status_strings, //
|
||||
NULL, //
|
||||
};
|
||||
Reference in new issue
Block a user