/*
 * Created on 11. jan.. 2007
 *
 * Copyright (c) 2005, Karl Trygve Kalleberg <karltk near strategoxt.org>
 * 
 * Licensed under the GNU General Public License, v2
 */
package org.spoofax.interpreter.library.ssl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.library.AbstractPrimitive;
import org.spoofax.interpreter.stratego.Strategy;
import org.spoofax.interpreter.terms.IStrategoTerm;

public class SSL_copy extends AbstractPrimitive {

    SSL_copy() {
        super("SSL_copy", 0, 2);
    }
    
    @Override
    public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars)
            throws InterpreterException {

        if(!Tools.isTermString(tvars[0]))
            return false;
        
        if(!Tools.isTermString(tvars[1]))
            return false;
        
        SSLLibrary op = (SSLLibrary) env.getOperatorRegistry(SSLLibrary.REGISTRY_NAME);

        byte[] bs = new byte[1024];
        int read;

        try {
            InputStream fis = op.getIOAgent().openInputStream(Tools.javaString(tvars[0]));
            OutputStream fos =  op.getIOAgent().openFileOutputStream(Tools.javaString(tvars[1]));
        
            read = fis.read(bs, 0, 1024);
            while(read != -1) {
                fos.write(bs,0, read);
                read = fis.read(bs, 0, 1024);
            }
            
            fis.close();
            fos.close();
        } catch(IOException e) {
            throw new InterpreterException(e);
        }
        
        
        return true;
    }

}

@@ -535,25 +535,125 @@
 ter.
-stratego.Strategy
+library.IOAgent;%0Aimport org.spoofax.interpreter.stratego.Strategy;%0Aimport org.spoofax.interpreter.terms.IStrategoAppl
 ;%0Aim
@@ -963,41 +963,128 @@
     
-if(!Tools.isTermString(tvars%5B0%5D))
+SSLLibrary op = (SSLLibrary) env.getOperatorRegistry(SSLLibrary.REGISTRY_NAME);%0A        IOAgent agent = op.getIOAgent();
 %0A   
@@ -1092,35 +1092,167 @@
     
+%0A   
     
-return false;%0A        %0A
+ InputStream fis = null;%0A        OutputStream fos = null;%0A        %0A        boolean closeIn = true;%0A        boolean closeOut = true;%0A%0A        try %7B%0A    
     
@@ -1261,10 +1261,10 @@
   if
+ 
 (
-!
 Tool
@@ -1288,13 +1288,19 @@
 ars%5B
-1%5D))%0A
+0%5D)) %7B%0A    
     
@@ -1311,20 +1311,63 @@
     
-return false
+fis = agent.openInputStream(Tools.javaString(tvars%5B0%5D))
 ;%0A  
@@ -1376,106 +1376,187 @@
     
-%0A
     
-    SSLLibrary op = (SSLLibrary) env
+%7D else if (Tools.isTermAppl(tvars%5B0%5D) && Tools.hasConstructor((IStrategoAppl) tvars%5B0%5D, %22stdin%22)) %7B%0A                fis = agent
 .get
-OperatorRegistry(SSLLibrary.REGISTRY_NAME);%0A%0A
+InputStream(IOAgent.CONST_STDIN);%0A  
         
 byte
@@ -1555,54 +1555,95 @@
     
-byte%5B%5D bs = new byte%5B1024%5D;%0A        int read;%0A
+      closeIn = false;%0A            %7D else %7B%0A                return false;%0A            %7D
 %0A   
@@ -1639,37 +1639,36 @@
       %7D%0A        
-try %7B
+    
 %0A            Inp
@@ -1668,48 +1668,84 @@
     
-InputStream fi
+if (Tools.isTermString(tvars%5B1%5D)) %7B%0A                fo
 s = 
-op.getIOA
+ a
 gent
-()
 .open
-In
+FileOut
 putS
@@ -1765,33 +1765,33 @@
 avaString(tvars%5B
-0
+1
 %5D));%0A           
@@ -1795,92 +1795,556 @@
     
-OutputStream fos =  op.getIOAgent().openFileOutputStream(Tools.javaString(tvars%5B1%5D))
+%7D else if (Tools.isTermAppl(tvars%5B1%5D) && Tools.hasConstructor((IStrategoAppl) tvars%5B1%5D, %22stdout%22)) %7B%0A                fos =  agent.getOutputStream(IOAgent.CONST_STDOUT);%0A                closeOut = false;%0A            %7D else if (Tools.isTermAppl(tvars%5B1%5D) && Tools.hasConstructor((IStrategoAppl) tvars%5B1%5D, %22stderr%22)) %7B%0A                fos =  agent.getOutputStream(IOAgent.CONST_STDERR);%0A                closeOut = false;%0A            %7D else %7B%0A                return false;%0A            %7D%0A    %0A            byte%5B%5D bs = new byte%5B1024%5D;%0A            int read
 ;%0A  
@@ -2515,32 +2515,33 @@
 ;%0A            %7D%0A
+%0A
             %0A   
@@ -2540,23 +2540,24 @@
     
-%0A            fi
+if (closeOut) fo
 s.cl
@@ -2575,18 +2575,31 @@
         
-fo
+if (closeIn) fi
 s.close(

Support stdin/stdout/stderr in SSL_copy.