Fixed bug with Exception being thrown with wrong cause
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / MicroInstructionMemoryParser.java
index d049e37..a141a86 100644 (file)
@@ -1,60 +1,90 @@
 package net.mograsim.machine.mi;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
 
-import net.mograsim.machine.MemoryDefinition;
 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
 import net.mograsim.machine.mi.parameters.ParameterClassification;
 
 public class MicroInstructionMemoryParser
 {
+       private final static String lineSeparator = System.getProperty("line.separator");
+
        public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
        {
-               try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
+               try (InputStream input = new FileInputStream(inputPath))
                {
-                       parseMemory(memory, reader);
+                       parseMemory(memory, input);
                }
        }
 
-       public static void parseMemory(final MicroInstructionMemory memory, BufferedReader input)
+       /**
+        * @param input The input to parse must be in csv format; The stream is closed after being consumed.
+        * 
+        * @throws IOException
+        */
+       public static MicroInstructionMemory parseMemory(MicroInstructionMemoryDefinition memDef, InputStream input) throws IOException
        {
-               MicroInstructionMemoryDefinition def = memory.getDefinition();
-               MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
-
-               long minAddr = def.getMinimalAddress();
-               long maxAddr = def.getMaximalAddress();
-
-               String line;
-               long i = minAddr;
                try
                {
-                       for (; i <= maxAddr && input.ready() && !"".equals((line = input.readLine())); i++)
-                       {
-                               long iFinal = i;
-                               memory.setCell(i, parse(() -> memory.notifyObservers(iFinal), miDef, line));
-                       }
+                       MicroInstructionMemory memory = new StandardMicroInstructionMemory(memDef);
+                       parseMemory(memory, input);
+                       return memory;
                }
-               catch (IOException e)
+               catch (NullPointerException e)
                {
-                       e.printStackTrace();
+                       throw new MicroInstructionMemoryParseException(e);
                }
+       }
 
-               for (; i <= maxAddr; i++)
+       /**
+        *
+        * @param input The input to parse must be in csv format; The stream is closed after being consumed.
+        * 
+        * @throws IOException
+        */
+       public static void parseMemory(final MicroInstructionMemory memory, InputStream input) throws IOException
+       {
+               try (BufferedReader reader = new BufferedReader(new InputStreamReader(input)))
                {
-                       long iFinal = i;
-                       memory.setCell(i, miDef.createDefaultInstruction(() -> memory.notifyObservers(iFinal)));
+                       MicroInstructionMemoryDefinition def = memory.getDefinition();
+                       MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
+
+                       long minAddr = def.getMinimalAddress();
+                       long maxAddr = def.getMaximalAddress();
+
+                       String line;
+                       long i = minAddr;
+                       try
+                       {
+                               for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
+                               {
+                                       memory.setCell(i, parse(miDef, line));
+                               }
+                       }
+                       catch (IOException e)
+                       {
+                               e.printStackTrace();
+                       }
+
+                       for (; i <= maxAddr; i++)
+                       {
+                               memory.setCell(i, miDef.createDefaultInstruction());
+                       }
                }
        }
 
-       public static MicroInstruction parse(Runnable updateCallback, MicroInstructionDefinition definition, String toParse)
+       /**
+        * must be in csv format
+        */
+       public static MicroInstruction parse(MicroInstructionDefinition definition, String input)
        {
                int size = definition.size();
-               String[] strings = toParse.split(",");
+               String[] strings = input.split(",");
                if (size != strings.length)
                        throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
                MicroInstructionParameter[] params = new MicroInstructionParameter[size];
@@ -65,29 +95,11 @@ public class MicroInstructionMemoryParser
                        {
                                params[i] = classes[i].parse(strings[i]);
                        }
-                       return new StandardMicroInstruction(updateCallback, params);
+                       return new StandardMicroInstruction(params);
                }
                catch (Exception e)
                {
-                       throw new MicroInstructionMemoryParseException(e.getCause());
-               }
-       }
-
-       public static void write(MicroInstructionMemory memory, String outputPath) throws IOException
-       {
-               try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
-               {
-                       write(memory, writer);
-               }
-       }
-
-       public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
-       {
-               MemoryDefinition def = memory.getDefinition();
-               long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
-               for (long i = min; i < max; i++)
-               {
-                       output.write(toCSV(memory.getCell(i)) + "\n");
+                       throw new MicroInstructionMemoryParseException(e);
                }
        }
 
@@ -103,4 +115,25 @@ public class MicroInstructionMemoryParser
                sb.append(inst.getParameter(max).toString());
                return sb.toString();
        }
+
+       public static InputStream write(MicroInstructionMemory memory)
+       {
+               return new InputStream()
+               {
+                       long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress();
+                       InputStream instStream = new ByteArrayInputStream(new byte[0]);
+
+                       @Override
+                       public int read() throws IOException
+                       {
+                               int val = instStream.read();
+                               if (val == -1 && instIndex <= maxAddress)
+                               {
+                                       instStream = new ByteArrayInputStream((toCSV(memory.getCell(instIndex++)) + lineSeparator).getBytes());
+                                       val = instStream.read();
+                               }
+                               return val;
+                       }
+               };
+       }
 }