source: SHVCSoftware/branches/0.1.1-bugfix/source/Lib/TAppCommon/program_options_lite.h @ 1606

Last change on this file since 1606 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

File size: 8.9 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2012, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33#include <iostream>
34#include <sstream>
35#include <string>
36#include <list>
37#include <map>
38#include "TLibCommon/TypeDef.h"
39
40//! \ingroup TAppCommon
41//! \{
42
43
44namespace df
45{
46  namespace program_options_lite
47  {
48    struct Options;
49   
50    void doHelp(std::ostream& out, Options& opts, unsigned columns = 80);
51    unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]);
52    unsigned parseSHORT(Options& opts, unsigned argc, const char* argv[]);
53    std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[]);
54    void scanLine(Options& opts, std::string& line);
55    void scanFile(Options& opts, std::istream& in);
56    void setDefaults(Options& opts);
57    void parseConfigFile(Options& opts, const std::string& filename);
58    bool storePair(Options& opts, const std::string& name, const std::string& value);
59   
60    /** OptionBase: Virtual base class for storing information relating to a
61     * specific option This base class describes common elements.  Type specific
62     * information should be stored in a derived class. */
63    struct OptionBase
64    {
65      OptionBase(const std::string& name, const std::string& desc)
66      : opt_string(name), opt_desc(desc)
67      {};
68     
69      virtual ~OptionBase() {}
70     
71      /* parse argument arg, to obtain a value for the option */
72      virtual void parse(const std::string& arg) = 0;
73      /* set the argument to the default value */
74      virtual void setDefault() = 0;
75     
76      std::string opt_string;
77      std::string opt_desc;
78    };
79   
80    /** Type specific option storage */
81    template<typename T>
82    struct Option : public OptionBase
83    {
84      Option(const std::string& name, T& storage, T default_val, const std::string& desc)
85      : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val)
86      {}
87     
88      void parse(const std::string& arg);
89     
90      void setDefault()
91      {
92        opt_storage = opt_default_val;
93      }
94     
95      T& opt_storage;
96      T opt_default_val;
97    };
98   
99    /* Generic parsing */
100    template<typename T>
101    inline void
102    Option<T>::parse(const std::string& arg)
103    {
104      std::istringstream arg_ss (arg,std::istringstream::in);
105      arg_ss >> opt_storage;
106    }
107   
108    /* string parsing is specialized -- copy the whole string, not just the
109     * first word */
110    template<>
111    inline void
112    Option<std::string>::parse(const std::string& arg)
113    {
114      opt_storage = arg;
115    }
116   
117    /** Option class for argument handling using a user provided function */
118    struct OptionFunc : public OptionBase
119    {
120      typedef void (Func)(Options&, const std::string&);
121     
122      OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc)
123      : OptionBase(name, desc), parent(parent_), func(func_)
124      {}
125     
126      void parse(const std::string& arg)
127      {
128        func(parent, arg);
129      }
130     
131      void setDefault()
132      {
133        return;
134      }
135     
136    private:
137      Options& parent;
138      void (*func)(Options&, const std::string&);
139    };
140   
141    class OptionSpecific;
142    struct Options
143    {
144      ~Options();
145     
146      OptionSpecific addOptions();
147     
148      struct Names
149      {
150        Names() : opt(0) {};
151        ~Names()
152        {
153          if (opt)
154            delete opt;
155        }
156        std::list<std::string> opt_long;
157        std::list<std::string> opt_short;
158        OptionBase* opt;
159      };
160
161      void addOption(OptionBase *opt);
162     
163      typedef std::list<Names*> NamesPtrList;
164      NamesPtrList opt_list;
165     
166      typedef std::map<std::string, NamesPtrList> NamesMap;
167      NamesMap opt_long_map;
168      NamesMap opt_short_map;
169    };
170   
171    /* Class with templated overloaded operator(), for use by Options::addOptions() */
172    class OptionSpecific
173    {
174    public:
175      OptionSpecific(Options& parent_) : parent(parent_) {}
176     
177      /**
178       * Add option described by name to the parent Options list,
179       *   with storage for the option's value
180       *   with default_val as the default value
181       *   with desc as an optional help description
182       */
183      template<typename T>
184      OptionSpecific&
185      operator()(const std::string& name, T& storage, T default_val, const std::string& desc = "")
186      {
187        parent.addOption(new Option<T>(name, storage, default_val, desc));
188        return *this;
189      }
190#if 1 //SVC_EXTENSION
191      template<typename T>
192      OptionSpecific&
193        operator()(const std::string& name, T* storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" )
194      {
195        std::string cNameBuffer;
196        std::string cDescriptionBuffer;
197
198        cNameBuffer       .resize( name.size() + 10 );
199        cDescriptionBuffer.resize( desc.size() + 10 );
200
201        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
202        {
203          // isn't there are sprintf function for string??
204          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
205          sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
206
207          size_t pos = cNameBuffer.find_first_of('\0');
208          if(pos != std::string::npos)
209            cNameBuffer.resize(pos);
210
211          parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer ));
212        }
213
214        return *this;
215      }
216
217      template<typename T>
218      OptionSpecific&
219        operator()(const std::string& name, T** storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" )
220      {
221        std::string cNameBuffer;
222        std::string cDescriptionBuffer;
223
224        cNameBuffer       .resize( name.size() + 10 );
225        cDescriptionBuffer.resize( desc.size() + 10 );
226
227        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
228        {
229          // isn't there are sprintf function for string??
230          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
231          sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
232
233          size_t pos = cNameBuffer.find_first_of('\0');
234          if(pos != std::string::npos)
235            cNameBuffer.resize(pos);
236
237          parent.addOption(new Option<T>( cNameBuffer, *(storage[uiK]), default_val, cDescriptionBuffer ));
238        }
239
240        return *this;
241      }
242#endif
243      /**
244       * Add option described by name to the parent Options list,
245       *   with desc as an optional help description
246       * instead of storing the value somewhere, a function of type
247       * OptionFunc::Func is called.  It is upto this function to correctly
248       * handle evaluating the option's value.
249       */
250      OptionSpecific&
251      operator()(const std::string& name, OptionFunc::Func *func, const std::string& desc = "")
252      {
253        parent.addOption(new OptionFunc(name, parent, func, desc));
254        return *this;
255      }
256    private:
257      Options& parent;
258    };
259   
260  }; /* namespace: program_options_lite */
261}; /* namespace: df */
262
263//! \}
Note: See TracBrowser for help on using the repository browser.