Back to index

php5  5.3.10
regposerr.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   regposerr.c - Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2005  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include "config.h"
00031 #include "onigposix.h"
00032 
00033 #ifdef HAVE_STRING_H
00034 # include <string.h>
00035 #else
00036 # include <strings.h>
00037 #endif
00038 
00039 static char* ESTRING[] = {
00040   NULL,
00041   "failed to match",                         /* REG_NOMATCH    */
00042   "Invalid regular expression",              /* REG_BADPAT     */
00043   "invalid collating element referenced",    /* REG_ECOLLATE   */
00044   "invalid character class type referenced", /* REG_ECTYPE     */
00045   "bad backslash-escape sequence",           /* REG_EESCAPE    */
00046   "invalid back reference number",           /* REG_ESUBREG    */
00047   "imbalanced [ and ]",                      /* REG_EBRACK     */
00048   "imbalanced ( and )",                      /* REG_EPAREN     */
00049   "imbalanced { and }",                      /* REG_EBRACE     */
00050   "invalid repeat range {n,m}",              /* REG_BADBR      */
00051   "invalid range",                           /* REG_ERANGE     */
00052   "Out of memory",                           /* REG_ESPACE     */
00053   "? * + not preceded by valid regular expression", /* REG_BADRPT   */
00054 
00055   /* Extended errors */
00056   "internal error",                          /* REG_EONIG_INTERNAL */
00057   "invalid wide char value",                 /* REG_EONIG_BADWC    */
00058   "invalid argument",                        /* REG_EONIG_BADARG   */
00059   "multi-thread error"                       /* REG_EONIG_THREAD   */
00060 };
00061 
00062 #include <stdio.h>
00063 
00064 
00065 extern size_t
00066 regerror(int posix_ecode, const regex_t* reg, char* buf, size_t size)
00067 {
00068   char* s;
00069   char tbuf[35];
00070   size_t len;
00071 
00072   if (posix_ecode > 0 && posix_ecode < sizeof(ESTRING) / sizeof(ESTRING[0])) {
00073     s = ESTRING[posix_ecode];
00074   }
00075   else if (posix_ecode == 0) {
00076     s = "";
00077   }
00078   else {
00079     sprintf(tbuf, "undefined error code (%d)", posix_ecode);
00080     s = tbuf;
00081   }
00082 
00083   len = strlen(s) + 1; /* use strlen() because s is ascii encoding. */
00084 
00085   if (buf != NULL && size > 0) {
00086     strncpy(buf, s, size - 1);
00087     buf[size - 1] = '\0';
00088   }
00089   return len;
00090 }