New API cleanup.

This commit is contained in:
Håvard Pettersson 2015-04-10 00:08:40 +02:00
parent cacd200a5c
commit ed96d36711
7 changed files with 219 additions and 171 deletions

View file

@ -183,3 +183,36 @@ twc_hash_tox_id(const uint8_t *tox_id)
return hash;
}
/**
* Read an entire file into memory.
*
* @return TWC_RC_OK on success, TWC_RC_ERROR if file can not be opened, and
* TWC_RC_ERROR_MALLOC if an appropriate buffer can not be allocated.
*/
enum t_twc_rc
twc_read_file(const char *path, uint8_t **data, size_t *size)
{
FILE *file;
if (file = fopen(path, "r"))
{
// get file size
fseek(file, 0, SEEK_END);
*size = ftell(file);
rewind(file);
if (data = malloc(sizeof(*data) * *size))
{
fread(data, sizeof(uint8_t), *size, file);
fclose(file);
return TWC_RC_OK;
}
else
{
fclose(file);
return TWC_RC_ERROR_MALLOC;
}
}
return TWC_RC_ERROR;
}