|
| 1 | +/* |
| 2 | + * Console.cpp |
| 3 | + * |
| 4 | + * Created on: Jun 15, 2018 |
| 5 | + * Author: kolban |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Example: |
| 10 | + * Argtable argtable; |
| 11 | + * argtable.addString("myparam", "l", "list", "Get Listings"); |
| 12 | + * argtable.parse(argc, argv); |
| 13 | + * ArgTableEntry_String* pStr = (ArgTableEntry_String*)argTable.get("myparam"); |
| 14 | + * pStr->getValue(); |
| 15 | + */ |
| 16 | +#include "Console.h" |
| 17 | + |
| 18 | +/** |
| 19 | + * Argtable instance constructor. |
| 20 | + */ |
| 21 | +ArgTable::ArgTable() { |
| 22 | + m_argtable = nullptr; |
| 23 | + m_argEnd = nullptr; |
| 24 | +} // ArgTable#ArgTable |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Argtable instance destructor. |
| 29 | + */ |
| 30 | +ArgTable::~ArgTable() { |
| 31 | + freeArgtable(); // Release any resources associated with the argtable. |
| 32 | +} // ArgTable#~ArgTable |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Build the ArgTable that will be used for parsing. |
| 37 | + */ |
| 38 | +/* private */ void ArgTable::build() { |
| 39 | + /* |
| 40 | + * The m_argTableEntries is a std::list that contains the argtable entries in the form of a std::pair. We |
| 41 | + * allocate storage for the ArgTable and then populate it. The last entry of the argtable must be an end marker. |
| 42 | + */ |
| 43 | + int size = m_argTableEntries.size(); |
| 44 | + m_argtable = new void*[size + 1]; |
| 45 | + int i=0; |
| 46 | + for (auto it = m_argTableEntries.begin(); it != m_argTableEntries.end(); ++it) { |
| 47 | + m_argtable[i] = it->second->getEntry(); |
| 48 | + i++; |
| 49 | + } |
| 50 | + m_argEnd = arg_end(10); |
| 51 | + m_argtable[i] = m_argEnd; |
| 52 | +} // ArgTable#build |
| 53 | + |
| 54 | + |
| 55 | +ArgTableEntry_Date ArgTable::addDate(std::string name, std::string shortopts, std::string longopts, std::string glossary) { |
| 56 | + ArgTableEntry_Date* pDate = new ArgTableEntry_Date(shortopts, longopts, glossary); |
| 57 | + m_argTableEntries.push_back(std::make_pair(name, pDate)); |
| 58 | + return *pDate; |
| 59 | +} // ArgTable#addDate |
| 60 | + |
| 61 | + |
| 62 | +ArgTableEntry_Double ArgTable::addDouble(std::string name, std::string shortopts, std::string longopts, std::string glossary) { |
| 63 | + ArgTableEntry_Double* pDouble = new ArgTableEntry_Double(shortopts, longopts, glossary); |
| 64 | + m_argTableEntries.push_back(std::make_pair(name, pDouble)); |
| 65 | + return *pDouble; |
| 66 | +} // ArgTable#addDouble |
| 67 | + |
| 68 | + |
| 69 | +ArgTableEntry_File ArgTable::addFile(std::string name, std::string shortopts, std::string longopts, std::string glossary) { |
| 70 | + ArgTableEntry_File* pFile = new ArgTableEntry_File(shortopts, longopts, glossary); |
| 71 | + m_argTableEntries.push_back(std::make_pair(name, pFile)); |
| 72 | + return *pFile; |
| 73 | +} // ArgTable#addFile |
| 74 | + |
| 75 | + |
| 76 | +ArgTableEntry_Int ArgTable::addInt(std::string name, std::string shortopts, std::string longopts, std::string glossary) { |
| 77 | + ArgTableEntry_Int* pInt = new ArgTableEntry_Int(shortopts, longopts, glossary); |
| 78 | + m_argTableEntries.push_back(std::make_pair(name, pInt)); |
| 79 | + return *pInt; |
| 80 | +} // ArgTable#addInt |
| 81 | + |
| 82 | + |
| 83 | +ArgTableEntry_Lit ArgTable::addLit(std::string name, std::string shortopts, std::string longopts, std::string glossary) { |
| 84 | + ArgTableEntry_Lit* pLit = new ArgTableEntry_Lit(shortopts, longopts, glossary); |
| 85 | + m_argTableEntries.push_back(std::make_pair(name, pLit)); |
| 86 | + return *pLit; |
| 87 | +} // ArgTable#addLit |
| 88 | + |
| 89 | + |
| 90 | +ArgTableEntry_String ArgTable::addString(std::string name, std::string shortopts, std::string longopts, std::string glossary, int min, int max) { |
| 91 | + ArgTableEntry_String* pStr = new ArgTableEntry_String(shortopts, longopts, glossary, min, max); |
| 92 | + m_argTableEntries.push_back(std::make_pair(name, pStr)); |
| 93 | + return *pStr; |
| 94 | +} // ArgTable#addString |
| 95 | + |
| 96 | + |
| 97 | +/** |
| 98 | + * Parse the input and output parameters against this argtable. |
| 99 | + * @param argc A count of the number of parameters. |
| 100 | + * @param argv An array of string parameters. |
| 101 | + * @return The number of errors detected. |
| 102 | + */ |
| 103 | +int ArgTable::parse(int argc, char* argv[]) { |
| 104 | + if (m_argtable == nullptr) { // If we don't have an argtable, build it. |
| 105 | + build(); |
| 106 | + } |
| 107 | + int nErrors = arg_parse(argc, argv, m_argtable); |
| 108 | + return nErrors; |
| 109 | +} // ArgTable#parse |
| 110 | + |
| 111 | + |
| 112 | +/** |
| 113 | + * Print any errors associated with the parsing. |
| 114 | + */ |
| 115 | +void ArgTable::printErrors(FILE* fp, std::string progName) { |
| 116 | + if (m_argEnd != nullptr) { |
| 117 | + arg_print_errors(fp, m_argEnd, progName.c_str()); |
| 118 | + } |
| 119 | +} // ArgTable#printErrors |
| 120 | + |
| 121 | + |
| 122 | +/** |
| 123 | + * Release the argtable data. |
| 124 | + */ |
| 125 | +/* private */void ArgTable::freeArgtable() { |
| 126 | + if (m_argtable != nullptr) { |
| 127 | + arg_free(m_argtable); |
| 128 | + m_argtable = nullptr; |
| 129 | + m_argEnd = nullptr; |
| 130 | + } |
| 131 | +} // ArgTable#freeArgtable |
| 132 | + |
| 133 | + |
| 134 | +ArgTableEntry_Date::ArgTableEntry_Date(std::string shortopts, std::string longopts, std::string glossary) { |
| 135 | + m_argDate = arg_daten(shortopts.c_str(), longopts.c_str(), "", "", 0, 1, glossary.c_str()); |
| 136 | + m_type = ArgType_t::DATE; |
| 137 | +} // ArgTableEntry_Date#ArgTableEntry_Date |
| 138 | + |
| 139 | + |
| 140 | +int ArgTableEntry_Date::getCount() { |
| 141 | + return m_argDate->count; |
| 142 | +} // ArgTableEntry_Date#getCount |
| 143 | + |
| 144 | + |
| 145 | +ArgTableEntry_Double::ArgTableEntry_Double(std::string shortopts, std::string longopts, std::string glossary) { |
| 146 | + m_argDbl = arg_dbln(shortopts.c_str(), longopts.c_str(), "", 0, 1, glossary.c_str()); |
| 147 | + m_type = ArgType_t::DBL; |
| 148 | +} |
| 149 | + |
| 150 | +int ArgTableEntry_Double::getCount() { |
| 151 | + return m_argDbl->count; |
| 152 | +} |
| 153 | + |
| 154 | +ArgTableEntry_File::ArgTableEntry_File(std::string shortopts, std::string longopts, std::string glossary) { |
| 155 | + m_argFile = arg_filen(shortopts.c_str(), longopts.c_str(), "", 0, 1, glossary.c_str()); |
| 156 | + m_type = ArgType_t::FILE; |
| 157 | +} |
| 158 | + |
| 159 | +int ArgTableEntry_File::getCount() { |
| 160 | + return m_argFile->count; |
| 161 | +} |
| 162 | + |
| 163 | +ArgTableEntry_Int::ArgTableEntry_Int(std::string shortopts, std::string longopts, std::string glossary) { |
| 164 | + m_argInt = arg_intn(shortopts.c_str(), longopts.c_str(), "", 0, 1, glossary.c_str()); |
| 165 | + m_type = ArgType_t::INT; |
| 166 | +} |
| 167 | + |
| 168 | +int ArgTableEntry_Int::getCount() { |
| 169 | + return m_argInt->count; |
| 170 | +} |
| 171 | + |
| 172 | +ArgTableEntry_Lit::ArgTableEntry_Lit(std::string shortopts, std::string longopts, std::string glossary) { |
| 173 | + m_argLit = arg_litn(shortopts.c_str(), longopts.c_str(), 0, 1, glossary.c_str()); |
| 174 | + m_type = ArgType_t::LIT; |
| 175 | +} |
| 176 | + |
| 177 | +int ArgTableEntry_Lit::getCount() { |
| 178 | + return m_argLit->count; |
| 179 | +} |
| 180 | + |
| 181 | +int ArgTableEntry_Regex::getCount() { |
| 182 | + return m_argRex->count; |
| 183 | +} |
| 184 | + |
| 185 | +ArgTableEntry_String::ArgTableEntry_String(std::string shortopts, std::string longopts, std::string glossary, int min, int max) { |
| 186 | + m_argStr = arg_strn(shortopts.c_str(), longopts.c_str(), "", min, max, glossary.c_str()); |
| 187 | + m_type = ArgType_t::STR; |
| 188 | +} |
| 189 | + |
| 190 | +int ArgTableEntry_String::getCount() { |
| 191 | + return m_argStr->count; |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +Console::Console() { |
| 196 | +} |
| 197 | + |
| 198 | +Console::~Console() { |
| 199 | +} |
| 200 | + |
0 commit comments