aboutsummaryrefslogtreecommitdiff
path: root/libwmc/src/errors.c
blob: a1d87ba18cad2a72d9a501916ae0675390e718de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "errors.h"
#include <stdlib.h>
#include <string.h>

WmcError *
wmc_error_new (u_int32_t domain,
               u_int32_t code,
               const char *format,
               ...)
{
    WmcError *error;
    va_list args;
    int n;

    wmc_return_val_if_fail (format != NULL, NULL);
    wmc_return_val_if_fail (format[0] != '\0', NULL);

    error = malloc (sizeof (WmcError));
    wmc_assert (error != NULL);

    error->domain = domain;
    error->code = code;

    va_start (args, format);
    n = vasprintf (&error->message, format, args);
    va_end (args);

    if (n < 0) {
        free (error);
        return NULL;
    }

    return error;
}

void
wmc_error_set (WmcError **error,
               u_int32_t domain,
               u_int32_t code,
               const char *format,
               ...)
{
    va_list args;
    int n;

    if (error == NULL)
        return;
    wmc_return_if_fail (*error == NULL);
    wmc_return_if_fail (format[0] != '\0');

    *error = malloc (sizeof (WmcError));
    wmc_assert (*error != NULL);

    (*error)->domain = domain;
    (*error)->code = code;

    va_start (args, format);
    n = vasprintf (&(*error)->message, format, args);
    va_end (args);

    if (n < 0) {
        free (*error);
        *error = NULL;
    }
}

static void
free_error (WmcError *error)
{
    if (error) {
        if (error->message)
            free (error->message);
        memset (error, 0, sizeof (*error));
        free (error);
    }
}

void
wmc_clear_error (WmcError **error)
{
    if (error) {
        free_error (*error);
        *error = NULL;
    }
}

void
wmc_free_error (WmcError *error)
{
    free_error (error);
}