libfort/src/fort_utils.h

244 lines
5.3 KiB
C
Raw Normal View History

2018-01-17 19:22:57 +01:00
#ifndef FORT_IMPL_H
#define FORT_IMPL_H
2018-04-17 19:14:50 +02:00
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS /* To disable warnings for unsafe functions */
#endif
2018-01-17 19:22:57 +01:00
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
2018-03-18 10:17:33 +01:00
#include "fort.h"
2018-01-17 19:22:57 +01:00
/* Define FT_INTERNAL to make internal libfort functions static
* in the result amalgamed source file.
*/
#ifdef FT_AMALGAMED_SOURCE
#define FT_INTERNAL static
#else
#define FT_INTERNAL
#endif /* FT_AMALGAMED_SORCE */
2018-04-17 19:14:50 +02:00
2018-04-16 20:01:45 +02:00
#define FORT_DEFAULT_COL_SEPARATOR '|'
extern char g_col_separator;
2018-01-17 19:22:57 +01:00
2018-05-02 16:55:29 +02:00
#define FORT_COL_SEPARATOR_LENGTH 1
2018-01-17 19:22:57 +01:00
#define FORT_UNUSED __attribute__((unused))
#define F_MALLOC fort_malloc
#define F_FREE fort_free
#define F_CALLOC fort_calloc
#define F_REALLOC fort_realloc
2018-01-17 19:22:57 +01:00
#define F_STRDUP fort_strdup
2018-03-05 19:08:14 +01:00
#define F_WCSDUP fort_wcsdup
2019-08-14 21:01:57 +02:00
/* @todo: replace with custom impl !!!*/
#define F_UTF8DUP utf8dup
2018-01-17 19:22:57 +01:00
#define F_CREATE(type) ((type *)F_CALLOC(sizeof(type), 1))
2019-01-01 19:43:21 +01:00
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
2018-01-17 19:22:57 +01:00
2019-08-14 21:01:57 +02:00
#define FT_NEWLINE "\n"
#define FT_SPACE " "
2018-01-17 19:22:57 +01:00
2019-08-27 14:04:54 +02:00
/*****************************************************************************
* DEFAULT_SIZES
* ***************************************************************************/
#define DEFAULT_STR_BUF_SIZE 1024
#define DEFAULT_VECTOR_CAPACITY 10
/*****************************************************************************
* DATA TYPES
* ***************************************************************************/
enum f_get_policy {
2019-08-27 13:27:19 +02:00
CREATE_ON_NULL,
DONT_CREATE_ON_NULL
2018-01-17 19:22:57 +01:00
};
2019-08-27 14:04:54 +02:00
enum f_bool {
2018-01-17 19:22:57 +01:00
F_FALSE = 0,
F_TRUE = 1
};
2019-08-27 14:04:54 +02:00
enum f_cell_type {
COMMON_CELL,
GROUP_MASTER_CELL,
GROUP_SLAVE_CELL
};
enum f_geometry_type {
VISIBLE_GEOMETRY,
INTERN_REPR_GEOMETRY
};
enum f_string_type {
2019-08-14 21:01:57 +02:00
CHAR_BUF,
#ifdef FT_HAVE_WCHAR
W_CHAR_BUF,
#endif /* FT_HAVE_WCHAR */
#ifdef FT_HAVE_UTF8
UTF8_BUF,
#endif /* FT_HAVE_WCHAR */
};
2019-08-27 14:04:54 +02:00
struct f_string_view {
2019-08-26 11:57:07 +02:00
union {
2019-08-26 12:35:24 +02:00
const char *cstr;
2019-08-26 11:57:07 +02:00
#ifdef FT_HAVE_WCHAR
2019-08-26 12:35:24 +02:00
const wchar_t *wstr;
2019-08-26 11:57:07 +02:00
#endif
#ifdef FT_HAVE_UTF8
2019-08-26 12:35:24 +02:00
const void *u8str;
2019-08-26 11:57:07 +02:00
#endif
2019-08-26 12:35:24 +02:00
const void *data;
2019-08-26 11:57:07 +02:00
} u;
2019-08-27 14:04:54 +02:00
enum f_string_type type;
2019-08-26 11:57:07 +02:00
};
2019-08-27 14:04:54 +02:00
typedef struct f_string_view f_string_view_t;
2019-08-26 11:57:07 +02:00
2019-08-14 21:01:57 +02:00
2018-05-05 21:34:45 +02:00
#define FT_STR_2_CAT_(arg1, arg2) \
2018-01-17 19:22:57 +01:00
arg1##arg2
2018-05-05 21:34:45 +02:00
#define FT_STR_2_CAT(arg1, arg2) \
FT_STR_2_CAT_(arg1, arg2)
2018-01-17 19:22:57 +01:00
#define UNIQUE_NAME_(prefix) \
2018-05-05 21:34:45 +02:00
FT_STR_2_CAT(prefix,__COUNTER__)
2018-01-17 19:22:57 +01:00
#define UNIQUE_NAME(prefix) \
UNIQUE_NAME_(prefix)
2019-08-27 14:04:54 +02:00
typedef int f_status;
2018-01-17 19:22:57 +01:00
2019-08-27 14:04:54 +02:00
struct f_table_properties;
struct f_row;
struct f_vector;
struct f_cell;
struct f_string_buffer;
struct f_separator {
2018-02-04 14:21:04 +01:00
int enabled;
};
2018-01-17 19:22:57 +01:00
2019-08-27 14:04:54 +02:00
typedef struct f_table_properties f_table_properties_t;
typedef struct f_vector f_vector_t;
typedef struct f_cell f_cell_t;
typedef struct f_string_buffer f_string_buffer_t;
typedef struct f_row f_row_t;
typedef struct f_separator f_separator_t;
struct f_context {
f_table_properties_t *table_properties;
2018-02-26 19:42:48 +01:00
size_t row;
size_t column;
};
2019-08-27 14:04:54 +02:00
typedef struct f_context f_context_t;
2018-05-02 16:55:29 +02:00
2019-08-27 14:04:54 +02:00
struct f_conv_context {
2019-08-25 09:01:47 +02:00
union {
char *buf;
2019-08-26 11:57:07 +02:00
#ifdef FT_HAVE_WCHAR
2019-08-25 09:01:47 +02:00
wchar_t *wbuf;
2019-08-27 14:04:54 +02:00
#endif
#ifdef FT_HAVE_UTF8
const void *u8str;
2019-08-26 11:57:07 +02:00
#endif
2019-08-25 09:09:34 +02:00
} u;
2019-08-14 21:01:57 +02:00
size_t raw_avail;
2019-08-27 14:04:54 +02:00
struct f_context *cntx;
enum f_string_type b_type;
2019-08-14 21:01:57 +02:00
};
2019-08-27 14:04:54 +02:00
typedef struct f_conv_context f_conv_context_t;
2019-08-14 21:01:57 +02:00
2018-01-17 19:22:57 +01:00
/*****************************************************************************
* LIBFORT helpers
*****************************************************************************/
extern void *(*fort_malloc)(size_t size);
extern void (*fort_free)(void *ptr);
extern void *(*fort_calloc)(size_t nmemb, size_t size);
extern void *(*fort_realloc)(void *ptr, size_t size);
2018-11-24 10:38:01 +01:00
FT_INTERNAL
void set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr));
2018-11-24 10:38:01 +01:00
FT_INTERNAL
2018-05-06 16:04:34 +02:00
char *fort_strdup(const char *str);
2018-11-24 10:38:01 +01:00
2019-08-27 14:04:54 +02:00
2018-11-24 10:38:01 +01:00
FT_INTERNAL
2019-08-27 14:04:54 +02:00
size_t number_of_columns_in_format_string(const f_string_view_t *fmt);
2018-11-24 10:38:01 +01:00
2019-08-26 11:57:07 +02:00
FT_INTERNAL
2019-08-27 14:04:54 +02:00
size_t number_of_columns_in_format_buffer(const f_string_buffer_t *fmt);
2019-08-26 11:57:07 +02:00
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-11-24 10:38:01 +01:00
FT_INTERNAL
2018-11-16 19:27:25 +01:00
wchar_t *fort_wcsdup(const wchar_t *str);
#endif
2018-11-24 10:38:01 +01:00
2019-08-27 14:04:54 +02:00
2019-08-26 11:24:09 +02:00
2018-11-24 10:38:01 +01:00
FT_INTERNAL
2019-08-27 14:04:54 +02:00
int print_n_strings(f_conv_context_t *cntx, size_t n, const char *str);
2019-08-14 21:01:57 +02:00
2018-11-24 10:38:01 +01:00
FT_INTERNAL
2019-08-27 14:04:54 +02:00
int ft_nprint(f_conv_context_t *cntx, const char *str, size_t strlen);
2019-08-14 21:01:57 +02:00
#ifdef FT_HAVE_WCHAR
FT_INTERNAL
2019-08-27 14:04:54 +02:00
int ft_nwprint(f_conv_context_t *cntx, const wchar_t *str, size_t strlen);
2019-08-14 21:01:57 +02:00
#endif /* FT_HAVE_WCHAR */
#ifdef FT_HAVE_UTF8
FT_INTERNAL
2019-08-27 14:04:54 +02:00
int ft_nu8print(f_conv_context_t *cntx, const void *beg, const void *end);
2019-08-14 21:01:57 +02:00
#endif /* FT_HAVE_UTF8 */
2018-03-05 19:08:14 +01:00
2019-08-14 21:01:57 +02:00
/*#define PRINT_DEBUG_INFO fprintf(stderr, "error in %s(%s:%d)\n", __FUNCTION__, __FILE__, __LINE__);*/
#define PRINT_DEBUG_INFO
#define FT_CHECK(statement) \
do { \
tmp = statement; \
if (tmp < 0) {\
PRINT_DEBUG_INFO \
goto clear; \
} \
} while(0)
2018-01-17 19:22:57 +01:00
2018-03-31 16:54:01 +02:00
#define CHCK_RSLT_ADD_TO_WRITTEN(statement) \
do { \
tmp = statement; \
if (tmp < 0) {\
2019-08-14 21:01:57 +02:00
PRINT_DEBUG_INFO \
2018-03-31 16:54:01 +02:00
goto clear; \
} \
2019-01-01 17:55:00 +01:00
written += (size_t)tmp; \
2018-03-31 16:54:01 +02:00
} while(0)
2018-11-10 07:58:21 +01:00
#define CHCK_RSLT_ADD_TO_INVISIBLE_WRITTEN(statement) \
do { \
tmp = statement; \
if (tmp < 0) {\
2019-08-14 21:01:57 +02:00
PRINT_DEBUG_INFO \
2018-11-10 07:58:21 +01:00
goto clear; \
} \
2019-01-01 17:55:00 +01:00
invisible_written += (size_t)tmp; \
2018-11-10 07:58:21 +01:00
} while(0)
2018-04-16 21:33:05 +02:00
#define CHECK_NOT_NEGATIVE(x) \
2019-01-01 19:43:21 +01:00
do { if ((x) < 0) goto fort_fail; } while (0)
2018-04-16 21:33:05 +02:00
2018-03-09 10:44:16 +01:00
#endif /* FORT_IMPL_H */