File: cmdline_parser.hpp

package info (click to toggle)
mapnik 0.5.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,136 kB
  • ctags: 14,550
  • sloc: cpp: 68,887; python: 24,895; xml: 1,534; makefile: 503; sh: 79
file content (85 lines) | stat: -rw-r--r-- 3,205 bytes parent folder | download | duplicates (2)
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
// ----------------------------------------------------------------------------
// Copyright (C) 2002-2005 Marcin Kalicinski
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org
// ----------------------------------------------------------------------------
#ifndef BOOST_PROPERTY_TREE_CMDLINE_PARSER_HPP_INCLUDED
#define BOOST_PROPERTY_TREE_CMDLINE_PARSER_HPP_INCLUDED

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/detail/ptree_utils.hpp>

namespace boost { namespace property_tree { namespace cmdline_parser
{

    template<class Ptree>
    void read_cmdline(int argc, 
                      typename Ptree::char_type *argv[], 
                      const std::basic_string<typename Ptree::char_type> &metachars,
                      Ptree &pt)
    {

        typedef typename Ptree::char_type Ch;
        typedef std::basic_string<Ch> Str;

        Ptree local;
        
        // For all arguments
        for (int i = 0; i < argc; ++i)
        {
            Str text = detail::trim<Ch>(argv[i]);
            if (!text.empty())
                if (metachars.find(text[0]) != Str::npos)
                {
                    if (text.size() == 1)
                    {
                        Ptree &child = local.put(text, Str());
                        Str key; 
                        if (child.size() < 10) 
                            key.push_back(typename Ptree::char_type('0' + child.size()));
                        child.push_back(std::make_pair(key, Ptree(child.data())));
                    }
                    else if (text.size() == 2)
                    {
                        Ptree &child = local.put(text.substr(1, 1), Str());
                        Str key; 
                        if (child.size() < 10) 
                            key.push_back(typename Ptree::char_type('0' + child.size()));
                        child.push_back(std::make_pair(key, Ptree(child.data())));
                    }
                    else
                    {
                        Ptree &child = local.put(text.substr(1, 1), detail::trim<Ch>(text.substr(2, Str::npos)));
                        Str key; 
                        if (child.size() < 10) 
                            key.push_back(typename Ptree::char_type('0' + child.size()));
                        child.push_back(std::make_pair(key, Ptree(child.data())));
                    }
                }
                else
                {
                    Ptree &child = local.put(Str(), detail::trim<Ch>(text));
                    Str key; 
                    if (child.size() < 10) 
                        key.push_back(typename Ptree::char_type('0' + child.size()));
                    child.push_back(std::make_pair(key, Ptree(child.data())));
                }
        }

        // Swap local and pt
        pt.swap(local);

    }

} } }

namespace boost { namespace property_tree
{
    using cmdline_parser::read_cmdline;
} }

#endif