202 lines
5.6 KiB
C
202 lines
5.6 KiB
C
/*
|
|
libfort
|
|
|
|
MIT License
|
|
|
|
Copyright (c) 2017 - 2018 Seleznev Anton
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#ifndef LIBFORT_H
|
|
#define LIBFORT_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
|
|
/*
|
|
* Determine compiler
|
|
*/
|
|
#if defined(__clang__)
|
|
#define FORT_CLANG_COMPILER
|
|
#elif defined(__GNUC__)
|
|
#define FORT_GCC_COMPILER
|
|
#elif defined(_MSC_VER)
|
|
#define FORT_MICROSOFT_COMPILER
|
|
#else
|
|
#define FORT_UNDEFINED_COMPILER
|
|
#endif
|
|
|
|
/*
|
|
* Declare restrict
|
|
*/
|
|
#if defined(__cplusplus)
|
|
#if defined(FORT_CLANG_COMPILER)
|
|
#define FORT_RESTRICT __restrict__
|
|
#else
|
|
#define FORT_RESTRICT __restrict
|
|
#endif /* if defined(FORT_CLANG_COMPILER) */
|
|
#else
|
|
#define FORT_RESTRICT restrict
|
|
#endif /* if defined(__cplusplus) */
|
|
|
|
|
|
/*
|
|
* C++ needs to know that types and declarations are C, not C++.
|
|
*/
|
|
#ifdef __cplusplus
|
|
# define FORT_BEGIN_DECLS extern "C" {
|
|
# define FORT_END_DECLS }
|
|
#else
|
|
# define FORT_BEGIN_DECLS
|
|
# define FORT_END_DECLS
|
|
#endif
|
|
|
|
|
|
#ifndef FORT_EXTERN
|
|
#define FORT_EXTERN extern
|
|
#endif
|
|
|
|
/*
|
|
* libfort structures and functions declarations
|
|
*/
|
|
|
|
FORT_BEGIN_DECLS
|
|
|
|
struct fort_table;
|
|
typedef struct fort_table FTABLE;
|
|
|
|
FORT_EXTERN FTABLE * ft_create_table(void);
|
|
FORT_EXTERN void ft_destroy_table(FTABLE *FORT_RESTRICT table);
|
|
|
|
|
|
//FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...);
|
|
FORT_EXTERN int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...);
|
|
FORT_EXTERN int ft_row_printf(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTRICT fmt, ...);
|
|
//FORT_EXTERN int ft_cell_printf(FTABLE *FORT_RESTRICT table, size_t row, size_t col, const char* FORT_RESTRICT fmt, ...);
|
|
|
|
FORT_EXTERN int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
|
|
FORT_EXTERN int ft_write_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
|
|
|
|
#define FT_HDR_PRINTF(table, ...) \
|
|
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf(table, __VA_ARGS__))
|
|
#define FT_ROW_PRINTF(table, row, ...) \
|
|
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_row_printf(table, row, __VA_ARGS__))
|
|
|
|
|
|
FORT_EXTERN const char* ft_to_string(const FTABLE *FORT_RESTRICT table);
|
|
//FORT_EXTERN ssize_t ft_n_to_string(const FTABLE *FORT_RESTRICT table, char *FORT_RESTRICT dst, size_t dst_len);
|
|
|
|
//FORT_EXTERN char* ft_header_to_string(const FTABLE *FORT_RESTRICT table);
|
|
//FORT_EXTERN ssize_t ft_n_header_to_string(const FTABLE *FORT_RESTRICT table, char *FORT_RESTRICT dst, size_t dst_len);
|
|
|
|
//FORT_EXTERN char* ft_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row);
|
|
//FORT_EXTERN ssize_t ft_n_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row, char *FORT_RESTRICT dst, size_t dst_len);
|
|
|
|
|
|
|
|
enum TextAlignment
|
|
{
|
|
LeftAligned,
|
|
CenterAligned,
|
|
RightAligned
|
|
};
|
|
|
|
|
|
/*****************************************************************************
|
|
* TABLE BORDER
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
* TL TT TT TT TV TT TT TT TV TB TB TB TR <----- TopSeparator
|
|
* LL IV IV RR
|
|
* LH IH IH IH II IH IH IH II IH IH IH RH <----- InsideSeparator
|
|
* LL IV IV RR
|
|
* BL BB BB BB BV BB BB BB BV BB BB BB BR <----- BottomSeparator
|
|
*/
|
|
|
|
enum HorSeparatorPos
|
|
{
|
|
TopSeparator,
|
|
InsideSeparator,
|
|
BottomSeparator
|
|
};
|
|
|
|
enum BorderItemPos
|
|
{
|
|
TL_bip = 0,
|
|
TT_bip = 1,
|
|
TV_bip = 2,
|
|
TR_bip = 3,
|
|
|
|
LL_bip = 4,
|
|
IV_bip = 5,
|
|
RR_bip = 6,
|
|
|
|
LH_bip = 7,
|
|
IH_bip = 8,
|
|
II_bip = 9,
|
|
RH_bip = 10,
|
|
|
|
BL_bip = 11,
|
|
BB_bip = 12,
|
|
BV_bip = 13,
|
|
BR_bip = 14,
|
|
|
|
BorderItemPosSize
|
|
};
|
|
|
|
struct vector;
|
|
typedef struct vector vector_t;
|
|
|
|
struct fort_column_options
|
|
{
|
|
int col_min_width;
|
|
enum TextAlignment align;
|
|
};
|
|
typedef struct fort_column_options fort_column_options_t;
|
|
|
|
struct fort_table_options
|
|
{
|
|
int cell_padding_top;
|
|
int cell_padding_bottom;
|
|
int cell_padding_left;
|
|
int cell_padding_right;
|
|
int cell_empty_string_height;
|
|
|
|
char border_chars[BorderItemPosSize];
|
|
char header_border_chars[BorderItemPosSize];
|
|
vector_t *col_options;
|
|
|
|
};
|
|
typedef struct fort_table_options fort_table_options_t;
|
|
typedef fort_table_options_t context_t;
|
|
|
|
FORT_EXTERN int ft_set_default_options(const fort_table_options_t *options);
|
|
FORT_EXTERN int ft_get_default_options(fort_table_options_t *options);
|
|
FORT_EXTERN int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options);
|
|
FORT_EXTERN int ft_set_column_min_width(FTABLE * FORT_RESTRICT table, size_t column, size_t width);
|
|
FORT_EXTERN int ft_set_column_alignment(FTABLE * FORT_RESTRICT table, size_t column, enum TextAlignment align);
|
|
|
|
|
|
|
|
FORT_END_DECLS
|
|
|
|
#endif // LIBFORT_H
|