ext/xlsxwriter/libxlsxwriter/src/utility.c in xlsxwriter-0.2.1.pre.2 vs ext/xlsxwriter/libxlsxwriter/src/utility.c in xlsxwriter-0.2.2

- old
+ new

@@ -1,20 +1,21 @@ /***************************************************************************** * utility - Utility functions for libxlsxwriter. * * Used in conjunction with the libxlsxwriter library. * - * Copyright 2014-2019, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. + * Copyright 2014-2020, John McNamara, jmcnamara@cpan.org. See LICENSE.txt. * */ #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #include "xlsxwriter.h" +#include "xlsxwriter/common.h" #include "xlsxwriter/third_party/tmpfileplus.h" char *error_strings[LXW_MAX_ERRNO + 1] = { "No error.", "Memory error, failed to malloc() required memory.", @@ -25,23 +26,24 @@ "Zip error ZIP_PARAMERROR while creating the xlsx file.", "Zip error ZIP_BADZIPFILE (use_zip64 option may be required).", "Zip error ZIP_INTERNALERROR while creating the xlsx file.", "File error or unknown zip error when adding sub file to xlsx file.", "Unknown zip error when closing xlsx file.", + "Feature is not currently supported in this configuration.", "NULL function parameter ignored.", "Function parameter validation error.", "Worksheet name exceeds Excel's limit of 31 characters.", "Worksheet name cannot contain invalid characters: '[ ] : * ? / \\'", "Worksheet name cannot start or end with an apostrophe.", "Worksheet name is already in use.", - "Worksheet name 'History' is reserved by Excel.", "Parameter exceeds Excel's limit of 32 characters.", "Parameter exceeds Excel's limit of 128 characters.", "Parameter exceeds Excel's limit of 255 characters.", "String exceeds Excel's limit of 32,767 characters.", "Error finding internal string index.", "Worksheet row or column index out of range.", + "Maximum hyperlink length (2079) exceeded.", "Maximum number of worksheet URLs (65530) exceeded.", "Couldn't read image dimensions or DPI.", "Unknown error number." }; @@ -60,11 +62,11 @@ void lxw_col_to_name(char *col_name, lxw_col_t col_num, uint8_t absolute) { uint8_t pos = 0; size_t len; - uint8_t i; + size_t i; /* Change from 0 index to 1 index. */ col_num++; /* Convert the column number to a string in reverse order. */ @@ -310,14 +312,15 @@ else return -1; } /* - * Convert a lxw_datetime struct to an Excel serial date. + * Convert a lxw_datetime struct to an Excel serial date, with a 1900 + * or 1904 epoch. */ double -lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904) +lxw_datetime_to_excel_date_epoch(lxw_datetime *datetime, uint8_t date_1904) { int year = datetime->year; int month = datetime->month; int day = datetime->day; int hour = datetime->hour; @@ -405,10 +408,19 @@ days++; return days + seconds; } +/* + * Convert a lxw_datetime struct to an Excel serial date, for the 1900 epoch. + */ +double +lxw_datetime_to_excel_datetime(lxw_datetime *datetime) +{ + return lxw_datetime_to_excel_date_epoch(datetime, LXW_FALSE); +} + /* Simple strdup() implementation since it isn't ANSI C. */ char * lxw_strdup(const char *str) { size_t len; @@ -559,32 +571,41 @@ return 0; } #endif /* - * Retrieve runtime library version + * Retrieve runtime library version. */ const char * lxw_version(void) { return LXW_VERSION; } /* + * Retrieve runtime library version ID. + */ +uint16_t +lxw_version_id(void) +{ + return LXW_VERSION_ID; +} + +/* * Hash a worksheet password. Based on the algorithm provided by Daniel Rentz * of OpenOffice. */ uint16_t lxw_hash_password(const char *password) { size_t count; - uint8_t i; + size_t i; uint16_t hash = 0x0000; count = strlen(password); - for (i = 0; i < count; i++) { + for (i = 0; i < (uint8_t) count; i++) { uint32_t low_15; uint32_t high_15; uint32_t letter = password[i] << (i + 1); low_15 = letter & 0x7fff; @@ -598,5 +619,47 @@ hash ^= count; hash ^= 0xCE4B; return hash; } + +/* Make a simple portable version of fopen() for Windows. */ +#ifdef __MINGW32__ +#undef _WIN32 +#endif + +#ifdef _WIN32 + +#include <windows.h> + +FILE * +lxw_fopen(const char *filename, const char *mode) +{ + int n; + wchar_t wide_filename[_MAX_PATH + 1] = L""; + wchar_t wide_mode[_MAX_PATH + 1] = L""; + + n = MultiByteToWideChar(CP_UTF8, 0, filename, (int) strlen(filename), + wide_filename, _MAX_PATH); + + if (n == 0) { + LXW_ERROR("MultiByteToWideChar error: filename"); + return NULL; + } + + n = MultiByteToWideChar(CP_UTF8, 0, mode, (int) strlen(mode), + wide_mode, _MAX_PATH); + + if (n == 0) { + LXW_ERROR("MultiByteToWideChar error: mode"); + return NULL; + } + + return _wfopen(wide_filename, wide_mode); +} +#else +FILE * +lxw_fopen(const char *filename, const char *mode) +{ + return fopen(filename, mode); +} +#endif