|
| 1 | +#include "test-tool.h" |
| 2 | +#include "git-compat-util.h" |
| 3 | +#include "strbuf.h" |
| 4 | +#include "gettext.h" |
| 5 | +#include "parse-options.h" |
| 6 | +#include "utf8.h" |
| 7 | + |
| 8 | +int cmd__iconv(int argc, const char **argv) |
| 9 | +{ |
| 10 | + struct strbuf buf = STRBUF_INIT; |
| 11 | + char *from = NULL, *to = NULL, *p; |
| 12 | + size_t len; |
| 13 | + int ret = 0; |
| 14 | + const char * const iconv_usage[] = { |
| 15 | + N_("test-helper --iconv [<options>]"), |
| 16 | + NULL |
| 17 | + }; |
| 18 | + struct option options[] = { |
| 19 | + OPT_STRING('f', "from-code", &from, "encoding", "from"), |
| 20 | + OPT_STRING('t', "to-code", &to, "encoding", "to"), |
| 21 | + OPT_END() |
| 22 | + }; |
| 23 | + |
| 24 | + argc = parse_options(argc, argv, NULL, options, |
| 25 | + iconv_usage, 0); |
| 26 | + |
| 27 | + if (argc > 1 || !from || !to) |
| 28 | + usage_with_options(iconv_usage, options); |
| 29 | + |
| 30 | + if (!argc) { |
| 31 | + if (strbuf_read(&buf, 0, 2048) < 0) |
| 32 | + die_errno("Could not read from stdin"); |
| 33 | + } else if (strbuf_read_file(&buf, argv[0], 2048) < 0) |
| 34 | + die_errno("Could not read from '%s'", argv[0]); |
| 35 | + |
| 36 | + p = reencode_string_len(buf.buf, buf.len, to, from, &len); |
| 37 | + if (!p) |
| 38 | + die_errno("Could not reencode"); |
| 39 | + if (write(1, p, len) < 0) |
| 40 | + ret = !!error_errno("Could not write %"PRIuMAX" bytes", |
| 41 | + (uintmax_t)len); |
| 42 | + |
| 43 | + strbuf_release(&buf); |
| 44 | + free(p); |
| 45 | + |
| 46 | + return ret; |
| 47 | +} |
0 commit comments