179 lines
4.0 KiB
C
179 lines
4.0 KiB
C
#ifndef _EASYCSV
|
|
#define _EASYCSV
|
|
|
|
#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>
|
|
|
|
#include "easycsv_error.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 for error messages */
|
|
typedef enum {
|
|
EASYCSV_ERRORSTDERR = 0,
|
|
EASYCSV_ERROROFF,
|
|
EASYCSV_ERRORSTDOUT
|
|
} EASYCSV_ERRORMSG;
|
|
|
|
/** 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;
|
|
|
|
/* Private easycsv members */
|
|
typedef 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;
|
|
#ifdef EASYCSV_DEBUG
|
|
clock_t start;
|
|
#endif
|
|
} _easycsv;
|
|
|
|
/* (Constructor) Initialise _easycsv */
|
|
_easycsv*
|
|
_easycsv_priv_init(const char*,
|
|
const EASYCSV_MODE,
|
|
const EASYCSV_ERRORMSG);
|
|
|
|
/* (Destructor) Free _easycsv memory */
|
|
void
|
|
_easycsv_priv_free(_easycsv*);
|
|
|
|
/* Verifies mode of file */
|
|
// int _easycsv_checkmode(struct easycsv*, const char);
|
|
|
|
/* Copies data from temp FILE to file FILE */
|
|
int
|
|
_easycsv_update(_easycsv*);
|
|
|
|
/* Rewind easycsv, checks for readability */
|
|
int
|
|
_easycsv_rewind(_easycsv*,
|
|
const EASYCSV_MODE);
|
|
|
|
/* Returns string of a specific row,
|
|
includes the '\n' character as well! */
|
|
char*
|
|
_easycsv_getrow(const _easycsv*,
|
|
const unsigned int);
|
|
|
|
/* Return column number of a named column */
|
|
int
|
|
_easycsv_getcolumn(const _easycsv*,
|
|
const char*);
|
|
|
|
/* Verifies the type of the value (eg: string or int) */
|
|
_EASYCSV_TYPE
|
|
_easycsv_checktype(const _easycsv*,
|
|
const int,
|
|
const int);
|
|
|
|
/* Verifies if there is a value or not */
|
|
int
|
|
_easycsv_checkifvalue(const _easycsv*,
|
|
const int,
|
|
const int);
|
|
|
|
/* Grab const char* in row */
|
|
char*
|
|
_easycsv_getvalueinrow(const _easycsv*,
|
|
const char*,
|
|
const unsigned int);
|
|
|
|
/* Returns char pointer to start of value in rowstr */
|
|
char*
|
|
_easycsv_setcharptovalue(const _easycsv*,
|
|
const char*,
|
|
const unsigned int);
|
|
|
|
/* Insert value in row in specific column */
|
|
char*
|
|
_easycsv_insertvalueinrow(const _easycsv*,
|
|
const char*,
|
|
const char*,
|
|
const unsigned int);
|
|
|
|
/* Calculate rows */
|
|
int
|
|
_easycsv_rows(_easycsv*,
|
|
const EASYCSV_MODE);
|
|
|
|
/* Calculate columns*/
|
|
int
|
|
_easycsv_columns(_easycsv*,
|
|
const EASYCSV_MODE);
|
|
|
|
void
|
|
_easycsv_printerror(const _easycsv*,
|
|
const EASYCSV_ERROR);
|
|
|
|
/* Print error from _easycsv struct in stderr */
|
|
void
|
|
_easycsv_geterror(const _easycsv*,
|
|
const EASYCSV_ERROR,
|
|
FILE*);
|
|
|
|
/* Check if easycsv* and const char* are NULL */
|
|
int
|
|
_easycsv_checkcsvandstring_one(const _easycsv*,
|
|
const char*);
|
|
|
|
/* Check if easycsv* and two const char* are NULL*/
|
|
int
|
|
_easycsv_checkcsvandstring_two(const _easycsv*,
|
|
const char*,
|
|
const char*);
|
|
|
|
/* Verifies if the string is not NULL or empty, returns 0 on success and -1 on failure */
|
|
// int _easycsv_checkstring(const char*);
|
|
|
|
/* Verifies if easycsv is not NULL or unallocated */
|
|
// int _easycsv_checkeasycsv(const struct easycsv*);
|
|
|
|
/* Verifies if int is */
|
|
// int _easycsv_checkunsigned(const int);
|
|
|
|
#endif
|