# 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)
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);
return -1;
else
n += ret;
+ p++;
} else {
if (putc(*p, fp) == EOF)
return -1;
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 <stdarg.h>
-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);
}
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, ...)
#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)
{
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];