Added checks to message loading code.

Added extra members to RegEx class.
Updated docs accordingly.


[SVN r12244]
This commit is contained in:
John Maddock
2002-01-07 13:03:50 +00:00
parent e4138b5bdc
commit 52dbd281ca
5 changed files with 120 additions and 63 deletions

View File

@ -92,10 +92,13 @@ are allowed. </p>
</i>&nbsp;&nbsp; <i>// </i>&nbsp;&nbsp; <i>//
</i></font>&nbsp;&nbsp; <b>unsigned</b> <b>int</b> Position(<b>int</b> i = 0)<b>const</b>; </i></font>&nbsp;&nbsp; <b>unsigned</b> <b>int</b> Position(<b>int</b> i = 0)<b>const</b>;
&nbsp;&nbsp; <b>unsigned</b> <b>int</b> Length(<b>int</b> i = 0)<b>const</b>; &nbsp;&nbsp; <b>unsigned</b> <b>int</b> Length(<b>int</b> i = 0)<b>const</b>;
<strong>bool</strong> Matched(<strong>int</strong> i = 0)<strong>const</strong>;
&nbsp;&nbsp; <b>unsigned</b> <b>int</b> Line()<b>const</b>; &nbsp;&nbsp; <b>unsigned</b> <b>int</b> Line()<b>const</b>;
&nbsp;&nbsp; <b>unsigned int</b> Marks() const; &nbsp;&nbsp; <b>unsigned int</b> Marks() const;
&nbsp;&nbsp; std::string What(<b>int</b> i)<b>const</b>; &nbsp;&nbsp; std::string What(<b>int</b> i)<b>const</b>;
&nbsp;&nbsp; std::string <b>operator</b>[](<b>int</b> i)<b>const</b> ; &nbsp;&nbsp; std::string <b>operator</b>[](<b>int</b> i)<b>const</b> ;
<strong>static const unsigned int</strong> npos;
}; &nbsp; &nbsp; </pre> }; &nbsp; &nbsp; </pre>
<p>Member functions for class RegEx are defined as follows: <br> <p>Member functions for class RegEx are defined as follows: <br>
@ -495,8 +498,8 @@ are allowed. </p>
Position(<b>int</b> i = 0)<b>const</b>;</td> Position(<b>int</b> i = 0)<b>const</b>;</td>
<td valign="top" width="42%">Returns the position of what <td valign="top" width="42%">Returns the position of what
matched sub-expression <i>i</i>. If <i>i = 0</i> then matched sub-expression <i>i</i>. If <i>i = 0</i> then
returns the position of the whole match. Returns -1 if returns the position of the whole match. Returns RegEx::npos
the supplied index is invalid, or if the specified sub-expression if the supplied index is invalid, or if the specified sub-expression
did not participate in the match.</td> did not participate in the match.</td>
<td valign="top" width="7%">&nbsp;</td> <td valign="top" width="7%">&nbsp;</td>
</tr> </tr>
@ -506,18 +509,26 @@ are allowed. </p>
Length(<b>int</b> i = 0)<b>const</b>;</td> Length(<b>int</b> i = 0)<b>const</b>;</td>
<td valign="top" width="42%">Returns the length of what <td valign="top" width="42%">Returns the length of what
matched sub-expression <i>i</i>. If <i>i = 0</i> then matched sub-expression <i>i</i>. If <i>i = 0</i> then
returns the length of the whole match. Returns -1 if the returns the length of the whole match. Returns RegEx::npos
supplied index is invalid, or if the specified sub-expression if the supplied index is invalid, or if the specified sub-expression
did not participate in the match.</td> did not participate in the match.</td>
<td valign="top" width="7%">&nbsp;</td> <td valign="top" width="7%">&nbsp;</td>
</tr> </tr>
<tr>
<td>&nbsp;</td>
<td><strong>bool</strong> Matched(<strong>int</strong> i
= 0)<strong>const</strong>;</td>
<td>Returns true if sub-expression <em>i</em> was
matched, false otherwise.</td>
<td>&nbsp;</td>
</tr>
<tr> <tr>
<td valign="top" width="7%">&nbsp;</td> <td valign="top" width="7%">&nbsp;</td>
<td valign="top" width="43%"><b>unsigned</b> <b>int</b> <td valign="top" width="43%"><b>unsigned</b> <b>int</b>
Line()<b>const</b>;</td> Line()<b>const</b>;</td>
<td valign="top" width="42%">Returns the line on which <td valign="top" width="42%">Returns the line on which
the match occurred, indexes start from 1 not zero, if no the match occurred, indexes start from 1 not zero, if no
match occurred then returns (unsigned)-1.</td> match occurred then returns RegEx::npos.</td>
<td valign="top" width="7%">&nbsp;</td> <td valign="top" width="7%">&nbsp;</td>
</tr> </tr>
<tr> <tr>

