libotutil: factor out utility to parse file by line
This will be used in the checkout CLI as well. Closes: #1442 Approved by: cgwalters
This commit is contained in:
1 parent
4a98a86b72
commit
5cba67520e
3 files changed
+37
-30
No files matched your search
@@ -221,3 +221,29 @@ ot_map_anonymous_tmpfile_from_content (GInputStream *instream,
|
||||
return NULL;
|
||||
return g_mapped_file_get_bytes (mfile);
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_parse_file_by_line (const char *path,
|
||||
gboolean (*cb)(const char*, void*, GError**),
|
||||
void *cbdata,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
g_autofree char *contents =
|
||||
glnx_file_get_contents_utf8_at (AT_FDCWD, path, NULL, cancellable, error);
|
||||
if (!contents)
|
||||
return FALSE;
|
||||
|
||||
g_auto(GStrv) lines = g_strsplit (contents, "\n", -1);
|
||||
for (char **iter = lines; iter && *iter; iter++)
|
||||
{
|
||||
/* skip empty lines at least */
|
||||
if (**iter == '\0')
|
||||
continue;
|
||||
|
||||
if (!cb (*iter, cbdata, error))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
@@ -90,4 +90,11 @@ ot_map_anonymous_tmpfile_from_content (GInputStream *instream,
|
||||
GBytes *ot_fd_readall_or_mmap (int fd, goffset offset,
|
||||
GError **error);
|
||||
|
||||
gboolean
|
||||
ot_parse_file_by_line (const char *path,
|
||||
gboolean (*cb)(const char*, void*, GError**),
|
||||
void *cbdata,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
Reference in new issue
Block a user