file-handle-def: New function fh_equal().
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 6 Dec 2021 05:13:00 +0000 (21:13 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 6 Dec 2021 17:05:31 +0000 (09:05 -0800)
This will have its first user in an upcoming commit.

src/data/file-handle-def.c
src/data/file-handle-def.h

index de09ae39a75566b664995442667fcf73757b43f6..09340833be4134d4125eefb96ec2ff1141884a11 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "data/dataset.h"
 #include "data/variable.h"
+#include "libpspp/assertion.h"
 #include "libpspp/cast.h"
 #include "libpspp/compiler.h"
 #include "libpspp/hash-functions.h"
@@ -392,6 +393,40 @@ fh_get_encoding (const struct file_handle *handle)
   return handle->encoding;
 }
 
+/* Returns true if A and B refer to the same file or dataset, false
+   otherwise. */
+bool
+fh_equal (const struct file_handle *a, const struct file_handle *b)
+{
+  if (a->referent != b->referent)
+    return false;
+
+  switch (a->referent)
+    {
+    case FH_REF_FILE:
+      {
+        struct file_identity *a_id = fh_get_identity (a);
+        struct file_identity *b_id = fh_get_identity (b);
+
+        int cmp = fh_compare_file_identities (a_id, b_id);
+
+        fh_free_identity (a_id);
+        fh_free_identity (b_id);
+
+        return cmp == 0;
+      }
+
+    case FH_REF_INLINE:
+      return true;
+
+    case FH_REF_DATASET:
+      return a->ds == b->ds;
+
+    default:
+      NOT_REACHED ();
+    }
+}
+
 /* Returns the dataset handle associated with HANDLE.
    Applicable to only FH_REF_DATASET files. */
 struct dataset *
index 2f944ed1c29803cb11e975ee9401d63b660b24cc..df9a757835520df6cce509ee8019764c96c8faf1 100644 (file)
@@ -96,6 +96,8 @@ const char *fh_get_name (const struct file_handle *);
 enum fh_referent fh_get_referent (const struct file_handle *);
 const char *fh_get_encoding (const struct file_handle *);
 
+bool fh_equal (const struct file_handle *, const struct file_handle *);
+
 /* Properties of FH_REF_FILE file handles. */
 const char *fh_get_file_name (const struct file_handle *);
 const char *fh_get_file_name_encoding (const struct file_handle *handle);