From 355a95eeea6e50a4e883e4b7f47f8bc4f5c86f6d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 15 Dec 2019 05:13:15 +1100 Subject: [PATCH] Add wuffs_base__status__message --- doc/note/statuses.md | 4 ++++ example/crc32/crc32.cc | 4 ++-- example/gifplayer/gifplayer.c | 10 +++++----- example/library/library.c | 6 +++--- example/zcat/zcat.c | 6 +++--- internal/cgen/base/core-public.h | 11 +++++++++++ internal/cgen/data.go | 2 +- release/c/wuffs-unsupported-snapshot.c | 11 +++++++++++ 8 files changed, 40 insertions(+), 14 deletions(-) diff --git a/doc/note/statuses.md b/doc/note/statuses.md index 0a48cfc4..f7e2b59e 100644 --- a/doc/note/statuses.md +++ b/doc/note/statuses.md @@ -86,3 +86,7 @@ When that Wuffs code is compiled to C, it produces: const char* wuffs_deflate__error__bad_huffman_code = "#deflate: bad Huffman code"; ``` + +When printing a status message, the `wuffs_base__status__message` function will +advance a (non null) pointer by 1 byte, skipping that leading `'@'`, `'#'` or +`'$'`. diff --git a/example/crc32/crc32.cc b/example/crc32/crc32.cc index c1850d6f..92c964b2 100644 --- a/example/crc32/crc32.cc +++ b/example/crc32/crc32.cc @@ -58,9 +58,9 @@ uint8_t src_buffer[SRC_BUFFER_SIZE]; int main(int argc, char** argv) { wuffs_crc32__ieee_hasher h; - const char* status = h.initialize(sizeof h, WUFFS_VERSION, 0); + wuffs_base__status status = h.initialize(sizeof h, WUFFS_VERSION, 0); if (status) { - fprintf(stderr, "%s\n", status); + fprintf(stderr, "%s\n", wuffs_base__status__message(status)); return 1; } diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 19ccaded..1b787acb 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -295,7 +295,7 @@ const char* play() { wuffs_base__status status = wuffs_gif__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0); if (status) { - return status; + return wuffs_base__status__message(status); } if (quirk_honor_background_color_flag) { @@ -315,7 +315,7 @@ const char* play() { wuffs_base__image_config ic = {0}; status = wuffs_gif__decoder__decode_image_config(&dec, &ic, &src); if (status) { - return status; + return wuffs_base__status__message(status); } if (!wuffs_base__image_config__is_valid(&ic)) { return "invalid image configuration"; @@ -337,7 +337,7 @@ const char* play() { } status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, pixbuf); if (status) { - return status; + return wuffs_base__status__message(status); } memset(pixbuf.ptr, 0, pixbuf.len); } @@ -350,7 +350,7 @@ const char* play() { if (status == wuffs_base__warning__end_of_data) { break; } - return status; + return wuffs_base__status__message(status); } if (wuffs_base__frame_config__index(&fc) == 0) { @@ -423,7 +423,7 @@ const char* play() { // TODO: should a zero duration mean to show this frame forever? if (decode_frame_status) { - return decode_frame_status; + return wuffs_base__status__message(decode_frame_status); } } diff --git a/example/library/library.c b/example/library/library.c index f8e258a2..deb65305 100644 --- a/example/library/library.c +++ b/example/library/library.c @@ -98,19 +98,19 @@ static const char* decode() { if (!dec) { return "out of memory"; } - const char* status = wuffs_gzip__decoder__initialize( + wuffs_base__status status = wuffs_gzip__decoder__initialize( dec, sizeof__wuffs_gzip__decoder(), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED); if (status) { free(dec); - return status; + return wuffs_base__status__message(status); } status = wuffs_gzip__decoder__decode_io_writer( dec, &dst, &src, wuffs_base__make_slice_u8(work_buffer, WORK_BUFFER_SIZE)); if (status) { free(dec); - return status; + return wuffs_base__status__message(status); } fwrite(dst.data.ptr, sizeof(uint8_t), dst.meta.wi, stdout); free(dec); diff --git a/example/zcat/zcat.c b/example/zcat/zcat.c index 20d7fe41..87ab3ce2 100644 --- a/example/zcat/zcat.c +++ b/example/zcat/zcat.c @@ -85,10 +85,10 @@ static void ignore_return_value(int ignored) {} static const char* decode() { wuffs_gzip__decoder dec; - const char* status = + wuffs_base__status status = wuffs_gzip__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0); if (status) { - return status; + return wuffs_base__status__message(status); } wuffs_base__io_buffer dst; @@ -141,7 +141,7 @@ static const char* decode() { if (status == wuffs_base__suspension__short_write) { continue; } - return status; + return wuffs_base__status__message(status); } wuffs_base__io_buffer__compact(&src); diff --git a/internal/cgen/base/core-public.h b/internal/cgen/base/core-public.h index 67681e25..06fd96c2 100644 --- a/internal/cgen/base/core-public.h +++ b/internal/cgen/base/core-public.h @@ -164,6 +164,17 @@ wuffs_base__status__is_warning(wuffs_base__status z) { return z && (*z != '$') && (*z != '#'); } +// wuffs_base__status__message strips the leading '$', '#' or '@'. +static inline const char* // +wuffs_base__status__message(wuffs_base__status z) { + if (z) { + if ((*z == '$') || (*z == '#') || (*z == '@')) { + return z + 1; + } + } + return z; +} + // -------- // FourCC constants. diff --git a/internal/cgen/data.go b/internal/cgen/data.go index d097061d..639ca888 100644 --- a/internal/cgen/data.go +++ b/internal/cgen/data.go @@ -69,7 +69,7 @@ const baseCorePublicH = "" + "// --------\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__make_empty_struct() {\n wuffs_base__empty_struct ret;\n ret.private_impl = 0;\n return ret;\n}\n\n// wuffs_base__utility is" + " a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" + "" + - "// --------\n\n// See https://github.com/google/wuffs/blob/master/doc/note/statuses.md\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_complete(wuffs_base__status z) {\n return (z == NULL) || ((*z != '$') && (*z != '#'));\n}\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z == '#');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_warning(wuffs_base__status z) {\n return z && (*z != '$') && (*z != '#');\n}\n\n" + + "// --------\n\n// See https://github.com/google/wuffs/blob/master/doc/note/statuses.md\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_complete(wuffs_base__status z) {\n return (z == NULL) || ((*z != '$') && (*z != '#'));\n}\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z == '#');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_warning(wuffs_base__status z) {\n return z && (*z != '$') && (*z != '#');\n}\n\n// wuffs_base__status__message strips the leading '$', '#' or '@'.\nstatic inline const char* //\nwuffs_base__status__message(wuffs_base__status z) {\n if (z) {\n if ((*z == '$') || (*z == '#') || (*z == '@')) {\n return z + 1;\n }\n }\n return z;\n}\n\n" + "" + "// --------\n\n// FourCC constants.\n\n// !! INSERT FourCCs.\n\n" + "" + diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index cccf291c..19730676 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -206,6 +206,17 @@ wuffs_base__status__is_warning(wuffs_base__status z) { return z && (*z != '$') && (*z != '#'); } +// wuffs_base__status__message strips the leading '$', '#' or '@'. +static inline const char* // +wuffs_base__status__message(wuffs_base__status z) { + if (z) { + if ((*z == '$') || (*z == '#') || (*z == '@')) { + return z + 1; + } + } + return z; +} + // -------- // FourCC constants.