aboutsummaryrefslogtreecommitdiff
path: root/odb/option-parsers.hxx
blob: 0bd37360965d21ace9064f1b908ab0a8efc1ca74 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// file      : odb/option-parsers.hxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#ifndef ODB_OPTION_PARSERS_HXX
#define ODB_OPTION_PARSERS_HXX

#include <vector>
#include <sstream>

#include <odb/option-types.hxx>
#include <odb/options.hxx>

namespace cli
{
  // Return true if there is a database prefix.
  //
  template <typename V>
  bool
  parse_option_value (std::string const& o, std::string const& ov,
                      database& k, V& v)
  {
    bool r (false);
    std::string::size_type p = ov.find (':');

    std::string vstr;
    if (p != std::string::npos)
    {
      std::string kstr (ov, 0, p);

      // See if this prefix resolves to the database name. If not,
      // assume there is no prefix.
      //
      std::istringstream ks (kstr);

      if (ks >> k && ks.eof ())
      {
        r = true;
        vstr.assign (ov, p + 1, std::string::npos);
      }
    }

    if (!r)
      vstr = ov; // Use the whole value.

    if (!vstr.empty ())
    {
      std::istringstream vs (vstr);

      if (!(vs >> v && vs.eof ()))
        throw invalid_value (o, ov);
    }
    else
      v = V ();

    return r;
  }

  // Specialization for std::string.
  //
  bool
  parse_option_value (std::string const&, std::string const& ov,
                      database& k, std::string& v)
  {
    bool r (false);
    std::string::size_type p = ov.find (':');

    if (p != std::string::npos)
    {
      std::string kstr (ov, 0, p);

      // See if this prefix resolves to the database name. If not,
      // assume there is no prefix.
      //
      std::istringstream ks (kstr);

      if (ks >> k && ks.eof ())
      {
        r = true;
        v.assign (ov, p + 1, std::string::npos);
      }
    }

    if (!r)
      v = ov; // Use the whole value.

    return r;
  }

  template <typename V>
  struct parser<database_map<V> >
  {
    typedef database_map<V> map;

    static void
    parse (map& m, bool& xs, scanner& s)
    {
      xs = true;
      std::string o (s.next ());

      if (s.more ())
      {
        database k;
        V v;

        if (parse_option_value (o, s.next (), k, v))
          m[k] = v; // Override any old value.
        else
        {
          // No database prefix is specified which means it applies to
          // all the databases. We also don't want to override database-
          // specific values, so use insert().
          //
          m.insert (typename map::value_type (database::common, v));
          m.insert (typename map::value_type (database::mssql, v));
          m.insert (typename map::value_type (database::mysql, v));
          m.insert (typename map::value_type (database::oracle, v));
          m.insert (typename map::value_type (database::pgsql, v));
          m.insert (typename map::value_type (database::sqlite, v));
        }
      }
      else
        throw missing_value (o);
    }
  };

  template <typename V>
  struct parser<database_map<std::vector<V> > >
  {
    typedef database_map<std::vector<V> > map;

    static void
    parse (map& m, bool& xs, scanner& s)
    {
      xs = true;
      std::string o (s.next ());

      if (s.more ())
      {
        database k;
        V v;

        if (parse_option_value (o, s.next (), k, v))
          m[k].push_back (v);
        else
        {
          // No database prefix is specified which means it applies to
          // all the databases.
          //
          m[database::common].push_back (v);
          m[database::mssql].push_back (v);
          m[database::mysql].push_back (v);
          m[database::oracle].push_back (v);
          m[database::pgsql].push_back (v);
          m[database::sqlite].push_back (v);
        }
      }
      else
        throw missing_value (o);
    }
  };

  template <typename V>
  struct parser<database_map<std::set<V> > >
  {
    typedef database_map<std::set<V> > map;

    static void
    parse (map& m, bool& xs, scanner& s)
    {
      xs = true;
      std::string o (s.next ());

      if (s.more ())
      {
        database k;
        V v;

        if (parse_option_value (o, s.next (), k, v))
          m[k].insert (v);
        else
        {
          // No database prefix is specified which means it applies to
          // all the databases.
          //
          m[database::common].insert (v);
          m[database::mssql].insert (v);
          m[database::mysql].insert (v);
          m[database::oracle].insert (v);
          m[database::pgsql].insert (v);
          m[database::sqlite].insert (v);
        }
      }
      else
        throw missing_value (o);
    }
  };
}

#endif // ODB_OPTION_PARSERS_HXX