X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Ffile-handle-def.c;h=04f773452f89fe1ad7b2be248876aa26b230fbf7;hb=69bd50b717021786aedf541394a150b72e4e9a7c;hp=4546a9ca0c4a137571878b2dab9086d4b63134c0;hpb=96994a54e60e9c95b8bba54c2281acf7059b1203;p=pspp diff --git a/src/data/file-handle-def.c b/src/data/file-handle-def.c index 4546a9ca0c..04f773452f 100644 --- a/src/data/file-handle-def.c +++ b/src/data/file-handle-def.c @@ -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" @@ -115,12 +116,17 @@ fh_done (void) HMAP_FOR_EACH_SAFE (handle, next, struct file_handle, name_node, &named_handles) unname_handle (handle); + + free_handle (inline_file); } /* Free HANDLE and remove it from the global list. */ static void free_handle (struct file_handle *handle) { + if (handle == NULL) + return; + /* Remove handle from global list. */ if (handle->id != NULL) hmap_delete (&named_handles, &handle->name_node); @@ -153,6 +159,8 @@ unname_handle (struct file_handle *handle) struct file_handle * fh_ref (struct file_handle *handle) { + if (handle == fh_inline_file ()) + return handle; assert (handle->ref_cnt > 0); handle->ref_cnt++; return handle; @@ -165,6 +173,8 @@ fh_unref (struct file_handle *handle) { if (handle != NULL) { + if (handle == fh_inline_file ()) + return; assert (handle->ref_cnt > 0); if (--handle->ref_cnt == 0) free_handle (handle); @@ -195,9 +205,7 @@ fh_from_id (const char *id) HMAP_FOR_EACH_WITH_HASH (handle, struct file_handle, name_node, utf8_hash_case_string (id, 0), &named_handles) if (!utf8_strcasecmp (id, handle->id)) - { - return fh_ref (handle); - } + return fh_ref (handle); return NULL; } @@ -212,19 +220,18 @@ static struct file_handle * create_handle (const char *id, char *handle_name, enum fh_referent referent, const char *encoding) { - struct file_handle *handle = xzalloc (sizeof *handle); + struct file_handle *handle = xmalloc (sizeof *handle); + *handle = (struct file_handle) { + .ref_cnt = 1, + .id = xstrdup_if_nonnull (id), + .name = handle_name, + .referent = referent, + .encoding = xstrdup (encoding), + }; - handle->ref_cnt = 1; - handle->id = id != NULL ? xstrdup (id) : NULL; - handle->name = handle_name; - handle->referent = referent; - handle->encoding = xstrdup (encoding); - - if (id != NULL) - { - hmap_insert (&named_handles, &handle->name_node, - utf8_hash_case_string (handle->id, 0)); - } + if (id) + hmap_insert (&named_handles, &handle->name_node, + utf8_hash_case_string (handle->id, 0)); return handle; } @@ -246,13 +253,11 @@ struct file_handle * fh_create_file (const char *id, const char *file_name, const char *file_name_encoding, const struct fh_properties *properties) { - char *handle_name; - struct file_handle *handle; - - handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name); - handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding); + char *handle_name = id ? xstrdup (id) : xasprintf ("`%s'", file_name); + struct file_handle *handle = create_handle (id, handle_name, FH_REF_FILE, + properties->encoding); handle->file_name = xstrdup (file_name); - handle->file_name_encoding = file_name_encoding ? xstrdup (file_name_encoding) : NULL; + handle->file_name_encoding = xstrdup_if_nonnull (file_name_encoding); handle->mode = properties->mode; handle->line_ends = properties->line_ends; handle->record_width = properties->record_width; @@ -383,6 +388,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 * @@ -409,7 +448,7 @@ fh_set_default_handle (struct file_handle *new_default_handle) fh_unref (default_handle); default_handle = new_default_handle; if (default_handle != NULL) - fh_ref (default_handle); + default_handle = fh_ref (default_handle); } /* Information about a file handle's readers or writers. */