X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Futil.c;h=0c00b2a6793e06cc27db70901bb228db59661e3f;hp=626fba49f17667ec3bfb9b0c114357b4274fd9f1;hb=34935d41624c903db230efbd5b0a1f37e7fdcc32;hpb=14baa6ae892debbaa18dba8119931580efd0e517 diff --git a/src/util.c b/src/util.c index 626fba49..0c00b2a6 100644 --- a/src/util.c +++ b/src/util.c @@ -48,18 +48,19 @@ # define strerror_r(errnum, buf, bufsize) strerror_s(buf, bufsize, errnum) #endif -static size_t utf16le_strlen(const utf16lechar *s) +size_t +utf16le_strlen(const utf16lechar *s) { const utf16lechar *p = s; - while (p) + while (*p) p++; - return (p - s) / sizeof(utf16lechar); + return (p - s) * sizeof(utf16lechar); } /* Handle %W for UTF16-LE printing and %U for UTF-8 printing. * - * WARNING: this is not yet done properly--- it's assumed that if the format - * string contains %W and/or %U, then it contains no other format specifiers. + * TODO: this is not yet done properly--- it's assumed that if the format string + * contains %W and/or %U, then it contains no other format specifiers. */ static int wimlib_vfprintf(FILE *fp, const char *format, va_list va) @@ -67,31 +68,31 @@ wimlib_vfprintf(FILE *fp, const char *format, va_list va) const char *p; for (p = format; *p; p++) - if (*p == '%' && ((*p + 1) == 'W' || *(p + 1) == 'U')) + if (*p == '%' && (*(p + 1) == 'W' || *(p + 1) == 'U')) goto special; return vfprintf(fp, format, va); special: ; int n = 0; for (p = format; *p; p++) { - if (*p == '%' && ((*p + 1) == 'W' || *(p + 1) == 'U')) { + if (*p == '%' && (*(p + 1) == 'W' || *(p + 1) == 'U')) { int ret; mbchar *mbs; - size_t mbs_len; + size_t mbs_nbytes; if (*(p + 1) == 'W') { utf16lechar *ucs = va_arg(va, utf16lechar*); size_t ucs_nbytes = utf16le_strlen(ucs); ret = utf16le_to_mbs(ucs, ucs_nbytes, - &mbs, &mbs_len); + &mbs, &mbs_nbytes); } else { utf8char *ucs = va_arg(va, utf8char*); size_t ucs_nbytes = strlen(ucs); ret = utf8_to_mbs(ucs, ucs_nbytes, - &mbs, &mbs_len); + &mbs, &mbs_nbytes); } if (ret) { - ret = fprintf(fp, "???"); + ret = fprintf(fp, "??????"); } else { ret = fprintf(fp, "%s", mbs); FREE(mbs); @@ -100,6 +101,7 @@ special: return -1; else n += ret; + p++; } else { if (putc(*p, fp) == EOF) return -1; @@ -133,17 +135,14 @@ wimlib_fprintf(FILE *fp, const char *format, ...) return ret; } -/* True if wimlib is to print an informational message when an error occurs. - * This can be turned off by calling wimlib_set_print_errors(false). */ -#ifdef ENABLE_ERROR_MESSAGES -#include -static bool wimlib_print_errors = false; - +#if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG) static void wimlib_vmsg(const char *tag, const char *format, va_list va, bool perror) { +#ifndef DEBUG if (wimlib_print_errors) { +#endif int errno_save = errno; fflush(stdout); fputs(tag, stderr); @@ -160,8 +159,17 @@ wimlib_vmsg(const char *tag, const char *format, } putc('\n', stderr); errno = errno_save; +#ifndef DEBUG } +#endif } +#endif + +/* True if wimlib is to print an informational message when an error occurs. + * This can be turned off by calling wimlib_set_print_errors(false). */ +#ifdef ENABLE_ERROR_MESSAGES +static bool wimlib_print_errors = false; + void wimlib_error(const char *format, ...) @@ -205,6 +213,21 @@ wimlib_warning_with_errno(const char *format, ...) #endif +#if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG) +void wimlib_debug(const char *file, int line, const char *func, + const char *format, ...) +{ + + va_list va; + char buf[strlen(file) + strlen(func) + 30]; + + sprintf(buf, "[%s %d] %s(): ", file, line, func); + va_start(va, format); + wimlib_vmsg(buf, format, va, false); + va_end(va); +} +#endif + WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages) { @@ -348,7 +371,7 @@ static const mbchar *error_strings[] = { WIMLIBAPI const mbchar * wimlib_get_error_string(enum wimlib_error_code code) { - if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML) + if (code < 0 || code >= ARRAY_LEN(error_strings)) return NULL; else return error_strings[code];