glib/libglib/src/gerror.c
branchRCL_3
changeset 57 2efc27d87e1c
parent 0 e4d67989cc36
equal deleted inserted replaced
56:acd3cd4aaceb 57:2efc27d87e1c
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 /*
       
    22  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    23  * file for a list of people on the GLib Team.  See the ChangeLog
       
    24  * files for a list of changes.  These files are distributed with
       
    25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    26  */
       
    27 
       
    28 #include "config.h"
       
    29 
       
    30 #include "glib.h"
       
    31 #include "galias.h"
       
    32 
       
    33 
       
    34 static GError* 
       
    35 g_error_new_valist (GQuark         domain,
       
    36                     gint           code,
       
    37                     const gchar   *format,
       
    38                     va_list        args)
       
    39 {
       
    40   GError *error;
       
    41   error = g_new (GError, 1);
       
    42   
       
    43   error->domain = domain;
       
    44   error->code = code;
       
    45   error->message = g_strdup_vprintf (format, args);
       
    46   
       
    47   return error;
       
    48 }
       
    49 
       
    50 /**
       
    51  * g_error_new:
       
    52  * @domain: error domain 
       
    53  * @code: error code
       
    54  * @format: printf()-style format for error message
       
    55  * @Varargs: parameters for message format
       
    56  * 
       
    57  * Creates a new #GError with the given @domain and @code,
       
    58  * and a message formatted with @format.
       
    59  * 
       
    60  * Return value: a new #GError
       
    61  **/
       
    62 EXPORT_C GError*
       
    63 g_error_new (GQuark       domain,
       
    64              gint         code,
       
    65              const gchar *format,
       
    66              ...)
       
    67 {
       
    68   GError* error;
       
    69   va_list args;
       
    70 
       
    71   g_return_val_if_fail (format != NULL, NULL);
       
    72   g_return_val_if_fail (domain != 0, NULL);
       
    73 
       
    74   va_start (args, format);
       
    75   error = g_error_new_valist (domain, code, format, args);
       
    76   va_end (args);
       
    77 
       
    78   return error;
       
    79 }
       
    80 
       
    81 /**
       
    82  * g_error_new_literal:
       
    83  * @domain: error domain
       
    84  * @code: error code
       
    85  * @message: error message
       
    86  * 
       
    87  * Creates a new #GError; unlike g_error_new(), @message is not
       
    88  * a printf()-style format string. Use this 
       
    89  * function if @message contains text you don't have control over, 
       
    90  * that could include printf() escape sequences.
       
    91  * 
       
    92  * Return value: a new #GError
       
    93  **/
       
    94 EXPORT_C GError*
       
    95 g_error_new_literal (GQuark         domain,
       
    96                      gint           code,
       
    97                      const gchar   *message)
       
    98 {
       
    99   GError* err;
       
   100 
       
   101   g_return_val_if_fail (message != NULL, NULL);
       
   102   g_return_val_if_fail (domain != 0, NULL);
       
   103   err = g_new (GError, 1);
       
   104 
       
   105   err->domain = domain;
       
   106   err->code = code;
       
   107   err->message = g_strdup (message);
       
   108   
       
   109   return err;
       
   110 }
       
   111 
       
   112 /**
       
   113  * g_error_free:
       
   114  * @error: a #GError
       
   115  *
       
   116  * Frees a #GError and associated resources.
       
   117  * 
       
   118  **/
       
   119 EXPORT_C void
       
   120 g_error_free (GError *error)
       
   121 {
       
   122   g_return_if_fail (error != NULL);  
       
   123 
       
   124   g_free (error->message);
       
   125 
       
   126   g_free (error);
       
   127 }
       
   128 
       
   129 /**
       
   130  * g_error_copy:
       
   131  * @error: a #GError
       
   132  * 
       
   133  * Makes a copy of @error.
       
   134  * 
       
   135  * Return value: a new #GError
       
   136  **/
       
   137 EXPORT_C GError*
       
   138 g_error_copy (const GError *error)
       
   139 {
       
   140   GError *copy;
       
   141   
       
   142   g_return_val_if_fail (error != NULL, NULL);
       
   143   copy = g_new (GError, 1);
       
   144 
       
   145   *copy = *error;
       
   146 
       
   147   copy->message = g_strdup (error->message);
       
   148 
       
   149   return copy;
       
   150 }
       
   151 
       
   152 /**
       
   153  * g_error_matches:
       
   154  * @error: a #GError
       
   155  * @domain: an error domain
       
   156  * @code: an error code
       
   157  * 
       
   158  * Returns %TRUE if @error matches @domain and @code, %FALSE
       
   159  * otherwise.
       
   160  * 
       
   161  * Return value: whether @error has @domain and @code
       
   162  **/
       
   163 EXPORT_C gboolean
       
   164 g_error_matches (const GError *error,
       
   165                  GQuark        domain,
       
   166                  gint          code)
       
   167 {
       
   168   return error &&
       
   169     error->domain == domain &&
       
   170     error->code == code;
       
   171 }
       
   172 
       
   173 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
       
   174                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
       
   175                "The overwriting error message was: %s"
       
   176 
       
   177 /**
       
   178  * g_set_error:
       
   179  * @err: a return location for a #GError, or %NULL
       
   180  * @domain: error domain
       
   181  * @code: error code 
       
   182  * @format: printf()-style format
       
   183  * @Varargs: args for @format 
       
   184  * 
       
   185  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err must
       
   186  * be %NULL. A new #GError is created and assigned to *@err.
       
   187  **/
       
   188 EXPORT_C void
       
   189 g_set_error (GError      **err,
       
   190              GQuark        domain,
       
   191              gint          code,
       
   192              const gchar  *format,
       
   193              ...)
       
   194 {
       
   195   GError *new;
       
   196   
       
   197   va_list args;
       
   198 
       
   199   if (err == NULL)
       
   200     return;
       
   201   
       
   202   va_start (args, format);
       
   203   new = g_error_new_valist (domain, code, format, args);
       
   204   va_end (args);
       
   205 
       
   206   if (*err == NULL)
       
   207     *err = new;
       
   208   else
       
   209     g_warning (ERROR_OVERWRITTEN_WARNING, new->message);    
       
   210 }
       
   211 
       
   212 /**
       
   213  * g_propagate_error:
       
   214  * @dest: error return location
       
   215  * @src: error to move into the return location
       
   216  * 
       
   217  * If @dest is %NULL, free @src; otherwise,
       
   218  * moves @src into *@dest. *@dest must be %NULL.
       
   219  **/
       
   220 EXPORT_C void    
       
   221 g_propagate_error (GError       **dest,
       
   222 		   GError        *src)
       
   223 {
       
   224   g_return_if_fail (src != NULL);
       
   225   
       
   226   if (dest == NULL)
       
   227     {
       
   228       if (src)
       
   229         g_error_free (src);
       
   230       return;
       
   231     }
       
   232   else
       
   233     {
       
   234       if (*dest != NULL)
       
   235         g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
       
   236       else
       
   237         *dest = src;
       
   238     }
       
   239 }
       
   240 
       
   241 /**
       
   242  * g_clear_error:
       
   243  * @err: a #GError return location
       
   244  * 
       
   245  * If @err is %NULL, does nothing. If @err is non-%NULL,
       
   246  * calls g_error_free() on *@err and sets *@err to %NULL.
       
   247  **/
       
   248 EXPORT_C void
       
   249 g_clear_error (GError **err)
       
   250 {
       
   251   if (err && *err)
       
   252     {
       
   253       g_error_free (*err);
       
   254       *err = NULL;
       
   255     }
       
   256 }
       
   257 
       
   258 #define __G_ERROR_C__
       
   259 #include "galiasdef.c"