EasyCSV/include/easycsv.h
Pradana AUMARS 638af0080d In easycsv_print_columns/rows, update docs
Signed-off-by: Pradana AUMARS <paumars@courrier.dev>
2021-07-17 13:51:39 +02:00

367 lines
8.4 KiB
C

#ifndef EASYCSV_H
#define EASYCSV_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdint.h>
/*** Generic type definitions for internal use ***/
typedef uint32_t row_t; // max length of 2^32 - 1
typedef uint32_t column_t;
typedef char* rowstr; // non-const, to free it after usage
/*** Flags ***/
/** Flags to facilitate changing modes during file copying phases */
typedef enum {
EASYCSV_UNKNOWNMODE = 0,
EASYCSV_R,
EASYCSV_W
} EASYCSV_MODE;
/** Flags to parameterise the easycsv_insertvalue */
typedef enum {
EASYCSV_REPLACE, ///< (default) replaces word
EASYCSV_CONCAT, ///< concantates word at the end
EASYCSV_RCONCAT, ///< concantates word from the start
} EASYCSV_VALMODE;
/** Flags to parameterise sorting */
typedef enum {
EASYCSV_ALPHA, ///< sort alphabetically
EASYCSV_RALPHA, ///< sort reverse alphabetically
EASYCSV_NUMER, ///< sort numerically, from lowest value to highest
EASYCSV_RNUMER ///< sort from highest value to lowest
} EASYCSV_SORT;
/** Flags denoting cell type */
typedef enum {
EASYCSV_NONE,
EASYCSV_STRING,
EASYCSV_INT,
EASYCSV_FLOAT,
EASYCSV_UNKNOWNTYPE
} _EASYCSV_TYPE;
/*** Structures ***/
/**
* easycsv:
*
* A CSV file.
*/
typedef struct _easycsv easycsv;
struct _easycsv {
FILE *file; ///< original CSV file
FILE *temp; ///< temporary CSV file for writing
char *fp;
char *tmpfp;
unsigned int rows;
unsigned int cols;
// EASYCSV_ERRORMSG error;
EASYCSV_MODE mode;
};
/*** Constructors ***/
/**
* Initialise easycsv
* @param[in] relative path to CSV file
* @param[in] easycsv mode
* @return pointer to easycsv structure
*/
easycsv*
easycsv_init(const char*,
const EASYCSV_MODE);
/* /\** */
/* * Initialise easycsv structure with a given error message mode */
/* * @param[in] relative path to CSV file */
/* * @param[in] easycsv mode */
/* * @param[in] easycsv error message mode */
/* * @return pointer to easycsv structure */
/* *\/ */
/* easycsv* */
/* easycsv_init_errormsg(const char*, */
/* const EASYCSV_MODE, */
/* const EASYCSV_ERRORMSG); */
/**
* Destroy easycsv structure
* @param[in] Pointer to easycsv stucture
*/
void
easycsv_free(easycsv*);
/*** Error handling ***/
/**
* Get error message
* @return easycsv error message
*/
char*
easycsv_get_error();
/*** Access, find ***/
/**
* Find value and returns row and column
* @param[in] constant pointer to easycsv structure
* @param[in] value to find
* @param[out] pointer to column number, unchanged if error
* @param[out] pointer to row number, unchanged if error
* @return 0 if success, -1 if error
*/
int
easycsv_find_value(const easycsv*,
const char*,
unsigned int*,
unsigned int*);
/**
* Find number of instances of value
* @param[in] constant pointer to easycsv structure
* @param[in] value to find
* @return positive integer if success, -1 if error
*/
int
easycsv_find_num_value(const easycsv*,
const char*);
/*** Access, read ***/
/**
* Read string in a specific cell
* String is heap-allocated, it must be destroyed (free'd) manually
* @param[in] constant pointer to easycsv structure
* @param[in] row number
* @param[in] column number
* @return string value of cell, NULL if error
*/
char*
easycsv_read_value(const easycsv*,
unsigned int,
unsigned int);
/* /\** */
/* * Read string in a specific cell with named column in row 1 */
/* * @param[in] constant pointer to easycsv structure */
/* * @param[in] row value */
/* * @param[in] column number */
/* * @return string value of cell, NULL if empty cell */
/* *\/ */
/* char* */
/* easycsv_readcolumnvalue(const easycsv*, */
/* const char*, */
/* unsigned int); */
/*** Access, print ***/
/**
* Return number of rows
* @param[in] constant pointer to easycsv structure
* @return number of rows, -1 if error
*/
int
easycsv_print_rows(const easycsv*);
/**
* Return number of columns
* @param[in] constant pointer to easycsv structure
* @return number of columns, -1 if error
*/
int
easycsv_print_columns(const easycsv*);
/*** Modifications, sort ***/
/**
* Sort row
* @param[in] pointer to easycsv structure
* @param[in] row value
* @param[in] easycsv sort mode
* @return easycsv error code
*/
int
easycsv_sortrow(easycsv*,
const char*,
EASYCSV_SORT);
/**
* Sort column
* @param[in] pointer to easycsv structure
* @param[in] column value
* @param[in] easycsv sort mode
* @return easycsv error code
*/
int
easycsv_sortcolumn(easycsv*,
const char*,
EASYCSV_SORT);
/*** Modifications, insert ***/
/* /\** */
/* * Insert row into CSV file */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] cell value */
/* * @param[in] row number */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_insertrow(easycsv*, */
/* const char*, */
/* int); */
/**
* Insert string in a specific cell
* @param[in] pointer to easycsv structure
* @param[in] cell value
* @param[in] row number
* @param[in] column number
* @return easycsv error code
*/
int
easycsv_insertvalue(easycsv*,
const char*,
unsigned int,
unsigned int);
/* /\** */
/* * Insert string in a specific cell with specific mode */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] cell value */
/* * @param[in] row number */
/* * @param[in] column number */
/* * @param[in] easycsv value mode */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_insertvaluemode(easycsv*, */
/* const char*, */
/* unsigned int, */
/* unsigned int, */
/* EASYCSV_VALMODE); */
/* /\** */
/* * Insert string in a specific cell with named column in a given row */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] cell value */
/* * @param[in] row number */
/* * @param[in] column value */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_insertcolumnvalue(easycsv*, */
/* const char*, */
/* unsigned int, */
/* const char*); */
/* /\** */
/* * Insert string in a specific cell with named column in a given row with specific mode */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] cell value */
/* * @param[in] row number */
/* * @param[in] column value */
/* * @param[in] easycsv value mode */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_insertcolumnvaluemode(easycsv*, */
/* const char*, */
/* unsigned int, */
/* const char*, */
/* EASYCSV_VALMODE); */
/*** Modifications, push ***/
/* /\** */
/* * Create new column on the right-hand side of an existing one */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] column value */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_pushcolumn(easycsv*, */
/* const char*); */
/* /\** */
/* * Insert string in a vacant cell under a named column */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] column value */
/* * @param[in] cell value */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_pushcolumnvalue(easycsv*, */
/* const char*, */
/* const char*); */
/*** Modifications, delete ***/
/**
* Delete cell value
* @param[in] pointer to easycsv structure
* @param[in] row number
* @param[in] column number
* @return easycsv error code
*/
int
easycsv_deletevalue(easycsv*,
unsigned int,
unsigned int);
/* /\** */
/* * Delete numbered column */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] column number */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_deletecolumnint(easycsv*, */
/* unsigned int); */
/* /\** Delete named column */
/* * @param[in] pointer to easycsv structure */
/* * @param[in] column value */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_deletecolumnstr(easycsv*, */
/* const char*); */
/* Delete numered row */
/* Delete named row */
/*** Modifications, append ***/
/* /\** */
/* * Append two CSV files */
/* * @param[in] destination CSV file */
/* * @param[in] source CSV file */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_appendcsv(easycsv*, */
/* easycsv*); */
/*** Modifications, miscellenaeous ***/
/* /\** */
/* * Compress a CSV file */
/* * @param[in] pointer to easycsv structure */
/* * @return easycsv error code */
/* *\/ */
/* int */
/* easycsv_compress(easycsv*); */
#endif /* EASYCSV_H */