001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:




















































































































































































































































































































































/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <omni/argparser.hpp>
#include <omni/exception.hpp>
#include <omni/strings.hpp>

omni::application::argparser::argparser() :
    OMNI_CTOR_FW(omni::application::argparser)
    m_args(),
    m_argc(0)
{
    OMNI_D5_FW("created");
}

omni::application::argparser::argparser(const omni::application::argparser &cp) :
    OMNI_CPCTOR_FW(cp)
    m_args(cp.m_args),
    m_argc(cp.m_argc)
{
    OMNI_D5_FW("copied");
}

omni::application::argparser::argparser(const unsigned int& ac, const char** av) :
    OMNI_CTOR_FW(omni::application::argparser)
    m_args(),
    m_argc(0)
{
    this->set(ac, av);
}

omni::application::argparser::argparser(const unsigned int& ac, const wchar_t** av) :
    OMNI_CTOR_FW(omni::application::argparser)
    m_args(),
    m_argc(0)
{
    this->set(ac, av);
}

omni::application::argparser::argparser(const omni::seq::std_string_t& av) :
    OMNI_CTOR_FW(omni::application::argparser)
    m_args(),
    m_argc(0)
{
    this->m_argc = av.size();
    omni::seq::std_string_t::const_iterator it = av.begin();
    while (it != av.end()) {
        omni::string_t tmp = omni::string::util::to_string_t(*it);
        this->m_args.push_back(tmp);
        ++it;
    }
    OMNI_D5_FW("created");
}

omni::application::argparser::argparser(const omni::seq::std_wstring_t& av) :
    OMNI_CTOR_FW(omni::application::argparser)
    m_args(),
    m_argc(0)
{
    this->m_argc = av.size();
    omni::seq::std_wstring_t::const_iterator it = av.begin();
    while (it != av.end()) {
        omni::string_t tmp = omni::string::util::to_string_t(*it);
        this->m_args.push_back(tmp);
        ++it;
    }
    OMNI_D5_FW("created");
}

omni::application::argparser::~argparser()
{
    OMNI_TRY_FW
    OMNI_DTOR_FW
    this->m_args.clear();
    OMNI_CATCH_FW
}

omni::seq::string_t omni::application::argparser::argv() const
{
    omni::seq::string_t ret;
    std::copy(this->m_args.begin(), this->m_args.end(), std::back_inserter(ret));
    return ret;
}

omni::string_t omni::application::argparser::at(const unsigned int& index) const
{
    if (index > this->m_args.size() || this->m_args.empty()) {
        OMNI_ERR_RETV_FW("Invalid index specified: index out of range", omni::exceptions::index_out_of_range(), omni::string_t())
    }
    return this->m_args[index];
}

bool omni::application::argparser::contains(const std::string& sw) const
{
    if (sw.empty()) { return false; }
    omni::string_t tmp = omni::string::util::to_string_t(sw);
    return (this->_find(this->m_args.begin(), tmp) != this->m_args.end());
}

bool omni::application::argparser::contains(const std::wstring& sw) const
{
    if (sw.empty()) { return false; }
    omni::string_t tmp = omni::string::util::to_string_t(sw);
    return (this->_find(this->m_args.begin(), tmp) != this->m_args.end());
}

omni::string_t omni::application::argparser::get_arg(const unsigned int& index) const
{
    if (index > this->m_args.size() || this->m_args.empty()) {
        OMNI_ERR_RETV_FW("Invalid index specified: index out of range", omni::exceptions::index_out_of_range(), omni::string_t())
    }
    return this->m_args[index];
}

omni::string_t omni::application::argparser::get_switch(const std::string& sw) const
{
    if (sw.empty()) {
        OMNI_D1_FW("invalid switch specified");
        return omni::string_t();
    }
    omni::string_t tmp = omni::string::util::to_string_t(sw);
    omni::seq::string_t::const_iterator it = this->_find(this->m_args.begin(), tmp);
    if (it != this->m_args.end()) {
        if ((++it) != this->m_args.end()) {
            return omni::string_t(*it);
        }
    }
    return omni::string_t();
}

omni::string_t omni::application::argparser::get_switch(const std::wstring& sw) const
{
    if (sw.empty()) {
        OMNI_D1_FW("invalid switch specified");
        return omni::string_t();
    }
    omni::string_t tmp = omni::string::util::to_string_t(sw);
    omni::seq::string_t::const_iterator it = this->_find(this->m_args.begin(), tmp);
    if (it != this->m_args.end()) {
        if ((++it) != this->m_args.end()) {
            return omni::string_t(*it);
        }
    }
    return omni::string_t();
}

omni::seq::string_t omni::application::argparser::get_switches(const omni::string_t& sw) const
{
    if (sw.empty()) {
        OMNI_D1_FW("invalid switch specified");
        return omni::seq::string_t();
    }
    omni::seq::string_t ret;
    omni::seq::string_t::const_iterator f = this->_find(this->m_args.begin(), sw);
    while (f != this->m_args.end()) {
        if ((++f) != this->m_args.end()) {
            ret.push_back(*f);
            f = this->_find(f, sw);
        }
    }
    return ret;
}

