/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ /* nautilus-glib-extensions.c - implementation of new functions that conceptually belong in glib. Perhaps some of these will be actually rolled into glib someday. Copyright (C) 2000 Eazel, Inc. The Gnome Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The Gnome Library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the Gnome Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Authors: John Sullivan */ #include "nautilus-glib-extensions.h" #include "nautilus-lib-self-check-functions.h" /** * nautilus_g_date_new_tm: * * Get a new GDate * for the date represented by a tm struct. * The caller is responsible for g_free-ing the result. * @time_pieces: Pointer to a tm struct representing the date to be converted. * * Returns: Newly allocated date. * **/ GDate * nautilus_g_date_new_tm (struct tm *time_pieces) { /* tm uses 0-based months; GDate uses 1-based months. * tm_year needs 1900 added to get the full year. */ return g_date_new_dmy (time_pieces->tm_mday, time_pieces->tm_mon + 1, time_pieces->tm_year + 1900); } /** * nautilus_strdup_strftime: * * Cover for standard date-and-time-formatting routine strftime that returns * a newly-allocated string of the correct size. The caller is responsible * for g_free-ing the returned string. * @format: format string to pass to strftime. See strftime documentation * for details. * @time_pieces: date/time, in struct format. * * Return value: Newly allocated string containing the formatted time. **/ char * nautilus_strdup_strftime (const char *format, struct tm *time_pieces) { char *result; size_t string_length; string_length = strftime (NULL, G_MAXINT, format, time_pieces); result = g_malloc (string_length + 1); strftime (result, string_length + 1, format, time_pieces); return result; } /** * nautilus_g_list_equal * * Compares two lists to see if they are equal. * @list_a: First list. * @list_b: Second list. * * Return value: TRUE if the lists are the same length with the same elements. **/ gboolean nautilus_g_list_equal (GList *list_a, GList *list_b) { GList *p, *q; for (p = list_a, q = list_b; p != NULL && q != NULL; p = p->next, q = q->next) { if (p->data != q->data) { return FALSE; } } return p == NULL && q == NULL; } /** * nautilus_g_list_free_deep * * Frees the elements of a list and then the list. * @list: List of elements that can be freed with g_free. **/ void nautilus_g_list_free_deep (GList *list) { g_list_foreach (list, (GFunc) g_free, NULL); g_list_free (list); } /** * nautilus_g_strv_find * * Get index of string in array of strings. * * @strv: NULL-terminated array of strings. * @find_me: string to search for. * * Return value: index of array entry in @strv that * matches @find_me, or -1 if no matching entry. */ int nautilus_g_strv_find (char **strv, const char *find_me) { int index; g_return_val_if_fail (find_me != NULL, -1); for (index = 0; strv[index] != NULL; ++index) { if (strcmp (strv[index], find_me) == 0) { return index; } } return -1; } /** * nautilus_g_list_safe_for_each * * A version of g_list_foreach that works if the passed function * deletes the current element. * * @list: List to iterate. * @function: Function to call on each element. * @user_data: Data to pass to function. */ void nautilus_g_list_safe_for_each (GList *list, GFunc function, gpointer user_data) { GList *p, *next; for (p = list; p != NULL; p = next) { next = p->next; (* function) (p->data, user_data); } } #if !defined (NAUTILUS_OMIT_SELF_CHECK) static void check_tm_to_g_date (time_t time) { struct tm *before_conversion; struct tm after_conversion; GDate *date; before_conversion = localtime (&time); date = nautilus_g_date_new_tm (before_conversion); g_date_to_struct_tm (date, &after_conversion); g_date_free (date); NAUTILUS_CHECK_INTEGER_RESULT (after_conversion.tm_mday, before_conversion->tm_mday); NAUTILUS_CHECK_INTEGER_RESULT (after_conversion.tm_mon, before_conversion->tm_mon); NAUTILUS_CHECK_INTEGER_RESULT (after_conversion.tm_year, before_conversion->tm_year); } void nautilus_self_check_glib_extensions (void) { char **strv; check_tm_to_g_date (0); /* lower limit */ check_tm_to_g_date ((time_t) -1); /* upper limit */ check_tm_to_g_date (time (NULL)); /* current time */ strv = g_strsplit ("zero|one|two|three|four", "|", 0); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, "zero"), 0); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, "one"), 1); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, "four"), 4); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, "five"), -1); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, ""), -1); NAUTILUS_CHECK_INTEGER_RESULT (nautilus_g_strv_find (strv, "o"), -1); g_strfreev (strv); } #endif /* !NAUTILUS_OMIT_SELF_CHECK */