3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
19 * VERSION: see <boost/version.hpp>
20 * DESCRIPTION: Implements the Posix API wrappers.
23 #define BOOST_REGEX_SOURCE
26 #include <boost/regex.hpp>
32 unsigned int magic_value = 25631;
34 const char* names[] = {"REG_NOERROR", "REG_NOMATCH", "REG_BADPAT", "REG_ECOLLATE",
35 "REG_ECTYPE", "REG_EESCAPE", "REG_ESUBREG", "REG_EBRACK",
36 "REG_EPAREN", "REG_EBRACE", "REG_BADBR", "REG_ERANGE",
37 "REG_ESPACE", "REG_BADRPT", "REG_EMPTY", "REG_E_UNKNOWN"};
40 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
43 if(expression->re_magic != magic_value)
46 #ifndef BOOST_NO_EXCEPTIONS
49 expression->guts = new regex();
50 #ifndef BOOST_NO_EXCEPTIONS
56 if(0 == expression->guts)
61 boost::uint_fast32_t flags = (f & REG_EXTENDED) ? regbase::extended : regbase::basic;
62 expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : 0;
63 // and translate those that are actually set:
66 flags |= regbase::nocollate;
69 expression->eflags |= match_any;
72 flags |= regbase::literal;
74 flags |= regbase::icase;
75 if(f & REG_ESCAPE_IN_LISTS)
76 flags |= regbase::escape_in_lists;
77 if(f & REG_NEWLINE_ALT)
78 flags |= regbase::newline_alt;
82 p2 = expression->re_endp;
83 else p2 = ptr + std::strlen(ptr);
87 #ifndef BOOST_NO_EXCEPTIONS
90 expression->re_magic = magic_value;
91 static_cast<regex*>(expression->guts)->set_expression(ptr, p2, flags);
92 expression->re_nsub = static_cast<regex*>(expression->guts)->mark_count() - 1;
93 result = static_cast<regex*>(expression->guts)->error_code();
94 #ifndef BOOST_NO_EXCEPTIONS
97 result = REG_E_UNKNOWN;
101 regfreeA(expression);
106 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
109 std::size_t result = 0;
113 if(code <= REG_E_UNKNOWN)
115 result = std::strlen(names[code]) + 1;
116 if(buf_size >= result)
117 std::strcpy(buf, names[code]);
127 for(int i = 0; i <= REG_E_UNKNOWN; ++i)
129 if(std::strcmp(e->re_endp, names[i]) == 0)
131 std::sprintf(localbuf, "%d", i);
132 if(std::strlen(localbuf) < buf_size)
133 std::strcpy(buf, localbuf);
134 return std::strlen(localbuf) + 1;
137 std::sprintf(localbuf, "%d", 0);
138 if(std::strlen(localbuf) < buf_size)
139 std::strcpy(buf, localbuf);
140 return std::strlen(localbuf) + 1;
142 if(code <= REG_E_UNKNOWN)
145 if((e) && (e->re_magic == magic_value))
146 p = static_cast<regex*>(e->guts)->get_traits().error_string(code);
149 boost::regex_traits<char> t;
150 p = t.error_string(code);
152 std::size_t len = p.size();
155 std::strcpy(buf, p.c_str());
164 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
168 boost::uint_fast32_t flags = match_default | expression->eflags;
173 if(eflags & REG_NOTBOL)
174 flags |= match_not_bol;
175 if(eflags & REG_NOTEOL)
176 flags |= match_not_eol;
177 if(eflags & REG_STARTEND)
179 start = buf + array[0].rm_so;
180 end = buf + array[0].rm_eo;
185 end = buf + std::strlen(buf);
188 #ifndef BOOST_NO_EXCEPTIONS
191 if(expression->re_magic == magic_value)
193 result = regex_search(start, end, m, *static_cast<regex*>(expression->guts), flags);
197 #ifndef BOOST_NO_EXCEPTIONS
200 return REG_E_UNKNOWN;
206 // extract what matched:
208 for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
210 array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
211 array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
213 // and set anything else to -1:
214 for(i = expression->re_nsub + 1; i < n; ++i)
224 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
227 if(expression->re_magic == magic_value)
229 delete static_cast<regex*>(expression->guts);
231 expression->re_magic = 0;