summaryrefslogtreecommitdiff
path: root/httpd/tnt/regex.h
blob: b2a30e8e4214f5fe7c630044d7b8312169e0a17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* tnt/regex.h
 * Copyright (C) 2005 Tommi Maekitalo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#ifndef TNT_REGEX_H
#define TNT_REGEX_H

#include <string>
#include <sys/types.h>
#include <regex.h>

namespace tnt
{
  /// collects matches in a regex
  class regex_smatch
  {
      friend class regex;

      std::string str;
      regmatch_t matchbuf[10];

    public:
      unsigned size() const;
      std::string get(unsigned n) const;
      std::string format(const std::string& s) const;
  };

  /// regex(3)-wrapper.
  /// Warning: incomplete, but sufficient for tntnet.
  /// Regular expression is not automatically freed. Tntnet needs to
  /// put regex into a stl-container, so it needs to be copyable.
  /// For this class to be complete, the regex_t needs to be
  /// reference-counted. This is unneeded for tntnet, because the regex is
  /// never freed anyway.
  class regex
  {
      regex_t expr;

      void checkerr(int ret) const;

    public:
      explicit regex(const char* ex, int cflags = REG_EXTENDED)
      {
        checkerr(::regcomp(&expr, ex, cflags));
      }

      explicit regex(const std::string& ex, int cflags = REG_EXTENDED)
      {
        checkerr(::regcomp(&expr, ex.c_str(), cflags));
      }

      bool match(const std::string& str_, regex_smatch& smatch, int eflags = 0) const;
      bool match(const std::string& str_, int eflags = 0) const;

      void free();

    private:
      friend class value_type;
  };

}

#endif // TNT_REGEX_H