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