#include <config.h>
#include "libpspp/temp-file.h"
+#include "libpspp/hmapx.h"
+#include "libpspp/hash-functions.h"
#include <stdlib.h>
- It honors the $TMPDIR environment variable.
- - The file will not be automatically deleted upon close. You have to call
- close_temp_file() if you want it to be deleted before the process exits.
*/
+
+
+static struct temp_dir *temp_dir;
+struct hmapx map;
+
+static void
+setup (void)
+{
+ hmapx_init (&map);
+ temp_dir = create_temp_dir ("pspp", NULL, true);
+}
+
+static void
+cleanup (void)
+{
+ struct hmapx_node *node;
+ const char *fn;
+
+ cleanup_temp_dir (temp_dir);
+
+ HMAPX_FOR_EACH (fn, node, &map)
+ {
+ free (fn);
+ }
+
+ hmapx_destroy (&map);
+}
+
FILE *
create_temp_file (void)
{
static int idx = 0;
- static struct temp_dir *temp_dir;
char *file_name;
FILE *stream;
if (temp_dir == NULL)
{
- temp_dir = create_temp_dir ("pspp", NULL, true);
+ setup ();
if (temp_dir == NULL)
return NULL;
+ atexit (cleanup);
}
file_name = xasprintf ("%s/%d", temp_dir->dir_name, idx++);
+ register_temp_file (temp_dir, file_name);
stream = fopen_temp (file_name, "wb+");
- if (stream != NULL)
+ if (stream == NULL)
+ unregister_temp_file (temp_dir, file_name);
+ else
setvbuf (stream, NULL, _IOFBF, 65536);
- free (file_name);
+
+ hmapx_insert (&map, file_name, hash_pointer (stream, 0));
return stream;
}
close_temp_file (FILE *file)
{
if (file != NULL)
- fclose_temp (file);
+ {
+ struct hmapx_node *node = hmapx_first_with_hash (&map, hash_pointer (file, 0));
+ char *fn = node->data;
+ fclose_temp (file);
+ cleanup_temp_file (temp_dir, fn);
+ hmapx_delete (&map, node);
+ free (fn);
+ }
}
src/libpspp/temp-file.c \
tests/libpspp/heap-test.c
tests_libpspp_heap_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_heap_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_hmap_test_SOURCES = \
src/libpspp/hmap.c \
src/libpspp/temp-file.c \
tests/libpspp/range-set-test.c
tests_libpspp_range_set_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_range_set_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_str_test_SOURCES = \
tests/libpspp/str-test.c
src/libpspp/string-set.c \
tests/libpspp/string-map-test.c
tests_libpspp_string_map_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_string_map_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_stringi_map_test_SOURCES = \
src/libpspp/hash-functions.c \
src/libpspp/temp-file.c \
tests/libpspp/stringi-map-test.c
tests_libpspp_stringi_map_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_stringi_map_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_string_set_test_SOURCES = \
src/libpspp/hash-functions.c \
src/libpspp/temp-file.c \
tests/libpspp/stringi-set-test.c
tests_libpspp_stringi_set_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_stringi_set_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_tower_test_SOURCES = \
src/libpspp/abt.c \
src/libpspp/tower.c \
tests/libpspp/tower-test.c
tests_libpspp_tower_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_tower_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_u8_istream_test_SOURCES = tests/libpspp/u8-istream-test.c
tests_libpspp_u8_istream_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests/libpspp/sparse-array-test.c \
src/libpspp/temp-file.c
tests_libpspp_sparse_array_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_sparse_array_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_libpspp_sparse_xarray_test_SOURCES = \
src/libpspp/argv-parser.c \
src/libpspp/temp-file.c \
tests/libpspp/sparse-xarray-test.c
tests_libpspp_sparse_xarray_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10
+tests_libpspp_sparse_xarray_test_LDADD = src/libpspp/libpspp.la gl/libgl.la
tests_data_inexactify_SOURCES = tests/data/inexactify.c
src/libpspp/temp-file.c \
tests/language/lexer/scan-test.c
tests_language_lexer_scan_test_CFLAGS = $(AM_CFLAGS)
+tests_language_lexer_scan_test_LDADD = \
+ src/libpspp/libpspp.la \
+ gl/libgl.la
check_PROGRAMS += tests/language/lexer/segment-test
tests_language_lexer_segment_test_SOURCES = \
src/libpspp/temp-file.c \
tests/language/lexer/segment-test.c
tests_language_lexer_segment_test_CFLAGS = $(AM_CFLAGS)
+tests_language_lexer_segment_test_LDADD = \
+ src/libpspp/libpspp.la \
+ gl/libgl.la
check_PROGRAMS += tests/libpspp/zip-test
tests_libpspp_zip_test_SOURCES = \
src/libpspp/zip-writer.c \
tests/libpspp/zip-test.c
tests_libpspp_zip_test_CFLAGS = $(AM_CFLAGS)
+tests_libpspp_zip_test_LDADD = \
+ src/libpspp/libpspp.la \
+ gl/libgl.la
check_PROGRAMS += tests/output/render-test