poke-devel
[Top][All Lists]
Advanced

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

[PATCH] libpoke,ios: new search function to find IO spaces


From: Mohammad-Reza Nabipoor
Subject: [PATCH] libpoke,ios: new search function to find IO spaces
Date: Wed, 1 Nov 2023 00:28:24 +0100

This commit adds a function which can find the IO space whose
handler contains the provided partial handler.

2023-11-01  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/libpoke.h (pk_ios_search_by_partial_handler): New
        function to find IO spaces using part of the handler.
        * libpoke/libpoke.c (pk_ios_search_by_partial_handler): Likewise.
        * libpoke/ios.h (ios_search_partial): Likewise.
        * libpoke/ios.c (ios_search_partial): Likewise.
        * testsuite/poke.libpoke/api.c (STREQ): New macro.
        (test_pk_ios): New function for testing IO space functions.
        (main): Call `test_pk_ios'.
---
 ChangeLog                    | 11 +++++
 libpoke/ios.c                | 16 +++++++
 libpoke/ios.h                |  6 +++
 libpoke/libpoke.c            |  8 ++++
 libpoke/libpoke.h            |  7 +++
 testsuite/poke.libpoke/api.c | 90 ++++++++++++++++++++++++++++++++++++
 6 files changed, 138 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index a7bfa9cf..4316936a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-11-01  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/libpoke.h (pk_ios_search_by_partial_handler): New
+       function to find IO spaces using part of the handler.
+       * libpoke/libpoke.c (pk_ios_search_by_partial_handler): Likewise.
+       * libpoke/ios.h (ios_search_partial): Likewise.
+       * libpoke/ios.c (ios_search_partial): Likewise.
+       * testsuite/poke.libpoke/api.c (STREQ): New macro.
+       (test_pk_ios): New function for testing IO space functions.
+       (main): Call `test_pk_ios'.
+
 2023-10-30  Jose E. Marchesi  <jemarch@gnu.org>
 
        * poke/pk-help.pk (pk_help): Hint at activating the page at the
diff --git a/libpoke/ios.c b/libpoke/ios.c
index 974aface..870cc305 100644
--- a/libpoke/ios.c
+++ b/libpoke/ios.c
@@ -342,6 +342,22 @@ ios_search (ios_context ios_ctx, const char *handler)
   return io;
 }
 
+ios ios_search_partial (ios_context ios_ctx, const char *partial_handler)
+{
+  ios ios_matched = NULL;
+  size_t n_matched = 0;
+
+  for (ios io = ios_ctx->io_list; io; io = io->next)
+    if (strstr (io->handler, partial_handler) != NULL)
+      {
+        if (++n_matched > 1)
+          return NULL;
+        ios_matched = io;
+      }
+
+  return ios_matched;
+}
+
 ios
 ios_search_by_id (ios_context ios_ctx, int id)
 {
diff --git a/libpoke/ios.h b/libpoke/ios.h
index 3c695bc4..d4b05a9d 100644
--- a/libpoke/ios.h
+++ b/libpoke/ios.h
@@ -193,6 +193,12 @@ void ios_set_cur (ios_context ios_ctx, ios io);
 
 ios ios_search (ios_context ios_ctx, const char *handler);
 
+/* Return the IO space whose handler matches the given PARTIAL_HANDLER.
+   A handler matches the PARTIAL_HANDLER if the PARTIAL_HANDLER is a
+   sub-string of that handler.  Return NULL if there's no unique match.  */
+
+ios ios_search_partial (ios_context ios_ctx, const char *partial_handler);
+
 /* Return the IO space having the given ID.  Return NULL if no such
    space exists.  */
 
diff --git a/libpoke/libpoke.c b/libpoke/libpoke.c
index 57d28828..7fbb5607 100644
--- a/libpoke/libpoke.c
+++ b/libpoke/libpoke.c
@@ -624,6 +624,14 @@ pk_ios_search (pk_compiler pkc, const char *handler)
   return (pk_ios) ios_search (pvm_ios_context (pkc->vm), handler);
 }
 
