blob: 6d05576afbd8995b75ae35fe9898936e82ddea5d [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$
2 */
3/*
4 * PJLIB - PJ Foundation Library
5 * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
6 *
7 * Author:
8 * Benny Prijono <bennylp@bulukucing.org>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24#include <pj/errno.h>
25#include <pj/string.h>
26#include <pj/compat/sprintf.h>
27
28/* Prototype for platform specific error message, which will be defined
29 * in separate file.
30 */
31extern int platform_strerror( pj_os_err_type code,
32 char *buf, pj_size_t bufsize );
33
34/* PJLIB's own error codes/messages */
35static const struct
36{
37 int code;
38 const char *msg;
39} err_str[] =
40{
41 { PJ_EUNKNOWN, "Unknown Error" },
42 { PJ_EPENDING, "Pending operation" },
43 { PJ_ETOOMANYCONN, "Too many connecting sockets" },
44 { PJ_EINVAL, "Invalid value or argument" },
45 { PJ_ENAMETOOLONG, "Name too long" },
46 { PJ_ENOTFOUND, "Not found" },
47 { PJ_ENOMEM, "Not enough memory" },
48 { PJ_EBUG, "BUG DETECTED!" },
49 { PJ_ETIMEDOUT, "Operation timed out" },
50 { PJ_ETOOMANY, "Too many objects of the specified type"},
51 { PJ_EBUSY, "Object is busy"},
52 { PJ_ENOTSUP, "Option/operation is not supported"},
Benny Prijono85d3f452005-11-09 15:37:19 +000053 { PJ_EINVALIDOP, "Invalid operation"},
Benny Prijonof010e692005-11-11 19:01:31 +000054 { PJ_ECANCELLED, "Operation cancelled"},
Benny Prijonoe7224612005-11-13 19:40:44 +000055 { PJ_EEXISTS, "Object already exists" }
56};
57
58/*
59 * pjlib_error()
60 *
61 * Retrieve message string for PJLIB's own error code.
62 */
63static int pjlib_error(pj_status_t code, char *buf, pj_size_t size)
64{
65 unsigned i;
66
67 for (i=0; i<sizeof(err_str)/sizeof(err_str[0]); ++i) {
68 if (err_str[i].code == code) {
69 pj_size_t len = strlen(err_str[i].msg);
70 if (len >= size) len = size-1;
71 pj_memcpy(buf, err_str[i].msg, len);
72 buf[len] = '\0';
73 return len;
74 }
75 }
76
77 *buf++ = '?';
78 *buf++ = '?';
79 *buf++ = '?';
80 *buf++ = '\0';
81 return 3;
82}
83
84/*
85 * pj_strerror()
86 */
87PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode,
88 char *buf, pj_size_t bufsize )
89{
90 int len = -1;
91 pj_str_t errstr;
92
93 if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) {
94 len = pj_snprintf( buf, bufsize, "Unknown error %d", statcode);
95
96 } else if (statcode < PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) {
97 len = pjlib_error(statcode, buf, bufsize);
98
99 } else if (statcode < PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) {
100 len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize);
101
102 } else if (statcode < PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE) {
103 len = pj_snprintf( buf, bufsize, "User error %d", statcode);
104
105 } else {
106 len = pj_snprintf( buf, bufsize, "Invalid error %d", statcode);
107
108 }
109
110 if (len < 1) {
111 *buf = '\0';
112 len = 0;
113 }
114
115 errstr.ptr = buf;
116 errstr.slen = len;
117
118 return errstr;
119}
120