gerror.c 3.66 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3 4
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This 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
12
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19

20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "glib.h"
Owen Taylor's avatar
Owen Taylor committed
28

Havoc Pennington's avatar
Havoc Pennington committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static GError* 
g_error_new_valist(GQuark         domain,
                   gint           code,
                   const gchar   *format,
                   va_list        args)
{
  GError *error;
  
  error = g_new (GError, 1);
  
  error->domain = domain;
  error->code = code;
  error->message = g_strdup_vprintf (format, args);
  
  return error;
}

GError*
g_error_new (GQuark       domain,
             gint         code,
             const gchar *format,
             ...)
{
  GError* error;
  va_list args;
Owen Taylor's avatar
Owen Taylor committed
54

Havoc Pennington's avatar
Havoc Pennington committed
55 56
  g_return_val_if_fail (format != NULL, NULL);
  g_return_val_if_fail (domain != 0, NULL);
Owen Taylor's avatar
Owen Taylor committed
57

Havoc Pennington's avatar
Havoc Pennington committed
58 59 60
  va_start (args, format);
  error = g_error_new_valist (domain, code, format, args);
  va_end (args);
Owen Taylor's avatar
Owen Taylor committed
61

Havoc Pennington's avatar
Havoc Pennington committed
62 63
  return error;
}
Owen Taylor's avatar
Owen Taylor committed
64

Havoc Pennington's avatar
Havoc Pennington committed
65 66 67 68 69 70
GError*
g_error_new_literal (GQuark         domain,
                     gint           code,
                     const gchar   *message)
{
  GError* err;
Owen Taylor's avatar
Owen Taylor committed
71

Havoc Pennington's avatar
Havoc Pennington committed
72 73
  g_return_val_if_fail (message != NULL, NULL);
  g_return_val_if_fail (domain != 0, NULL);
Owen Taylor's avatar
Owen Taylor committed
74

Havoc Pennington's avatar
Havoc Pennington committed
75
  err = g_new (GError, 1);
Owen Taylor's avatar
Owen Taylor committed
76

Havoc Pennington's avatar
Havoc Pennington committed
77 78 79 80 81 82
  err->domain = domain;
  err->code = code;
  err->message = g_strdup (message);
  
  return err;
}
Owen Taylor's avatar
Owen Taylor committed
83 84

void
Havoc Pennington's avatar
Havoc Pennington committed
85
g_error_free (GError *error)
Owen Taylor's avatar
Owen Taylor committed
86
{
Havoc Pennington's avatar
Havoc Pennington committed
87
  g_return_if_fail (error != NULL);  
88

Havoc Pennington's avatar
Havoc Pennington committed
89
  g_free (error->message);
90

Havoc Pennington's avatar
Havoc Pennington committed
91
  g_free (error);
Owen Taylor's avatar
Owen Taylor committed
92 93
}

Havoc Pennington's avatar
Havoc Pennington committed
94 95
GError*
g_error_copy (const GError *error)
Owen Taylor's avatar
Owen Taylor committed
96
{
Havoc Pennington's avatar
Havoc Pennington committed
97 98 99
  GError *copy;
  
  g_return_val_if_fail (error != NULL, NULL);
100

Havoc Pennington's avatar
Havoc Pennington committed
101
  copy = g_new (GError, 1);
Owen Taylor's avatar
Owen Taylor committed
102

Havoc Pennington's avatar
Havoc Pennington committed
103
  *copy = *error;
Owen Taylor's avatar
Owen Taylor committed
104

Havoc Pennington's avatar
Havoc Pennington committed
105
  copy->message = g_strdup (error->message);
Owen Taylor's avatar
Owen Taylor committed
106

Havoc Pennington's avatar
Havoc Pennington committed
107
  return copy;
108 109
}

Havoc Pennington's avatar
Havoc Pennington committed
110 111 112 113
gboolean
g_error_matches (const GError *error,
                 GQuark        domain,
                 gint          code)
114
{
Havoc Pennington's avatar
Havoc Pennington committed
115 116 117
  return error &&
    error->domain == domain &&
    error->code == code;
Owen Taylor's avatar
Owen Taylor committed
118 119
}

120 121 122
#define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
               "This indicates a bug in someone's code. You must ensure an error is NULL before it's set."

Havoc Pennington's avatar
Havoc Pennington committed
123
void
124 125 126 127
g_set_error (GError      **err,
             GQuark        domain,
             gint          code,
             const gchar  *format,
Havoc Pennington's avatar
Havoc Pennington committed
128
             ...)
Owen Taylor's avatar
Owen Taylor committed
129
{
Havoc Pennington's avatar
Havoc Pennington committed
130
  va_list args;
Owen Taylor's avatar
Owen Taylor committed
131

Havoc Pennington's avatar
Havoc Pennington committed
132 133
  if (err == NULL)
    return;
Owen Taylor's avatar
Owen Taylor committed
134

Havoc Pennington's avatar
Havoc Pennington committed
135
  if (*err != NULL)
136
    g_warning (ERROR_OVERWRITTEN_WARNING);
Havoc Pennington's avatar
Havoc Pennington committed
137 138 139 140 141
  
  va_start (args, format);
  *err = g_error_new_valist (domain, code, format, args);
  va_end (args);
}
Owen Taylor's avatar
Owen Taylor committed
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
void    
g_propagate_error (GError       **dest,
		   GError        *src)
{
  g_return_if_fail (src != NULL);

  if (dest == NULL)
    return;
  
  if (*dest != NULL)
    g_warning (ERROR_OVERWRITTEN_WARNING);
 
  *dest = src;
}

Havoc Pennington's avatar
Havoc Pennington committed
158 159 160 161
void
g_clear_error (GError **err)
{
  if (err && *err)
Owen Taylor's avatar
Owen Taylor committed
162
    {
Havoc Pennington's avatar
Havoc Pennington committed
163 164
      g_error_free (*err);
      *err = NULL;
Owen Taylor's avatar
Owen Taylor committed
165 166
    }
}