File: core.c

package info (click to toggle)
discover 2.1.2-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,756 kB
  • sloc: sh: 7,862; ansic: 7,280; xml: 1,809; makefile: 679
file content (147 lines) | stat: -rw-r--r-- 3,564 bytes parent folder | download | duplicates (9)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * @file core.c
 * @brief Core and miscellaneous routines
 *
 * This file provides core routines that don't really fit anyplace
 * else.  There are likely some other routines that really should be in
 * here.
 *
 */

/* $Progeny$
 *
 * Copyright 2002 Progeny Linux Systems, Inc.
 * Copyright 2002 Hewlett-Packard Company
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <assert.h>

#include <discover/discover.h>
#include <discover/discover-xml.h>

#include <discover/utils.h>

static char *discover_errlist[] = {
    N_("Success."),
    N_("Input/output error."),
    N_("XML parsing error."),
    N_("System error."),
    N_("Disabled bus."),
    N_("Bus not found."),
    N_("Data source not found."),
    N_("Device not found."),
    N_("Invalid version range."),
    N_("Not implemented.")
};
static int
discover_nerr = sizeof(discover_errlist) / sizeof(discover_errlist[0]);

/**
 * @defgroup core Core functionality
 * @{
 */

/**
 * Allocate and initialize a discover_error_t structure.
 */
discover_error_t *
discover_error_new(void)
{
    discover_error_t *status = _discover_xmalloc(sizeof(discover_error_t));
    status->code = 0;
    status->message = NULL;
    status->create_message = _discover_create_message;
    return status;
}

/**
 * Allocate memory for a char * variable.
 * This routine was written for discover_error_t struct.
 */
void *
_discover_create_message(discover_error_t **status, char *message)
{
    if((*status)->message) {
        free((*status)->message);
    }
    (*status)->message = _discover_xmalloc(strlen(message) + 1);
    strcpy((*status)->message, message);
}

/**
 * Free a discover_error_t structure.
 */
void
discover_error_free(discover_error_t *status)
{
    assert(status != NULL);
    if (status->message != NULL) {
        free(status->message);
    }
    free(status);
}

/**
 * Convert an error code to a human-readable string localized
 * according to the current locale (XXX: no i18n yet).
 *
 * @param err Error code to convert
 */
char *
discover_strerror(discover_error_t *err)
{
    assert(err != NULL);
    assert(err->code >= 0);
    assert(err->code < discover_nerr);

    return _(discover_errlist[err->code]);
}

/**
 * Get the major version number.
 */
int
discover_major_version(void)
{
    return PACKAGE_MAJOR;
}

/**
 * Get the minor version number.
 */
int
discover_minor_version(void)
{
    return PACKAGE_MINOR;
}

/**
 * Get the micro version number.
 */
int
discover_micro_version(void)
{
    return PACKAGE_MICRO;
}

/** @} */