]> git.saurik.com Git - wxWidgets.git/blame - src/regex/regerror.c
fixed Makefile.in for samples
[wxWidgets.git] / src / regex / regerror.c
CommitLineData
1f0db2d9 1/*
c0a7edde
RN
2 * regerror - error-code expansion
3 *
4 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
5 *
6 * Development of this software was funded, in part, by Cray Research Inc.,
7 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
8 * Corporation, none of whom are responsible for the results. The author
9 * thanks all of them.
10 *
11 * Redistribution and use in source and binary forms -- with or without
12 * modification -- are permitted for any purpose, provided that
13 * redistributions in source form retain this entire copyright notice and
14 * indicate the origin and nature of any modifications.
15 *
16 * I'd appreciate being given credit for this package in the documentation
17 * of software which uses it, but that is not a requirement.
18 *
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Header: /projects/cvsroot/pgsql-server/src/backend/regex/regerror.c,v 1.26 2003/08/04 00:43:21 momjian Exp $
31 *
1f0db2d9 32 */
c0a7edde
RN
33
34#include "regguts.h"
35
36/* unknown-error explanation */
37static char unk[] = "*** unknown regex error code 0x%x ***";
38
39/* struct to map among codes, code names, and explanations */
40static struct rerr
41{
07dcc217
VZ
42 int code;
43 char *name;
44 char *explain;
c0a7edde
RN
45} rerrs[] =
46
47{
48 /* the actual table is built from regex.h */
49#include "regerrs.h"
50 {
51 -1, "", "oops"
52 }, /* explanation special-cased in code */
07dcc217
VZ
53};
54
c0a7edde
RN
55size_t
56regerror(int errcode, /* error code, or REG_ATOI or REG_ITOA */
57 const regex_t *preg, /* associated regex_t (unused at present) */
58 char *errbuf, /* result buffer (unless errbuf_size==0) */
59 size_t errbuf_size) /* available space in errbuf, can be 0 */
60 { return regerror(errcode, preg, errbuf, errbuf_size); }
07dcc217 61/*
c0a7edde 62 * pg_regerror - the interface to error numbers
07dcc217
VZ
63 */
64/* ARGSUSED */
c0a7edde
RN
65size_t /* actual space needed (including NUL) */
66wx_regerror(int errcode, /* error code, or REG_ATOI or REG_ITOA */
67 const regex_t *preg, /* associated regex_t (unused at present) */
68 char *errbuf, /* result buffer (unless errbuf_size==0) */
69 size_t errbuf_size) /* available space in errbuf, can be 0 */
07dcc217 70{
c0a7edde
RN
71 struct rerr *r;
72 char *msg;
73 char convbuf[sizeof(unk) + 50]; /* 50 = plenty for int */
74 size_t len;
75 int icode;
07dcc217 76
c0a7edde
RN
77 switch (errcode)
78 {
79 case REG_ATOI: /* convert name to number */
07dcc217 80 for (r = rerrs; r->code >= 0; r++)
c0a7edde 81 if (strcmp(r->name, errbuf) == 0)
07dcc217 82 break;
c0a7edde
RN
83 sprintf(convbuf, "%d", r->code); /* -1 for unknown */
84 msg = convbuf;
85 break;
86 case REG_ITOA: /* convert number to name */
87 icode = atoi(errbuf); /* not our problem if this fails */
88 for (r = rerrs; r->code >= 0; r++)
89 if (r->code == icode)
90 break;
91 if (r->code >= 0)
92 msg = r->name;
93 else
94 { /* unknown; tell him the number */
95 sprintf(convbuf, "REG_%u", (unsigned) icode);
96 msg = convbuf;
97 }
98 break;
99 default: /* a real, normal error code */
100 for (r = rerrs; r->code >= 0; r++)
101 if (r->code == errcode)
102 break;
1f0db2d9 103 if (r->code >= 0)
c0a7edde 104 msg = r->explain;
1f0db2d9 105 else
c0a7edde
RN
106 { /* unknown; say so */
107 sprintf(convbuf, unk, errcode);
108 msg = convbuf;
109 }
110 break;
07dcc217
VZ
111 }
112
c0a7edde
RN
113 len = strlen(msg) + 1; /* space needed, including NUL */
114 if (errbuf_size > 0)
115 {
07dcc217 116 if (errbuf_size > len)
c0a7edde
RN
117 strcpy(errbuf, msg);
118 else
119 { /* truncate to fit */
120 memcpy(errbuf, msg, errbuf_size - 1); /*RN - was strncpy*/
121 errbuf[errbuf_size - 1] = '\0';
07dcc217
VZ
122 }
123 }
124
c0a7edde 125 return len;
07dcc217 126}