31 std::ifstream stream( path.
str().c_str() ) ;
33 throw Error(
"cannot open batch file" , path.
str() ) ;
34 m_line = readFrom( stream , path.
str() , true ) ;
35 m_args = split( m_line ) ;
40 std::ifstream stream( path.
str().c_str() ) ;
43 m_line = readFrom( stream , path.
str() , false ) ;
44 m_args = split( m_line ) ;
50 m_line = readFrom( stream , name ,
true ) ;
51 m_args = split( m_line ) ;
54 bool G::BatchFile::ignorable(
const std::string & trimmed_line )
57 trimmed_line.empty() ||
58 Str::lower(trimmed_line+
" ").find(
"@echo ") == 0U ||
59 Str::lower(trimmed_line+
" ").find(
"rem ") == 0U ;
62 bool G::BatchFile::relevant(
const std::string & trimmed_line )
64 return ! ignorable( trimmed_line ) ;
67 std::string G::BatchFile::join(
const std::string & file_name ,
unsigned int line_number )
69 std::ostringstream ss ;
70 ss << file_name <<
"(" << line_number <<
")" ;
74 std::string G::BatchFile::readFrom( std::istream & stream ,
const std::string & stream_name ,
bool strict )
77 unsigned int line_number = 1U ;
78 for( ; stream.good() ; line_number++ )
90 throw Error(
"too many lines in batch file" , join(stream_name,line_number) ) ;
94 if( strict && line.empty() )
95 throw Error(
"batch file is empty" , join(stream_name,line_number) ) ;
101 typedef std::string::size_type size_type ;
102 size_type
const npos = std::string::npos ;
104 std::string start =
"start " ;
105 size_type start_pos =
Str::lower(line).find(start) ;
106 size_type command_pos = start_pos == npos ? 0U : line.find_first_not_of(
" ",start_pos+start.size()) ;
108 bool named = start_pos != npos && line.at(command_pos) ==
'"' ;
111 size_t name_start_pos = command_pos ;
112 size_t name_end_pos = line.find(
"\"" , name_start_pos+1U ) ;
113 if( name_end_pos == npos )
114 throw Error(
"mismatched quotes in batch file" , stream_name ) ;
115 if( (name_end_pos+2U) >= line.size() || line.at(name_end_pos+1U) !=
' ' )
116 throw Error(
"invalid window name in batch file" , stream_name ) ;
118 m_name = line.substr( name_start_pos+1U , name_end_pos-(name_start_pos+1U) ) ;
122 command_pos = line.find_first_not_of(
" ",name_end_pos+2U) ;
125 if( command_pos != npos )
127 line.erase( 0U , command_pos ) ;
153 void G::BatchFile::dequote( std::string & s )
155 if( s.size() >= 2U && s.find(
"\"") == 0U && (s.rfind(
"\"")+1U) == s.size() )
156 s = s.substr( 1U , s.size()-2U ) ;
161 G_ASSERT( args.size() > 0U ) ;
162 if( args.size() == 0U )
163 throw Error(
"invalid contents for startup batch file" ) ;
165 std::string name = name_in ;
173 std::ofstream stream( path.
str().c_str() , std::ios::binary | std::ios::out | std::ios::trunc ) ;
175 throw Error(
"cannot create batch file" , path.
str() ) ;
177 stream <<
"start \"" << name <<
"\"" ;
178 for( StringArray::const_iterator p = args.begin() ; p != args.end() ; ++p )
180 stream <<
" " << percents(quote(*p)) ;
186 throw Error(
"cannot write batch file" , path.
str() ) ;
195 return args.
array() ;
198 std::string G::BatchFile::percents(
const std::string & s )
200 std::string result( s ) ;
205 std::string G::BatchFile::quote(
const std::string & s )
208 s.find(
"\"") == std::string::npos && s.find_first_of(
" \t") != std::string::npos ?
209 "\"" + s +
"\"" : s ;
std::string line() const
Returns the main command-line from within the batchfile, with normalised spaces, without any "start" ...
std::string str() const
Returns the path string.
static void write(const G::Path &, const StringArray &args, const std::string &start_window_name=std::string())
Writes a startup batch file, including a "start" prefix.
BatchFile(const G::Path &)
Constructor that reads from a file.
Path withoutExtension() const
Returns a path without the basename extension, if any.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
std::vector< std::string > StringArray
A std::vector of std::strings.
std::string name() const
Returns the "start" window name, if any.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
static void trimLeft(std::string &s, const std::string &ws, size_type limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
const G::StringArray & args() const
Returns the startup command-line broken up into de-quoted pieces.
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 uppercase characters have been replaced by lowercase chara...
StringArray array(unsigned int shift=0U) const
Returns the arguments a string array, including the program name in the first position.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
static std::string unique(std::string s, char c= ' ', char r= ' ')
Returns a string with repeated 'c' characters replaced by one 'r' character.
void parse(HINSTANCE hinstance, const std::string &command_line_tail)
Parses the given command-line tail, splitting it up into an array of tokens.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Overload discriminator for G::BatchFile.
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.