omni::seq::string_t omni::application::argparser::get_range(std::size_t start, std::size_t end) const
{
    if (start > this->m_argc) {
        OMNI_ERR_RETV_FW("index out of range: start > argc", omni::exceptions::index_out_of_range(), omni::seq::string_t())
    }
    if (end > this->m_argc) {
        OMNI_ERR_RETV_FW("index out of range: end > argc", omni::exceptions::index_out_of_range(), omni::seq::string_t())
    }
    if (start > end) {
        OMNI_ERR_RETV_FW("start > end", omni::exceptions::index_out_of_range(), omni::seq::string_t())
    };
    omni::seq::string_t rng;
    omni::seq::string_t::const_iterator it = this->m_args.begin();
    for (std::size_t t = start; t > 0; --t) { ++it; }
    while (it != this->m_args.end() && start<=end) {
        rng.push_back(*it);
        ++it;
        ++start;
    }
    return rng;
}

void omni::application::argparser::set(const unsigned int& ac, const char** av)
{
    this->m_argc = ac;
    this->m_args.clear();
    for (unsigned int i = 0; i < ac; ++i) {
        this->m_args.push_back(omni::string::util::to_string_t(av[i]));
    }
}

void omni::application::argparser::set(const unsigned int& ac, const wchar_t** av)
{
    this->m_argc = ac;
    this->m_args.clear();
    for (unsigned int i = 0; i < ac; ++i) {
        omni::string_t tmp = omni::string::util::to_string_t(av[i]);
        this->m_args.push_back(tmp);
    }
}

void omni::application::argparser::set(const omni::seq::string_t& av)
{
    this->m_argc = av.size();
    this->m_args.clear();
    omni::seq::string_t::const_iterator it = av.begin();
    while (it != av.end()) {
        this->m_args.push_back(*it);
        ++it;
    }
}

const omni::string_t omni::application::argparser::to_string_t() const
{
    return this->to_string_t(false);
}

const omni::string_t omni::application::argparser::to_string_t(bool includeArg1) const
{
    #if defined(OMNI_UNICODE)
        return this->to_wstring(includeArg1);
    #else
        return this->to_string(includeArg1);
    #endif
}

const std::string omni::application::argparser::to_string() const
{
    return this->to_string(false);
}

const std::string omni::application::argparser::to_string(bool includeArg1) const
{
    std::string ret, tmp;
    std::string q("\"");
    omni::seq::string_t::const_iterator it = this->m_args.begin();
    if (!includeArg1) { ++it; }
    while (it != this->m_args.end()) {
        tmp = omni::string::util::to_string(*it);
        if (omni::cstring::contains(tmp, " ")) {
            ret.append(q).append(tmp).append(q);
        } else {
            ret.append(tmp);
        }
        if (++it != this->m_args.end()) { ret.append(" "); }
    }
    return ret;
}

const std::wstring omni::application::argparser::to_wstring() const
{
    return this->to_wstring(false);
}

const std::wstring omni::application::argparser::to_wstring(bool includeArg1) const
{
    std::wstring ret, tmp;
    std::wstring q(L"\"");
    omni::seq::string_t::const_iterator it = this->m_args.begin();
    if (!includeArg1) { ++it; }
    while (it != this->m_args.end()) {
        tmp = omni::string::util::to_wstring(*it);
        if (omni::wstring::contains(tmp, L" ")) {
            ret.append(q).append(tmp).append(q);
        } else {
            ret.append(tmp);
        }
        if (++it != this->m_args.end()) { ret.append(std::wstring(L" ")); }
    }
    return ret;
}

omni::string_t omni::application::argparser::operator[](const std::string &sw) const
{
    return this->get_switch(omni::string::util::to_string_t(sw));
}

omni::string_t omni::application::argparser::operator[](const std::wstring &sw) const
{
    return this->get_switch(omni::string::util::to_string_t(sw));
}

omni::string_t omni::application::argparser::operator[](const unsigned int& index) const
{
    if (index > this->m_args.size() || this->m_args.empty()) {
        OMNI_ERR_RETV_FW("Invalid index specified: index out of range", omni::exceptions::index_out_of_range(), omni::string_t())
    }
    return this->m_args[index];
}

omni::application::argparser& omni::application::argparser::operator=(const omni::application::argparser &ap)
{
    if (this != &ap) {
        OMNI_ASSIGN_FW(ap);
        this->m_args.clear();
        this->m_args = ap.m_args;
        this->m_argc = this->m_args.size();
    }
    return *this;
}

bool omni::application::argparser::operator==(const omni::application::argparser &o) const
{
    if (this == &o) { return true; }
    return (this->m_argc == o.m_argc && this->m_args == o.m_args)
    OMNI_EQUAL_FW(o);
}

bool omni::application::argparser::operator!=(const omni::application::argparser &o) const
{
    return !(*this == o);
}

///////// start private methods /////////

omni::seq::string_t::const_iterator omni::application::argparser::_find(omni::seq::string_t::const_iterator it, const omni::string_t& f) const
{
    if (this->m_args.empty()) { return this->m_args.end(); }
    for (; it != this->m_args.end(); ++it) { if (f == (*it)) { break; } }
    return it;
}