[ create a new paste ] login | about

Link: http://codepad.org/QKjU3ZZS    [ raw code | fork ]

Eugene.Mineev - C++, pasted on Jun 20:
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <iterator>

typedef std::wstring Word;
typedef std::vector<Word> Text;
namespace Utilities
{
	namespace FileSystem
	{
		/**
		* Read text from file
		* @param path Path to the file
		* @return Read text
		*/
		Text ReadText(std::wstring path)
		{
			Text readText;
			std::wifstream file(path);
			std::istream_iterator<Word, Word::value_type> fileBegin(file);
			std::istream_iterator<Word, Word::value_type> fileEnd;

			std::copy(fileBegin, fileEnd, std::back_inserter(readText));

			return readText;
		}

		/**
		* Write text to file
		* @param path Path to the file
		* @param text Text for writting
		*/
		void WriteText(std::wstring path, Text& text)
		{
			std::wofstream file(path);
			std::ostream_iterator<std::wstring, std::wstring::value_type> fileIt(file, L" ");
			std::copy(text.begin(), text.end(), fileIt);
		}
	}
	namespace Shafflers
	{

		/**
		* Shaffle letters in the word
		* Shuffle all letters and punctuation marks wich placed on middle of the word.
		* @param word Letters for shaffle
		*/
		void ShaffleWordLetters(Word& word)
		{
			Word::iterator endWord = std::find_if(
				word.begin(), 
				word.end(), 
				std::bind2nd(
					std::less<wchar_t>(), 
					65));
			if(std::distance(word.begin() + 1, endWord - 1) >= 2)
				std::random_shuffle(word.begin() + 1, endWord - 1);
		}

		void ShuffleTextLetters(Text &text)
		{
			std::for_each(text.begin(), text.end(), ShaffleWordLetters);
		}
	}
	/**
	* Contains converter function
	*/
	namespace Converters
	{
		wchar_t M2WCharTransform(char ch, std::locale loc)
		{
			return std::use_facet<std::ctype<wchar_t>>(loc).widen(ch);
		}
		std::wstring M2WStringTransform(std::string& charString)
		{
			std::wstring wideString;
			std::transform(
				charString.begin(), 
				charString.end(), 
				std::back_inserter(wideString), 
				std::bind2nd(
					std::pointer_to_binary_function<char, std::locale, wchar_t>(M2WCharTransform), 
					std::locale()));

			return wideString;
		}
	}
}

using namespace Utilities;
int main(int argc, char *argv[])
{
	std::vector<std::wstring> params;
	if(argc >= 3)
	{
		std::vector<std::string> tempParams(argv + 1, argv + argc);
	
		std::transform(
			tempParams.begin(), 
			tempParams.end(), 
			std::back_inserter(params),
			Converters::M2WStringTransform);
	}

	Text text = FileSystem::ReadText(params[0]);
	Shafflers::ShuffleTextLetters(text);
	FileSystem::WriteText(params[1], text);
	return 0;
}


Create a new paste based on this one


Comments: