Skip to content

Commit

Permalink
Add hipack-cat tool
Browse files Browse the repository at this point in the history
  • Loading branch information
aperezdc committed Apr 1, 2015
1 parent 8768ec5 commit c02362c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,5 +3,6 @@
.*.sw[op]
*.gcda
*.gcno
tools/hipack-cat
tools/hipack-parse
tools/hipack-roundtrip
9 changes: 6 additions & 3 deletions Makefile
Expand Up @@ -13,6 +13,7 @@ hipack: ${hipack}
hipack-clean:
${RM} ${hipack} ${hipack_OBJS}
${RM} ${hipack_PATH}/tools/*.o \
${hipack_PATH}/tools/hipack-cat \
${hipack_PATH}/tools/hipack-parse \
${hipack_PATH}/tools/hipack-roundtrip

Expand All @@ -21,18 +22,20 @@ ${hipack}: ${hipack_OBJS}
${AR} rcu ${hipack} ${hipack_OBJS}

hipack-tools: \
${hipack_PATH}/tools/hipack-cat \
${hipack_PATH}/tools/hipack-parse \
${hipack_PATH}/tools/hipack-roundtrip

${hipack_PATH}/tools/hipack-cat: \
${hipack_PATH}/tools/hipack-cat.o ${hipack}

${hipack_PATH}/tools/hipack-parse: \
${hipack_PATH}/tools/hipack-parse.o ${hipack}

${hipack_PATH}/tools/hipack-roundtrip: \
${hipack_PATH}/tools/hipack-roundtrip.o ${hipack}

hipack-check: \
${hipack_PATH}/tools/hipack-parse \
${hipack_PATH}/tools/hipack-roundtrip
hipack-check: hipack-tools
@${hipack_PATH}/tools/run-tests

${hipack_PATH}/hipack-writer.o: ${hipack_PATH}/fpconv/src/fpconv.c
Expand Down
55 changes: 55 additions & 0 deletions tools/hipack-cat.c
@@ -0,0 +1,55 @@
/*
* hipack-cat.c
* Copyright (C) 2015 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/

#include "../hipack.h"
#include <stdlib.h>
#include <errno.h>


int
main (int argc, const char *argv[])
{
if (argc != 2) {
fprintf (stderr, "Usage: %s PATH\n", argv[0]);
return EXIT_FAILURE;
}

FILE *fp = fopen (argv[1], "rb");
if (!fp) {
fprintf (stderr, "%s: Cannot open '%s' (%s)\n",
argv[0], argv[1], strerror (errno));
return EXIT_FAILURE;
}

int retcode = EXIT_SUCCESS;
hipack_reader_t reader = {
.getchar = hipack_stdio_getchar,
.getchar_data = fp,
};
hipack_dict_t *message = hipack_read (&reader);
if (!message) {
assert (reader.error);
fprintf (stderr, "line %u, column %u: %s\n",
reader.error_line, reader.error_column,
(reader.error == HIPACK_READ_ERROR)
? strerror (errno) : reader.error);
retcode = EXIT_FAILURE;
goto cleanup;
}

hipack_writer_t writer = {
.putchar = hipack_stdio_putchar,
.putchar_data = stdout,
};
hipack_write (&writer, message);

cleanup:
fclose (fp);
hipack_dict_free (message);
return retcode;
}

0 comments on commit c02362c

Please sign in to comment.