View File

@ -342,7 +342,14 @@ void BOOST_REGEX_CALL re_message_update()
message_cat = (nl_catd)-1; message_cat = (nl_catd)-1;
} }
if(*boost::re_detail::c_traits_base::get_catalogue()) if(*boost::re_detail::c_traits_base::get_catalogue())
{
message_cat = catopen(boost::re_detail::c_traits_base::get_catalogue(), 0); message_cat = catopen(boost::re_detail::c_traits_base::get_catalogue(), 0);
if(message_cat == (nl_catd)-1)
{
std::string m("Unable to open message catalog: ");
throw std::runtime_error(m + boost::re_detail::c_traits_base::get_catalogue());
}
}
#endif #endif
for(int i = 0; i < boost::REG_E_UNKNOWN; ++i) for(int i = 0; i < boost::REG_E_UNKNOWN; ++i)
{ {

View File

@ -189,8 +189,19 @@ message_data<char>::message_data(const std::locale& l, const std::string& regex_
{ {
is.imbue(l); is.imbue(l);
#ifndef BOOST_NO_STD_MESSAGES #ifndef BOOST_NO_STD_MESSAGES
const std::messages<char>* pm = &BOOST_USE_FACET(std::messages<char>, l);
std::messages<char>::catalog cat = regex_message_catalogue.size() ? pm->open(regex_message_catalogue, l) : -1; const std::messages<char>* pm = 0;
std::messages<char>::catalog cat = -1;
if(regex_message_catalogue.size())
{
pm = &BOOST_USE_FACET(std::messages<char>, l);
cat = pm->open(regex_message_catalogue, l);
if(cat < 0)
{
std::string m("Unable to open message catalog: ");
throw std::runtime_error(m + regex_message_catalogue);
}
}
#endif #endif
std::memset(syntax_map, cpp_regex_traits<char>::syntax_char, 256); std::memset(syntax_map, cpp_regex_traits<char>::syntax_char, 256);
unsigned int i; unsigned int i;
@ -222,39 +233,26 @@ message_data<char>::message_data(const std::locale& l, const std::string& regex_
std::string c1, c2; std::string c1, c2;
i = 400; i = 400;
if((int)cat >= 0) if((int)cat >= 0)
c2 = pm->get(cat, 0, i, c1);
while(c2.size())
{ {
const char* p1, *p2, *p3, *p4;;
p1 = c2.c_str();
while(*p1 && std::isspace((char)*p1, l))++p1;
p2 = p1;
while(*p2 && !std::isspace((char)*p2, l))++p2;
p3 = p2;
while(*p3 && std::isspace((char)*p3, l))++p3;
p4 = p3;
while(*p4 && !std::isspace((char)*p4, l))++p4;
collating_elements[std::string(p1, p2)] = std::string(p3, p4);
++i;
c2 = pm->get(cat, 0, i, c1); c2 = pm->get(cat, 0, i, c1);
while(c2.size())
{
const char* p1, *p2, *p3, *p4;;
p1 = c2.c_str();
while(*p1 && std::isspace((char)*p1, l))++p1;
p2 = p1;
while(*p2 && !std::isspace((char)*p2, l))++p2;
p3 = p2;
while(*p3 && std::isspace((char)*p3, l))++p3;
p4 = p3;
while(*p4 && !std::isspace((char)*p4, l))++p4;
collating_elements[std::string(p1, p2)] = std::string(p3, p4);
++i;
c2 = pm->get(cat, 0, i, c1);
}
} }
#endif #endif
/*
std::string n("zero");
std::map<std::string, std::string, std::less<std::string > >::const_iterator pos = collating_elements.find(n);
if(pos != collating_elements.end())
_zero = *(*pos).second.c_str();
else
_zero = '0';
n = "ten";
pos = collating_elements.find(n);
if(pos != collating_elements.end())
_ten = *(*pos).second.c_str();
else
_ten = 'a';
*/
std::string m; std::string m;
std::string s; std::string s;
#ifndef BOOST_NO_STD_MESSAGES #ifndef BOOST_NO_STD_MESSAGES
@ -562,7 +560,16 @@ message_data<wchar_t>::message_data(const std::locale& l, const std::string& reg
const cvt_type& cvt = BOOST_USE_FACET(cvt_type, l); const cvt_type& cvt = BOOST_USE_FACET(cvt_type, l);
#ifndef BOOST_NO_STD_MESSAGES #ifndef BOOST_NO_STD_MESSAGES
const std::messages<wchar_t>& msgs = BOOST_USE_FACET(std::messages<wchar_t>, l); const std::messages<wchar_t>& msgs = BOOST_USE_FACET(std::messages<wchar_t>, l);
std::messages<wchar_t>::catalog cat = regex_message_catalogue.size() ? msgs.open(regex_message_catalogue, l) : -1; std::messages<wchar_t>::catalog cat = -1;
if(regex_message_catalogue.size())
{
cat = msgs.open(regex_message_catalogue, l);
if(cat < 0)
{
std::string m("Unable to open message catalog: ");
throw std::runtime_error(m + regex_message_catalogue);
}
}
#endif #endif
scoped_array<char> a; scoped_array<char> a;
unsigned array_size = 0; unsigned array_size = 0;
@ -602,22 +609,24 @@ message_data<wchar_t>::message_data(const std::locale& l, const std::string& reg
string_type c1, c2; string_type c1, c2;
i = 400; i = 400;
if((int)cat >= 0) if((int)cat >= 0)
c2 = msgs.get(cat, 0, i, c1);
while(c2.size())
{ {
const wchar_t* p1, *p2, *p3, *p4;;
p1 = c2.c_str();
while(*p1 && std::isspace((wchar_t)*p1, l))++p1;
p2 = p1;
while(*p2 && !std::isspace((wchar_t)*p2, l))++p2;
p3 = p2;
while(*p3 && std::isspace((wchar_t)*p3, l))++p3;
p4 = p3;
while(*p4 && !std::isspace((wchar_t)*p4, l))++p4;
collating_elements[std::basic_string<wchar_t>(p1, p2)] = std::basic_string<wchar_t>(p3, p4);
++i;
c2 = msgs.get(cat, 0, i, c1); c2 = msgs.get(cat, 0, i, c1);
while(c2.size())
{
const wchar_t* p1, *p2, *p3, *p4;;
p1 = c2.c_str();
while(*p1 && std::isspace((wchar_t)*p1, l))++p1;
p2 = p1;
while(*p2 && !std::isspace((wchar_t)*p2, l))++p2;
p3 = p2;
while(*p3 && std::isspace((wchar_t)*p3, l))++p3;
p4 = p3;
while(*p4 && !std::isspace((wchar_t)*p4, l))++p4;
collating_elements[std::basic_string<wchar_t>(p1, p2)] = std::basic_string<wchar_t>(p3, p4);
++i;
c2 = msgs.get(cat, 0, i, c1);
}
} }
if((int)cat >= 0) if((int)cat >= 0)

View File

@ -465,18 +465,18 @@ unsigned int RegEx::Position(int i)const
switch(pdata->t) switch(pdata->t)
{ {
case re_detail::RegExData::type_pc: case re_detail::RegExData::type_pc:
return pdata->m[i].matched ? pdata->m[i].first - pdata->pbase : (unsigned int)-1; return pdata->m[i].matched ? pdata->m[i].first - pdata->pbase : RegEx::npos;
case re_detail::RegExData::type_pf: case re_detail::RegExData::type_pf:
return pdata->fm[i].matched ? pdata->fm[i].first - pdata->fbase : (unsigned int)-1; return pdata->fm[i].matched ? pdata->fm[i].first - pdata->fbase : RegEx::npos;
case re_detail::RegExData::type_copy: case re_detail::RegExData::type_copy:
{ {
std::map<int, int, std::less<int> >::iterator pos = pdata->positions.find(i); std::map<int, int, std::less<int> >::iterator pos = pdata->positions.find(i);
if(pos == pdata->positions.end()) if(pos == pdata->positions.end())
return (unsigned int)-1; return RegEx::npos;
return (*pos).second; return (*pos).second;
} }
} }
return (unsigned int)-1; return RegEx::npos;
} }
unsigned int RegEx::Line()const unsigned int RegEx::Line()const
@ -485,15 +485,15 @@ unsigned int RegEx::Line()const
switch(pdata->t) switch(pdata->t)
{ {
case re_detail::RegExData::type_pc: case re_detail::RegExData::type_pc:
return pdata->m[0].matched ? pdata->m.line() : (unsigned int)-1; return pdata->m[0].matched ? pdata->m.line() : RegEx::npos;
case re_detail::RegExData::type_pf: case re_detail::RegExData::type_pf:
return pdata->fm[0].matched ? pdata->fm.line() : (unsigned int)-1; return pdata->fm[0].matched ? pdata->fm.line() : RegEx::npos;
case re_detail::RegExData::type_copy: case re_detail::RegExData::type_copy:
{ {
return pdata->line; return pdata->line;
} }
} }
return (unsigned int)-1; return RegEx::npos;
} }
unsigned int RegEx::Marks()const unsigned int RegEx::Marks()const
@ -509,20 +509,41 @@ unsigned int RegEx::Length(int i)const
switch(pdata->t) switch(pdata->t)
{ {
case re_detail::RegExData::type_pc: case re_detail::RegExData::type_pc:
return pdata->m[i].matched ? pdata->m[i].second - pdata->m[i].first : (unsigned)-1; return pdata->m[i].matched ? pdata->m[i].second - pdata->m[i].first : RegEx::npos;
case re_detail::RegExData::type_pf: case re_detail::RegExData::type_pf:
return pdata->fm[i].matched ? pdata->fm[i].second - pdata->fm[i].first : (unsigned)-1; return pdata->fm[i].matched ? pdata->fm[i].second - pdata->fm[i].first : RegEx::npos;
case re_detail::RegExData::type_copy: case re_detail::RegExData::type_copy:
{ {
std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i); std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
if(pos == pdata->strings.end()) if(pos == pdata->strings.end())
return (unsigned)-1; return RegEx::npos;
return (*pos).second.size(); return (*pos).second.size();
} }
} }
return (unsigned)-1; return RegEx::npos;
} }
bool RegEx::Matched(int i)const
{
BOOST_RE_GUARD_STACK
switch(pdata->t)
{
case re_detail::RegExData::type_pc:
return pdata->m[i].matched;
case re_detail::RegExData::type_pf:
return pdata->fm[i].matched;
case re_detail::RegExData::type_copy:
{
std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
if(pos == pdata->strings.end())
return false;
return true;
}
}
return false;
}
std::string RegEx::What(int i)const std::string RegEx::What(int i)const
{ {
BOOST_RE_GUARD_STACK BOOST_RE_GUARD_STACK
@ -548,6 +569,8 @@ std::string RegEx::What(int i)const
return result; return result;
} }
const unsigned int RegEx::npos = ~0u;
} // namespace boost } // namespace boost

View File

@ -230,10 +230,17 @@ void BOOST_REGEX_CALL w32_traits_base::do_init()
if(is_init == 0) if(is_init == 0)
{ {
// //
// upadte the messages first: // update the messages first:
is_init = true; is_init = true;
if(*regex_message_catalogue) if(*regex_message_catalogue)
{
hresmod = LoadLibraryA(regex_message_catalogue); hresmod = LoadLibraryA(regex_message_catalogue);
if(hresmod == NULL)
{
std::string s("Unable to open dll: ");
throw std::runtime_error(s + regex_message_catalogue);
}
}
unsigned int i; unsigned int i;
for(i = 0; i < REG_E_UNKNOWN; ++i) for(i = 0; i < REG_E_UNKNOWN; ++i)
{ {