Set all members to NULL or 0 manually and implement fopen under EASYCSV_W

This commit is contained in:
Pradana AUMARS 2021-06-29 22:56:55 +02:00
parent 4c65f21664
commit b2f9953f31

View File

@ -688,22 +688,35 @@ _easycsv_priv_init(const char *fp,
const EASYCSV_ERRORMSG error)
{
_easycsv *_priv = malloc(sizeof(_easycsv));
memset(_priv, 0, sizeof(*_priv)); // set all members to NULL or 0
_priv->file = NULL;
_priv->temp = NULL;
_priv->fp = NULL;
_priv->tmpfp = NULL;
_priv->rows = 0;
_priv->cols = 0;
#ifdef EASYCSV_DEBUG
_priv->start = 0;
#endif
_priv->error = error;
/* Open file according to mode */
switch (mode) {
case EASYCSV_R:
case EASYCSV_W: {
case EASYCSV_R: {
_priv->file = fopen(fp, "r");
break;
}
case EASYCSV_W: {
_priv->file = fopen(fp, "w");
break;
}
default: {
_easycsv_printerror(_priv, EASYCSV_UNKNOWNIOMODE);
_easycsv_priv_free(_priv);
return NULL;
}
}
if (_priv->file == NULL)
{
@ -724,17 +737,14 @@ _easycsv_priv_init(const char *fp,
_priv->rows = _easycsv_rows(_priv, mode);
_priv->cols = _easycsv_columns(_priv, mode);
}
else {
_priv->rows = 0;
_priv->cols = 0;
}
if (mode == EASYCSV_W) {
/* csv->tmpfp = malloc(16 + stfp + 1); */
/* strncpy(csv->tmpfp, prefix, 16); */
if (mode == EASYCSV_W) {
/* csv->tmpfp = malloc(16 + stfp + 1); */
/* strncpy(csv->tmpfp, prefix, 16); */
/* strncat(csv->tmpfp, fp, stfp); */
do {
/* Write to temporary file */
unsigned int i = 1;
@ -784,8 +794,7 @@ _easycsv_priv_init(const char *fp,
*/
}
}
return _priv;
}