package de.tu_darmstadt.informatik.rbg.mhartle.cpp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import org.anarres.cpp.LexerException;
import org.anarres.cpp.Preprocessor;
import org.anarres.cpp.Token;

public class CppTask extends Task {

	public static class Macro {
		private String name;
		private String value;

		public void setName(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}
	}

	private File			input = null;
	private File			output = null;
	private Preprocessor	cpp;

	public CppTask() {
		super();
		cpp = new Preprocessor();
	}

	public void setInput(File input) {
		this.input = input;
	}

	public void setOutput(File output) {
		this.output = output;
	}

	public void addMacro(Macro macro) {
		try {
			cpp.addMacro(macro.getName(), macro.getValue());
		}
		catch (LexerException e) {
			throw new BuildException(e);
		}
	}

	public void execute() {
		FileWriter writer = null;
		try {
			writer = new FileWriter(this.output);
			for (;;) {
				Token	tok = cpp.token();
				if (tok != null && tok.getType() == Token.EOF)
					break;
				writer.write(tok.getText());
			}
		}
		catch (Exception e) {
			throw new BuildException(e);
		}
		finally {
			if (writer != null) {
				try {
					writer.close();
				}
				catch (IOException e) {
				}
			}
		}
	}

}

@@ -5,46 +5,19 @@
 age 
-de.tu_darmstadt.informatik.rbg.mhartle
+org.anarres
 .cpp
@@ -650,23 +650,16 @@
 %09%09%09input
- = null
 ;%0A%09priva
@@ -674,23 +674,16 @@
 %09%09output
- = null
 ;%0A%09priva
@@ -1118,24 +1118,24 @@
 = null;%0A
-
 %09%09try %7B%0A
 %09%09%09write
@@ -1126,16 +1126,197 @@
 %09%09try %7B%0A
+%09%09%09if (input == null)%0A%09%09%09%09throw new BuildException(%22Input not specified%22);%0A%09%09%09if (output == null)%0A%09%09%09%09throw new BuildException(%22Output not specified%22);%0A%09%09%09cpp.addInput(this.input);%0A
 %09%09%09write

clean up cpp task some more