utility.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * libxlsxwriter
  3. *
  4. * Copyright 2014-2018, John McNamara, [email protected]. See LICENSE.txt.
  5. */
  6. /**
  7. * @file utility.h
  8. *
  9. * @brief Utility functions for libxlsxwriter.
  10. *
  11. * <!-- Copyright 2014-2018, John McNamara, [email protected] -->
  12. *
  13. */
  14. #ifndef __LXW_UTILITY_H__
  15. #define __LXW_UTILITY_H__
  16. #include <stdint.h>
  17. #include "common.h"
  18. #include "xmlwriter.h"
  19. /**
  20. * @brief Convert an Excel `A1` cell string into a `(row, col)` pair.
  21. *
  22. * Convert an Excel `A1` cell string into a `(row, col)` pair.
  23. *
  24. * This is a little syntactic shortcut to help with worksheet layout:
  25. *
  26. * @code
  27. * worksheet_write_string(worksheet, CELL("A1"), "Foo", NULL);
  28. *
  29. * //Same as:
  30. * worksheet_write_string(worksheet, 0, 0, "Foo", NULL);
  31. * @endcode
  32. *
  33. * @note
  34. *
  35. * This macro shouldn't be used in performance critical situations since it
  36. * expands to two function calls.
  37. */
  38. #define CELL(cell) \
  39. lxw_name_to_row(cell), lxw_name_to_col(cell)
  40. /**
  41. * @brief Convert an Excel `A:B` column range into a `(col1, col2)` pair.
  42. *
  43. * Convert an Excel `A:B` column range into a `(col1, col2)` pair.
  44. *
  45. * This is a little syntactic shortcut to help with worksheet layout:
  46. *
  47. * @code
  48. * worksheet_set_column(worksheet, COLS("B:D"), 20, NULL, NULL);
  49. *
  50. * // Same as:
  51. * worksheet_set_column(worksheet, 1, 3, 20, NULL, NULL);
  52. * @endcode
  53. *
  54. */
  55. #define COLS(cols) \
  56. lxw_name_to_col(cols), lxw_name_to_col_2(cols)
  57. /**
  58. * @brief Convert an Excel `A1:B2` range into a `(first_row, first_col,
  59. * last_row, last_col)` sequence.
  60. *
  61. * Convert an Excel `A1:B2` range into a `(first_row, first_col, last_row,
  62. * last_col)` sequence.
  63. *
  64. * This is a little syntactic shortcut to help with worksheet layout.
  65. *
  66. * @code
  67. * worksheet_print_area(worksheet, 0, 0, 41, 10); // A1:K42.
  68. *
  69. * // Same as:
  70. * worksheet_print_area(worksheet, RANGE("A1:K42"));
  71. * @endcode
  72. */
  73. #define RANGE(range) \
  74. lxw_name_to_row(range), lxw_name_to_col(range), \
  75. lxw_name_to_row_2(range), lxw_name_to_col_2(range)
  76. /* *INDENT-OFF* */
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /* *INDENT-ON* */
  81. /**
  82. * @brief Converts a libxlsxwriter error number to a string.
  83. *
  84. * The `%lxw_strerror` function converts a libxlsxwriter error number defined
  85. * by #lxw_error to a pointer to a string description of the error.
  86. * Similar to the standard library strerror(3) function.
  87. *
  88. * For example:
  89. *
  90. * @code
  91. * lxw_error error = workbook_close(workbook);
  92. *
  93. * if (error)
  94. * printf("Error in workbook_close().\n"
  95. * "Error %d = %s\n", error, lxw_strerror(error));
  96. * @endcode
  97. *
  98. * This would produce output like the following if the target file wasn't
  99. * writable:
  100. *
  101. * Error in workbook_close().
  102. * Error 2 = Error creating output xlsx file. Usually a permissions error.
  103. *
  104. * @param error_num The error number returned by a libxlsxwriter function.
  105. *
  106. * @return A pointer to a statically allocated string. Do not free.
  107. */
  108. char *lxw_strerror(lxw_error error_num);
  109. /* Create a quoted version of the worksheet name */
  110. char *lxw_quote_sheetname(const char *str);
  111. void lxw_col_to_name(char *col_name, lxw_col_t col_num, uint8_t absolute);
  112. void lxw_rowcol_to_cell(char *cell_name, lxw_row_t row, lxw_col_t col);
  113. void lxw_rowcol_to_cell_abs(char *cell_name,
  114. lxw_row_t row,
  115. lxw_col_t col, uint8_t abs_row, uint8_t abs_col);
  116. void lxw_rowcol_to_range(char *range,
  117. lxw_row_t first_row, lxw_col_t first_col,
  118. lxw_row_t last_row, lxw_col_t last_col);
  119. void lxw_rowcol_to_range_abs(char *range,
  120. lxw_row_t first_row, lxw_col_t first_col,
  121. lxw_row_t last_row, lxw_col_t last_col);
  122. void lxw_rowcol_to_formula_abs(char *formula, const char *sheetname,
  123. lxw_row_t first_row, lxw_col_t first_col,
  124. lxw_row_t last_row, lxw_col_t last_col);
  125. uint32_t lxw_name_to_row(const char *row_str);
  126. uint16_t lxw_name_to_col(const char *col_str);
  127. uint32_t lxw_name_to_row_2(const char *row_str);
  128. uint16_t lxw_name_to_col_2(const char *col_str);
  129. double lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904);
  130. char *lxw_strdup(const char *str);
  131. char *lxw_strdup_formula(const char *formula);
  132. size_t lxw_utf8_strlen(const char *str);
  133. void lxw_str_tolower(char *str);
  134. FILE *lxw_tmpfile(char *tmpdir);
  135. /* Use a user defined function to format doubles in sprintf or else a simple
  136. * macro (the default). */
  137. #ifdef USE_DOUBLE_FUNCTION
  138. int lxw_sprintf_dbl(char *data, double number);
  139. #else
  140. #define lxw_sprintf_dbl(data, number) \
  141. lxw_snprintf(data, LXW_ATTR_32, "%.16g", number)
  142. #endif
  143. /* *INDENT-OFF* */
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. /* *INDENT-ON* */
  148. #endif /* __LXW_UTILITY_H__ */