/* $Revision: 15 $ $Date: 3/03/05 11:59 $ Copyright © 1995-2007, FSL Technologies Limited. Contact "http://fost.3.felspar.com". */ #include "stdafx.h" using namespace std; using namespace FSLib; namespace { Revision c_revision( L"$Archive: /FOST.3/F3Util/args.cpp $", __DATE__, L"$Revision: 15 $", L"$Date: 3/03/05 11:59 $" ); } /* FSLib::Arguments */ Arguments::Arguments( int argc, wchar_t *argv[] ) { load( argc, argv ); } Arguments::Arguments( int argc, wchar_t *argv[], wchar_t *envp[] ) { load( argc, argv ); load( envp ); } void Arguments::load( int argc, wchar_t *argv[] ) { for ( int c( 0 ); c != argc; ++c ) { if ( argv[ c ][ 0 ] == L'-' || argv[ c ][ 0 ] == L'/' ) { if ( argc == c + 1 ) m_switches[ argv[ c ] + 1 ] = L""; else { m_switches[ argv[ c ] + 1 ] = argv[ c + 1 ]; ++c; } } else { m_arguments.push_back( argv[ c ] ); } } } void Arguments::load( wchar_t *envp[] ) { for ( int arg( 0 ); envp[ arg ] != NULL; ++arg ) { FSLib::wstring setting( envp[ arg ] ); std::pair< FSLib::wstring, Nullable< FSLib::wstring > > set = partition( setting, L"=" ); if ( !set.second.isnull() ) m_environment[ set.first ] = set.second.value(); } } void Arguments::environment( const FSLib::wstring &envName, const FSLib::wstring §ion, const FSLib::wstring &name ) { if ( m_environment.find( envName ) != m_environment.end() ) { m_registered.push_back( boost::shared_ptr< Setting >( new Setting( L"Environment", section, name, m_environment[ envName ], false ) ) ); } } void Arguments::argument( Arguments::size_type argument, const FSLib::wstring §ion, const FSLib::wstring &name ) { if ( argument >= 0 && argument < arguments() ) { m_registered.push_back( boost::shared_ptr< Setting >( new Setting( L"Command argument", section, name, m_arguments[ argument ], false ) ) ); } } Nullable< FSLib::wstring > Arguments::commandSwitch( const FSLib::wstring &s ) const { std::map< FSLib::wstring, FSLib::wstring >::const_iterator pos( m_switches.find( s ) ); if ( pos == m_switches.end() ) return Null; else return (*pos).second; } void Arguments::commandSwitch( const FSLib::wstring &theSwitch, const FSLib::wstring §ion, const FSLib::wstring &name ) { if ( m_switches.find( theSwitch) != m_switches.end() ) { m_registered.push_back( boost::shared_ptr< Setting >( new Setting( L"Command switch", section, name, m_switches[ theSwitch ], false ) ) ); } } Arguments::size_type Arguments::arguments() const { return m_arguments.size(); } Nullable< FSLib::wstring > Arguments::operator []( Arguments::size_type pos ) const { if ( pos >= arguments() ) { return Nullable< FSLib::wstring >(); } else { return m_arguments[ pos ]; } } wostream &Arguments::printOn( wostream &o ) const { {for ( map< FSLib::wstring, FSLib::wstring >::const_iterator it( m_environment.begin() ); it != m_environment.end(); ++it ) { o << L"argument,environment," << (*it).first << L"," << (*it).second << endl; }} {for ( vector< FSLib::wstring >::const_iterator it( m_arguments.begin() ); it != m_arguments.end(); ++it ) { o << L"argument,command line argument," << (*it) << endl; }} {for ( map< FSLib::wstring, FSLib::wstring >::const_iterator it( m_switches.begin() ); it != m_switches.end(); ++it ) { o << L"argument,command line switch," << (*it).first << L"," << (*it).second << endl; }} return o; }