Make cases simpler, faster, and easier to understand.
[pspp-builds.git] / src / data / case-tmpfile.c
index 9bc83d82f2d7eceb41be30dc0dbda8347b1ebbc5..ca3a6a564421552f4241900caa2fb21708f08226 100644 (file)
@@ -1,20 +1,18 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 2007 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
 
-   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 2 of the
-   License, or (at your option) any later version.
+   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.
+   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, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
@@ -34,7 +32,7 @@
 #define _(msgid) gettext (msgid)
 
 /* A temporary file that stores an array of cases. */
-struct case_tmpfile 
+struct case_tmpfile
   {
     struct taint *taint;        /* Taint. */
     FILE *file;                 /* Underlying file. */
@@ -49,12 +47,12 @@ struct case_tmpfile
 
 /* Creates and returns a new case_tmpfile. */
 struct case_tmpfile *
-case_tmpfile_create (size_t value_cnt) 
+case_tmpfile_create (size_t value_cnt)
 {
   struct case_tmpfile *ctf = xmalloc (sizeof *ctf);
   ctf->taint = taint_create ();
   ctf->file = tmpfile ();
-  if (ctf->file == NULL) 
+  if (ctf->file == NULL)
     {
       error (0, errno, _("failed to create temporary file"));
       taint_set_taint (ctf->taint);
@@ -69,10 +67,10 @@ case_tmpfile_create (size_t value_cnt)
    error on case_tmpfile access or by taint propagation to the
    case_tmpfile. */
 bool
-case_tmpfile_destroy (struct case_tmpfile *ctf) 
+case_tmpfile_destroy (struct case_tmpfile *ctf)
 {
   bool ok = true;
-  if (ctf != NULL) 
+  if (ctf != NULL)
     {
       struct taint *taint = ctf->taint;
       if (ctf->file != NULL)
@@ -87,21 +85,21 @@ case_tmpfile_destroy (struct case_tmpfile *ctf)
    error on case_tmpfile access or by taint propagation to the
    case_tmpfile. */
 bool
-case_tmpfile_error (const struct case_tmpfile *ctf) 
+case_tmpfile_error (const struct case_tmpfile *ctf)
 {
   return taint_is_tainted (ctf->taint);
 }
 
 /* Marks CTF as tainted. */
 void
-case_tmpfile_force_error (struct case_tmpfile *ctf) 
+case_tmpfile_force_error (struct case_tmpfile *ctf)
 {
   taint_set_taint (ctf->taint);
 }
 
 /* Returns CTF's taint object. */
 const struct taint *
-case_tmpfile_get_taint (const struct case_tmpfile *ctf) 
+case_tmpfile_get_taint (const struct case_tmpfile *ctf)
 {
   return ctf->taint;
 }
@@ -112,11 +110,11 @@ case_tmpfile_get_taint (const struct case_tmpfile *ctf)
    otherwise tainted, false otherwise. */
 static bool
 do_seek (const struct case_tmpfile *ctf_,
-         casenumber case_idx, size_t value_idx) 
+         casenumber case_idx, size_t value_idx)
 {
   struct case_tmpfile *ctf = (struct case_tmpfile *) ctf_;
 
-  if (!case_tmpfile_error (ctf)) 
+  if (!case_tmpfile_error (ctf))
     {
       off_t value_ofs = value_idx + (off_t) ctf->value_cnt * case_idx;
       off_t byte_ofs = sizeof (union value) * value_ofs;
@@ -168,12 +166,12 @@ do_read (const struct case_tmpfile *ctf_, size_t bytes, void *buffer)
    Returns true if successful, false upon an I/O error (in which
    case CTF is marked tainted). */
 static bool
-do_write (struct case_tmpfile *ctf, size_t bytes, const void *buffer) 
+do_write (struct case_tmpfile *ctf, size_t bytes, const void *buffer)
 {
   assert (!case_tmpfile_error (ctf));
   if (fwrite (buffer, bytes, 1, ctf->file) != 1)
     {
-      case_tmpfile_force_error (ctf); 
+      case_tmpfile_force_error (ctf);
       error (0, errno, _("writing to temporary file"));
       return false;
     }
@@ -191,7 +189,7 @@ do_write (struct case_tmpfile *ctf, size_t bytes, const void *buffer)
 bool
 case_tmpfile_get_values (const struct case_tmpfile *ctf,
                          casenumber case_idx, size_t start_value,
-                         union value values[], size_t value_cnt) 
+                         union value values[], size_t value_cnt)
 {
   assert (value_cnt <= ctf->value_cnt);
   assert (value_cnt + start_value <= ctf->value_cnt);
@@ -200,26 +198,24 @@ case_tmpfile_get_values (const struct case_tmpfile *ctf,
           && do_read (ctf, sizeof *values * value_cnt, values));
 }
 
-/* Reads the case numbered CASE_IDX from CTF into C.
-   Returns true if successful, false if CTF is tainted or an I/O
-   error occurs during the operation.
+/* Reads the case numbered CASE_IDX from CTF.
+   Returns the case if successful or a null pointer if CTF is
+   tainted or an I/O error occurs during the operation.
 
    The results of this function are undefined if the case read
    from CTF had not previously been written. */
-bool
-case_tmpfile_get_case (const struct case_tmpfile *ctf, casenumber case_idx,
-                       struct ccase *c) 
+struct ccase *
+case_tmpfile_get_case (const struct case_tmpfile *ctf, casenumber case_idx)
 {
-  case_create (c, ctf->value_cnt);
+  struct ccase *c = case_create (ctf->value_cnt);
   if (case_tmpfile_get_values (ctf, case_idx, 0,
                                case_data_all_rw (c), ctf->value_cnt))
-    return true;
+    return c;
   else
     {
-      case_destroy (c);
-      case_nullify (c);
-      return false;
-    } 
+      case_unref (c);
+      return NULL;
+    }
 }
 
 /* Writes VALUE_CNT values from VALUES, into the case numbered
@@ -237,18 +233,18 @@ case_tmpfile_put_values (struct case_tmpfile *ctf,
 
   return (do_seek (ctf, case_idx, start_value)
           && do_write (ctf, sizeof *values * value_cnt, values));
-}                         
+}
 
 /* Writes C to CTF as the case numbered CASE_IDX.
    Returns true if successful, false if CTF is tainted or an I/O
    error occurs during the operation. */
 bool
 case_tmpfile_put_case (struct case_tmpfile *ctf, casenumber case_idx,
-                       struct ccase *c) 
+                       struct ccase *c)
 {
   bool ok = case_tmpfile_put_values (ctf, case_idx, 0,
                                      case_data_all (c), ctf->value_cnt);
-  case_destroy (c);
+  case_unref (c);
   return ok;
 }