struct encoding * get_path_encoding(const char *path, struct encoding *default_encoding) { const char *check_attr_argv[] = { "git", "check-attr", "encoding", "--", path, NULL }; char buf[SIZEOF_STR]; char *encoding; /* <path>: encoding: <encoding> */ if (!*path || !io_run_buf(check_attr_argv, buf, sizeof(buf)) || !(encoding = strstr(buf, ENCODING_SEP))) return default_encoding; encoding += STRING_SIZE(ENCODING_SEP); if (!strcmp(encoding, ENCODING_UTF8) || !strcmp(encoding, "unspecified") || !strcmp(encoding, "set")) { const char *file_argv[] = { "file", "-I", "--", path, NULL }; if (!*path || !io_run_buf(file_argv, buf, sizeof(buf)) || !(encoding = strstr(buf, CHARSET_SEP))) return default_encoding; encoding += STRING_SIZE(CHARSET_SEP); } return encoding_open(encoding); }
static enum status_code parse_encoding(struct encoding **encoding_ref, const char *arg, bool priority) { char buf[SIZEOF_STR]; enum status_code code = parse_string(buf, arg, sizeof(buf)); if (code == SUCCESS) { struct encoding *encoding = *encoding_ref; if (encoding && !priority) return code; encoding = encoding_open(buf); if (encoding) *encoding_ref = encoding; } return code; }