Skip to content

Commit

Permalink
support localized path for template file
Browse files Browse the repository at this point in the history
  • Loading branch information
rhash committed Apr 3, 2018
1 parent 36756a0 commit 04ba344
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
18 changes: 10 additions & 8 deletions parse_cmdline.c
Expand Up @@ -313,12 +313,13 @@ enum option_type_t
F_UFLG = 1, /* set a bit flag in a uint32_t field */
F_UENC = F_UFLG | F_OUTPUT_OPT, /* an encoding changing option */
F_CSTR = 2 | F_NEED_PARAM, /* store parameter as a C string */
F_TOUT = 3 | F_NEED_PARAM | F_OUTPUT_OPT,
F_VFNC = 4, /* just call a function */
F_PFNC = 5 | F_NEED_PARAM, /* process option parameter by calling a handler */
F_TFNC = 6 | F_NEED_PARAM, /* process option parameter by calling a handler */
F_UFNC = 7 | F_NEED_PARAM, /* pass UTF-8 encoded parameter to the handler */
F_PRNT = 8, /* print a constant C-string and exit */
F_TSTR = 3 | F_NEED_PARAM, /* store parameter as a tstr_t */
F_TOUT = 4 | F_NEED_PARAM | F_OUTPUT_OPT,
F_VFNC = 5, /* just call a function */
F_PFNC = 6 | F_NEED_PARAM, /* process option parameter by calling a handler */
F_TFNC = 7 | F_NEED_PARAM, /* process option parameter by calling a handler */
F_UFNC = 8 | F_NEED_PARAM, /* pass UTF-8 encoded parameter to the handler */
F_PRNT = 9, /* print a constant C-string and exit */
};

#define is_param_required(option_type) ((option_type) & F_NEED_PARAM)
Expand Down Expand Up @@ -374,7 +375,7 @@ cmdline_opt_t cmdline_opt[] =
{ F_UFLG, 'm', 0, "magnet", &opt.fmt, FMT_MAGNET },
{ F_UFLG, 0, 0, "uppercase", &opt.flags, OPT_UPPERCASE },
{ F_UFLG, 0, 0, "lowercase", &opt.flags, OPT_LOWERCASE },
{ F_CSTR, 0, 0, "template", &opt.template_file, 0 },
{ F_TSTR, 0, 0, "template", &opt.template_file, 0 },
{ F_CSTR, 'p', 0, "printf", &opt.printf_str, 0 },

/* other options */
Expand Down Expand Up @@ -465,7 +466,7 @@ static void apply_option(options_t *opts, parsed_option_t* option)
}

#ifdef _WIN32
if (option_type == F_TOUT || option_type == F_TFNC) {
if (option_type == F_TOUT || option_type == F_TFNC || option_type == F_TSTR) {
/* leave the value in UTF-16 */
value = (char*)rsh_wcsdup((wchar_t*)option->parameter);
}
Expand All @@ -489,6 +490,7 @@ static void apply_option(options_t *opts, parsed_option_t* option)
*(unsigned*)((char*)opts + ((char*)o->ptr - (char*)&opt)) |= o->param;
break;
case F_CSTR:
case F_TSTR:
case F_TOUT:
/* save the option parameter */
*(char**)((char*)opts + ((char*)o->ptr - (char*)&opt)) = value;
Expand Down
2 changes: 1 addition & 1 deletion parse_cmdline.h
Expand Up @@ -74,7 +74,7 @@ struct options_t
unsigned openssl_mask; /* bit-mask for enabled OpenSSL hash functions */
const char* config_file; /* config file path */
char* printf_str; /* printf-like format */
char* template_file; /* printf-like template file path */
opt_tchar* template_file; /* printf-like template file path */
opt_tchar* output; /* file to output calculation or checking results to */
opt_tchar* log; /* file to log percents and other info to */
char* embed_crc_delimiter;
Expand Down
16 changes: 11 additions & 5 deletions rhash_main.c
Expand Up @@ -136,13 +136,18 @@ static void ctrl_c_handler(int signum)
*/
static int load_printf_template(void)
{
FILE* fd = fopen(opt.template_file, "rb");
FILE* fd;
file_t file;
char buffer[8192];
size_t len;
int error = 0;

if (!fd) {
log_file_error(opt.template_file);
file_tinit(&file, opt.template_file, FILE_OPT_DONT_FREE_PATH);
fd = file_fopen(&file, FOpenRead | FOpenBin);
if (!fd)
{
log_file_t_error(&file);
file_cleanup(&file);
return 0;
}

Expand All @@ -154,17 +159,18 @@ static int load_printf_template(void)

rsh_str_append_n(rhash_data.template_text, buffer, len);
if (rhash_data.template_text->len >= MAX_TEMPLATE_SIZE) {
log_msg(_("%s: template file is too big\n"), opt.template_file);
log_msg(_("%s: template file is too big\n"), file_cpath(&file));
error = 1;
}
}

if (ferror(fd)) {
log_file_error(opt.template_file);
log_file_t_error(&file);
error = 1;
}

fclose(fd);
file_cleanup(&file);
rhash_data.printf_str = rhash_data.template_text->str;
return !error;
}
Expand Down

0 comments on commit 04ba344

Please sign in to comment.