Mostly, std::regexp can be used as a drop-in replacement. Note: unfortunately ECMA regexps do not support lookbehind assertions. This lookbehind is necesary here because we want to allow parsing values from strings with additional content, which means we need explicitly to exclude mismatches due to invalid syntax. We can work around that issue like "either line start, or *not* one of these characters. Alternatively we could consider to make the match more rigid, i.e we would require the string to conain *only* the timecode spec to be parsed.
89 lines
2.2 KiB
C++
89 lines
2.2 KiB
C++
/*
|
|
Cmdline - abstraction of the usual commandline, a sequence of strings
|
|
|
|
Copyright (C) Lumiera.org
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of
|
|
the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
/** @file cmdline.cpp
|
|
** Implementation of a wrapper / marker to handle "commandline arguments".
|
|
*/
|
|
|
|
|
|
|
|
#include "lib/util.hpp"
|
|
#include "include/logging.h"
|
|
#include "lib/cmdline.hpp"
|
|
#include "lib/format-util.hpp"
|
|
|
|
#include <regex>
|
|
|
|
using std::regex;
|
|
using std::smatch;
|
|
using std::regex_search;
|
|
using util::join;
|
|
using util::noneg;
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
/** create as a tokenised <i>copy</i> of the current commandline.
|
|
* Note that argv[0] is always ignored. */
|
|
Cmdline::Cmdline (int argc, const char** argv)
|
|
: vector<string> (noneg(argc-1))
|
|
{
|
|
for (int i=1; i<argc; ++i)
|
|
{
|
|
ASSERT (argv[i]);
|
|
(*this)[i-1] = argv[i];
|
|
}
|
|
}
|
|
|
|
|
|
/** create by tokenising a string
|
|
* (e.g. "fake" cmdline, separated by whitespace)
|
|
*/
|
|
Cmdline::Cmdline (const string cmdline)
|
|
{
|
|
static regex TOKENDEF{"\\S+"};
|
|
smatch match;
|
|
string::const_iterator it = cmdline.begin();
|
|
string::const_iterator end = cmdline.end();
|
|
|
|
while (regex_search(it, end, match, TOKENDEF))
|
|
{
|
|
string ss(match[0]);
|
|
this->push_back(ss);
|
|
it = match[0].second;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/** conversion to string by joining the tokens */
|
|
Cmdline::operator string () const
|
|
{
|
|
return join(*this," ");
|
|
}
|
|
|
|
|
|
|
|
} // namespace lib
|