summaryrefslogtreecommitdiff
path: root/src/generic/apt/matching/pattern.cc
blob: 75339e1dc05fe507b6ad178e954ccf6ba4ee4449 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// pattern.cc
//
//   Copyright (C) 2008-2009 Daniel Burrows
//
//   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 WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  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; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

/// \file pattern.cc

#include "pattern.h"

#include <cwidget/generic/util/ssprintf.h>

#include <aptitude.h>

using cwidget::util::ssprintf;

namespace aptitude
{
  namespace matching
  {
    MatchingException::MatchingException(const std::string &_msg)
      : msg(_msg)
    {
    }

    std::string MatchingException::errmsg() const
    {
      return msg;
    }

    regex::regex(const std::string &pattern, int cflags)
    {
      int err = regcomp(&r, pattern.c_str(), cflags);
      if(err != 0)
	{
	  size_t needed = regerror(err, &r, NULL, 0);

	  char *buf = new char[needed+1];

	  regerror(err, &r, buf, needed+1);

	  std::string msg(ssprintf("Regex compilation error: %s", buf));

	  delete[] buf;

	  throw MatchingException(msg);
	}
    }

    regex::~regex()
    {
      regfree(&r);
    }

    bool regex::exec(const char *s, regmatch_t *matches, size_t num_matches,
		     int eflags) const
    {
      return 0 == regexec(&r, s, num_matches, matches, eflags);
    }

    cwidget::util::ref_ptr<pattern>
    pattern::make_action(const action_type act)
    {
      return new pattern(action, act);
    }

    bool is_pattern(const std::string &s)
    {
      // regex characters are ".?+*|[^$"
      // pattern characters are "~?"
      return s.find_first_of("~?.?+*|[^$") != s.npos;
    }
  }
}

#if 0
// A template switch statement that can be copied&pasted as the
// skeleton for a new pattern-processing function.

switch(p->get_type())
  {
    // Structural patterns:
  case pattern::all_versions:
  case pattern::and_tp:
  case pattern::any_version:
  case pattern::for_tp:
  case pattern::narrow:
  case pattern::not_tp:
  case pattern::or_tp:
  case pattern::widen:

    // Atomic patterns:
  case pattern::archive:
  case pattern::action:
  case pattern::architecture:
  case pattern::automatic:
  case pattern::bind:
  case pattern::broken:
  case pattern::broken_type:
  case pattern::candidate_version:
  case pattern::config_files:
  case pattern::current_version:
  case pattern::depends:
  case pattern::description:
  case pattern::essential:
  case pattern::equal:
  case pattern::false_tp:
  case pattern::foreign_architecture:
  case pattern::garbage:
  case pattern::install_version:
  case pattern::installed:
  case pattern::maintainer:
  case pattern::multiarch:
  case pattern::name:
  case pattern::native_architecture:
  case pattern::new_tp:
  case pattern::obsolete:
  case pattern::origin:
  case pattern::priority:
  case pattern::provides:
  case pattern::reverse_depends:
  case pattern::reverse_provides:
  case pattern::section:
  case pattern::source_package:
  case pattern::source_version:
  case pattern::tag:
  case pattern::task:
  case pattern::term:
  case pattern::true_tp:
  case pattern::upgradable:
  case pattern::user_tag:
  case pattern::version:
  case pattern::virtual_tp:

  default:
    throw MatchingException(std::string("Internal error: unhandled pattern type in ") + __FUNC__);
  }
#endif