libfort/lib/fort.h

889 lines
27 KiB
C
Raw Normal View History

2018-01-01 09:26:34 +01:00
/*
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.
*/
2018-05-05 17:38:45 +02:00
/**
* @file fort.h
* @brief Main header file describing libfort API.
*
2018-05-05 21:34:45 +02:00
* This file contains declarations of all libfort functions and macro
2018-05-05 17:38:45 +02:00
* definitions.
*/
2018-01-01 09:26:34 +01:00
#ifndef LIBFORT_H
#define LIBFORT_H
#include <stddef.h>
2018-01-01 19:45:01 +01:00
#include <stdlib.h>
2018-03-18 10:17:33 +01:00
#include <stdint.h>
#include <limits.h>
2018-01-01 09:26:34 +01:00
2018-04-16 20:01:45 +02:00
/*****************************************************************************
* Configuration
*****************************************************************************/
/**
* libfort configuration macros
* (to enable/disable some options this macros should be defined/undefined)
2018-01-01 09:26:34 +01:00
*/
2018-04-16 20:01:45 +02:00
/** #define FT_CONGIG_HAVE_WCHAR */
2018-05-05 17:38:45 +02:00
#if defined(FT_CONGIG_HAVE_WCHAR)
#define FT_HAVE_WCHAR
#endif
/*****************************************************************************
* RETURN CODES
*****************************************************************************/
typedef int fort_status_t;
#define FT_SUCCESS 0
#define FT_MEMORY_ERROR -1
#define FT_ERROR -2
#define FT_EINVAL -3
#define FT_IS_SUCCESS(arg) ((arg) >= 0)
#define FT_IS_ERROR(arg) ((arg) < 0)
/**
* @cond HELPER_MACROS
*/
2018-04-16 20:01:45 +02:00
/*****************************************************************************
* Determine compiler
*****************************************************************************/
2018-05-05 17:38:45 +02:00
2018-01-01 09:26:34 +01:00
#if defined(__clang__)
2018-03-14 19:30:27 +01:00
#define FT_CLANG_COMPILER
2018-01-01 09:26:34 +01:00
#elif defined(__GNUC__)
2018-03-14 19:30:27 +01:00
#define FT_GCC_COMPILER
2018-01-01 09:26:34 +01:00
#elif defined(_MSC_VER)
2018-03-14 19:30:27 +01:00
#define FT_MICROSOFT_COMPILER
2018-01-01 09:26:34 +01:00
#else
2018-03-14 19:30:27 +01:00
#define FT_UNDEFINED_COMPILER
2018-01-01 09:26:34 +01:00
#endif
2018-03-09 10:44:16 +01:00
2018-04-16 20:01:45 +02:00
/*****************************************************************************
* Declare inline
*****************************************************************************/
2018-05-05 17:38:45 +02:00
2018-03-09 10:44:16 +01:00
#if defined(__cplusplus)
2018-03-14 19:30:27 +01:00
#define FT_INLINE inline
2018-03-09 10:44:16 +01:00
#else
2018-03-14 19:30:27 +01:00
#define FT_INLINE __inline
2018-03-09 10:44:16 +01:00
#endif /* if defined(__cplusplus) */
2018-03-09 09:17:12 +01:00
2018-04-16 20:01:45 +02:00
/*****************************************************************************
* C++ needs to know that types and declarations are C, not C++.
*****************************************************************************/
2018-05-05 17:38:45 +02:00
2018-03-31 12:33:37 +02:00
#ifdef __cplusplus
# define FT_BEGIN_DECLS extern "C" {
# define FT_END_DECLS }
2018-01-01 09:26:34 +01:00
#else
2018-03-14 19:30:27 +01:00
# define FT_BEGIN_DECLS
# define FT_END_DECLS
2018-01-01 09:26:34 +01:00
#endif
2018-04-16 20:01:45 +02:00
/*****************************************************************************
* Helper macros
*****************************************************************************/
2018-02-27 19:53:00 +01:00
2018-05-05 21:34:45 +02:00
#define FT_STR_2_CAT_(arg1, arg2) \
arg1##arg2
2018-05-05 21:34:45 +02:00
#define FT_STR_2_CAT(arg1, arg2) \
FT_STR_2_CAT_(arg1, arg2)
2018-05-05 21:34:45 +02:00
/**
* @interanl
*/
2018-03-31 12:33:37 +02:00
static FT_INLINE int ft_check_if_string_helper(const char *str)
{
(void)str;
2018-01-22 19:31:03 +01:00
return 0;
}
2018-05-05 21:34:45 +02:00
/**
* @interanl
*/
2018-03-31 12:33:37 +02:00
static FT_INLINE int ft_check_if_wstring_helper(const wchar_t *str)
2018-03-05 19:08:14 +01:00
{
(void)str;
return 0;
}
2018-05-05 21:34:45 +02:00
#define FT_NARGS_IMPL_(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,N,...) N
#define FT_EXPAND_(x) x
2018-05-05 21:34:45 +02:00
#define FT_PP_NARG_(...) \
FT_EXPAND_(FT_NARGS_IMPL_(__VA_ARGS__,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))
#define FT_CHECK_IF_STR_32(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_31(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_31(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_30(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_30(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_29(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_29(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_28(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_28(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_27(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_27(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_26(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_26(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_25(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_25(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_24(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_24(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_23(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_23(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_22(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_22(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_21(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_21(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_20(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_20(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_19(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_19(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_18(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_18(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_17(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_17(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_16(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_16(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_15(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_15(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_14(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_14(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_13(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_13(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_12(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_12(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_11(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_11(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_10(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_10(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_9(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_9(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_8(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_8(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_7(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_7(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_6(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_6(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_5(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_5(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_4(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_4(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_3(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_3(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_2(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_2(checker,arg,...) (checker(arg),FT_EXPAND_(FT_CHECK_IF_STR_1(checker,__VA_ARGS__)))
#define FT_CHECK_IF_STR_1(checker,arg) (checker(arg))
#define FT_CHECK_IF_ARGS_ARE_STRINGS__(checker,func, ...) \
2018-04-01 10:36:52 +02:00
FT_EXPAND_(func(checker,__VA_ARGS__))
2018-05-05 21:34:45 +02:00
#define FT_CHECK_IF_ARGS_ARE_STRINGS_(checker,basis, n, ...) \
FT_CHECK_IF_ARGS_ARE_STRINGS__(checker,FT_STR_2_CAT_(basis, n), __VA_ARGS__)
#define FT_CHECK_IF_ARGS_ARE_STRINGS(...) \
FT_CHECK_IF_ARGS_ARE_STRINGS_(ft_check_if_string_helper,FT_CHECK_IF_STR_,FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__)
2018-03-14 19:30:27 +01:00
#ifdef FT_HAVE_WCHAR
2018-04-01 10:36:52 +02:00
#define CHECK_IF_ARGS_ARE_WSTRINGS(...) \
2018-05-05 21:34:45 +02:00
FT_CHECK_IF_ARGS_ARE_STRINGS_(ft_check_if_wstring_helper,FT_CHECK_IF_STR_,FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__)
2018-03-05 19:08:14 +01:00
#endif
2018-02-27 19:53:00 +01:00
2018-05-05 17:38:45 +02:00
/**
* @endcond
*/
2018-04-16 20:01:45 +02:00
/*****************************************************************************
2018-05-05 17:38:45 +02:00
* Attribute format for argument checking
2018-04-16 20:01:45 +02:00
*****************************************************************************/
2018-05-05 17:38:45 +02:00
#if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER)
#define FT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check) \
__attribute__ ((format (printf, string_index, first_to_check)))
#else
#define FT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check)
#endif /* defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) */
/*****************************************************************************
* libfort API
*****************************************************************************/
2018-02-27 19:53:00 +01:00
2018-03-14 19:30:27 +01:00
FT_BEGIN_DECLS
2018-02-27 19:53:00 +01:00
2018-05-05 21:34:45 +02:00
/**
* The main structure of libfort containing information about formatted table.
*/
struct ft_table;
/**
* The main structure of libfort containing information about formatted table.
*
* ft_table_t objects should be created by a call to ft_create_table and
* destroyed with ft_destroy_table.
*/
typedef struct ft_table ft_table_t;
2018-02-27 19:53:00 +01:00
2018-04-01 10:36:52 +02:00
/**
* Create formatted table.
*
* @return
2018-05-05 21:34:45 +02:00
* The pointer to the new allocated ft_table_t, on success. NULL on error.
2018-04-01 10:36:52 +02:00
*/
2018-05-05 21:34:45 +02:00
ft_table_t *ft_create_table(void);
2018-02-27 19:53:00 +01:00
2018-04-01 10:36:52 +02:00
/**
* Destroy formatted table.
*
* Destroy formatted table and free all resources allocated during table creation
* and work with it.
*
* @param table
2018-04-16 20:01:45 +02:00
* Pointer to formatted table previousley created with ft_create_table. If
* table is a null pointer, the function does nothing.
2018-04-01 10:36:52 +02:00
*/
2018-05-05 21:34:45 +02:00
void ft_destroy_table(ft_table_t *table);
2018-11-02 22:16:20 +01:00
/**
* Copy formatted table.
*
* @param table
* Pointer to formatted table previousley created with ft_create_table. If
* table is a null pointer, the function returns null.
* @return
* The pointer to the new allocated ft_table_t, on success. NULL on error.
*/
ft_table_t *ft_copy_table(ft_table_t *table);
2018-02-27 19:53:00 +01:00
2018-04-01 10:36:52 +02:00
/**
* Move current position to the first cell of the next line(row).
*
* @param table
* Pointer to formatted table.
*/
2018-05-05 21:34:45 +02:00
void ft_ln(ft_table_t *table);
2018-04-16 20:01:45 +02:00
/**
2018-04-22 20:42:22 +02:00
* Get row number of the current cell.
2018-04-16 20:01:45 +02:00
*
* @param table
* Pointer to formatted table.
* @return
2018-04-22 20:42:22 +02:00
* Row number of the current cell.
2018-04-16 20:01:45 +02:00
*/
2018-05-05 21:34:45 +02:00
size_t ft_cur_row(ft_table_t *table);
2018-04-16 20:01:45 +02:00
/**
2018-04-22 20:42:22 +02:00
* Get column number of the current cell.
2018-04-16 20:01:45 +02:00
*
* @param table
* Pointer to formatted table.
* @return
2018-04-22 20:42:22 +02:00
* Column number of the current cell.
2018-04-16 20:01:45 +02:00
*/
2018-05-05 21:34:45 +02:00
size_t ft_cur_col(ft_table_t *table);
2018-02-27 19:53:00 +01:00
2018-04-22 20:42:22 +02:00
/**
* Set current cell position.
*
* Current cell - cell that will be edited with all modifiing functions
* (ft_printf, ft_write ...).
*
* @param table
* Pointer to formatted table.
* @param row
* New row number for the current cell.
* @param col
* New row number for the current cell.
*/
2018-05-05 21:34:45 +02:00
void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col);
2018-04-22 20:42:22 +02:00
2018-02-27 19:53:00 +01:00
2018-03-14 19:30:27 +01:00
#if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER)
2018-04-16 20:01:45 +02:00
/**
2018-05-05 21:34:45 +02:00
* Write data formatted acording to the format string to a variety of table
2018-04-16 20:01:45 +02:00
* cells.
*
* @param table
* Pointer to formatted table.
* @param fmt
* Pointer to a null-terminated multibyte string specifying how to interpret
* the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell
* separator.
* @param ...
* Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type
* expected by the corresponding conversion specifier, or if there are fewer
* arguments than required by format, the behavior is undefined. If there are
* more arguments than required by format, the extraneous arguments are
* evaluated and ignored.
* @return
2018-04-22 20:42:22 +02:00
* - Number of printed cells
* - (<0): In case of error
2018-04-16 20:01:45 +02:00
*/
2018-05-05 21:34:45 +02:00
int ft_printf(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
2018-04-16 20:01:45 +02:00
/**
2018-05-05 21:34:45 +02:00
* Write data formatted acording to the format string to a variety of table
* cells and move current position to the first cell of the next line(row).
2018-04-16 20:01:45 +02:00
*
* @param table
* Pointer to formatted table.
* @param fmt
* Pointer to a null-terminated multibyte string specifying how to interpret
* the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell
* separator.
* @param ...
* Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type
* expected by the corresponding conversion specifier, or if there are fewer
* arguments than required by format, the behavior is undefined. If there are
* more arguments than required by format, the extraneous arguments are
* evaluated and ignored.
* @return
2018-04-22 20:42:22 +02:00
* - Number of printed cells.
* - (<0): In case of error.
2018-04-16 20:01:45 +02:00
*/
2018-05-05 21:34:45 +02:00
int ft_printf_ln(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
2018-02-27 19:53:00 +01:00
#else
2018-05-05 21:34:45 +02:00
/**
* @cond IGNORE_DOC
*/
int ft_printf_impl(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
int ft_printf_ln_impl(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
2018-02-27 19:53:00 +01:00
#define ft_printf(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_impl(table, __VA_ARGS__))
#define ft_printf_ln(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln_impl(table, __VA_ARGS__))
2018-05-05 21:34:45 +02:00
/**
* @endcond
*/
#endif
2018-02-27 19:53:00 +01:00
2018-05-05 21:34:45 +02:00
/**
* Write strings to the the table.
*
* Write specified strings to the same number of consecutive cells in the
* current row.
*
* @param table
* Pointer to formatted table.
* @param ...
* Strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-24 19:41:14 +02:00
#define ft_write(table, ...)\
2018-05-05 21:34:45 +02:00
(0 ? FT_CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite(table, FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__))
/**
* Write strings to the the table and go to the next line.
*
* Write specified strings to the same number of consecutive cells in the
* current row and move current position to the first cell of the next
* line(row).
*
* @param table
* Pointer to formatted table.
* @param ...
* Strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-24 19:41:14 +02:00
#define ft_write_ln(table, ...)\
2018-05-05 21:34:45 +02:00
(0 ? FT_CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite_ln(table, FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__))
2018-05-05 21:34:45 +02:00
/**
* Write specified number of strings to the the table.
*
* Write specified number of strings to the same number of consecutive cells in
* the current row.
*
* @note In most cases it is more preferable to use MACRO @ref ft_write instead
* of @ref ft_nwrite, which is more safe (@ref ft_write automatically counts the
* number of string arguments and at compile check that all passed arguments are
* strings).
*
* @param table
* Pointer to formatted table.
* @param count
* Number of strings to write.
* @param cell_content
* First string to write.
* @param ...
* Other strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_nwrite(ft_table_t *table, size_t count, const char *cell_content, ...);
/**
* Write specified number of strings to the the table and go to the next line.
*
* Write specified number of strings to the same number of consecutive cells
* in the current row and move current position to the first cell of the next
* line(row).
*
* @note In most cases it is more preferable to use MACRO @ref ft_write instead
* of @ref ft_nwrite, which is more safe (@ref ft_write automatically counts the
* number of string arguments and at compile check that all passed arguments are
* strings).
*
* @param table
* Pointer to formatted table.
* @param count
* Number of strings to write.
* @param cell_content
* First string to write.
* @param ...
* Other strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_nwrite_ln(ft_table_t *table, size_t count, const char *cell_content, ...);
2018-03-05 19:08:14 +01:00
2018-05-05 21:34:45 +02:00
/**
* Write strings from the array to the table.
*
* Write specified number of strings from the array to the same number of
* consecutive cells in the current row.
*
* @param table
* Pointer to formatted table.
* @param cols
* Number of elements in row_cells.
* @param row_cells
* Array of strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_row_write(ft_table_t *table, size_t cols, const char *row_cells[]);
2018-05-05 21:34:45 +02:00
/**
* Write strings from the array to the table and go to the next line.
*
* Write specified number of strings from the array to the same number of
* consecutive cells in the current row and move current position to the first
* cell of the next line(row).
*
* @param table
* Pointer to formatted table.
* @param cols
* Number of elements in row_cells.
* @param row_cells
* Array of strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_row_write_ln(ft_table_t *table, size_t cols, const char *row_cells[]);
/**
* Write strings from the 2D array to the table.
*
* Write specified number of strings from the 2D array to the formatted table.
*
* @param table
* Pointer to formatted table.
* @param rows
* Number of rows in the 2D array.
* @param cols
* Number of columns in the 2D array.
* @param table_cells
* 2D array of strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_table_write(ft_table_t *table, size_t rows, size_t cols, const char *table_cells[]);
/**
* Write strings from the 2D array to the table and go to the next line.
*
* Write specified number of strings from the 2D array to the formatted table
* and move current position to the first cell of the next line(row).
*
* @param table
* Pointer to formatted table.
* @param rows
* Number of rows in the 2D array.
* @param cols
* Number of columns in the 2D array.
* @param table_cells
* 2D array of strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
int ft_table_write_ln(ft_table_t *table, size_t rows, size_t cols, const char *table_cells[]);
2018-04-01 10:36:52 +02:00
/**
* Add separator after the current row.
*
* @param table
* Formatted table.
* @return
* - 0: Success; separator was added.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 10:36:52 +02:00
*/
2018-05-05 21:34:45 +02:00
int ft_add_separator(ft_table_t *table);
2018-04-01 10:36:52 +02:00
/**
* Convert table to string representation.
*
2018-05-05 21:34:45 +02:00
* ft_table_t has ownership of the returned pointer. So there is no need to
2018-04-01 10:36:52 +02:00
* free it. To take ownership user should explicitly copy the returned
2018-04-16 20:01:45 +02:00
* string with strdup or similar functions.
*
* Returned pointer may be later invalidated by:
2018-04-22 20:42:22 +02:00
* - Calling ft_destroy_table;
* - Other invocations of ft_to_string.
2018-04-01 10:36:52 +02:00
*
* @param table
* Formatted table.
* @return
2018-05-05 21:34:45 +02:00
* - The pointer to the string representation of formatted table, on success.
2018-07-22 21:52:55 +02:00
* - NULL on error.
2018-04-01 10:36:52 +02:00
*/
2018-05-05 21:34:45 +02:00
const char *ft_to_string(const ft_table_t *table);
2018-01-03 08:40:54 +01:00
2018-01-07 10:22:26 +01:00
2018-03-18 10:17:33 +01:00
2018-04-01 10:36:52 +02:00
2018-03-18 10:17:33 +01:00
2018-04-01 12:27:02 +02:00
2018-04-16 20:01:45 +02:00
/**
2018-04-22 20:42:22 +02:00
* Structure describing border appearance.
2018-04-16 20:01:45 +02:00
*/
2018-03-31 12:33:37 +02:00
struct ft_border_chars {
2018-05-02 20:16:41 +02:00
const char *top_border_ch;
const char *separator_ch;
const char *bottom_border_ch;
const char *side_border_ch;
const char *out_intersect_ch;
const char *in_intersect_ch;
2018-02-25 09:39:41 +01:00
};
2018-04-16 20:01:45 +02:00
/**
2018-04-22 20:42:22 +02:00
* Structure describing border style.
2018-04-16 20:01:45 +02:00
*/
2018-03-31 12:33:37 +02:00
struct ft_border_style {
2018-03-12 20:28:55 +01:00
struct ft_border_chars border_chs;
struct ft_border_chars header_border_chs;
2018-05-02 20:16:41 +02:00
const char *hor_separator_char;
2018-03-12 20:28:55 +01:00
};
2018-04-01 12:27:02 +02:00
/**
2018-05-05 17:38:45 +02:00
* @defgroup BasicStyles
* @name Built-in table border styles.
* @{
2018-04-01 12:27:02 +02:00
*/
2018-03-31 12:33:37 +02:00
extern struct ft_border_style *FT_BASIC_STYLE;
2018-05-07 22:25:45 +02:00
extern struct ft_border_style *FT_BASIC2_STYLE;
2018-03-31 12:33:37 +02:00
extern struct ft_border_style *FT_SIMPLE_STYLE;
extern struct ft_border_style *FT_PLAIN_STYLE;
extern struct ft_border_style *FT_DOT_STYLE;
extern struct ft_border_style *FT_EMPTY_STYLE;
2018-05-02 20:16:41 +02:00
extern struct ft_border_style *FT_SOLID_STYLE;
2018-05-04 20:25:29 +02:00
extern struct ft_border_style *FT_SOLID_ROUND_STYLE;
2018-11-16 18:35:56 +01:00
extern struct ft_border_style *FT_NICE_STYLE;
2018-05-02 20:16:41 +02:00
extern struct ft_border_style *FT_DOUBLE_STYLE;
2018-05-04 20:25:29 +02:00
extern struct ft_border_style *FT_DOUBLE2_STYLE;
extern struct ft_border_style *FT_BOLD_STYLE;
extern struct ft_border_style *FT_BOLD2_STYLE;
extern struct ft_border_style *FT_FRAME_STYLE;
2018-05-05 17:38:45 +02:00
/** @} */
2018-03-12 20:28:55 +01:00
2018-04-01 12:27:02 +02:00
/**
* Set default border style for all new formatted tables.
*
* @param style
2018-04-22 20:42:22 +02:00
* Pointer to border style.
2018-04-01 12:27:02 +02:00
* @return
* - 0: Success; default border style was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 12:27:02 +02:00
*/
2018-07-22 21:52:55 +02:00
int ft_set_default_border_style(const struct ft_border_style *style);
2018-04-01 12:27:02 +02:00
/**
* Set border style for the table.
*
* @param table
2018-05-05 21:34:45 +02:00
* A pointer to the ft_table_t structure.
2018-04-01 12:27:02 +02:00
* @param style
2018-04-22 20:42:22 +02:00
* Pointer to border style.
2018-04-01 12:27:02 +02:00
* @return
* - 0: Success; table border style was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 12:27:02 +02:00
*/
2018-07-22 21:52:55 +02:00
int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style);
2018-05-05 17:38:45 +02:00
2018-03-14 20:18:59 +01:00
2018-04-01 12:27:02 +02:00
/**
2018-05-05 17:38:45 +02:00
* @name Special macros to define cell position (row and column).
* @{
2018-04-01 12:27:02 +02:00
*/
2018-05-05 17:38:45 +02:00
#define FT_ANY_COLUMN (UINT_MAX) /**< Any column (can be used to refer to all cells in a row)*/
#define FT_CUR_COLUMN (UINT_MAX - 1) /**< Current column */
#define FT_ANY_ROW (UINT_MAX) /**< Any row (can be used to refer to all cells in a column)*/
#define FT_CUR_ROW (UINT_MAX - 1) /**< Current row */
/** @} */
2018-04-01 12:27:02 +02:00
/**
2018-11-03 21:50:30 +01:00
* @name Cell properties identifiers.
2018-05-05 17:38:45 +02:00
* @{
2018-04-01 12:27:02 +02:00
*/
2018-11-03 21:50:30 +01:00
#define FT_CPROP_MIN_WIDTH (0x01U << 0) /**< Minimum width */
#define FT_CPROP_TEXT_ALIGN (0x01U << 1) /**< Text alignment */
#define FT_CPROP_TOP_PADDING (0x01U << 2) /**< Top padding for cell content */
#define FT_CPROP_BOTTOM_PADDING (0x01U << 3) /**< Bottom padding for cell content */
#define FT_CPROP_LEFT_PADDING (0x01U << 4) /**< Left padding for cell content */
#define FT_CPROP_RIGHT_PADDING (0x01U << 5) /**< Right padding for cell content */
#define FT_CPROP_EMPTY_STR_HEIGHT (0x01U << 6) /**< Height of empty cell */
#define FT_CPROP_ROW_TYPE (0x01U << 7) /**< Row type */
2018-11-10 07:58:21 +01:00
#define FT_CPROP_CONT_FG_COLOR (0x01U << 8) /**< Cell content foreground text color */
#define FT_CPROP_CELL_BG_COLOR (0x01U << 9) /**< Cell background color */
#define FT_CPROP_CONT_BG_COLOR (0x01U << 10) /**< Cell content background color */
#define FT_CPROP_CELL_TEXT_STYLE (0x01U << 11) /**< Cell text style */
#define FT_CPROP_CONT_TEXT_STYLE (0x01U << 12) /**< Cell content text style */
2018-05-05 17:38:45 +02:00
/** @} */
2018-11-10 07:58:21 +01:00
/**
* @name Colors.
* @{
*/
#define FT_COLOR_DEFAULT 0
#define FT_COLOR_BLACK 1
#define FT_COLOR_RED 2
#define FT_COLOR_GREEN 3
#define FT_COLOR_YELLOW 4
#define FT_COLOR_BLUE 5
#define FT_COLOR_MAGENTA 6
#define FT_COLOR_CYAN 7
#define FT_COLOR_LIGHT_GRAY 8
#define FT_COLOR_DARK_GRAY 9
#define FT_COLOR_LIGHT_RED 10
#define FT_COLOR_LIGHT_GREEN 11
#define FT_COLOR_LIGHT_YELLOW 12
#define FT_COLOR_LIGHT_BLUE 13
#define FT_COLOR_LIGHT_MAGENTA 15
#define FT_COLOR_LIGHT_CYAN 16
#define FT_COLOR_LIGHT_WHYTE 17
/** @} */
/**
* @name Text styles.
* @{
*/
#define FT_TSTYLE_DEFAULT 0
#define FT_TSTYLE_BOLD 1
#define FT_TSTYLE_DIM 2
2018-11-14 20:50:36 +01:00
#define FT_TSTYLE_ITALIC 3
#define FT_TSTYLE_UNDERLINED 4
#define FT_TSTYLE_BLINK 5
#define FT_TSTYLE_INVERTED 6
#define FT_TSTYLE_HIDDEN 7
2018-11-10 07:58:21 +01:00
/** @} */
2018-04-01 12:27:02 +02:00
/**
2018-04-22 20:42:22 +02:00
* Alignment of cell content.
2018-04-01 12:27:02 +02:00
*/
enum ft_text_alignment {
2018-05-05 17:38:45 +02:00
FT_ALIGNED_LEFT = 0, /**< Align left */
FT_ALIGNED_CENTER, /**< Align center */
FT_ALIGNED_RIGHT /**< Align right */
2018-04-01 12:27:02 +02:00
};
/**
2018-05-05 17:38:45 +02:00
* Type of table row. Determines appearance of row.
2018-04-01 12:27:02 +02:00
*/
enum ft_row_type {
2018-05-05 17:38:45 +02:00
FT_ROW_COMMON = 0, /**< Common row */
FT_ROW_HEADER /**< Header row */
2018-04-01 12:27:02 +02:00
};
2018-04-01 12:40:56 +02:00
/**
2018-11-03 21:50:30 +01:00
* Set default cell property for all new formatted tables.
2018-04-01 12:40:56 +02:00
*
2018-11-03 21:50:30 +01:00
* @param property
* Cell property identifier.
2018-04-01 12:40:56 +02:00
* @param value
2018-11-03 21:50:30 +01:00
* Cell property value.
2018-04-01 12:40:56 +02:00
* @return
2018-11-03 21:50:30 +01:00
* - 0: Success; default cell property was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 12:40:56 +02:00
*/
2018-11-03 21:50:30 +01:00
int ft_set_default_cell_prop(uint32_t property, int value);
2018-04-01 12:40:56 +02:00
/**
2018-11-03 21:50:30 +01:00
* Set property for the specified cell of the table.
2018-04-01 12:40:56 +02:00
*
* @param table
2018-05-05 21:34:45 +02:00
* A pointer to the ft_table_t structure.
2018-04-01 12:40:56 +02:00
* @param row
2018-04-22 20:42:22 +02:00
* Cell row.
2018-04-01 12:40:56 +02:00
* @param col
2018-04-22 20:42:22 +02:00
* Cell column.
2018-11-03 21:50:30 +01:00
* @param property
* Cell property identifier.
2018-04-01 12:40:56 +02:00
* @param value
2018-11-03 21:50:30 +01:00
* Cell property value.
2018-04-01 12:40:56 +02:00
* @return
2018-11-03 21:50:30 +01:00
* - 0: Success; cell property was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 12:40:56 +02:00
*/
2018-11-03 21:50:30 +01:00
int ft_set_cell_prop(ft_table_t *table, size_t row, size_t col, uint32_t property, int value);
2018-05-05 17:38:45 +02:00
2018-03-25 10:11:08 +02:00
2018-04-01 12:27:02 +02:00
/**
2018-11-03 21:50:30 +01:00
* @name Table properties identifiers.
2018-05-05 17:38:45 +02:00
* @{
2018-04-01 12:27:02 +02:00
*/
2018-11-03 21:50:30 +01:00
#define FT_TPROP_LEFT_MARGIN (0x01U << 0)
#define FT_TPROP_TOP_MARGIN (0x01U << 1)
#define FT_TPROP_RIGHT_MARGIN (0x01U << 2)
#define FT_TPROP_BOTTOM_MARGIN (0x01U << 3)
2018-05-05 17:38:45 +02:00
/** @} */
2018-04-01 12:27:02 +02:00
2018-04-01 15:01:30 +02:00
/**
2018-11-03 21:50:30 +01:00
* Set default table property.
2018-04-01 15:01:30 +02:00
*
2018-11-03 21:50:30 +01:00
* @param property
* Table property identifier.
2018-04-01 15:01:30 +02:00
* @param value
2018-11-03 21:50:30 +01:00
* Table property value.
2018-04-01 15:01:30 +02:00
* @return
2018-11-03 21:50:30 +01:00
* - 0: Success; default table property was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 15:01:30 +02:00
*/
2018-11-03 21:50:30 +01:00
int ft_set_default_tbl_prop(uint32_t property, int value);
2018-04-01 15:01:30 +02:00
/**
2018-11-03 21:50:30 +01:00
* Set table property.
2018-04-01 15:01:30 +02:00
*
* @param table
2018-05-05 21:34:45 +02:00
* A pointer to the ft_table_t structure.
2018-11-03 21:50:30 +01:00
* @param property
* Table property identifier.
2018-04-01 15:01:30 +02:00
* @param value
2018-11-03 21:50:30 +01:00
* Table property value.
2018-04-01 15:01:30 +02:00
* @return
2018-11-03 21:50:30 +01:00
* - 0: Success; default table property was changed.
2018-05-01 18:59:45 +02:00
* - (<0): In case of error
2018-04-01 15:01:30 +02:00
*/
2018-11-03 21:50:30 +01:00
int ft_set_tbl_prop(ft_table_t *table, uint32_t property, int value);
2018-03-14 20:18:59 +01:00
2018-05-04 20:25:29 +02:00
/**
* Set column span for the specified cell of the table.
*
* @param table
2018-05-05 21:34:45 +02:00
* A pointer to the ft_table_t structure.
2018-05-04 20:25:29 +02:00
* @param row
* Cell row.
* @param col
* Cell column.
* @param hor_span
* Column span.
* @return
2018-11-03 21:50:30 +01:00
* - 0: Success; cell span was changed.
2018-05-04 20:25:29 +02:00
* - (<0): In case of error
*/
2018-05-05 21:34:45 +02:00
int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span);
2018-05-04 20:25:29 +02:00
2018-04-16 20:01:45 +02:00
/**
* Set functions for memory allocation and deallocation to be used instead of
* standard ones.
*
* @param f_malloc
* Pointer to a function for memory allocation that should be used instead of
* malloc.
* @param f_free
* Pointer to a function for memory deallocation that should be used instead
* of free.
* @note
* To return memory allocation/deallocation functions to their standard values
* set f_malloc and f_free to NULL.
*/
2018-05-05 17:38:45 +02:00
void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr));
2018-03-14 20:18:59 +01:00
2018-05-02 16:55:29 +02:00
2018-03-14 20:18:59 +01:00
#ifdef FT_HAVE_WCHAR
2018-05-05 21:34:45 +02:00
int ft_wprintf(ft_table_t *table, const wchar_t *fmt, ...);
int ft_wprintf_ln(ft_table_t *table, const wchar_t *fmt, ...);
2018-04-24 20:36:07 +02:00
2018-04-24 19:41:14 +02:00
#define ft_wwrite(table, ...)\
2018-05-05 21:34:45 +02:00
(0 ? CHECK_IF_ARGS_ARE_WSTRINGS(__VA_ARGS__) : ft_nwwrite(table, FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__))
2018-04-24 19:41:14 +02:00
#define ft_wwrite_ln(table, ...)\
2018-05-05 21:34:45 +02:00
(0 ? CHECK_IF_ARGS_ARE_WSTRINGS(__VA_ARGS__) : ft_nwwrite_ln(table, FT_PP_NARG_(__VA_ARGS__), __VA_ARGS__))
int ft_nwwrite(ft_table_t *table, size_t n, const wchar_t *cell_content, ...);
int ft_nwwrite_ln(ft_table_t *table, size_t n, const wchar_t *cell_content, ...);
2018-03-14 20:18:59 +01:00
2018-05-05 21:34:45 +02:00
int ft_row_wwrite(ft_table_t *table, size_t cols, const wchar_t *row_cells[]);
int ft_row_wwrite_ln(ft_table_t *table, size_t cols, const wchar_t *row_cells[]);
2018-03-14 20:18:59 +01:00
2018-05-05 21:34:45 +02:00
int ft_table_wwrite(ft_table_t *table, size_t rows, size_t cols, const wchar_t *table_cells[]);
int ft_table_wwrite_ln(ft_table_t *table, size_t rows, size_t cols, const wchar_t *table_cells[]);
2018-04-24 19:41:14 +02:00
2018-05-05 21:34:45 +02:00
const wchar_t *ft_to_wstring(const ft_table_t *table);
2018-03-14 20:18:59 +01:00
#endif
2018-02-25 09:39:41 +01:00
2018-02-25 19:43:20 +01:00
2018-02-25 09:39:41 +01:00
2018-03-14 19:30:27 +01:00
FT_END_DECLS
2018-01-01 09:26:34 +01:00
2018-03-09 10:44:16 +01:00
#endif /* LIBFORT_H */