mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
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:
committed by
Jan Höppner
parent
37348ef662
commit
1df4d66387
@@ -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 @} */
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user