]> git.lyx.org Git - lyx.git/blob - boost/libs/regex/src/wide_posix_api.cpp
update
[lyx.git] / boost / libs / regex / src / wide_posix_api.cpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr John Maddock
5  *
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.
13  *
14  */
15  
16  /*
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.
21   */
22
23 #define BOOST_REGEX_SOURCE
24
25 #include <boost/regex/config.hpp>
26
27 #ifndef BOOST_NO_WREGEX
28
29 #include <boost/regex.hpp>
30
31 #include <cwchar>
32 #include <cstring>
33 #include <cstdio>
34
35 namespace boost{
36
37 namespace {
38
39 unsigned int wmagic_value = 28631;
40
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"};
45
46 }
47
48 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW* expression, const wchar_t* ptr, int f)
49 {
50    BOOST_RE_GUARD_STACK
51    if(expression->re_magic != wmagic_value)
52    {
53       expression->guts = 0;
54 #ifndef BOOST_NO_EXCEPTIONS
55       try{
56 #endif
57       expression->guts = new wregex();
58 #ifndef BOOST_NO_EXCEPTIONS
59       } catch(...)
60       {
61          return REG_ESPACE;
62       }
63 #else
64       if(0 == expression->guts)
65          return REG_E_MEMORY;
66 #endif
67    }
68    // set default flags:
69    boost::uint_fast32_t flags = (f & REG_EXTENDED) ? regbase::extended : regbase::basic;
70    expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : 0;
71
72    // and translate those that are actually set:
73    if(f & REG_NOCOLLATE)
74       flags |= regbase::nocollate;
75
76    if(f & REG_NOSUB)
77       expression->eflags |= match_any;
78
79    if(f & REG_NOSPEC)
80       flags |= regbase::literal;
81    if(f & REG_ICASE)
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;
87
88    const wchar_t* p2;
89    if(f & REG_PEND)
90       p2 = expression->re_endp;
91    else p2 = ptr + std::wcslen(ptr);
92
93    int result;
94
95 #ifndef BOOST_NO_EXCEPTIONS
96    try{
97 #endif
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
103    } catch(...)
104    {
105       result = REG_E_UNKNOWN;
106    }
107 #endif
108    if(result)
109       regfreeW(expression);
110    return result;
111
112 }
113
114 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW* e, wchar_t* buf, regsize_t buf_size)
115 {
116    BOOST_RE_GUARD_STACK
117    std::size_t result = 0;
118    if(code & REG_ITOA)
119    {
120       code &= ~REG_ITOA;
121       if(code <= REG_E_UNKNOWN)
122       {
123          result = std::wcslen(wnames[code]) + 1;
124          if(buf_size >= result)
125             std::wcscpy(buf, wnames[code]);
126          return result;
127       }
128       return result;
129    }
130 #if !defined(BOOST_NO_SWPRINTF)
131    if(code == REG_ATOI)
132    {
133       wchar_t localbuf[5];
134       if(e == 0)
135          return 0;
136       for(int i = 0; i <= REG_E_UNKNOWN; ++i)
137       {
138          if(std::wcscmp(e->re_endp, wnames[i]) == 0)
139          {
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;
144          }
145       }
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;
150    }
151 #endif
152    if(code <= REG_E_UNKNOWN)
153    {
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());
161       if(len < buf_size)
162       {
163          pt->strwiden(buf, buf_size, p.c_str());
164       }
165       return len + 1;
166    }
167    if(buf_size)
168       *buf = 0;
169    return 0;
170 }
171
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)
173 {
174    BOOST_RE_GUARD_STACK
175    bool result = false;
176    boost::uint_fast32_t flags = match_default | expression->eflags;
177    const wchar_t* end;
178    const wchar_t* start;
179    wcmatch m;
180    
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)
186    {
187       start = buf + array[0].rm_so;
188       end = buf + array[0].rm_eo;
189    }
190    else
191    {
192       start = buf;
193       end = buf + std::wcslen(buf);
194    }
195
196 #ifndef BOOST_NO_EXCEPTIONS
197    try{
198 #endif
199    if(expression->re_magic == wmagic_value)
200    {
201       result = regex_search(start, end, m, *static_cast<wregex*>(expression->guts), flags);
202    }
203    else
204       return result;
205 #ifndef BOOST_NO_EXCEPTIONS
206    } catch(...)
207    {
208       return REG_E_UNKNOWN;
209    }
210 #endif
211    if(result)
212    {
213       // extract what matched:
214      unsigned int i;
215       for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
216       {
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);
219       }
220       // and set anything else to -1:
221       for(i = expression->re_nsub + 1; i < n; ++i)
222       {
223          array[i].rm_so = -1;
224          array[i].rm_eo = -1;
225       }
226       return 0;
227    }
228    return REG_NOMATCH;
229 }
230
231 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW* expression)
232 {
233    BOOST_RE_GUARD_STACK
234    if(expression->re_magic == wmagic_value)
235    {
236       delete static_cast<wregex*>(expression->guts);
237    }
238    expression->re_magic = 0;
239 }
240
241 } // namespace boost;
242
243 #endif
244
245
246