
#include <iostream>
#include <string>
#include <vector>

int main( int argc, char **args ) {
	std::cerr << "Converts lines from stdin to latex tabular lines. By A.N. Yzelman, 2011" << std::endl;
	std::cerr << "Usage: " << args[0] << " <prefix> <postfix> <character>" << std::endl;
	std::cerr << "Prefix    to put before every line (e.g., & for leading empty column)" << std::endl;
	std::cerr << "Postfix   to but after  every line (e.g., \\\\ for endline)" << std::endl;
	std::cerr << "Character to put before and after every value on each line (e.g., $ for numerical values)" << std::endl;
	std::cerr << "All parameters are optional and empty by default." << std::endl;
	std::cerr << "(This help is printed to stderr)" << std::endl;
	std::string input_line;
	std::vector< std::string > parsed_line;
	std::string prefix (argc<2?"":args[1]);
	std::string postfix(argc<3?"":args[2]);
	std::string infix  (argc<4?"":args[3]);
	while(std::cin) {
		getline(std::cin, input_line);
		int skip = 1;
		int start = 0;
		int count = 0;
		for( int i=0; i<input_line.size(); i++, count++ ) {
			if( input_line[i] == ' ' && !skip ) {
				parsed_line.push_back( input_line.substr( start, count ) );
				count = 0;
				skip = 1;
			} else if( input_line[i] != ' ' && skip ) {
				skip = 0;
				count = 0;
				start = i;
			}
		}
		if( count != 0 && !skip )
			parsed_line.push_back( input_line.substr( start ) );
		std::vector< std::string >::iterator it = parsed_line.begin();
		std::cout << prefix << infix << (*it) << infix;
		++it;
		for( ; it!=parsed_line.end(); ++it )
			std::cout << " & " << infix << (*it) << infix;
		std::cout << postfix << std::endl;
		parsed_line.clear();
	};
	return 0;
}

