Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/regex/src/regex.cpp @ 13

Last change on this file since 13 was 12, checked in by landauf, 17 years ago

added boost

File size: 5.3 KB
Line 
1/*
2 *
3 * Copyright (c) 1998-2004
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 /*
13  *   LOCATION:    see http://www.boost.org for most recent version.
14  *   FILE:        regex.cpp
15  *   VERSION:     see <boost/version.hpp>
16  *   DESCRIPTION: Misc boost::regbase member funnctions.
17  */
18
19
20#define BOOST_REGEX_SOURCE
21
22#include <new>
23#include <boost/regex.hpp>
24#include <boost/throw_exception.hpp>
25
26#if defined(BOOST_REGEX_HAS_MS_STACK_GUARD) && defined(_MSC_VER) && (_MSC_VER >= 1300)
27#  include <malloc.h>
28#endif
29#ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
30#define WIN32_LEAN_AND_MEAN
31#define NOMINMAX
32#define NOGDI
33#define NOUSER
34#include <windows.h>
35#endif
36
37#if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_REGEX_V3)
38#if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
39#include <new>
40#else
41#include <boost/regex/v4/mem_block_cache.hpp>
42#endif
43#endif
44
45
46namespace boost{
47
48//
49// fix: these are declared out of line here to ensure
50// that dll builds contain the Virtual table for these
51// types - this ensures that exceptions can be thrown
52// from the dll and caught in an exe.
53regex_error::regex_error(const std::string& s, regex_constants::error_type err, std::ptrdiff_t pos) 
54   : std::runtime_error(s)
55   , m_error_code(err)
56   , m_position(pos) 
57{
58}
59
60regex_error::regex_error(regex_constants::error_type err) 
61   : std::runtime_error(::boost::re_detail::get_default_error_string(err))
62   , m_error_code(err)
63   , m_position(0) 
64{
65}
66
67regex_error::~regex_error() throw() 
68{
69}
70
71void regex_error::raise()const
72{
73#ifndef BOOST_NO_EXCEPTIONS
74   ::boost::throw_exception(*this);
75#endif
76}
77
78
79
80namespace re_detail{
81
82BOOST_REGEX_DECL void BOOST_REGEX_CALL raise_runtime_error(const std::runtime_error& ex)
83{
84   ::boost::throw_exception(ex);
85}
86//
87// error checking API:
88//
89BOOST_REGEX_DECL void BOOST_REGEX_CALL verify_options(boost::regex::flag_type /*ef*/, match_flag_type mf)
90{
91#ifndef BOOST_REGEX_V3
92   //
93   // can't mix match_extra with POSIX matching rules:
94   //
95   if((mf & match_extra) && (mf & match_posix))
96   {
97      std::logic_error msg("Usage Error: Can't mix regular expression captures with POSIX matching rules");
98      throw_exception(msg);
99   }
100#endif
101}
102
103#ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
104
105static void execute_eror()
106{
107   // we only get here after a stack overflow,
108   // this has to be a separate proceedure because we
109   // can't mix __try{}__except block with local objects 
110   // that have destructors:
111   reset_stack_guard_page();
112   std::runtime_error err("Out of stack space, while attempting to match a regular expression.");
113   raise_runtime_error(err);
114}
115
116bool BOOST_REGEX_CALL abstract_protected_call::execute()const
117{
118   __try{
119      return this->call();
120   }__except(EXCEPTION_STACK_OVERFLOW == GetExceptionCode())
121   {
122      execute_eror();
123   }
124   // We never really get here at all:
125   return false;
126}
127
128BOOST_REGEX_DECL void BOOST_REGEX_CALL reset_stack_guard_page()
129{
130#if defined(BOOST_REGEX_HAS_MS_STACK_GUARD) && defined(_MSC_VER) && (_MSC_VER >= 1300)
131   _resetstkoflw();
132#else
133   //
134   // We need to locate the current page being used by the stack,
135   // move to the page below it and then deallocate and protect
136   // that page.  Note that ideally we would protect only the lowest
137   // stack page that has been allocated: in practice there
138   // seems to be no easy way to locate this page, in any case as
139   // long as the next page is protected, then Windows will figure
140   // the rest out for us...
141   //
142   SYSTEM_INFO si;
143   GetSystemInfo(&si);
144   MEMORY_BASIC_INFORMATION mi;
145   DWORD previous_protection_status;
146   //
147   // this is an address in our stack space:
148   //
149   LPBYTE page = (LPBYTE)&page;
150   //
151   // Get the current memory page in use:
152   //
153   VirtualQuery(page, &mi, sizeof(mi));
154   //
155   // Go to the page one below this:
156   //
157   page = (LPBYTE)(mi.BaseAddress)-si.dwPageSize;
158   //
159   // Free and protect everything from the start of the
160   // allocation range, to the end of the page below the
161   // one in use:
162   //
163   if (!VirtualFree(mi.AllocationBase, (LPBYTE)page - (LPBYTE)mi.AllocationBase, MEM_DECOMMIT)
164      || !VirtualProtect(page, si.dwPageSize, PAGE_GUARD | PAGE_READWRITE, &previous_protection_status))
165   {
166      throw std::bad_exception();
167   }
168#endif
169}
170#endif
171
172#if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_REGEX_V3)
173
174#if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
175
176BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
177{
178   return ::operator new(BOOST_REGEX_BLOCKSIZE);
179}
180
181BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)
182{
183   ::operator delete(p);
184}
185
186#else
187
188#ifdef BOOST_HAS_THREADS
189mem_block_cache block_cache = { 0, 0, BOOST_STATIC_MUTEX_INIT, };
190#else
191mem_block_cache block_cache = { 0, 0, };
192#endif
193
194BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
195{
196   return block_cache.get();
197}
198
199BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)
200{
201   block_cache.put(p);
202}
203
204#endif
205
206#endif
207
208} // namespace re_detail
209
210
211
212} // namespace boost
213
214#if defined(BOOST_RE_USE_VCL) && defined(BOOST_REGEX_DYN_LINK)
215
216int WINAPI DllEntryPoint(HINSTANCE , unsigned long , void*)
217{
218   return 1;
219}
220#endif
221
222#if defined(__IBMCPP__) && defined(BOOST_REGEX_DYN_LINK)
223//
224// Is this correct - linker complains without it ?
225//
226int main()
227{ 
228   return 0; 
229}
230
231#endif
232
233
234
235
236
237
Note: See TracBrowser for help on using the repository browser.