diff --git a/examples/main.c b/examples/leave_comment.c similarity index 96% rename from examples/main.c rename to examples/leave_comment.c index bb756a7..f6b2206 100644 --- a/examples/main.c +++ b/examples/leave_comment.c @@ -1,5 +1,4 @@ /* Reads text from stdio and creates comment using unix fs driver in current directory */ -/* TODO: add path, displayname as arguments */ #include #include @@ -24,9 +23,11 @@ get_username() return ""; } -void usage() { +void +usage() +{ fprintf(stderr, "Simple utility to leave comment using unix_fs_driver\n"); - fprintf(stderr, "usage: [-p path] [-r reply_id] [-d displayname] [-i input_file]"); + fprintf(stderr, "usage: [-p path] [-r reply_id] [-d displayname] [-i input_file]\n"); fprintf(stderr, "-p path:\t path to comment storage \n"); fprintf(stderr, "-r reply_id:\t ID of comment you are replying to, 0 if yours is top-level\n"); fprintf(stderr, "-d displayname:\t Displayname to attach to comment\n"); diff --git a/examples/print_info.c b/examples/print_info.c new file mode 100644 index 0000000..8064828 --- /dev/null +++ b/examples/print_info.c @@ -0,0 +1,83 @@ +/* Simple utility to get human-readable comment data */ + +#include +#include +#include +#include +#include +#include + +#include "comment.h" +#include "driver.h" +#include "drivers/unix_fs/unix_fs_driver.h" + +#define BUF_SZ 4096 + +void +usage() +{ + fprintf(stderr, "Simple utility to get comment info\n"); + fprintf(stderr, "usage: [-p path] id\n"); + fprintf(stderr, "-p path: path to comment storage \n"); + fprintf(stderr, "id : id of comment \n"); +} + +int +main(int argc, char *argv[]) +{ + const Driver *driver = &unix_fs_driver; + char *path = "."; + + int id, opt; + + while ((opt = getopt(argc, argv, "p:")) != -1) { + switch (opt) { + case 'p': + path = optarg; + break; + default: + usage(); + exit(EXIT_FAILURE); + } + } + + if (optind >= argc) { + usage(); + exit(EXIT_FAILURE); + } + + id = atoi(argv[optind]); + + if (id <= 0) { + usage(); + exit(EXIT_FAILURE); + } + + CommentHeader *header = malloc(sizeof(CommentHeader)); + header->user_sid = malloc(BUF_SZ); + header->user_displayname = malloc(BUF_SZ); + + UnixFsDriverData driver_data = { path }; + + if (driver->get_header(&driver_data, header, id) < 0) { + fprintf(stderr, "failed to get header of comment, are you sure it exists?\n"); + exit(EXIT_FAILURE); + } + + printf("Got a valid comment header\n"); + printf("%-30s: %d\n", "Reply id", header->reply_id); + struct tm tm = *localtime(&header->creation_time); + printf("%-30s: %d-%02d-%02d %02d:%02d:%02d\n", "Creation time", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + printf("%-30s: %lu\n", "Text size in bytes", header->text_length); + printf("%-30s: %s\n", "User string ID", header->user_sid); + printf("%-30s: %s\n", "User displayname", header->user_displayname); + + char *text_buffer = malloc(header->text_length + 1); + if (driver->get_text(&driver_data, text_buffer, id) < 0) { + fprintf(stderr, "failed to get text of comment\n"); + exit(EXIT_FAILURE); + } + + printf("Comment text:\n"); + printf(text_buffer); +} diff --git a/src/drivers/unix_fs/unix_fs_driver.c b/src/drivers/unix_fs/unix_fs_driver.c index 7d91b10..b79a0c6 100644 --- a/src/drivers/unix_fs/unix_fs_driver.c +++ b/src/drivers/unix_fs/unix_fs_driver.c @@ -46,6 +46,8 @@ deserialize_header(int fd, CommentHeader *header) int bytes_read; size_t len, dst_idx, i; + i = 0; + dst_bufs[0] = header->user_sid; dst_bufs[1] = header->user_displayname; dst_idx = 0; @@ -56,9 +58,8 @@ deserialize_header(int fd, CommentHeader *header) while ((bytes_read = read(fd, buf, READ_BUF_SZ))) { for (; i < bytes_read; ++i) { - if (buf[i] != '\0') { - *(dst_bufs[dst_idx]++) = buf[i]; - } else { + *(dst_bufs[dst_idx]++) = buf[i]; + if (buf[i] == '\0') { dst_idx++; } }