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) const EASYCSV_ERRORMSG error)
{ {
_easycsv *_priv = malloc(sizeof(_easycsv)); _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; _priv->error = error;
/* Open file according to mode */ /* Open file according to mode */
switch (mode) { switch (mode) {
case EASYCSV_R: case EASYCSV_R: {
case EASYCSV_W: {
_priv->file = fopen(fp, "r"); _priv->file = fopen(fp, "r");
break; break;
} }
case EASYCSV_W: {
_priv->file = fopen(fp, "w");
break;
}
default: { default: {
_easycsv_printerror(_priv, EASYCSV_UNKNOWNIOMODE); _easycsv_printerror(_priv, EASYCSV_UNKNOWNIOMODE);
_easycsv_priv_free(_priv); _easycsv_priv_free(_priv);
return NULL; return NULL;
} }
}
if (_priv->file == NULL) if (_priv->file == NULL)
{ {
@ -724,15 +737,12 @@ _easycsv_priv_init(const char *fp,
_priv->rows = _easycsv_rows(_priv, mode); _priv->rows = _easycsv_rows(_priv, mode);
_priv->cols = _easycsv_columns(_priv, mode); _priv->cols = _easycsv_columns(_priv, mode);
} }
else {
_priv->rows = 0;
_priv->cols = 0;
}
if (mode == EASYCSV_W) { if (mode == EASYCSV_W) {
/* csv->tmpfp = malloc(16 + stfp + 1); */ /* csv->tmpfp = malloc(16 + stfp + 1); */
/* strncpy(csv->tmpfp, prefix, 16); */ /* strncpy(csv->tmpfp, prefix, 16); */
/* strncat(csv->tmpfp, fp, stfp); */ /* strncat(csv->tmpfp, fp, stfp); */
do { do {
@ -785,7 +795,6 @@ _easycsv_priv_init(const char *fp,
} }
}
return _priv; return _priv;
} }