Add a version of read_str which reads to a buffer instead of returning a new one.

This commit is contained in:
Jeffrey Townsend
2019-01-13 18:50:37 +00:00
parent d5ccafd718
commit 2d29c48c08
2 changed files with 45 additions and 0 deletions

View File

@@ -98,6 +98,30 @@ int onlp_file_vread_str(char** str, const char* fmt, va_list vargs);
*/
int onlp_file_read_str(char** str, const char* fmt, ...);
/**
* @brief Read and place the contents of the given file into the
* given buffer.
* @param dst Destination buffer
* @param size Max size
* @param fmt The filename format string
* @param vargs The filename format args.
* @note The contents of the file are assume to be a string.
* Trailing newlines are removed.
*/
int onlp_file_vread_str_dst(char* dst, int size, const char* fmt, va_list vargs);
/**
* @brief Read and place the contents of the given file into the
* given buffer.
* @param dst Destination buffer
* @param size Max size
* @param fmt The filename format string
* @param ... The filename format args.
* @note The contents of the file are assume to be a string.
* Trailing newlines are removed.
*/
int onlp_file_read_str_dst(char* dst, int size, const char* fmt, ...);
/**
* @brief Read and return the integer contents of the given file.
* @param value Receives the integer value.

View File

@@ -258,6 +258,27 @@ onlp_file_read_str(char** str, const char* fmt, ...)
return rv;
}
int
onlp_file_vread_str_dst(char* dst, int size, const char* fmt, va_list vargs)
{
char* s;
ONLP_TRY(onlp_file_vread_str(&s, fmt, vargs));
aim_strlcpy(dst, s, size);
aim_free(s);
return ONLP_STATUS_OK;
}
int
onlp_file_read_str_dst(char* dst, int size, const char* fmt, ...)
{
int rv;
va_list vargs;
va_start(vargs, fmt);
rv = onlp_file_vread_str_dst(dst, size, fmt, vargs);
va_end(vargs);
return rv;
}
int
onlp_file_vread_int(int* value, const char* fmt, va_list vargs)
{