Parses the format string ("-option %s %d %f") to extract the flag ("-option") and create a code string ("sdf"). After the code string is created, the param list of void* pointers is allocated to hold the argument variable pointers.
| 114 | // code string is created, the param list of void* pointers is |
| 115 | // allocated to hold the argument variable pointers. |
| 116 | int |
| 117 | ArgOption::initialize() |
| 118 | { |
| 119 | size_t n; |
| 120 | const char *s; |
| 121 | |
| 122 | if (m_format.empty() || m_format == "%*") { |
| 123 | m_type = Sublist; |
| 124 | m_count = 1; // sublist callback function pointer |
| 125 | m_code = "*"; |
| 126 | m_flag = ""; |
| 127 | } else if (m_format == "<SEPARATOR>") { |
| 128 | } else { |
| 129 | // extract the flag name |
| 130 | s = &m_format[0]; |
| 131 | assert(*s == '-'); |
| 132 | assert(isalpha(s[1]) || (s[1] == '-' && isalpha(s[2]))); |
| 133 | |
| 134 | s++; |
| 135 | if (*s == '-') |
| 136 | s++; |
| 137 | |
| 138 | while (isalnum(*s) || *s == '_' || *s == '-') s++; |
| 139 | |
| 140 | if (! *s) { |
| 141 | m_flag = m_format; |
| 142 | m_type = Flag; |
| 143 | m_count = 1; |
| 144 | m_code = "b"; |
| 145 | } else { |
| 146 | n = s - (&m_format[0]); |
| 147 | m_flag.assign (m_format.begin(), m_format.begin()+n); |
| 148 | |
| 149 | // Parse the scanf-like parameters |
| 150 | |
| 151 | m_type = Regular; |
| 152 | |
| 153 | n = (m_format.length() - n) / 2; // conservative estimate |
| 154 | m_code.clear (); |
| 155 | |
| 156 | while (*s != '\0') { |
| 157 | if (*s == '%') { |
| 158 | s++; |
| 159 | assert(*s != '\0'); |
| 160 | |
| 161 | m_count++; // adding another parameter |
| 162 | |
| 163 | switch (*s) { |
| 164 | case 'd': // 32bit int |
| 165 | case 'g': // float |
| 166 | case 'f': // float |
| 167 | case 'F': // double |
| 168 | case 's': // string |
| 169 | case 'L': // vector<string> |
| 170 | assert (m_type == Regular); |
| 171 | m_code += *s; |
| 172 | break; |
| 173 |