libutil: add util_file_read_va()

Takes a format string and parses a file accordingly and returns the
parsed values.

Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Sven Schnelle
2020-08-13 13:09:58 +02:00
committed by Jan Höppner
parent 37348ef662
commit 1df4d66387
2 changed files with 29 additions and 0 deletions

View File

@@ -25,4 +25,5 @@ int util_file_write_ll(long long val, int base, const char *fmt, ...);
int util_file_write_ul(unsigned long val, int base, const char *fmt, ...);
int util_file_write_ull(unsigned long long val, int base, const char *fmt, ...);
int util_file_read_va(const char *path, const char *fmt, ...);
#endif /** LIB_UTIL_FILE_H @} */

View File

@@ -508,3 +508,31 @@ int util_file_read_ull(unsigned long long *val, int base, const char *fmt, ...)
}
return (count == 1) ? 0 : -1;
}
/**
* Read a file and convert it according to format string
*
* @param[in] path File name to read
* @param[in] fmt Format string for parsing the content
* @param[out] ... Parameters for format string
*
* @retval != -1 Number of values parsed correctly
* @retval -1 Error while reading file
*/
int util_file_read_va(const char *path, const char *fmt, ...)
{
char buf[512];
va_list ap;
int ret;
if (file_gets(buf, sizeof(buf), path))
return -1;
va_start(ap, fmt);
ret = vsscanf(buf, fmt, ap);
va_end(ap);
if (ret == EOF)
return -1;
return ret;
}