Exemple #1
0
int _tmain(int argc, _TCHAR* argv[])
{
    try
	{
        po::options_description desc( "Options" );
        desc.add_options()
            ( "help,h", "Show detailed help." )
			( "options,p", "Show parameter information." )
			( "version,p", "Show version information." )
			( "input-file,i", po::value<std::vector<std::string>>(), "Define file(s) for the convertion input." )
			( "input-type,I", po::value<std::vector<std::string>>(), "Define file type(s) for automatic conversion of files in the working directory." )
			( "output-file,o", po::value<std::string>(), "Define file for the convertion output." )
			( "noheader,h", "Disable adding of header support defines." )
            ( "const,C", "Define array as const." )
			( "respectcase,r", "Disable converting file types into lower case." )
			( "wxnone,w", "Disable adding of wxWidgets support macro's." )
			( "wxheader,W", po::value<std::string>()->default_value( "wx/wx.h" ), "Select the header that includes wxWidgets (precompiled header?)." )
			( "appendtype,t", "Add the file type at the end of the identifier (myimage_png)." )
			( "text,T", "Disable binary output and use text output, converts feed codes to systems defaults." )
		;

        po::positional_options_description posdesc;
        posdesc.add( "input-file", -1 );

        po::variables_map opt;
        po::store( po::command_line_parser( argc, argv ).options( desc ).positional( posdesc ).run(), opt );
		po::store( po::parse_config_file( fs::ifstream( fs::path( "default.cfg" ) ), desc ), opt );
        po::notify( opt );

		std::cout << WXINCLUDE_INFO << std::endl;

		/* Show options when requested */
        if ( opt.count( "options" ) )
		{
			std::cout << desc << std::endl << std::endl;
            exit( 0 );
        }

		/* Show help when requested */
        if ( opt.count( "help" ) )
		{
			std::cout << WXINCLUDE_HELP;
			std::cout << std::endl << desc << std::endl << std::endl;
            exit( 0 );
        }

		/* Show version when requested */
        if ( opt.count( "version" ) )
		{
			std::cout << WXINCLUDE_VERSION << std::endl;
            exit( 0 );
        }

		/* Process */
        if ( opt.count( "input-file" ) || opt.count( "input-type" ) )
		{
			if ( opt.count( "output-file" ) )
			{	
				/* Create timer */
				boost::timer timer;

				/* Create output file */
				std::string headername( opt[ "output-file" ].as<std::string>() );

				fs::path outputpath( headername );
				fs::ofstream output( outputpath, std::ios::out | std::ios::trunc );

				/* Use buffer */
				char outbuffer[BUFFER_SIZE];
				output.rdbuf()->pubsetbuf( outbuffer, BUFFER_SIZE );

				if ( !opt.count( "text" ) )
					output.setf( std::ios::binary );

				if ( !output )
					throw std::runtime_error( "Failed to create output file!" );

				/* Show status */
				std::cout << "Build  : file '" << outputpath.leaf() << "'..." << std::endl;

				/* Get base name of file */
				headername = fs::basename( outputpath );

				/* Data string stream */
				std::ostringstream data;

				/* Write header start when wanted */
				if ( !opt.count( "noheader" ) )
					defineheader_start( data, headername, opt.count( "wxnone" ) ? false : true, opt.count( "const" ) ? true : false );

				/* Get defined or else default wx header */
				std::string includename( opt[ "wxheader" ].as<std::string>() );

				/* Write macros when wanted */
				if ( !opt.count( "wxnone" ) )
					definemacros( data, includename, opt[ "wxheader" ].defaulted() );

				/* Common input buffer */
				char inbuffer[BUFFER_SIZE];

				/* Process input files based on provided list */
				if ( opt.count( "input-file" ) )
				{
					std::vector<std::string> files( opt[ "input-file" ].as<std::vector<std::string>>() );

					BOOST_FOREACH( std::string& file, files )
					{
						fs::path inputpath( file );
						std::string fileext( fs::extension( inputpath ) );

						fs::ifstream input( inputpath, std::ios::in | std::ios::binary | std::ios::ate );
						input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE ); 

						if ( input.is_open() )
						{
							/* Show status */
							std::cout << "Process: file '" << file << "'..." << std::endl;

							/* Remove extension */
							boost::erase_last( file, fileext );

							if ( !opt.count( "respectcase" ) )
								boost::to_lower( fileext );

							/* Append type */
							if ( opt.count( "appendtype" ) )
							{
								boost::erase_first( fileext, "." );

								/* Static and NO copy constructor for speed */
								static boost::format fmt( "%1%_%2%" );
								file = boost::str( fmt % file % fileext );
							}

							/* Lower case names when wanted */
							if ( !opt.count( "respectcase" ) )
								boost::to_lower( file );

							/* Process file */
							definefile( data, input, file, opt.count( "const" ) ? true : false );
						}
						else
						{
							/* Only show warning, other files need to be processed */
							std::cout << "Warning: input file '" << file << "' failed to open." << std::endl;
						}
					}
				}

				/* Process input files based on provided type */
				if ( opt.count( "input-type" ) )
				{
					std::vector<std::string> types( opt[ "input-type" ].as<std::vector<std::string>>() );

					for ( fs::directory_iterator dir_itr( fs::initial_path() ); dir_itr != fs::directory_iterator(); ++dir_itr )
					{
						BOOST_FOREACH( std::string& type, types )
						{
							/* Normal file? */
							if ( fs::is_regular( dir_itr->status() ) )
							{
								/* Wanted type? */
								std::string fileext( fs::extension( dir_itr->path() ));

								bool equals = false;

								if ( opt.count( "respectcase" ) )
									equals = boost::equals( fileext, type );
								else
									equals = boost::iequals( fileext, type );

								if ( equals )
								{
									fs::ifstream input( dir_itr->path(), std::ios::in | std::ios::binary | std::ios::ate );
									input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE ); 

									std::string file( dir_itr->path().leaf() );

									if ( input.is_open() )
									{
										/* Show status */
										std::cout << "Process: file '" << file << "'..." << std::endl;

										/* Remove extension */
										boost::erase_last( file, fileext );

										/* Append type */
										if ( opt.count( "appendtype" ) )
										{
											boost::erase_first( fileext, "." );

											/* Static and NO copy constructor for speed */
											static boost::format fmt( "%1%_%2%" );
											file = boost::str( fmt % file % fileext );
										}

										/* Lower case names when wanted */
										if ( !opt.count( "respectcase" ) )
											boost::to_lower( file );

										/* Process file */
										definefile( data, input, file, opt.count( "const" ) ? true : false );
									}
									else
									{
										/* Only show warning, other files need to be processed */
										std::cout << "Warning: input file '" << file << "' failed to open." << std::endl;
									}
								}
							}
						}
					}
				}
Exemple #2
0
int main ( int argc, const char* argv[] )
{
	if(argc <= 2)
	{
		std::cerr << "You need to specify at least the output file and one input file." << std::endl
		          << "Example: " << argv[0] << " output_file input_file" << std::endl;
		return 1;
	}
	try
	{
		std::string headername (argv[1]);
		std::vector<std::string> input_files;
		for(int i = 2; i < argc; i++)
		{
			input_files.push_back(std::string(argv[i]));
		}
		/* Process */
		std::ofstream output ( headername, std::ios::out | std::ios::trunc| std::ios::binary );

		/* Use buffer */
		char outbuffer[BUFFER_SIZE];
		output.rdbuf()->pubsetbuf ( outbuffer, BUFFER_SIZE );

		if ( !output )
			throw std::runtime_error ( "Failed to create output file!" );

		/* Show status */
		std::cout << "Build  : file '" << headername << "'..." << std::endl;

		/* Get base name of file */
		headername = GetFileBasename ( headername );

		/* Data string stream */
		std::ostringstream data;

		/* Write header start when wanted */
		defineheader_start ( data, headername, /*use macro*/ true, /* const */ true );

		/* Write macros */
		definemacros ( data );

		/* Common input buffer */
		char inbuffer[BUFFER_SIZE];

		for ( auto iter = input_files.begin(); iter != input_files.end(); iter++ )
		{
			std::string &file = *iter;
			//std::string fileext = GetFileExtension ( file );

			std::ifstream input ( file, std::ios::in | std::ios::binary | std::ios::ate );
			input.rdbuf()->pubsetbuf ( inbuffer, BUFFER_SIZE );

			if ( input.is_open() )
			{
				/* Show status */
				std::cout << "Process: file '" << file << "'..." << std::endl;

				/* Remove extension */
				file = removeExtension(file);
				std::transform(file.begin(), file.end(), file.begin(), ::tolower);
				std::transform(file.begin(), file.end(), file.begin(), toUnderscores);

				/* Process file */
				definefile ( data, input, file, true );
			}
			else
			{
				/* Only show warning, other files need to be processed */
				std::cout << "Warning: input file '" << file << "' failed to open." << std::endl;
			}
		}

		/* Write header end when wanted */
		defineheader_end ( data, headername );

		/* Write data to output file */
		output.seekp ( 0, std::ios::beg );
		output << data.str();
	}
	catch ( std::exception& e )
	{
		std::cerr << "Error: " << e.what() << std::endl;
	}
	catch ( ... )
	{
		std::cerr << "Error: Exception of unknown type!" << std::endl;
	}

	return 0;
}