Esempio n. 1
0
int
main( int argc, char** argv )
{
	int status = EXIT_SUCCESS;
	GOptionContext *context;
	GError *error = NULL;
	NAObjectAction *action;
	GSList *msg = NULL;
	GSList *im;
	gchar *help;
	gint errors;

#if !GLIB_CHECK_VERSION( 2,36, 0 )
	g_type_init();
#endif

	setlocale( LC_ALL, "" );
	console_init_log_handler();

	context = init_options();

	if( argc == 1 ){
		g_set_prgname( argv[0] );
		help = g_option_context_get_help( context, FALSE, NULL );
		g_print( "\n%s", help );
		g_free( help );
		exit( status );
	}

	if( !g_option_context_parse( context, &argc, &argv, &error )){
		g_printerr( _( "Syntax error: %s\n" ), error->message );
		g_error_free (error);
		exit_with_usage();
	}

	if( version ){
		na_core_utils_print_version();
		exit( status );
	}

	errors = 0;

	if( !label || !g_utf8_strlen( label, -1 )){
		g_printerr( _( "Error: an action label is mandatory.\n" ));
		errors += 1;
	}

	if( enabled && disabled ){
		g_printerr( CANNOT_BOTH, "--enabled", "--disabled" );
		errors += 1;
	} else if( !disabled ){
		enabled = TRUE;
	}

	if( target_selection && nocontext ){
		g_printerr( CANNOT_BOTH, "--context", "--nocontext" );
		errors += 1;
	} else if( !nocontext ){
		target_selection = TRUE;
	}

	if( target_location && nolocation ){
		g_printerr( CANNOT_BOTH, "--location", "--nolocation" );
		errors += 1;
	}

	if( target_toolbar && notoolbar ){
		g_printerr( CANNOT_BOTH, "--toolbar", "--notoolbar" );
		errors += 1;
	}

	if( matchcase && nocase ){
		g_printerr( CANNOT_BOTH, "--match-case", "--nocase" );
		errors += 1;
	} else if( !nocase ){
		matchcase = TRUE;
	}

	if( accept_multiple && strlen( selection_count )){
		g_printerr( CANNOT_BOTH, "--accept-multiple", "--selection-count" );
		errors += 1;
	}

	if( onlyshow_array && notshow_array ){
		g_printerr( CANNOT_BOTH, "--only-show-in", "--not-show-in" );
		errors += 1;
	}

	if( output_stdout && output_desktop ){
		g_printerr( _( "Error: only one output option may be specified.\n" ));
		errors += 1;
	}

	if( errors ){
		exit_with_usage();
	}

	action = get_action_from_cmdline();

	if( output_desktop ){
		output_to_desktop( action, &msg );
	} else {
		output_to_stdout( action, &msg );
	}

	if( msg ){
		for( im = msg ; im ; im = im->next ){
			g_printerr( "%s\n", ( gchar * ) im->data );
		}
		na_core_utils_slist_free( msg );
		status = EXIT_FAILURE;
	}

	g_object_unref( action );
	g_option_context_free( context );

	exit( status );
}
Esempio n. 2
0
int main( int ac, char** av )
{
    std::string line;
    try
    {
        comma::command_line_options options( ac, av, usage );
        char delimiter = options.value< char >( "--delimiter,--delim,-d", ',' );
        char range_delimiter = options.value< char >( "--range-delimiter,--range-delim,-r", ':' );
        
        // read each input line.
        std::string line;
        
        std::vector< map_t > permutations;
        
        while (std::getline(std::cin, line))
        {
            if ( line.empty() ) { continue; }
            // ignored comments
            if ( line[0] == '#' ) { continue; }
            // get path and value. ( split at first = )
            std::string::size_type p = line.find_first_of( '=' );
            if( p == std::string::npos ) { COMMA_THROW( comma::exception, "expected '" << delimiter << "'-separated xpath=value pairs; got \"" << line << "\"" ); }
            const std::string& path = line.substr( 0, p );
            const std::string& value = line.substr( p + 1, std::string::npos );
            // use split_escaped to split by delimiter
            std::vector<std::string> values = comma::split_escaped(value, delimiter);
            
            map_t map;
            map.path = path;
            
            for (std::size_t i = 0; i < values.size(); ++i )
            {
                // multi values.
                //    for each value:
                //       split_escaped to check if it is a range
                
                std::vector<std::string> range = comma::split_escaped(values[i], range_delimiter);
                if ( range.size() == 1 )
                {
                    // if is not a range, add to std::vector<std::string> of values associated with the path.
                    map.values.push_back(values[i]);
                }
                else
                {
                    if (range.size() < 2 || range.size() > 3) 
                    { COMMA_THROW(comma::exception, "expected range is \"start" << range_delimiter << "stop" << range_delimiter << "step\", got \"" << values[i] << "\"" << std::endl ); }
                    
                    
                    try {
                        double start = boost::lexical_cast<double>(range[0]);
                        double stop = boost::lexical_cast<double>(range[1]);
                    
                        if ( !boost::math::isfinite(start) || !boost::math::isfinite(stop) ) 
                        { COMMA_THROW(comma::exception, "cannot expand range " << values[i] << std::endl ); }
                    
                        int sign = start < stop ? 1 : -1 ;
                        double step;
                        if ( range.size() == 3 ) { step = boost::lexical_cast<double>(range[2]); }
                        else { step = sign; }
                    
                        if ( step == 0 || ( sign * step < 0 ) ) { COMMA_THROW(comma::exception, "cannot expand range " << values[i] << std::endl) }
                    
                        // if it is a range: parse begin, end, and step (default 1)
                        // todo: support int, double, time??
                    
                        // throw on infinity, step = 0, sign mismatch
                        // for (i = start; i <= stop; i += step ) // (need to check for -ve step)
                        // push the value onto std::vector<std::string> of values associated with path
                        double i = start;
                        do
                        {
                            std::string s = boost::lexical_cast<std::string>(i);
                            map.values.push_back(s);
                            i += step;
                        } while ( ( sign > 0 && i < stop ) || ( sign < 0 && i > stop ) );
                    }
                    catch ( boost::bad_lexical_cast & e )
                    {
                        boost::posix_time::ptime start = boost::posix_time::from_iso_string(range[0]);
                        boost::posix_time::ptime stop = boost::posix_time::from_iso_string(range[1]);
                    
                        int sign = start < stop ? 1 : -1 ;
                        double step;
                        if ( range.size() == 3 ) { step = boost::lexical_cast<double>(range[2]); }
                        else { step = sign; }
                    
                        if ( step == 0 || ( sign * step < 0 ) ) { COMMA_THROW(comma::exception, "cannot expand range " << values[i] << std::endl) }
                    
                        // if it is a range: parse begin, end, and step (default 1)
                        // todo: support int, double, time??
                    
                        // throw on infinity, step = 0, sign mismatch
                        // for (i = start; i <= stop; i += step ) // (need to check for -ve step)
                        // push the value onto std::vector<std::string> of values associated with path
                        boost::posix_time::ptime i = start;
                        do
                        {
                            std::string s = boost::posix_time::to_iso_string(i);
                            map.values.push_back(s);
                            i += boost::posix_time::seconds(step);
                        } while ( ( sign > 0 && i < stop ) || ( sign < 0 && i > stop ) );
                    }
                }
            }
            permutations.push_back(map);
        }
        
        if (options.exists("--stdout"))
        {
            output_to_stdout(permutations, options);  
        }
        else
        {
            output_to_files(permutations, options);
        }
        
        return 0;
    }
    catch( std::exception& ex )
    { 
        std::cerr << "name-value-permute: " << ex.what() << std::endl;
    }
    catch( ... )
    { 
        std::cerr << "name-value-permute: unknown exception" << std::endl;
    }
    return 1;
}