/* LSN name config file shuffler Copyright M Towler 2006. This code may be freely distributed and used for any purpose whatsoever as long as this copyright notice is retained. It would be nice to send me any updates you make by going to http://p069.ezboard.com/bcodogames and sending a private message to 'General Strike'. This utility is intended to shuffle the names in LSN name config files, however no warranty is implied. I take no responsibility for this program reformatting your hard drive, taking over the world or any other impact it may have on your hardware, software or sanity. */ #include #include #include #include #include #include #include namespace { struct ProcessLine { std::string operator()( const std::string& line ) const { if( line.empty() ) return line; // skip comment/group lines starting with a # or [ for( size_t pos = 0 ; pos < line.size() ; ++pos ) { if( line[ pos ] == '#' || line[ 0 ] == '[' ) return line; else if( line[ pos ] != ' ' ) break; } std::vector< std::string > names; std::istringstream name_str( line ); std::string single_name; for( ; ; ) { std::getline( name_str, single_name, '|' ); if( !name_str.fail() ) names.push_back( single_name ); else break; } std::random_shuffle( names.begin(), names.end() ); std::ostringstream updated_str; std::copy( names.begin(), names.end(), std::ostream_iterator< std::string >( updated_str, "|" ) ); // trim off the trailing bar std::string updated_line = updated_str.str(); updated_line.resize( updated_line.size() - 1 ); return updated_line; } }; } int main( int argc, char** argv ) { if( argc != 2 ) { std::cerr << "usage NameShuffle \n"; return 1; } const char *filename = argv[ 1 ]; std::ifstream name_file( filename ); std::vector< std::string > lines; std::string line_buf; for( ; ; ) { std::getline( name_file, line_buf ); if( !name_file.fail() ) lines.push_back( line_buf ); else break; } if( lines.empty() ) { std::cerr << "filename " << filename << " could not be opened or was empty\n"; return 1; } std::transform( lines.begin(), lines.end(), lines.begin(), ProcessLine() ); std::ofstream name_out_file( filename ); std::copy( lines.begin(), lines.end(), std::ostream_iterator( name_out_file, "\n" ) ); if( name_out_file.fail() ) { std::cerr << "filename " << filename << " could not be written, perhaps it is read only\n"; return 1; } std::cout << "filename " << filename << " shuffled\n"; return 0; }