+pk_ios
+pk_ios_search_by_partial_handler (pk_compiler pkc, const char *partial_handler)
+{
+  pkc->status = PK_OK;
+  return (pk_ios) ios_search_partial (pvm_ios_context (pkc->vm),
+                                      partial_handler);
+}
+
 pk_ios
 pk_ios_search_by_id (pk_compiler pkc, int id)
 {
diff --git a/libpoke/libpoke.h b/libpoke/libpoke.h
index 988df231..edba01c2 100644
--- a/libpoke/libpoke.h
+++ b/libpoke/libpoke.h
@@ -478,6 +478,13 @@ void pk_ios_set_cur (pk_compiler pkc, pk_ios ios) 
LIBPOKE_API;
 
 pk_ios pk_ios_search (pk_compiler pkc, const char *handler) LIBPOKE_API;
 
+/* Return the IO space whose handler matches the given PARTIAL_HANDLER.
+   A handler matches the PARTIAL_HANDLER if the PARTIAL_HANDLER is a
+   sub-string of that handler.  Return NULL if there's no unique match.  */
+
+pk_ios pk_ios_search_by_partial_handler (pk_compiler pkc,
+                                         const char *partial_handler) 
LIBPOKE_API;
+
 /* Return the IO space having the given ID.  Return NULL if no such
    space exist in the given Poke incremental compiler.  */
 
diff --git a/testsuite/poke.libpoke/api.c b/testsuite/poke.libpoke/api.c
index 805c16e7..d19cb0c3 100644
--- a/testsuite/poke.libpoke/api.c
+++ b/testsuite/poke.libpoke/api.c
@@ -31,6 +31,8 @@
 
 #include "term-if.h"
 
+#define STREQ(a, b) (strcmp ((a), (b)) == 0)
+
 #define T(name, cond)                                                         \
   do                                                                          \
     {                                                                         \
@@ -135,6 +137,93 @@ test_pk_load (pk_compiler pkc)
   T ("pk_load_1 exception", exception == 0);
 }
 
+static void
+test_pk_ios (pk_compiler pkc)
+{
+#define N 4
+  int ios_id[N];
+  pk_ios ios[N];
+  uint64_t flags = 0;
+
+  ios_id[0] = pk_ios_open (pkc, "*foo*", flags, /*set_cur_p*/ 1);
+  T ("pk_ios_open_1", ios_id[0] == 0);
+  ios[0] = pk_ios_cur (pkc);
+  T ("pk_ios_cur_1", ios[0] != NULL);
+
+  ios_id[1] = pk_ios_open (pkc, "*foobar*", flags, /*set_cur_p*/ 0);
+  T ("pk_ios_open_2", ios_id[1] == 1);
+  T ("pk_ios_cur_2", ios[0] == pk_ios_cur (pkc));
+
+  ios_id[2] = pk_ios_open (pkc, "*funfoo*", flags, /*set_cur_p*/ 0);
+  T ("pk_ios_open_3", ios_id[2] == 2);
+  T ("pk_ios_cur_3", ios[0] == pk_ios_cur (pkc));
+
+  ios_id[3] = pk_ios_open (pkc, "*baz*", flags, /*set_cur_p*/ 0);
+  T ("pk_ios_open_4", ios_id[3] == 3);
+  T ("pk_ios_cur_4", ios[0] == pk_ios_cur (pkc));
+
+  ios[1] = pk_ios_search_by_id (pkc, ios_id[1]);
+  ios[2] = pk_ios_search_by_id (pkc, ios_id[2]);
+  ios[3] = pk_ios_search_by_id (pkc, ios_id[3]);
+  T ("pk_ios_search_by_id_1", ios[1] != NULL);
+  T ("pk_ios_search_by_id_2", ios[2] != NULL);
+  T ("pk_ios_search_by_id_3", ios[3] != NULL);
+
+  T ("pk_ios_get_id_1", pk_ios_get_id (ios[0]) == ios_id[0]);
+  T ("pk_ios_get_id_2", pk_ios_get_id (ios[1]) == ios_id[1]);
+  T ("pk_ios_get_id_3", pk_ios_get_id (ios[2]) == ios_id[2]);
+  T ("pk_ios_get_id_4", pk_ios_get_id (ios[3]) == ios_id[3]);
+
+  T ("pk_ios_handler_1", pk_ios_handler (ios[0]) != NULL);
+  T ("pk_ios_handler_2", pk_ios_handler (ios[1]) != NULL);
+  T ("pk_ios_handler_3", pk_ios_handler (ios[2]) != NULL);
+  T ("pk_ios_handler_4", pk_ios_handler (ios[3]) != NULL);
+
+  T ("pk_ios_handler_5", STREQ (pk_ios_handler (ios[0]), "*foo*"));
+  T ("pk_ios_handler_6", STREQ (pk_ios_handler (ios[1]), "*foobar*"));
+  T ("pk_ios_handler_7", STREQ (pk_ios_handler (ios[2]), "*funfoo*"));
+  T ("pk_ios_handler_8", STREQ (pk_ios_handler (ios[3]), "*baz*"));
+
+  T ("pk_ios_search_by_partial_handler_1",
+     pk_ios_search_by_partial_handler (pkc, "/some/non-existent/thing")
+         == NULL);
+
+  T ("pk_ios_search_by_partial_handler_2",
+     pk_ios_search_by_partial_handler (pkc, "baz") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_3",
+     pk_ios_search_by_partial_handler (pkc, "baz*") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_4",
+     pk_ios_search_by_partial_handler (pkc, "*baz") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_5",
+     pk_ios_search_by_partial_handler (pkc, "*baz*") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_6",
+     pk_ios_search_by_partial_handler (pkc, "az") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_7",
+     pk_ios_search_by_partial_handler (pkc, "z") == ios[3]);
+
+  T ("pk_ios_search_by_partial_handler_8",
+     pk_ios_search_by_partial_handler (pkc, "bar*") == ios[1]);
+
+  T ("pk_ios_search_by_partial_handler_9",
+     pk_ios_search_by_partial_handler (pkc, "bar") == ios[1]);
+
+  T ("pk_ios_search_by_partial_handler_10",
+     pk_ios_search_by_partial_handler (pkc, "foo") == NULL);
+
+  T ("pk_ios_search_by_partial_handler_11",
+     pk_ios_search_by_partial_handler (pkc, "ba") == NULL);
+
+  for (int i = N; i != 0; --i)
+    pk_ios_close (pkc, ios[i - 1]);
+
+#undef N
+}
+
 int
 main ()
 {
@@ -144,6 +233,7 @@ main ()
 
   test_pk_keyword_p (pkc);
   test_pk_load (pkc);
+  test_pk_ios (pkc);
   test_pk_compiler_free (pkc);
 
   return 0;
-- 
2.42.0




reply via email to

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