Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add wuffs_base__status__message
  • Loading branch information
nigeltao committed Dec 15, 2019
1 parent d1864b0 commit 355a95e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
4 changes: 4 additions & 0 deletions doc/note/statuses.md
Expand Up @@ -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
`'$'`.
4 changes: 2 additions & 2 deletions example/crc32/crc32.cc
Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions example/gifplayer/gifplayer.c
Expand Up @@ -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) {
Expand All @@ -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";
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions example/library/library.c
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions example/zcat/zcat.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions internal/cgen/base/core-public.h
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion internal/cgen/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions release/c/wuffs-unsupported-snapshot.c
Expand Up @@ -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.
Expand Down

0 comments on commit 355a95e

Please sign in to comment.