poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Decouple pk-utils from output functions


From: Tim Rühsen
Subject: [PATCH] Decouple pk-utils from output functions
Date: Sat, 2 May 2020 19:25:46 +0200

2020-05-02  Tim Rühsen  <address@hidden>

        * lib/pk-utils.c: Remove pkt.h from includes.
        (pk_print_binary): Add string output function as first param.
        * lib/pk-utils.h (pk_print_binary): Likewise.
        * lib/pvm-val.c (pvm_print_val_1): Add pk_puts as first param
        when calling pk_print_binary.
        * lib/pvm.jitter: Likewise.
---
 ChangeLog      |  9 +++++++++
 lib/pk-utils.c | 39 +++++++++++++--------------------------
 lib/pk-utils.h |  4 ++--
 lib/pvm-val.c  |  8 ++++----
 lib/pvm.jitter |  4 ++--
 5 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/lib/pk-utils.c b/lib/pk-utils.c
index 106761e5..55ba03d4 100644
--- a/lib/pk-utils.c
+++ b/lib/pk-utils.c
@@ -31,7 +31,6 @@
 #include <string.h> /* strcpy */
 #include <xalloc.h> /* xmalloc */

-#include "pkt.h"
 #include "pk-utils.h"

 char *
@@ -86,47 +85,35 @@ PK_POW (pk_upow, uint64_t)
 #undef PK_POW

 void
-pk_print_binary (uint64_t val, int size, int sign)
+pk_print_binary (
+  void (*puts_fn) (const char *str),
+  uint64_t val, int size, int sign)
 {
   char b[65];

   if (size != 64 && size != 32 && size != 16 && size != 8
       && size != 4)
-    pk_printf ("(%sint<%d>) ", sign ? "" : "u", size);
+    {
+      snprintf (b, sizeof(b), "(%sint<%d>) ", sign ? "" : "u", size);
+      puts_fn (b);
+    }

   for (int z = 0; z < size; z++) {
     b[size-1-z] = ((val >> z) & 0x1) + '0';
   }
   b[size] = '\0';

-  pk_printf ("0b%s", b);
+  puts_fn ("0b");
+  puts_fn (b);

   if (size == 64)
-    {
-      if (!sign)
-        pk_puts ("U");
-      pk_puts ("L");
-    }
+    puts_fn (sign ? "L" : "UL");
   else if (size == 16)
-    {
-      if (!sign)
-        pk_puts ("U");
-      pk_puts ("H");
-    }
+    puts_fn (sign ? "H" : "UH");
   else if (size == 8)
-    {
-      if (!sign)
-        pk_puts ("U");
-      pk_puts ("B");
-    }
+    puts_fn (sign ? "B" : "UB");
   else if (size == 4)
-    {
-      {
-        if (!sign)
-          pk_puts ("U");
-      }
-      pk_puts ("N");
-    }
+    puts_fn (sign ? "N" : "UN");
 }

 /* Concatenate 2+ strings.
diff --git a/lib/pk-utils.h b/lib/pk-utils.h
index 53d5d032..b08d42da 100644
--- a/lib/pk-utils.h
+++ b/lib/pk-utils.h
@@ -47,8 +47,8 @@ char *pk_file_readable (const char *filename);
 int64_t pk_ipow (int64_t base, uint32_t exp);
 uint64_t pk_upow (uint64_t base, uint32_t exp);

-/* Print the give unsigned 64-bit integer in binary.  */
-void pk_print_binary (uint64_t val, int size, int sign);
+/* Print the given unsigned 64-bit integer in binary. */
+void pk_print_binary (void (*puts_fn) (const char *str), uint64_t val, int 
size, int sign);

 /* Concatenate string arguments into an malloc'ed string. */
 char *pk_str_concat(const char *s0, ...);
diff --git a/lib/pvm-val.c b/lib/pvm-val.c
index b9b173fb..41737959 100644
--- a/lib/pvm-val.c
+++ b/lib/pvm-val.c
@@ -648,7 +648,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, int 
indent,
         ulongval = (uint64_t) longval & ((((uint64_t) 1) << size) - 1);

       if (base == 2)
-        pk_print_binary (ulongval, size, 1);
+        pk_print_binary (pk_puts, ulongval, size, 1);
       else
         {
           if (size == 64)
@@ -674,7 +674,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, int 
indent,
         uintval = (uint32_t) intval & ((((uint32_t) 1) << size) - 1);

       if (base == 2)
-        pk_print_binary ((uint64_t) uintval, size, 1);
+        pk_print_binary (pk_puts, (uint64_t) uintval, size, 1);
       else
         {
           if (size == 32)
@@ -700,7 +700,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, int 
indent,
       pk_term_class ("integer");

       if (base == 2)
-        pk_print_binary (ulongval, size, 0);
+        pk_print_binary (pk_puts, ulongval, size, 0);
       else
         {
           if (size == 64)
@@ -719,7 +719,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, int 
indent,
       pk_term_class ("integer");

       if (base == 2)
-        pk_print_binary (uintval, size, 0);
+        pk_print_binary (pk_puts, uintval, size, 0);
       else
         {
           if (size == 32)
diff --git a/lib/pvm.jitter b/lib/pvm.jitter
index ffb4e391..f6ed0f1f 100644
--- a/lib/pvm.jitter
+++ b/lib/pvm.jitter
@@ -393,7 +393,7 @@ late-header-c
       }                                                                     \
       else if (JITTER_ARGN1 == 2)                                           \
       {                                                                     \
-        pk_print_binary (val, JITTER_ARGN0, 1);                             \
+        pk_print_binary (pk_puts, val, JITTER_ARGN0, 1);                    \
         JITTER_DROP_STACK ();                                               \
         break;                                                              \
       }                                                                     \
@@ -438,7 +438,7 @@ late-header-c
       }                                                                     \
       else if (JITTER_ARGN1 == 2)                                           \
       {                                                                     \
-        pk_print_binary (val, JITTER_ARGN0, 1);                             \
+        pk_print_binary (pk_puts, val, JITTER_ARGN0, 1);                    \
         JITTER_DROP_STACK ();                                               \
         break;                                                              \
       }                                                                     \
--
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]