~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/libs/regex/src/posix_api.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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:        posix_api.cpp
 
19
  *   VERSION:     see <boost/version.hpp>
 
20
  *   DESCRIPTION: Implements the Posix API wrappers.
 
21
  */
 
22
 
 
23
#define BOOST_REGEX_SOURCE
 
24
 
 
25
#include <cstdio>
 
26
#include <boost/regex.hpp>
 
27
 
 
28
namespace boost{
 
29
 
 
30
namespace{
 
31
 
 
32
unsigned int magic_value = 25631;
 
33
 
 
34
const char* names[] = {"REG_NOERROR", "REG_NOMATCH", "REG_BADPAT", "REG_ECOLLATE",
 
35
                        "REG_ECTYPE", "REG_EESCAPE", "REG_ESUBREG", "REG_EBRACK",
 
36
                        "REG_EPAREN", "REG_EBRACE", "REG_BADBR", "REG_ERANGE",
 
37
                        "REG_ESPACE", "REG_BADRPT", "REG_EMPTY", "REG_E_UNKNOWN"};
 
38
} // namespace
 
39
 
 
40
BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
 
41
{
 
42
   BOOST_RE_GUARD_STACK
 
43
   if(expression->re_magic != magic_value)
 
44
   {
 
45
      expression->guts = 0;
 
46
#ifndef BOOST_NO_EXCEPTIONS
 
47
      try{
 
48
#endif
 
49
      expression->guts = new regex();
 
50
#ifndef BOOST_NO_EXCEPTIONS
 
51
      } catch(...)
 
52
      {
 
53
         return REG_ESPACE;
 
54
      }
 
55
#else
 
56
      if(0 == expression->guts)
 
57
         return REG_E_MEMORY;
 
58
#endif
 
59
   }
 
60
   // set default flags:
 
61
   boost::uint_fast32_t flags = (f & REG_EXTENDED) ? regbase::extended : regbase::basic;
 
62
   expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : 0;
 
63
   // and translate those that are actually set:
 
64
 
 
65
   if(f & REG_NOCOLLATE)
 
66
      flags |= regbase::nocollate;
 
67
 
 
68
   if(f & REG_NOSUB)
 
69
      expression->eflags |= match_any;
 
70
 
 
71
   if(f & REG_NOSPEC)
 
72
      flags |= regbase::literal;
 
73
   if(f & REG_ICASE)
 
74
      flags |= regbase::icase;
 
75
   if(f & REG_ESCAPE_IN_LISTS)
 
76
      flags |= regbase::escape_in_lists;
 
77
   if(f & REG_NEWLINE_ALT)
 
78
      flags |= regbase::newline_alt;
 
79
 
 
80
   const char* p2;
 
81
   if(f & REG_PEND)
 
82
      p2 = expression->re_endp;
 
83
   else p2 = ptr + std::strlen(ptr);
 
84
 
 
85
   int result;
 
86
 
 
87
#ifndef BOOST_NO_EXCEPTIONS
 
88
   try{
 
89
#endif
 
90
      expression->re_magic = magic_value;
 
91
      static_cast<regex*>(expression->guts)->set_expression(ptr, p2, flags);
 
92
      expression->re_nsub = static_cast<regex*>(expression->guts)->mark_count() - 1;
 
93
      result = static_cast<regex*>(expression->guts)->error_code();
 
94
#ifndef BOOST_NO_EXCEPTIONS
 
95
   } catch(...)
 
96
   {
 
97
      result = REG_E_UNKNOWN;
 
98
   }
 
99
#endif
 
100
   if(result)
 
101
      regfreeA(expression);
 
102
   return result;
 
103
 
 
104
}
 
105
 
 
106
BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
 
107
{
 
108
   BOOST_RE_GUARD_STACK
 
109
   std::size_t result = 0;
 
110
   if(code & REG_ITOA)
 
111
   {
 
112
      code &= ~REG_ITOA;
 
113
      if(code <= REG_E_UNKNOWN)
 
114
      {
 
115
         result = std::strlen(names[code]) + 1;
 
116
         if(buf_size >= result)
 
117
            std::strcpy(buf, names[code]);
 
118
         return result;
 
119
      }
 
120
      return result;
 
121
   }
 
122
   if(code == REG_ATOI)
 
123
   {
 
124
      char localbuf[5];
 
125
      if(e == 0)
 
126
         return 0;
 
127
      for(int i = 0; i <= REG_E_UNKNOWN; ++i)
 
128
      {
 
129
         if(std::strcmp(e->re_endp, names[i]) == 0)
 
130
         {
 
131
            std::sprintf(localbuf, "%d", i);
 
132
            if(std::strlen(localbuf) < buf_size)
 
133
               std::strcpy(buf, localbuf);
 
134
            return std::strlen(localbuf) + 1;
 
135
         }
 
136
      }
 
137
      std::sprintf(localbuf, "%d", 0);
 
138
      if(std::strlen(localbuf) < buf_size)
 
139
         std::strcpy(buf, localbuf);
 
140
      return std::strlen(localbuf) + 1;
 
141
   }
 
142
   if(code <= REG_E_UNKNOWN)
 
143
   {
 
144
      std::string p;
 
145
      if((e) && (e->re_magic == magic_value))
 
146
         p = static_cast<regex*>(e->guts)->get_traits().error_string(code);
 
147
      else
 
148
      {
 
149
         boost::regex_traits<char> t;
 
150
         p = t.error_string(code);
 
151
      }
 
152
      std::size_t len = p.size();
 
153
      if(len < buf_size)
 
154
      {
 
155
         std::strcpy(buf, p.c_str());
 
156
      }
 
157
      return len + 1;
 
158
   }
 
159
   if(buf_size)
 
160
      *buf = 0;
 
161
   return 0;
 
162
}
 
163
 
 
164
BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
 
165
{
 
166
   BOOST_RE_GUARD_STACK
 
167
   bool result = false;
 
168
   boost::uint_fast32_t flags = match_default | expression->eflags;
 
169
   const char* end;
 
170
   const char* start;
 
171
   cmatch m;
 
172
   
 
173
   if(eflags & REG_NOTBOL)
 
174
      flags |= match_not_bol;
 
175
   if(eflags & REG_NOTEOL)
 
176
      flags |= match_not_eol;
 
177
   if(eflags & REG_STARTEND)
 
178
   {
 
179
      start = buf + array[0].rm_so;
 
180
      end = buf + array[0].rm_eo;
 
181
   }
 
182
   else
 
183
   {
 
184
      start = buf;
 
185
      end = buf + std::strlen(buf);
 
186
   }
 
187
 
 
188
#ifndef BOOST_NO_EXCEPTIONS
 
189
   try{
 
190
#endif
 
191
   if(expression->re_magic == magic_value)
 
192
   {
 
193
      result = regex_search(start, end, m, *static_cast<regex*>(expression->guts), flags);
 
194
   }
 
195
   else
 
196
      return result;
 
197
#ifndef BOOST_NO_EXCEPTIONS
 
198
   } catch(...)
 
199
   {
 
200
      return REG_E_UNKNOWN;
 
201
   }
 
202
#endif
 
203
 
 
204
   if(result)
 
205
   {
 
206
      // extract what matched:
 
207
     unsigned int i;
 
208
      for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
 
209
      {
 
210
         array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
 
211
         array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
 
212
      }
 
213
      // and set anything else to -1:
 
214
      for(i = expression->re_nsub + 1; i < n; ++i)
 
215
      {
 
216
         array[i].rm_so = -1;
 
217
         array[i].rm_eo = -1;
 
218
      }
 
219
      return 0;
 
220
   }
 
221
   return REG_NOMATCH;
 
222
}
 
223
 
 
224
BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
 
225
{
 
226
   BOOST_RE_GUARD_STACK
 
227
   if(expression->re_magic == magic_value)
 
228
   {
 
229
      delete static_cast<regex*>(expression->guts);
 
230
   }
 
231
   expression->re_magic = 0;
 
232
}
 
233
 
 
234
} // namespace boost
 
235
 
 
236
 
 
237