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.
18 * FILE: wide_posix_api.cpp
19 * VERSION: see <boost/version.hpp>
20 * DESCRIPTION: Implements the wide character POSIX API wrappers.
23 #define BOOST_REGEX_SOURCE
25 #include <boost/regex/config.hpp>
27 #ifndef BOOST_NO_WREGEX
29 #include <boost/regex.hpp>
39 unsigned int wmagic_value = 28631;
41 const wchar_t* wnames[] = {L"REG_NOERROR", L"REG_NOMATCH", L"REG_BADPAT", L"REG_ECOLLATE",
42 L"REG_ECTYPE", L"REG_EESCAPE", L"REG_ESUBREG", L"REG_EBRACK",
43 L"REG_EPAREN", L"REG_EBRACE", L"REG_BADBR", L"REG_ERANGE",
44 L"REG_ESPACE", L"REG_BADRPT", L"REG_EMPTY", L"REG_E_UNKNOWN"};
48 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW* expression, const wchar_t* ptr, int f)
51 if(expression->re_magic != wmagic_value)
54 #ifndef BOOST_NO_EXCEPTIONS
57 expression->guts = new wregex();
58 #ifndef BOOST_NO_EXCEPTIONS
64 if(0 == expression->guts)
69 boost::uint_fast32_t flags = (f & REG_EXTENDED) ? regbase::extended : regbase::basic;
70 expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : 0;
72 // and translate those that are actually set:
74 flags |= regbase::nocollate;
77 expression->eflags |= match_any;
80 flags |= regbase::literal;
82 flags |= regbase::icase;
83 if(f & REG_ESCAPE_IN_LISTS)
84 flags |= regbase::escape_in_lists;
85 if(f & REG_NEWLINE_ALT)
86 flags |= regbase::newline_alt;
90 p2 = expression->re_endp;
91 else p2 = ptr + std::wcslen(ptr);
95 #ifndef BOOST_NO_EXCEPTIONS
98 expression->re_magic = wmagic_value;
99 static_cast<wregex*>(expression->guts)->set_expression(ptr, p2, flags);
100 expression->re_nsub = static_cast<wregex*>(expression->guts)->mark_count() - 1;
101 result = static_cast<wregex*>(expression->guts)->error_code();
102 #ifndef BOOST_NO_EXCEPTIONS
105 result = REG_E_UNKNOWN;
109 regfreeW(expression);
114 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW* e, wchar_t* buf, regsize_t buf_size)
117 std::size_t result = 0;
121 if(code <= REG_E_UNKNOWN)
123 result = std::wcslen(wnames[code]) + 1;
124 if(buf_size >= result)
125 std::wcscpy(buf, wnames[code]);
130 #if !defined(BOOST_NO_SWPRINTF)
136 for(int i = 0; i <= REG_E_UNKNOWN; ++i)
138 if(std::wcscmp(e->re_endp, wnames[i]) == 0)
140 std::swprintf(localbuf, 5, L"%d", i);
141 if(std::wcslen(localbuf) < buf_size)
142 std::wcscpy(buf, localbuf);
143 return std::wcslen(localbuf) + 1;
146 std::swprintf(localbuf, 5, L"%d", 0);
147 if(std::wcslen(localbuf) < buf_size)
148 std::wcscpy(buf, localbuf);
149 return std::wcslen(localbuf) + 1;
152 if(code <= REG_E_UNKNOWN)
154 regex_traits<wchar_t> rt;
155 const regex_traits<wchar_t>* pt = &rt;
156 if(e && (e->re_magic == wmagic_value))
157 pt = &static_cast<wregex*>(e->guts)->get_traits();
158 (void)pt; // warning suppression
159 std::string p = pt->error_string(code);
160 std::size_t len = pt->strwiden(static_cast<wchar_t*>(0), 0, p.c_str());
163 pt->strwiden(buf, buf_size, p.c_str());
172 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW* expression, const wchar_t* buf, regsize_t n, regmatch_t* array, int eflags)
176 boost::uint_fast32_t flags = match_default | expression->eflags;
178 const wchar_t* start;
181 if(eflags & REG_NOTBOL)
182 flags |= match_not_bol;
183 if(eflags & REG_NOTEOL)
184 flags |= match_not_eol;
185 if(eflags & REG_STARTEND)
187 start = buf + array[0].rm_so;
188 end = buf + array[0].rm_eo;
193 end = buf + std::wcslen(buf);
196 #ifndef BOOST_NO_EXCEPTIONS
199 if(expression->re_magic == wmagic_value)
201 result = regex_search(start, end, m, *static_cast<wregex*>(expression->guts), flags);
205 #ifndef BOOST_NO_EXCEPTIONS
208 return REG_E_UNKNOWN;
213 // extract what matched:
215 for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
217 array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
218 array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
220 // and set anything else to -1:
221 for(i = expression->re_nsub + 1; i < n; ++i)
231 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW* expression)
234 if(expression->re_magic == wmagic_value)
236 delete static_cast<wregex*>(expression->guts);
238 expression->re_magic = 0;
241 } // namespace boost;