1 /*
2  * libxlsxwriter
3  *
4  * Copyright 2014-2016, John McNamara, jmcnamara@cpan.org. See LICENSE.txt.
5  */
6 
7 /**
8  * @file utility.h
9  *
10  * @brief Utility functions for libxlsxwriter.
11  *
12  * <!-- Copyright 2014-2016, John McNamara, jmcnamara@cpan.org -->
13  *
14  */
15 module xlsxwriter.utility;
16 
17 import std.stdio;
18 import core.stdc.stdint;
19 import xlsxwriter.common;
20 
21 extern(C):
22 /* Max col: $XFD\0 */
23 enum MAX_COL_NAME_LENGTH   = 5;
24 
25 /* Max cell: $XFWD$1048576\0 */
26 enum MAX_CELL_NAME_LENGTH = 14;
27 
28 /* Max range: $XFWD$1048576:$XFWD$1048576\0 */
29 enum MAX_CELL_RANGE_LENGTH = MAX_CELL_NAME_LENGTH * 2;
30 
31 enum EPOCH_1900            = 0;
32 enum EPOCH_1904            = 1;
33 
34 /**
35  * @brief Convert an Excel `A1` cell string into a `(row, col)` pair.
36  *
37  * Convert an Excel `A1` cell string into a `(row, col)` pair.
38  *
39  * This is a little syntactic shortcut to help with worksheet layout:
40  *
41  * @code
42  *      worksheet_write_string(worksheet, CELL("A1"), "Foo", NULL);
43  *
44  *      //Same as:
45  *      worksheet_write_string(worksheet, 0, 0,       "Foo", NULL);
46  * @endcode
47  *
48  * @note
49  *
50  * This macro shouldn't be used in performance critical situations since it
51  * expands to two function calls.
52  */
53 /* #define CELL(cell) \ */
54 /*     lxw_get_row(cell), lxw_get_col(cell) */
55 
56 auto CELL(string cellName)
57 {
58 	import std.typecons;
59 	import std..string : toStringz;
60 	auto s = cellName.toStringz();
61 	return tuple(lxw_name_to_row(s), lxw_name_to_col(s));
62 }
63 
64 /** **/
65 uint32_t lxw_name_to_row(const char *row_str);
66 uint32_t lxw_name_to_row_2(const char *row_str);
67 
68 /** **/
69 uint16_t lxw_name_to_col(const char *col_str);
70 uint16_t lxw_name_to_col_2(const char *col_str);
71 
72 
73 /**
74  * @brief Convert an Excel `A:B` column range into a `(col1, col2)` pair.
75  *
76  * Convert an Excel `A:B` column range into a `(col1, col2)` pair.
77  *
78  * This is a little syntactic shortcut to help with worksheet layout:
79  *
80  * @code
81  *     worksheet_set_column(worksheet, COLS("B:D"), 20, NULL, NULL);
82  *
83  *     // Same as:
84  *     worksheet_set_column(worksheet, 1, 3,        20, NULL, NULL);
85  * @endcode
86  *
87  */
88 /* #define COLS(cols) \ */
89 /*     lxw_get_col(cols), lxw_get_col_2(cols) */
90 auto COLS(string colName)
91 {
92 	import std.typecons;
93 	import std..string : toStringz;
94 	auto s = colName.toStringz();
95     return tuple(lxw_name_to_col(s), lxw_name_to_col_2(s));
96 }
97 
98 /**
99  * @brief Convert an Excel `A1:B2` range into a `(first_row, first_col,
100  *        last_row, last_col)` sequence.
101  *
102  * Convert an Excel `A1:B2` range into a `(first_row, first_col, last_row,
103  * last_col)` sequence.
104  *
105  * This is a little syntactic shortcut to help with worksheet layout.
106  *
107  * @code
108  *     worksheet_print_area(worksheet, 0, 0, 41, 10); // A1:K42.
109  *
110  *     // Same as:
111  *     worksheet_print_area(worksheet, RANGE("A1:K42"));
112  * @endcode
113  */
114 /* #define RANGE(range) \ */
115 /*     lxw_get_row(range), lxw_get_col(range), lxw_get_row_2(range), lxw_get_col_2(range) */
116 auto RANGE(string rangeName)
117 {
118 	import std.typecons;
119 	import std..string : toStringz;
120 	auto s = rangeName.toStringz();
121     return tuple(lxw_name_to_row(s), lxw_name_to_col(s),
122                  lxw_name_to_row_2(s), lxw_name_to_col_2(s));
123 }
124 
125 /** @brief Struct to represent a date and time in Excel.
126  *
127  * Struct to represent a date and time in Excel. See @ref working_with_dates.
128  */
129 struct lxw_datetime {
130 
131     /** Year     : 1900 - 9999 */
132     int year;
133     /** Month    : 1 - 12 */
134     int month;
135     /** Day      : 1 - 31 */
136     int day;
137     /** Hour     : 0 - 23 */
138     int hour;
139     /** Minute   : 0 - 59 */
140     int min;
141     /** Seconds  : 0 - 59.999 */
142     double sec;
143 }
144 
145 /* Create a quoted version of the worksheet name */
146 char *lxw_quote_sheetname(char *str);
147 
148 void lxw_col_to_name(char *col_name, int col_num, uint8_t absolute);
149 
150 void lxw_rowcol_to_cell(char *cell_name, int row, int col);
151 
152 void lxw_rowcol_to_cell_abs(char *cell_name,
153                             int row,
154                             int col, uint8_t abs_row, uint8_t abs_col);
155 
156 void lxw_range(char *range,
157                int first_row, int first_col, int last_row, int last_col);
158 
159 void lxw_range_abs(char *range,
160                    int first_row, int first_col, int last_row, int last_col);
161 
162 double lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904);
163 
164 char *lxw_strdup(const char *str);
165 
166 void lxw_str_tolower(char *str);
167 
168 FILE *lxw_tmpfile();
169 
170 char* lxw_strerror(lxw_error error_num);