Task: Execute CopyPasta Language


C++: #include <fstream>
#include <iostream>
#include <sstream>
#include <streambuf>
#include <string>

#include <stdlib.h>

using namespace std;


void fatal_error(string errtext, char *argv[])
{
	cout << "%" << errtext << endl;
	cout << "usage: " << argv[0] << " [filename.cp]" << endl;
	exit(1);
}



string& ltrim(string& str, const string& chars = "\t\n\v\f\r ")
{
	str.erase(0, str.find_first_not_of(chars));
	return str;
}
string& rtrim(string& str, const string& chars = "\t\n\v\f\r ")
{
	str.erase(str.find_last_not_of(chars) + 1);
	return str;
}
string& trim(string& str, const string& chars = "\t\n\v\f\r ")
{
	return ltrim(rtrim(str, chars), chars);
}

int main(int argc, char *argv[])
{
	
	string fname = "";
	string source = "";
	try
	{
		fname = argv[1];
		ifstream t(fname);

		t.seekg(0, ios::end);
		source.reserve(t.tellg());
		t.seekg(0, ios::beg);

		source.assign((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
	}
	catch(const exception& e)
	{
		fatal_error("error while trying to read from specified file", argv);
	}

	
	string clipboard = "";

	
	int loc = 0;
	string remaining = source;
	string line = "";
	string command = "";
	stringstream ss;
	while(remaining.find("\n") != string::npos)
	{
		
		line = remaining.substr(0, remaining.find("\n"));
		command = trim(line);
		remaining = remaining.substr(remaining.find("\n") + 1);
		
		try
		{
			if(line == "Copy")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				clipboard += line;
			}
			else if(line == "CopyFile")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				if(line == "TheF*ckingCode")
					clipboard += source;
				else
				{
					string filetext = "";
					ifstream t(line);

					t.seekg(0, ios::end);
					filetext.reserve(t.tellg());
					t.seekg(0, ios::beg);

					filetext.assign((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
					clipboard += filetext;
				}
			}
			else if(line == "Duplicate")
			{
				line = remaining.substr(0, remaining.find("\n"));
				remaining = remaining.substr(remaining.find("\n") + 1);
				int amount = stoi(line);
				string origClipboard = clipboard;
				for(int i = 0; i < amount - 1; i++) {
					clipboard += origClipboard;
				}
			}
			else if(line == "Pasta!")
			{
				cout << clipboard << endl;
				return 0;
			}
			else
			{
				ss << (loc + 1);
				fatal_error("unknown command '" + command + "' encounter on line " + ss.str(), argv);
			}
		}
		catch(const exception& e)
		{
			ss << (loc + 1);
			fatal_error("error while executing command '" + command + "' on line " + ss.str(), argv);
		}

		
		loc += 2;
	}

	
	return 0;
}




Java: import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;

public class Copypasta
{
	
	public static void fatal_error(String errtext)
	{
		StackTraceElement[] stack = Thread.currentThread().getStackTrace();
		StackTraceElement main = stack[stack.length - 1];
		String mainClass = main.getClassName();
		System.out.println("%" + errtext);
		System.out.println("usage: " + mainClass + " [filename.cp]");
		System.exit(1);
	}
	public static void main(String[] args)
	{
		
		String fname = null;
		String source = null;
		try
		{
			fname = args[0];
			source = new String(Files.readAllBytes(new File(fname).toPath()));
		}
		catch(Exception e)
		{
			fatal_error("error while trying to read from specified file");
		}

		
		ArrayList<String> lines = new ArrayList<String>(Arrays.asList(source.split("\n")));
		
		
		String clipboard = "";
		
		
		int loc = 0;
		while(loc < lines.size())
		{
			
			String command = lines.get(loc).trim();

			try
			{
				if(command.equals("Copy"))
					clipboard += lines.get(loc + 1);
				else if(command.equals("CopyFile"))
				{
					if(lines.get(loc + 1).equals("TheF*ckingCode"))
						clipboard += source;
					else
					{
						String filetext = new String(Files.readAllBytes(new File(lines.get(loc + 1)).toPath()));
						clipboard += filetext;
					}
				}
				else if(command.equals("Duplicate"))
				{
					String origClipboard = clipboard;

					int amount = Integer.parseInt(lines.get(loc + 1)) - 1;
					for(int i = 0; i < amount; i++)
						clipboard += origClipboard;
				}
				else if(command.equals("Pasta!"))
				{
					System.out.println(clipboard);
					System.exit(0);
				}
				else
					fatal_error("unknown command '" + command + "' encountered on line " + new Integer(loc + 1).toString());
			}
			catch(Exception e)
			{
				fatal_error("error while executing command '" + command + "' on line " + new Integer(loc + 1).toString());
			}

			
			loc += 2;
		}
	}
}