1 //
2 // aegis - project change supervisor
3 // Copyright (C) 2002, 2004-2008, 2012 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published
7 // by the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #include <common/ac/assert.h>
20 
21 #include <common/mem.h>
22 #include <libaegis/aer/expr.h>
23 #include <libaegis/aer/func/downcase.h>
24 #include <libaegis/aer/value/error.h>
25 #include <libaegis/aer/value/integer.h>
26 #include <libaegis/aer/value/string.h>
27 #include <libaegis/sub.h>
28 
29 
~rpt_func_downcase()30 rpt_func_downcase::~rpt_func_downcase()
31 {
32 }
33 
34 
rpt_func_downcase()35 rpt_func_downcase::rpt_func_downcase()
36 {
37 }
38 
39 
40 rpt_func::pointer
create()41 rpt_func_downcase::create()
42 {
43     return pointer(new rpt_func_downcase());
44 }
45 
46 
47 const char *
name() const48 rpt_func_downcase::name()
49     const
50 {
51     return "downcase";
52 }
53 
54 
55 bool
optimizable() const56 rpt_func_downcase::optimizable()
57     const
58 {
59     return true;
60 }
61 
62 
63 bool
verify(const rpt_expr::pointer & ep) const64 rpt_func_downcase::verify(const rpt_expr::pointer &ep)
65     const
66 {
67     return (ep->get_nchildren() == 1);
68 }
69 
70 
71 rpt_value::pointer
run(const rpt_expr::pointer & ep,size_t,rpt_value::pointer * argv) const72 rpt_func_downcase::run(const rpt_expr::pointer &ep, size_t,
73     rpt_value::pointer *argv) const
74 {
75     rpt_value::pointer arg = argv[0];
76     assert(!arg->is_an_error());
77     arg = rpt_value::stringize(arg);
78     rpt_value_string *rvsp = dynamic_cast<rpt_value_string *>(arg.get());
79     if (!rvsp)
80     {
81         sub_context_ty sc;
82         sc.var_set_charstar("Function", "downcase");
83         sc.var_set_long("Number", 1);
84         sc.var_set_charstar("Name", argv[0]->name());
85         nstring s
86         (
87             sc.subst_intl
88             (
89                 i18n("$function: argument $number: string value required "
90                     "(was given $name)")
91             )
92         );
93         return rpt_value_error::create(ep->get_pos(), s);
94     }
95 
96     //
97     // build the result
98     //
99     nstring subject(rvsp->query());
100     nstring s = subject.downcase();
101     return rpt_value_string::create(s);
102 }
103 
104 
105 // vim: set ts=8 sw=4 et :
106