Add support for R REprintf().

Added macro support for R function REprintf() instead of
fprintf(stderr).

Closes #273
This commit is contained in:
John McNamara 2021-04-20 20:57:45 +01:00
parent b9a98af00d
commit f8919af346
2 changed files with 44 additions and 33 deletions

View File

@ -223,8 +223,18 @@ enum lxw_custom_property_types {
#define LXW_SCHEMA_DOCUMENT LXW_SCHEMA_ROOT "/officeDocument/2006/relationships"
#define LXW_SCHEMA_CONTENT LXW_SCHEMA_ROOT "/package/2006/content-types"
/* Use REprintf() for error handling when compiled as an R library. */
#ifdef USE_R_LANG
#include <R.h>
#define LXW_PRINTF REprintf
#define LXW_STDERR
#else
#define LXW_PRINTF fprintf
#define LXW_STDERR stderr,
#endif
#define LXW_ERROR(message) \
fprintf(stderr, "[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__)
LXW_PRINTF(LXW_STDERR "[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__)
#define LXW_MEM_ERROR() \
LXW_ERROR("Memory allocation failed.")
@ -258,50 +268,50 @@ enum lxw_custom_property_types {
}
#define LXW_WARN(message) \
fprintf(stderr, "[WARNING]: " message "\n")
LXW_PRINTF(LXW_STDERR "[WARNING]: " message "\n")
/* We can't use variadic macros here since we support ANSI C. */
#define LXW_WARN_FORMAT(message) \
fprintf(stderr, "[WARNING]: " message "\n")
LXW_PRINTF(LXW_STDERR "[WARNING]: " message "\n")
#define LXW_WARN_FORMAT1(message, var) \
fprintf(stderr, "[WARNING]: " message "\n", var)
LXW_PRINTF(LXW_STDERR "[WARNING]: " message "\n", var)
#define LXW_WARN_FORMAT2(message, var1, var2) \
fprintf(stderr, "[WARNING]: " message "\n", var1, var2)
LXW_PRINTF(LXW_STDERR "[WARNING]: " message "\n", var1, var2)
/* Chart axis type checks. */
#define LXW_WARN_CAT_AXIS_ONLY(function) \
if (!axis->is_category) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINTF(LXW_STDERR "[WARNING]: " \
function "() is only valid for category axes\n"); \
return; \
}
#define LXW_WARN_VALUE_AXIS_ONLY(function) \
if (!axis->is_value) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINTF(LXW_STDERR "[WARNING]: " \
function "() is only valid for value axes\n"); \
return; \
}
#define LXW_WARN_DATE_AXIS_ONLY(function) \
if (!axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINTF(LXW_STDERR "[WARNING]: " \
function "() is only valid for date axes\n"); \
return; \
}
#define LXW_WARN_CAT_AND_DATE_AXIS_ONLY(function) \
if (!axis->is_category && !axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINTF(LXW_STDERR "[WARNING]: " \
function "() is only valid for category and date axes\n"); \
return; \
}
#define LXW_WARN_VALUE_AND_DATE_AXIS_ONLY(function) \
if (!axis->is_value && !axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINTF(LXW_STDERR "[WARNING]: " \
function "() is only valid for value and date axes\n"); \
return; \
}

View File

@ -2080,9 +2080,9 @@ workbook_close(lxw_workbook *self)
/* If the packager fails it is generally due to a zip permission error. */
if (packager == NULL) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Error creating '%s'. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Error creating '%s'. "
"System error = %s\n", self->filename, strerror(errno));
error = LXW_ERROR_CREATING_XLSX_FILE;
goto mem_error;
@ -2096,49 +2096,50 @@ workbook_close(lxw_workbook *self)
/* Error and non-error conditions fall through to the cleanup code. */
if (error == LXW_ERROR_CREATING_TMPFILE) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Error creating tmpfile(s) to assemble '%s'. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Error creating tmpfile(s) to assemble '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}
/* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zip. */
if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_ERRNO error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip ZIP_ERRNO error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}
/* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_PARAMETER_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}
/* If LXW_ERROR_ZIP_BAD_ZIP_FILE then errno is set by zip. */
if (error == LXW_ERROR_ZIP_BAD_ZIP_FILE) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
"This may require the use_zip64 option for large files. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
"This may require the use_zip64 option for large files. "
"System error = %s\n", self->filename, strerror(errno));
}
/* If LXW_ERROR_ZIP_INTERNAL_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_INTERNAL_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}
/* The next 2 error conditions don't set errno. */
if (error == LXW_ERROR_ZIP_FILE_ADD) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip error adding file to xlsx file '%s'.\n", self->filename);
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip error adding file to xlsx file '%s'.\n",
self->filename);
}
if (error == LXW_ERROR_ZIP_CLOSE) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip error closing xlsx file '%s'.\n", self->filename);
LXW_PRINTF(LXW_STDERR "[ERROR] workbook_close(): "
"Zip error closing xlsx file '%s'.\n", self->filename);
}
mem_error: