1 //
2 // Copyright 1998-2018 by Craig Stuart Sapp, All Rights Reserved.
3 // Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
4 // Creation Date: Sun Apr  5 13:07:18 PDT 1998
5 // Last Modified: Sat Apr 21 10:52:19 PDT 2018 Removed using namespace std;
6 // Filename:      midifile/include/Options.h
7 // Web Address:   http://midifile.sapp.org
8 // Syntax:        C++11
9 // vim:           ts=3 noexpandtab
10 //
11 // Description:   Interface for command-line options.
12 //
13 
14 #ifndef _OPTIONS_H_INCLUDED
15 #define _OPTIONS_H_INCLUDED
16 
17 #include <iostream>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #define OPTION_BOOLEAN_TYPE   'b'
23 #define OPTION_CHAR_TYPE      'c'
24 #define OPTION_DOUBLE_TYPE    'd'
25 #define OPTION_FLOAT_TYPE     'f'
26 #define OPTION_INT_TYPE       'i'
27 #define OPTION_STRING_TYPE    's'
28 #define OPTION_UNKNOWN_TYPE   'x'
29 
30 namespace smf {
31 
32 class Option_register {
33 	public:
34 		                  Option_register    (void);
35 		                  Option_register    (const std::string& aDefinition,
36 		                                      char aType,
37 		                                      const std::string& aDefaultOption);
38 		                  Option_register    (const std::string& aDefinition,
39 		                                      char aType,
40 		                                      const std::string& aDefaultOption,
41 		                                      const std::string& aModifiedOption);
42 
43 		                 ~Option_register    ();
44 
45 	  void               clearModified      (void);
46 	  const std::string& getDefinition      (void);
47 	  const std::string& getDefault         (void);
48 	  const std::string& getOption          (void);
49 	  const std::string& getModified        (void);
50 	  const std::string& getDescription     (void);
51 	  bool               isModified         (void);
52 	  char               getType            (void);
53 	  void               reset              (void);
54 	  void               setDefault         (const std::string& aString);
55 	  void               setDefinition      (const std::string& aString);
56 	  void               setDescription     (const std::string& aString);
57 	  void               setModified        (const std::string& aString);
58 	  void               setType            (char aType);
59 	  std::ostream&      print              (std::ostream& out);
60 
61 	protected:
62 		std::string       definition;
63 		std::string       description;
64 		std::string       defaultOption;
65 		std::string       modifiedOption;
66 		bool              modifiedQ;
67 		char              type;
68 
69 };
70 
71 
72 
73 class Options {
74 	public:
75 		                   Options           (void);
76 		                   Options           (int argc, char** argv);
77 
78 		                  ~Options           ();
79 
80 		int                argc              (void) const;
81 		const std::vector<std::string>& argv (void) const;
82 		int                define            (const std::string& aDefinition);
83 		int                define            (const std::string& aDefinition,
84 		                                      const std::string& description);
85 		const std::string& getArg            (int index);
86 		const std::string& getArgument       (int index);
87 		int                getArgCount       (void);
88 		int                getArgumentCount  (void);
89 		const std::vector<std::string>& getArgList      (void);
90 		const std::vector<std::string>& getArgumentList (void);
91 		bool               getBoolean        (const std::string& optionName);
92 		std::string        getCommand        (void);
93 		const std::string& getCommandLine    (void);
94 		std::string        getDefinition     (const std::string& optionName);
95 		double             getDouble         (const std::string& optionName);
96 		char               getFlag           (void);
97 		char               getChar           (const std::string& optionName);
98 		float              getFloat          (const std::string& optionName);
99 		int                getInt            (const std::string& optionName);
100 		int                getInteger        (const std::string& optionName);
101 		std::string        getString         (const std::string& optionName);
102 		char               getType           (const std::string& optionName);
103 		int                optionsArg        (void);
104 		std::ostream&      print             (std::ostream& out);
105 		std::ostream&      printOptionList   (std::ostream& out);
106 		std::ostream&      printOptionListBooleanState(std::ostream& out);
107 		void               process           (int error_check = 1,
108 		                                      int suppress = 0);
109 		void               process           (int argc, char** argv,
110 		                                      int error_check = 1,
111 		                                      int suppress = 0);
112 		void               reset             (void);
113 		void               xverify           (int argc, char** argv,
114 		                                      int error_check = 1,
115 		                                      int suppress = 0);
116 		void               xverify           (int error_check = 1,
117 		                                      int suppress = 0);
118 		void               setFlag           (char aFlag);
119 		void               setModified       (const std::string& optionName,
120 		                                      const std::string& optionValue);
121 		void               setOptions        (int argc, char** argv);
122 		void               appendOptions     (int argc, char** argv);
123 		void               appendOptions     (const std::string& strang);
124 		void               appendOptions     (const std::vector<std::string>& argv);
125 		std::ostream&      printRegister     (std::ostream& out);
126 		bool               isDefined         (const std::string& name);
127 
128 	protected:
129 		int                           m_options_error_check;  // verify command
130 		int                           m_oargc;
131 		std::vector<std::string>      m_oargv;
132 		std::string                   m_commandString;
133 		char                          m_optionFlag;
134 		std::vector<std::string>      m_argument;
135 
136 		std::vector<Option_register*> m_optionRegister;
137 		std::map<std::string, int>    m_optionList;
138 
139 		int                           m_processedQ;
140 		int                           m_suppressQ;       // prevent --options
141 		int                           m_optionsArgument; // --options present
142 
143 		std::vector<std::string>      m_extraArgv;
144 		std::vector<std::string>      m_extraArgv_strings;
145 
146 	private:
147 		int                getRegIndex       (const std::string& optionName);
148 		int                optionQ           (const std::string& aString,
149 		                                      int& argp);
150 		int                storeOption       (int gargp, int& position,
151 		                                      int& running);
152 
153 };
154 
155 } // end of namespace smf
156 
157 
158 #endif  /* _OPTIONS_H_INCLUDED */
159 
160 
161 
162