summaryrefslogtreecommitdiff
path: root/pjlib/src/pj/errno.c
blob: 218c789f1cef23512d4e2a752f822e8ec8a78cbf (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
/* $Header: /pjproject-0.3/pjlib/src/pj/errno.c 2     10/14/05 12:26a Bennylp $ */
/*
 * $Log: /pjproject-0.3/pjlib/src/pj/errno.c $
 * 
 * 2     10/14/05 12:26a Bennylp
 * Finished error code framework, some fixes in ioqueue, etc. Pretty
 * major.
 * 
 * 1     10/08/05 9:53a Bennylp
 * Created.
 *
 */
#include <pj/errno.h>
#include <pj/string.h>
#include <pj/compat/sprintf.h>

/* Prototype for platform specific error message, which will be defined 
 * in separate file.
 */
extern int platform_strerror( pj_os_err_type code, 
                              char *buf, pj_size_t bufsize );

/* PJLIB's own error codes/messages */
static const struct 
{
    int code;
    const char *msg;
} err_str[] = 
{
    { PJ_EUNKNOWN,      "Unknown Error" },
    { PJ_EPENDING,      "Pending operation" },
    { PJ_ETOOMANYCONN,  "Too many connecting sockets" },
    { PJ_EINVAL,        "Invalid value or argument" },
    { PJ_ENAMETOOLONG,  "Name too long" },
    { PJ_ENOTFOUND,     "Not found" },
    { PJ_ENOMEM,        "Not enough memory" },
    { PJ_EBUG,          "BUG DETECTED!" },
    { PJ_ETIMEDOUT,     "Operation timed out" },
    { PJ_ETOOMANY,      "Too many objects of the specified type"},
    { PJ_EBUSY,         "Object is busy"},
    { PJ_ENOTSUP,	"Option/operation is not supported"},
    { PJ_EINVALIDOP,	"Invalid operation"}
};

/*
 * pjlib_error()
 *
 * Retrieve message string for PJLIB's own error code.
 */
static int pjlib_error(pj_status_t code, char *buf, pj_size_t size)
{
    unsigned i;

    for (i=0; i<sizeof(err_str)/sizeof(err_str[0]); ++i) {
        if (err_str[i].code == code) {
            pj_size_t len = strlen(err_str[i].msg);
            if (len >= size) len = size-1;
            pj_memcpy(buf, err_str[i].msg, len);
            buf[len] = '\0';
            return len;
        }
    }

    *buf++ = '?';
    *buf++ = '?';
    *buf++ = '?';
    *buf++ = '\0';
    return 3;
}

/*
 * pj_strerror()
 */
PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode, 
			      char *buf, pj_size_t bufsize )
{
    int len = -1;
    pj_str_t errstr;

    if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) {
        len = pj_snprintf( buf, bufsize, "Unknown error %d", statcode);

    } else if (statcode < PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) {
        len = pjlib_error(statcode, buf, bufsize);

    } else if (statcode < PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) {
        len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize);

    } else if (statcode < PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE) {
        len = pj_snprintf( buf, bufsize, "User error %d", statcode);

    } else {
        len = pj_snprintf( buf, bufsize, "Invalid error %d", statcode);

    }

    if (len < 1) {
        *buf = '\0';
        len = 0;
    }

    errstr.ptr = buf;
    errstr.slen = len;

    return errstr;
}