]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
parse: Move parse function into an extra file
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Nov 2023 18:17:05 +0000 (18:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Nov 2023 18:17:05 +0000 (18:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/include/pakfire/parse.h [new file with mode: 0644]
src/libpakfire/os.c
src/libpakfire/parse.c [new file with mode: 0644]

index 465ab30fd9422b0ffbd7fad3734b33890bf4aea1..a113ea4ff76532fc309ab6a80b2d0dcaaac7cd09 100644 (file)
@@ -242,6 +242,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/packager.c \
        src/libpakfire/packagelist.c \
        src/libpakfire/pakfire.c \
+       src/libpakfire/parse.c \
        src/libpakfire/parser.c \
        src/libpakfire/path.c \
        src/libpakfire/problem.c \
@@ -289,6 +290,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/packager.h \
        src/libpakfire/include/pakfire/packagelist.h \
        src/libpakfire/include/pakfire/pakfire.h \
+       src/libpakfire/include/pakfire/parse.h \
        src/libpakfire/include/pakfire/parser.h \
        src/libpakfire/include/pakfire/path.h \
        src/libpakfire/include/pakfire/private.h \
diff --git a/src/libpakfire/include/pakfire/parse.h b/src/libpakfire/include/pakfire/parse.h
new file mode 100644 (file)
index 0000000..abf6121
--- /dev/null
@@ -0,0 +1,39 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_PARSE_H
+#define PAKFIRE_PARSE_H
+
+#ifdef PAKFIRE_PRIVATE
+
+/*
+       A callback that is called for each line that there is to parse.
+*/
+typedef int (*pakfire_parse_line)(char* line, size_t length, void* data);
+
+// Parses the given file
+int pakfire_parse_file(const char* path, pakfire_parse_line parse, void* data);
+
+// Splits a line into two parts separated by the given delimiter
+int pakfire_parse_split_line(char* line, size_t length, char** key, char** value, char delim);
+
+#endif /* PAKFIRE_PRIVATE */
+
+#endif /* PAKFIRE_PARSE_H */
index 2fa63e3604081b55c5694a94e453b199c3a46cd5..9bf8658610362276ec056c4657deb980425d3e3f 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
 #include <pakfire/os.h>
+#include <pakfire/parse.h>
 #include <pakfire/string.h>
 
-typedef int (*pakfire_parse_line)(char* line, size_t length, void* data);
-
-static int pakfire_parse_file(const char* path, pakfire_parse_line parse, void* data) {
-       FILE* f = NULL;
-       char* line = NULL;
-       size_t length = 0;
-       int r = 0;
-
-       ssize_t bytes_read = 0;
-
-       // Open the file
-       f = fopen(path, "r");
-       if (!f)
-               return -errno;
-
-       // Walk through the file line by line
-       for (;;) {
-               bytes_read = getline(&line, &length, f);
-               if (bytes_read < 0)
-                       break;
-
-               r = parse(line, length, data);
-               if (r)
-                       goto ERROR;
-       }
-
-ERROR:
-       if (line)
-               free(line);
-       if (f)
-               fclose(f);
-
-       return r;
-}
-
-static int pakfire_split_line(char* line, size_t length, char** key, char** value, char delim) {
-       char* k = line;
-       char* v = NULL;
-       char* p = NULL;
-
-       // Strip any trailing whitespace
-       pakfire_string_strip(line);
-
-       if (!*line)
-               return 0;
-
-       // Find the delimiter
-       p = strchr(line, delim);
-       if (!p)
-               return -EINVAL;
-
-       // The value starts somewhere after the delimiter
-       v = p + 1;
-
-       // Replace the delimiter by NULL
-       *p = '\0';
-
-       // Strip the key
-       pakfire_string_strip(k);
-
-       // The key is empty
-       if (!*k)
-               return -EINVAL;
-
-       // Strip the value
-       pakfire_string_strip(v);
-
-       // Return the pointers
-       *key   = k;
-       *value = v;
-
-       return 0;
-}
-
 // CPU Info
 
 static int pakfire_parse_cpuinfo(char* line, size_t length, void* data) {
@@ -114,7 +39,7 @@ static int pakfire_parse_cpuinfo(char* line, size_t length, void* data) {
        char* v = NULL;
 
        // Split the line
-       r = pakfire_split_line(line, length, &k, &v, ':');
+       r = pakfire_parse_split_line(line, length, &k, &v, ':');
        if (r)
                return r;
 
@@ -258,7 +183,7 @@ static int pakfire_parse_meminfo(char* line, size_t length, void* data) {
        char* v = NULL;
 
        // Split the line
-       r = pakfire_split_line(line, length, &k, &v, ':');
+       r = pakfire_parse_split_line(line, length, &k, &v, ':');
        if (r)
                return r;
 
@@ -337,7 +262,7 @@ static int pakfire_parse_distro(char* line, size_t length, void* data) {
        char* v = NULL;
 
        // Split the line
-       r = pakfire_split_line(line, length, &k, &v, '=');
+       r = pakfire_parse_split_line(line, length, &k, &v, '=');
        if (r)
                return r;
 
@@ -393,7 +318,7 @@ static int pidfd_parse_pid(char* line, size_t length, void* data) {
        char* v = NULL;
 
        // Split the line
-       r = pakfire_split_line(line, length, &k, &v, ':');
+       r = pakfire_parse_split_line(line, length, &k, &v, ':');
        if (r)
                return r;
 
diff --git a/src/libpakfire/parse.c b/src/libpakfire/parse.c
new file mode 100644 (file)
index 0000000..7745e9f
--- /dev/null
@@ -0,0 +1,100 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pakfire/parse.h>
+#include <pakfire/string.h>
+
+int pakfire_parse_file(const char* path, pakfire_parse_line parse, void* data) {
+       FILE* f = NULL;
+       char* line = NULL;
+       size_t length = 0;
+       int r = 0;
+
+       ssize_t bytes_read = 0;
+
+       // Open the file
+       f = fopen(path, "r");
+       if (!f)
+               return -errno;
+
+       // Walk through the file line by line
+       for (;;) {
+               bytes_read = getline(&line, &length, f);
+               if (bytes_read < 0)
+                       break;
+
+               r = parse(line, length, data);
+               if (r)
+                       goto ERROR;
+       }
+
+ERROR:
+       if (line)
+               free(line);
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
+int pakfire_parse_split_line(char* line, size_t length, char** key, char** value, char delim) {
+       char* k = line;
+       char* v = NULL;
+       char* p = NULL;
+
+       // Strip any trailing whitespace
+       pakfire_string_strip(line);
+
+       if (!*line)
+               return 0;
+
+       // Find the delimiter
+       p = strchr(line, delim);
+       if (!p)
+               return -EINVAL;
+
+       // The value starts somewhere after the delimiter
+       v = p + 1;
+
+       // Replace the delimiter by NULL
+       *p = '\0';
+
+       // Strip the key
+       pakfire_string_strip(k);
+
+       // The key is empty
+       if (!*k)
+               return -EINVAL;
+
+       // Strip the value
+       pakfire_string_strip(v);
+
+       // Return the pointers
+       *key   = k;
+       *value = v;
+
+       return 0;
+}