All pastes #1843591 Raw Edit

Unbenannt

public text v1 · immutable
#1843591 ·published 2010-03-17 14:47 UTC
rendered paste body
package at.ac.tuwien.media.atmel.parser;

import java.io.*;
import java.util.*;
import java.util.logging.*;



public class ConvertPD {

	
	private class Pair{
	
		public int x;
		public int y;
	
		public Pair(int x,int y){
			this.x=x;
			this.y=y;
		}
	}
	
	private ObjectConf objConf;	
	private Logger log;
	private ArrayList<PDObject> objectList;
	
	
	public ConvertPD(String config)
	{
		objConf = new ObjectConf();
		log   = Logger.getLogger("convpd.PDConverter");
		readConfig(config);
      			
	}

	
	public void writeCHeader(String fileOut) {

		try{		
			
			FileWriter outputFileWriter  = new FileWriter(fileOut);
			BufferedWriter outputStream  = new BufferedWriter(outputFileWriter);
			outputStream.write(objConf.getStrC());
			outputStream.close();
			
		}catch(IOException e) {
			log.warning(e.getMessage());
			e.printStackTrace();
		}

	
	}
	

	public void generatePDS(String dir) {
		
		objConf.generateAllPDS( dir);

	}
	
	private void readConfig(String fileIn) {
	
		String inLine = null;
		int version=0;
		int subversion=0;

		try{
			
			FileReader inputFileReader   = new FileReader(fileIn);
			BufferedReader inputStream   = new BufferedReader(inputFileReader);
			
			log.info("config file: "+fileIn+" opened.");

			while ((inLine = inputStream.readLine()) != null) {
				
					if(inLine.startsWith("ATMEL_VERSION")){
						version=Integer.parseInt((inLine.split(" "))[1]);
						log.info("Version: "+version);
					}
					else if(inLine.startsWith("ATMEL_SUBVERSION")){
						version=Integer.parseInt((inLine.split(" "))[1]);
						log.info("Subversion: "+subversion);
					}



					else if(!inLine.startsWith("#"))objConf.addEntry(inLine);


			}
			objConf.setVersion(version,subversion);
			inputStream.close();

			log.info("config file closed");
		
		}catch(IOException e) {
			log.warning(e.getMessage());
			e.printStackTrace();
		}


		
	}
	
	public void importPD(String fileIn){
	
		objectList=new ArrayList<PDObject>();
		String inLine = null;
			
		try{
			
			FileReader inputFileReader   = new FileReader(fileIn);
			BufferedReader inputStream   = new BufferedReader(inputFileReader);
			
			log.info("input file: "+fileIn+" opened.");
			
			int c=0;
			
			while ((inLine = inputStream.readLine()) != null) {
				
				String[] split=inLine.split(" ");
				
				if(inLine.startsWith("#X text")) {
					
					objectList.add(new PDObject(c,0,"*dummy*","00"));
					log.info("Dummy Object !!!!!! "+c+" (text) added." );
					c++;

				}			

				if(inLine.startsWith("#X obj"))	{

					int lastWrd=-1;
					int objPos=4;
					if(split[objPos].endsWith(";")){
							split[objPos]=split[objPos].substring(0,split[objPos].length()-1);
							lastWrd=objPos-4;
					}
					
					PDObject tempObj = new PDObject(c,objConf.getInCount(split[objPos]),split[objPos],objConf.getBytecode(split[objPos]));
					for(int i=0;i<split.length;i++)	{
				
						if(split[i].endsWith(";")) {
							
							split[i]=split[i].substring(0,split[i].length()-1);
							lastWrd=i-4;
						}
						if(i>4)tempObj.addParam(split[i]);
					}
					
					if(lastWrd<0)throw new Exception("Corrupt PD File. (obj)");
					
					if(objConf.getParamCount(split[objPos])!=lastWrd) throw new Exception("Parameter count mismatch for: "+split[objPos]);
							
					objectList.add(tempObj);
					log.info("Object "+c+": "+split[objPos]+" added." );
					c++;
					
					
				}
				
				if(inLine.startsWith("#X connect")){
					
					if(split[5].endsWith(";")) split[5]=split[5].substring(0,split[5].length()-1);
					else throw new Exception("Corrupt PD File. (connect)");

					int out_id=Integer.parseInt(split[2]);
					int out_pos=Integer.parseInt(split[3]);
					int in_id=Integer.parseInt(split[4]);
					int in_pos=Integer.parseInt(split[5]);

					log.info("Connecting output "+out_pos+" of "+out_id+". "+objectList.get(out_id).getName()+" to input "+in_pos+" of "+in_id+". "+objectList.get(in_id).getName()+".");	
				
					objectList.get(in_id).addInput(in_pos,objectList.get(out_id),out_pos);
				}
						
			}
		
			inputStream.close();
			log.info("input file closed");
		
		}catch(IOException e) {
			log.warning(e.getMessage());
			e.printStackTrace();
		}catch(Exception e) {
			log.warning(e.getMessage());
			e.printStackTrace();
		}


	}

	public void writeROM(String fileOut) {
		
		String stringRom = new String("\n");
		
		ArrayList<Integer> markExported;
		ArrayList<Pair> markShifted;
		ArrayList<Integer> markResolved;

		try{		
			FileOutputStream fileOutStream  = new FileOutputStream(fileOut);
			BufferedOutputStream outputStream  = new BufferedOutputStream(fileOutStream);
			markExported=new ArrayList<Integer>();
			markShifted=new ArrayList<Pair>();
			markResolved=new ArrayList<Integer>();
		
			
			int nextID=0;
			
			boolean repeat=true;
			while(repeat){
			repeat=false;
		
			
			for(int i=0;i<objectList.size();i++)
			{
				
				PDObject temp=objectList.get(i);

				// if this object has not been added yet

//				System.out.println("try:"+temp.getName()+","+temp.inObjects.length);
				if(!markExported.contains(i)&&!temp.getName().equals("*dummy*"))
				{
					boolean resolved=true;

					for(int j=0;j<temp.inObjects.length;j++)
					{
						if(!markResolved.contains(objectList.get(i).inObjects[j][0].getID()))resolved=false;
					}
					if(resolved){
						
						for(int j=0;j<objectList.size();j++)
							for(int k=0;k<objectList.get(j).inObjects.length;k++)
								{
									boolean rename=true;
									for(int l=0;l<markShifted.size();l++)
									if(markShifted.get(l).x==j&&markShifted.get(l).y==k)rename=false;
									if(rename)
										if(objectList.get(j).inObjects[k][0].getID()==temp.getID()){
										log.config("renaming "+temp.getID()+" to "+nextID+" for pair: "+j+","+k);
											objectList.get(j).inObjects[k][0].setID(nextID);
											markShifted.add(new Pair(j,k));
										}
										
								}
							

					temp.setID(nextID);
			
								
						
						repeat=true;
//						System.out.println("export:"+temp.getName());
						outputStream.write(temp.getID());
						outputStream.write(Integer.parseInt(temp.bytecode,16));
						
						stringRom+=(temp.getID()+" = "+temp.getName()+" ");
						
						for(int k=0;k<temp.params.size();k++)
						{
							int c=Integer.parseInt(temp.params