2002-05-24 12:53:12 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 1998-2002
|
2006-03-05 18:22:35 +00:00
|
|
|
* John Maddock
|
2002-05-24 12:53:12 +00:00
|
|
|
*
|
2004-02-05 09:14:22 +00:00
|
|
|
* Use, modification and distribution are subject to the
|
|
|
|
* Boost Software License, Version 1.0. (See accompanying file
|
|
|
|
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
2002-05-24 12:53:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LOCATION: see http://www.boost.org for most recent version.
|
|
|
|
* FILE: posix_api.cpp
|
|
|
|
* VERSION: see <boost/version.hpp>
|
|
|
|
* DESCRIPTION: Implements the Posix API wrappers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BOOST_REGEX_SOURCE
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <boost/regex.hpp>
|
2008-09-30 18:01:03 +00:00
|
|
|
#include <boost/cregex.hpp>
|
2002-05-24 12:53:12 +00:00
|
|
|
|
2006-03-05 18:22:35 +00:00
|
|
|
#if defined(BOOST_NO_STDC_NAMESPACE)
|
|
|
|
namespace std{
|
|
|
|
using ::sprintf;
|
|
|
|
using ::strcpy;
|
|
|
|
using ::strcmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-05-24 12:53:12 +00:00
|
|
|
namespace boost{
|
|
|
|
|
|
|
|
namespace{
|
|
|
|
|
|
|
|
unsigned int magic_value = 25631;
|
|
|
|
|
2006-09-05 07:18:36 +00:00
|
|
|
const char* names[] = {
|
|
|
|
"REG_NOERROR",
|
|
|
|
"REG_NOMATCH",
|
|
|
|
"REG_BADPAT",
|
|
|
|
"REG_ECOLLATE",
|
|
|
|
"REG_ECTYPE",
|
|
|
|
"REG_EESCAPE",
|
|
|
|
"REG_ESUBREG",
|
|
|
|
"REG_EBRACK",
|
|
|
|
"REG_EPAREN",
|
|
|
|
"REG_EBRACE",
|
|
|
|
"REG_BADBR",
|
|
|
|
"REG_ERANGE",
|
|
|
|
"REG_ESPACE",
|
|
|
|
"REG_BADRPT",
|
|
|
|
"REG_EEND",
|
|
|
|
"REG_ESIZE",
|
|
|
|
"REG_ERPAREN",
|
|
|
|
"REG_EMPTY",
|
|
|
|
"REG_ECOMPLEXITY",
|
|
|
|
"REG_ESTACK",
|
2010-02-17 20:54:47 +00:00
|
|
|
"REG_E_PERL",
|
2006-09-05 07:18:36 +00:00
|
|
|
"REG_E_UNKNOWN",
|
|
|
|
};
|
2002-05-24 12:53:12 +00:00
|
|
|
} // namespace
|
|
|
|
|
2008-09-30 18:01:03 +00:00
|
|
|
typedef boost::basic_regex<char, c_regex_traits<char> > c_regex_type;
|
|
|
|
|
2002-05-24 12:53:12 +00:00
|
|
|
BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
|
|
|
|
{
|
|
|
|
if(expression->re_magic != magic_value)
|
|
|
|
{
|
|
|
|
expression->guts = 0;
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try{
|
|
|
|
#endif
|
2008-09-30 18:01:03 +00:00
|
|
|
expression->guts = new c_regex_type();
|
2002-05-24 12:53:12 +00:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch(...)
|
|
|
|
{
|
|
|
|
return REG_ESPACE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(0 == expression->guts)
|
|
|
|
return REG_E_MEMORY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// set default flags:
|
2006-03-05 18:22:35 +00:00
|
|
|
boost::uint_fast32_t flags = (f & REG_PERLEX) ? 0 : ((f & REG_EXTENDED) ? regex::extended : regex::basic);
|
2004-02-05 09:14:22 +00:00
|
|
|
expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : match_default;
|
2002-05-24 12:53:12 +00:00
|
|
|
// and translate those that are actually set:
|
|
|
|
|
|
|
|
if(f & REG_NOCOLLATE)
|
2004-02-05 09:14:22 +00:00
|
|
|
{
|
|
|
|
flags |= regex::nocollate;
|
|
|
|
#ifndef BOOST_REGEX_V3
|
|
|
|
flags &= ~regex::collate;
|
|
|
|
#endif
|
|
|
|
}
|
2002-05-24 12:53:12 +00:00
|
|
|
|
|
|
|
if(f & REG_NOSUB)
|
2006-03-05 18:22:35 +00:00
|
|
|
{
|
|
|
|
//expression->eflags |= match_any;
|
|
|
|
flags |= regex::nosubs;
|
|
|
|
}
|
2002-05-24 12:53:12 +00:00
|
|
|
|
|
|
|
if(f & REG_NOSPEC)
|
2004-02-05 09:14:22 +00:00
|
|
|
flags |= regex::literal;
|
2002-05-24 12:53:12 +00:00
|
|
|
if(f & REG_ICASE)
|
2004-02-05 09:14:22 +00:00
|
|
|
flags |= regex::icase;
|
2002-05-24 12:53:12 +00:00
|
|
|
if(f & REG_ESCAPE_IN_LISTS)
|
2006-03-05 18:22:35 +00:00
|
|
|
flags &= ~regex::no_escape_in_lists;
|
2002-05-24 12:53:12 +00:00
|
|
|
if(f & REG_NEWLINE_ALT)
|
2004-02-05 09:14:22 +00:00
|
|
|
flags |= regex::newline_alt;
|
2002-05-24 12:53:12 +00:00
|
|
|
|
|
|
|
const char* p2;
|
|
|
|
if(f & REG_PEND)
|
|
|
|
p2 = expression->re_endp;
|
|
|
|
else p2 = ptr + std::strlen(ptr);
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try{
|
|
|
|
#endif
|
|
|
|
expression->re_magic = magic_value;
|
2008-09-30 18:01:03 +00:00
|
|
|
static_cast<c_regex_type*>(expression->guts)->set_expression(ptr, p2, flags);
|
|
|
|
expression->re_nsub = static_cast<c_regex_type*>(expression->guts)->mark_count() - 1;
|
|
|
|
result = static_cast<c_regex_type*>(expression->guts)->error_code();
|
2002-05-24 12:53:12 +00:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-03-05 18:22:35 +00:00
|
|
|
}
|
|
|
|
catch(const boost::regex_error& be)
|
|
|
|
{
|
|
|
|
result = be.code();
|
|
|
|
}
|
|
|
|
catch(...)
|
2002-05-24 12:53:12 +00:00
|
|
|
{
|
|
|
|
result = REG_E_UNKNOWN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(result)
|
|
|
|
regfreeA(expression);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
|
|
|
|
{
|
|
|
|
std::size_t result = 0;
|
|
|
|
if(code & REG_ITOA)
|
|
|
|
{
|
|
|
|
code &= ~REG_ITOA;
|
2006-03-05 18:22:35 +00:00
|
|
|
if(code <= (int)REG_E_UNKNOWN)
|
2002-05-24 12:53:12 +00:00
|
|
|
{
|
|
|
|
result = std::strlen(names[code]) + 1;
|
|
|
|
if(buf_size >= result)
|
2006-03-05 18:22:35 +00:00
|
|
|
re_detail::strcpy_s(buf, buf_size, names[code]);
|
2002-05-24 12:53:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if(code == REG_ATOI)
|
|
|
|
{
|
|
|
|
char localbuf[5];
|
|
|
|
if(e == 0)
|
|
|
|
return 0;
|
2006-03-05 18:22:35 +00:00
|
|
|
for(int i = 0; i <= (int)REG_E_UNKNOWN; ++i)
|
2002-05-24 12:53:12 +00:00
|
|
|
{
|
|
|
|
if(std::strcmp(e->re_endp, names[i]) == 0)
|
|
|
|
{
|
2011-11-20 18:58:15 +00:00
|
|
|
//
|
|
|
|
// We're converting an integer i to a string, and since i <= REG_E_UNKNOWN
|
|
|
|
// a five character string is *always* large enough:
|
|
|
|
//
|
2008-09-30 18:01:03 +00:00
|
|
|
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
|
2011-11-20 18:58:15 +00:00
|
|
|
int r = (::sprintf_s)(localbuf, 5, "%d", i);
|
2006-03-05 18:22:35 +00:00
|
|
|
#else
|
2011-11-20 18:58:15 +00:00
|
|
|
int r = (std::sprintf)(localbuf, "%d", i);
|
2006-03-05 18:22:35 +00:00
|
|
|
#endif
|
2011-11-20 18:58:15 +00:00
|
|
|
if(r < 0)
|
|
|
|
return 0; // sprintf failed
|
2002-05-24 12:53:12 +00:00
|
|
|
if(std::strlen(localbuf) < buf_size)
|
2006-03-05 18:22:35 +00:00
|
|
|
re_detail::strcpy_s(buf, buf_size, localbuf);
|
2002-05-24 12:53:12 +00:00
|
|
|
return std::strlen(localbuf) + 1;
|
|
|
|
}
|
|
|
|
}
|
2008-09-30 18:01:03 +00:00
|
|
|
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
|
2006-03-05 18:22:35 +00:00
|
|
|
(::sprintf_s)(localbuf, 5, "%d", 0);
|
|
|
|
#else
|
|
|
|
(std::sprintf)(localbuf, "%d", 0);
|
|
|
|
#endif
|
2002-05-24 12:53:12 +00:00
|
|
|
if(std::strlen(localbuf) < buf_size)
|
2006-03-05 18:22:35 +00:00
|
|
|
re_detail::strcpy_s(buf, buf_size, localbuf);
|
2002-05-24 12:53:12 +00:00
|
|
|
return std::strlen(localbuf) + 1;
|
|
|
|
}
|
2006-03-05 18:22:35 +00:00
|
|
|
if(code <= (int)REG_E_UNKNOWN)
|
2002-05-24 12:53:12 +00:00
|
|
|
{
|
|
|
|
std::string p;
|
|
|
|
if((e) && (e->re_magic == magic_value))
|
2008-09-30 18:01:03 +00:00
|
|
|
p = static_cast<c_regex_type*>(e->guts)->get_traits().error_string(static_cast< ::boost::regex_constants::error_type>(code));
|
2002-05-24 12:53:12 +00:00
|
|
|
else
|
|
|
|
{
|
2006-03-05 18:22:35 +00:00
|
|
|
p = re_detail::get_default_error_string(static_cast< ::boost::regex_constants::error_type>(code));
|
2002-05-24 12:53:12 +00:00
|
|
|
}
|
|
|
|
std::size_t len = p.size();
|
|
|
|
if(len < buf_size)
|
|
|
|
{
|
2006-03-05 18:22:35 +00:00
|
|
|
re_detail::strcpy_s(buf, buf_size, p.c_str());
|
2002-05-24 12:53:12 +00:00
|
|
|
}
|
|
|
|
return len + 1;
|
|
|
|
}
|
|
|
|
if(buf_size)
|
|
|
|
*buf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
|
|
|
|
{
|
2006-03-05 18:22:35 +00:00
|
|
|
#ifdef BOOST_MSVC
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4267)
|
|
|
|
#endif
|
2002-05-24 12:53:12 +00:00
|
|
|
bool result = false;
|
2004-02-05 09:14:22 +00:00
|
|
|
match_flag_type flags = match_default | expression->eflags;
|
2002-05-24 12:53:12 +00:00
|
|
|
const char* end;
|
|
|
|
const char* start;
|
|
|
|
cmatch m;
|
|
|
|
|
|
|
|
if(eflags & REG_NOTBOL)
|
|
|
|
flags |= match_not_bol;
|
|
|
|
if(eflags & REG_NOTEOL)
|
|
|
|
flags |= match_not_eol;
|
|
|
|
if(eflags & REG_STARTEND)
|
|
|
|
{
|
|
|
|
start = buf + array[0].rm_so;
|
|
|
|
end = buf + array[0].rm_eo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = buf;
|
|
|
|
end = buf + std::strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try{
|
|
|
|
#endif
|
|
|
|
if(expression->re_magic == magic_value)
|
|
|
|
{
|
2008-09-30 18:01:03 +00:00
|
|
|
result = regex_search(start, end, m, *static_cast<c_regex_type*>(expression->guts), flags);
|
2002-05-24 12:53:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch(...)
|
|
|
|
{
|
|
|
|
return REG_E_UNKNOWN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
// extract what matched:
|
2006-03-05 18:22:35 +00:00
|
|
|
std::size_t i;
|
2002-05-24 12:53:12 +00:00
|
|
|
for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
|
|
|
|
{
|
|
|
|
array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
|
|
|
|
array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
|
|
|
|
}
|
|
|
|
// and set anything else to -1:
|
|
|
|
for(i = expression->re_nsub + 1; i < n; ++i)
|
|
|
|
{
|
|
|
|
array[i].rm_so = -1;
|
|
|
|
array[i].rm_eo = -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return REG_NOMATCH;
|
2006-03-05 18:22:35 +00:00
|
|
|
#ifdef BOOST_MSVC
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2002-05-24 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
|
|
|
|
{
|
|
|
|
if(expression->re_magic == magic_value)
|
|
|
|
{
|
2008-09-30 18:01:03 +00:00
|
|
|
delete static_cast<c_regex_type*>(expression->guts);
|
2002-05-24 12:53:12 +00:00
|
|
|
}
|
|
|
|
expression->re_magic = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
|