From 9c69efdb1652738ee096c8c43d2acbfd12eba75b Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Thu, 6 Dec 2007 10:23:15 -0600 Subject: [PATCH] moo_tempnam() --- moo/moopython/pygtk/mooapp-pygtk.defs | 12 --- moo/moopython/pygtk/mooutils-pygtk.defs | 5 + moo/mooutils/mooutils-misc.c | 132 ++++++++++++++++++++++-- moo/mooutils/mooutils-misc.h | 3 + 4 files changed, 132 insertions(+), 20 deletions(-) diff --git a/moo/moopython/pygtk/mooapp-pygtk.defs b/moo/moopython/pygtk/mooapp-pygtk.defs index ffbd72cb..1ac16939 100644 --- a/moo/moopython/pygtk/mooapp-pygtk.defs +++ b/moo/moopython/pygtk/mooapp-pygtk.defs @@ -115,18 +115,6 @@ ) ) -(define-method tempnam - (of-object "MooApp") - (c-name "moo_app_tempnam") - (return-type "char*") -) - -(define-method create_user_data_dir - (of-object "MooApp") - (c-name "moo_app_create_user_data_dir") - (return-type "char*") -) - (define-method send_msg (of-object "MooApp") (c-name "moo_app_send_msg") diff --git a/moo/moopython/pygtk/mooutils-pygtk.defs b/moo/moopython/pygtk/mooutils-pygtk.defs index b05dd947..67cd7088 100644 --- a/moo/moopython/pygtk/mooutils-pygtk.defs +++ b/moo/moopython/pygtk/mooutils-pygtk.defs @@ -2304,6 +2304,11 @@ ) ) +(define-function tempnam + (c-name "moo_tempnam") + (return-type "char*") +) + ;; mooutils/mooi18n.h diff --git a/moo/mooutils/mooutils-misc.c b/moo/mooutils/mooutils-misc.c index fc282aed..c197e5a2 100644 --- a/moo/mooutils/mooutils-misc.c +++ b/moo/mooutils/mooutils-misc.c @@ -44,7 +44,12 @@ G_LOCK_DEFINE_STATIC (moo_user_data_dir); +G_LOCK_DEFINE_STATIC (moo_temp_dir); static char *moo_user_data_dir; +static char *moo_temp_dir; + +static void moo_install_atexit (void); +static void moo_remove_tempdir (void); #ifdef __WIN32__ @@ -908,16 +913,10 @@ log_func_file (const char *log_domain, } #endif /* __WIN32__ */ -#ifdef __WIN32__ -#define LT "\r\n" -#else -#define LT "\n" -#endif if (log_domain) - string = g_strdup_printf ("%s: %s" LT, log_domain, message); + string = g_strdup_printf ("%s: %s\n", log_domain, message); else - string = g_strdup_printf ("%s" LT, message); -#undef LT + string = g_strdup_printf ("%s\n", message); print_func_file (string); g_free (string); @@ -1145,6 +1144,123 @@ moo_get_prgname (void) } +char * +moo_tempnam (void) +{ + int i; + char *filename = NULL; + static int counter; + G_LOCK_DEFINE_STATIC (counter); + + G_LOCK (moo_temp_dir); + + if (!moo_temp_dir) + { + char *dirname = NULL; + const char *short_name; + + short_name = moo_get_prgname (); + + for (i = 0; i < 1000; ++i) + { + char *basename; + + basename = g_strdup_printf ("%s-%08x", short_name, g_random_int ()); + dirname = g_build_filename (g_get_tmp_dir (), basename, NULL); + g_free (basename); + + if (_moo_mkdir (dirname)) + { + g_free (dirname); + dirname = NULL; + } + else + { + break; + } + } + + moo_temp_dir = dirname; + moo_install_atexit (); + } + + G_UNLOCK (moo_temp_dir); + + g_return_val_if_fail (moo_temp_dir != NULL, NULL); + + G_LOCK (counter); + + for (i = counter + 1; i < counter + 1000 && !filename; ++i) + { + char *basename; + + basename = g_strdup_printf ("tmpfile-%03d", i); + filename = g_build_filename (moo_temp_dir, basename, NULL); + + if (g_file_test (filename, G_FILE_TEST_EXISTS)) + { + g_free (filename); + filename = NULL; + } + + g_free (basename); + } + + counter = i; + + G_UNLOCK (counter); + + if (!filename) + g_warning ("%s: could not generate temp file name", G_STRLOC); + + return filename; +} + +static void +moo_remove_tempdir (void) +{ + if (moo_temp_dir) + { + GError *error = NULL; + _moo_remove_dir (moo_temp_dir, TRUE, &error); + + if (error) + { + _moo_message ("%s: %s", G_STRLOC, error->message); + g_error_free (error); + } + + g_free (moo_temp_dir); + moo_temp_dir = NULL; + } +} + +static void +moo_atexit_handler (void) +{ + moo_remove_tempdir (); +} + +static void +moo_install_atexit (void) +{ + static gboolean installed; + + if (!installed) + { + atexit (moo_atexit_handler); + installed = TRUE; + } +} + + +void +moo_cleanup (void) +{ + moo_remove_tempdir (); +} + + static char * moo_get_user_cache_dir (void) { diff --git a/moo/mooutils/mooutils-misc.h b/moo/mooutils/mooutils-misc.h index f97acb5b..916060fe 100644 --- a/moo/mooutils/mooutils-misc.h +++ b/moo/mooutils/mooutils-misc.h @@ -81,6 +81,9 @@ char **moo_get_data_subdirs (const char *subdir, guint *n_dirs); #define moo_get_data_files moo_get_data_subdirs +char *moo_tempnam (void); +void moo_cleanup (void); + const char *moo_get_locale_dir (void); const char *const *_moo_get_shared_data_dirs (void);