/* $Id$ */ /* * Copyright (C)2003-2006 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include /* 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 ); #define PJLIB_MAX_ERR_MSG_HANDLER 8 /* Error message handler. */ static unsigned err_msg_hnd_cnt; static struct err_msg_hnd { pj_status_t begin; pj_status_t end; pj_str_t (*strerror)(pj_status_t, char*, pj_size_t); } err_msg_hnd[PJLIB_MAX_ERR_MSG_HANDLER]; /* 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"}, { PJ_ECANCELLED, "Operation cancelled"}, { PJ_EEXISTS, "Object already exists" } }; /* * 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= size) len = size-1; pj_memcpy(buf, err_str[i].msg, len); buf[len] = '\0'; return len; } } *buf++ = '?'; *buf++ = '?'; *buf++ = '?'; *buf++ = '\0'; return 3; } #define IN_RANGE(val,start,end) ((val)>=(start) && (val)<(end)) /* Register strerror handle. */ PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start, pj_status_t space, pj_str_t (*f)(pj_status_t,char*, pj_size_t)) { unsigned i; /* Check arguments. */ PJ_ASSERT_RETURN(start && space && f, PJ_EINVAL); /* Check if there aren't too many handlers registered. */ PJ_ASSERT_RETURN(err_msg_hnd_cnt < PJ_ARRAY_SIZE(err_msg_hnd), PJ_ETOOMANY); /* Start error must be greater than PJ_ERRNO_START_USER */ PJ_ASSERT_RETURN(start >= PJ_ERRNO_START_USER, PJ_EEXISTS); /* Check that no existing handler has covered the specified range. */ for (i=0; i