]> git.ipfire.org Git - fireperf.git/commitdiff
util: Move some helper functions into an extra module
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 12:03:12 +0000 (12:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 12:03:12 +0000 (12:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/server.c
src/util.c [new file with mode: 0644]
src/util.h [new file with mode: 0644]

index bafa5356c6fcdf398ecf4148392232dddea5e737..9503f4ec5b8bbce8dfabf92384ea5cf93afbaa29 100644 (file)
@@ -67,7 +67,9 @@ fireperf_SOURCES = \
        src/main.c \
        src/main.h \
        src/server.c \
-       src/server.h
+       src/server.h \
+       src/util.c \
+       src/util.h
 
 # ------------------------------------------------------------------------------
 
index b119c800ab9b02506c7482ae8082e3c3288b213e..adf5fe76e9f6b1cf3a1f4c533a7fbc9184319249 100644 (file)
@@ -29,6 +29,7 @@
 #include "logging.h"
 #include "main.h"
 #include "server.h"
+#include "util.h"
 
 #define SOCKET_BACKLOG   1024
 
@@ -43,81 +44,6 @@ struct fireperf_server_stats {
        size_t total_bytes_received;
 };
 
-enum {
-       FIREPERF_FORMAT_BYTES,
-       FIREPERF_FORMAT_BITS,
-};
-
-const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
-const char* suffixes_bits[]  = { "b", "Kb", "Mb", "Gb", "Tb", NULL };
-
-static char* format_size(ssize_t size, int unit) {
-       const char** suffixes;
-       unsigned int divisor;
-
-       switch (unit) {
-               case FIREPERF_FORMAT_BYTES:
-                       suffixes = suffixes_bytes;
-                       divisor = 1024;
-                       break;
-
-               case FIREPERF_FORMAT_BITS:
-                       suffixes = suffixes_bits;
-                       divisor = 1000;
-                       break;
-
-               // Invalid input
-               default:
-                       return NULL;
-       }
-
-       const char** suffix;
-       char* retval = NULL;
-
-       // Convert into double
-       double s = size;
-
-       for (suffix = suffixes; *suffix; suffix++) {
-               if (abs(s) < divisor)
-                       break;
-
-               s /= divisor;
-       }
-
-       if (!*suffix)
-               return NULL;
-
-       // Format the output string
-       int r = asprintf(&retval, "%.02f %s", s, *suffix);
-       if (r < 0)
-               return NULL;
-
-       return retval;
-}
-
-static char __timespec[20];
-
-static const char* format_timespec(const struct timespec* t) {
-       // Convert to local time
-       struct tm* tm = localtime(&t->tv_sec);
-
-       size_t s = strftime(__timespec, sizeof(__timespec), "%F %T", tm);
-
-       if (s)
-               return __timespec;
-
-       return NULL;
-}
-
-static unsigned long timespec_delta(struct timespec* t1, struct timespec* t2) {
-       // Compute delta in milliseconds
-       return (
-               ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000))
-               -
-               ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000))
-       ) / 1000.0;
-}
-
 static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats* stats) {
        struct timespec now;
 
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..551574b
--- /dev/null
@@ -0,0 +1,96 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2021 IPFire 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 <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "util.h"
+
+static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
+static const char* suffixes_bits[]  = { "b", "Kb", "Mb", "Gb", "Tb", NULL };
+
+char* format_size(ssize_t size, int unit) {
+       const char** suffixes;
+       unsigned int divisor;
+
+       switch (unit) {
+               case FIREPERF_FORMAT_BYTES:
+                       suffixes = suffixes_bytes;
+                       divisor = 1024;
+                       break;
+
+               case FIREPERF_FORMAT_BITS:
+                       suffixes = suffixes_bits;
+                       divisor = 1000;
+                       break;
+
+               // Invalid input
+               default:
+                       return NULL;
+       }
+
+       const char** suffix;
+       char* retval = NULL;
+
+       // Convert into double
+       double s = size;
+
+       for (suffix = suffixes; *suffix; suffix++) {
+               if (abs(s) < divisor)
+                       break;
+
+               s /= divisor;
+       }
+
+       if (!*suffix)
+               return NULL;
+
+       // Format the output string
+       int r = asprintf(&retval, "%.02f %s", s, *suffix);
+       if (r < 0)
+               return NULL;
+
+       return retval;
+}
+
+static char __timespec[20];
+
+const char* format_timespec(const struct timespec* t) {
+       // Convert to local time
+       struct tm* tm = localtime(&t->tv_sec);
+
+       size_t s = strftime(__timespec, sizeof(__timespec), "%F %T", tm);
+
+       if (s)
+               return __timespec;
+
+       return NULL;
+}
+
+unsigned long timespec_delta(struct timespec* t1, struct timespec* t2) {
+       // Compute delta in milliseconds
+       return (
+               ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000))
+               -
+               ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000))
+       ) / 1000.0;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..077134f
--- /dev/null
@@ -0,0 +1,31 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2021 IPFire 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 <time.h>
+#include <unistd.h>
+
+enum {
+       FIREPERF_FORMAT_BYTES,
+       FIREPERF_FORMAT_BITS,
+};
+
+char* format_size(ssize_t size, int unit);
+const char* format_timespec(const struct timespec* t);
+unsigned long timespec_delta(struct timespec* t1, struct timespec* t2);