Mercurial > hg > sidinfo
diff sidlib.c @ 128:2636185649c6
Add si_fread_str() helper function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 27 Oct 2016 11:58:52 +0300 |
parents | c1462b7880e8 |
children | c2d2369feadc |
line wrap: on
line diff
--- a/sidlib.c Thu Oct 27 11:52:58 2016 +0300 +++ b/sidlib.c Thu Oct 27 11:58:52 2016 +0300 @@ -26,6 +26,27 @@ } PSIDLibHdr; +BOOL si_fread_str(th_ioctx *ctx, char **str, const size_t len) +{ + char *tmp = th_malloc(len + 1); + if (tmp == NULL) + goto err; + + if (!thfread_str(ctx, tmp, len)) + goto err; + + tmp[len] = 0; + + *str = tmp; + + return TRUE; + +err: + th_free(tmp); + return FALSE; +} + + int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid) { PSIDHeader *psid = NULL; @@ -80,9 +101,9 @@ psid->isRSID = psid->magic[0] == 'R'; - if (!thfread_str(ctx, (uint8_t *) psid->sidName, PSID_STR_LEN) || - !thfread_str(ctx, (uint8_t *) psid->sidAuthor, PSID_STR_LEN) || - !thfread_str(ctx, (uint8_t *) psid->sidCopyright, PSID_STR_LEN)) + if (!si_fread_str(ctx, &psid->sidName, PSID_STR_LEN) || + !si_fread_str(ctx, &psid->sidAuthor, PSID_STR_LEN) || + !si_fread_str(ctx, &psid->sidCopyright, PSID_STR_LEN)) { th_io_error(ctx, ctx->status, "Error reading SID file header: %s.\n", @@ -90,10 +111,6 @@ goto error; } - psid->sidName[PSID_STR_LEN] = 0; - psid->sidAuthor[PSID_STR_LEN] = 0; - psid->sidCopyright[PSID_STR_LEN] = 0; - // Check if we need to load PSIDv2NG header ... if (psid->version >= 2) {