Use the swig generated enum ScilabIntegerTypeEnum for int declaration instead of...
[scilab.git] / scilab / modules / javasci / src / java / org / scilab / modules / javasci / Scilab.java
index b9a781f..b4c4a69 100644 (file)
@@ -5,7 +5,7 @@
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
  * you should have received as part of this distribution.  The terms
- * are also available at    
+ * are also available at       
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
@@ -25,8 +25,8 @@ import org.scilab.modules.javasci.Call_Scilab;
 import org.scilab.modules.javasci.JavasciException.InitializationException;
 
 public class Scilab {
-    private String SCI = null;
-    
+       private String SCI = null;
+       
 
        /**
         * Creator of the Scilab Javasci object. 
@@ -37,10 +37,10 @@ public class Scilab {
         * Under Windows, use also the registery
         * @param SCI provide the path to Scilab data
         */
-    public Scilab() throws InitializationException {
+       public Scilab() throws InitializationException {
                // Auto detect 
                String detectedSCI;
-        try {
+               try {
                        detectedSCI = System.getProperty("SCI");
                        if (detectedSCI == null || detectedSCI.length() == 0) {
                                detectedSCI = System.getenv("SCI");
@@ -50,19 +50,19 @@ public class Scilab {
                        }
                        this.initScilab(detectedSCI);
 
-        } catch (Exception e) {
+               } catch (Exception e) {
                        throw new InitializationException("Auto detection of SCI failed.\nCould not retrieve the variable SCI.", e);
-        }
+               }
                // @TODO manage windows through the registery
-    }
+       }
 
        /**
         * Creator of the Scilab Javasci object. 
         * @param SCI provide the path to Scilab data
         */
-    public Scilab(String SCI) throws InitializationException {
+       public Scilab(String SCI) throws InitializationException {
                this.initScilab(SCI);
-    }
+       }
 
        private void initScilab(String SCI) throws InitializationException {
                File f = new File(SCI);
@@ -73,15 +73,15 @@ public class Scilab {
                this.SCI = SCI;
        }
 
-    /**
-     * Open a connection to the Scilab engine
-     * This function is based on Call_ScilabOpen from call_scilab
+       /**
+        * Open a connection to the Scilab engine
+        * This function is based on Call_ScilabOpen from call_scilab
         * Note: For now, only one instance of Scilab can be launched
         * A second launch will return FALSE
-     * @return if the operation is successful
-     */
-    public boolean open() throws InitializationException {
-//        System.out.println("SCI : " + SCI);
+        * @return if the operation is successful
+        */
+       public boolean open() throws InitializationException {
+//             System.out.println("SCI : " + SCI);
                int res = Call_Scilab.Call_ScilabOpen(this.SCI, null, -1);
                switch (res) {
                        case -1: 
@@ -93,182 +93,182 @@ public class Scilab {
                                throw new InitializationException("No existing directory");
                }
                return true;
-               //        return Call_Scilab.Call_ScilabOpen(this.SCI, null, null) == 0;
-    }
+               //              return Call_Scilab.Call_ScilabOpen(this.SCI, null, null) == 0;
+       }
 
-    /**
-     * Open a connection to the Scilab engine and run the command job
-     * This function is based on Call_ScilabOpen from call_scilab
+       /**
+        * Open a connection to the Scilab engine and run the command job
+        * This function is based on Call_ScilabOpen from call_scilab
         * Note: For now, only one instance of Scilab can be launched
         * A second launch will return FALSE
-     * @param job The job to run on startup
-     * @return if the operation is successful
-     */
-    public boolean open(String job) throws InitializationException {
-        if (!this.open()) {
+        * @param job The job to run on startup
+        * @return if the operation is successful
+        */
+       public boolean open(String job) throws InitializationException {
+               if (!this.open()) {
                        return false;
                }
 
-        return this.exec(job);
-    }
+               return this.exec(job);
+       }
 
-    /**
-     * Open a connection to the Scilab engine and run commands job
-     * This function is based on Call_ScilabOpen from call_scilab
+       /**
+        * Open a connection to the Scilab engine and run commands job
+        * This function is based on Call_ScilabOpen from call_scilab
         * Note: For now, only one instance of Scilab can be launched
         * A second launch will return FALSE
-     * @param jobs The serie of jobs to run on startup
-     * @return if the operation is successful
-     */
-    public boolean open(String jobs[]) throws InitializationException {
-        if (!this.open()) {
+        * @param jobs The serie of jobs to run on startup
+        * @return if the operation is successful
+        */
+       public boolean open(String jobs[]) throws InitializationException {
+               if (!this.open()) {
                        return false;
                }
 
-        return this.exec(jobs);
-    }
+               return this.exec(jobs);
+       }
 
 
-    /**
-     * Open a connection to the Scilab engine and run thefile scriptFilename
-     * This function is based on Call_ScilabOpen from call_scilab
+       /**
+        * Open a connection to the Scilab engine and run thefile scriptFilename
+        * This function is based on Call_ScilabOpen from call_scilab
         * Note: For now, only one instance of Scilab can be launched
         * A second launch will return FALSE
-     * @param job The script to execute on startup
-     * @return if the operation is successful
-     */
-    public boolean open(File scriptFilename) throws InitializationException, FileNotFoundException {
-        if (!this.open()) {
+        * @param job The script to execute on startup
+        * @return if the operation is successful
+        */
+       public boolean open(File scriptFilename) throws InitializationException, FileNotFoundException {
+               if (!this.open()) {
                        return false;
                }
 
-        return this.exec(scriptFilename);
-    }
-
-
-    /**
-     * Execute a single command in Scilab
-     * This function is based on SendScilabJob from call_scilab
-     * @param job the job to execute
-     * @return if the operation is successful
-     */
-    public boolean exec(String job) {
-        return (Call_Scilab.SendScilabJob(job) == 0);
-    }
-
-
-    /**
-     * Execute several commands in Scilab
-     * This function is based on SendScilabJob from call_scilab
-     * @param job the serie of job to execute
-     * @return if the operation is successful
-     */
-    public boolean exec(String jobs[]) {
-        return (Call_Scilab.SendScilabJobs(jobs, jobs.length) == 0);
-    }
-
-
-    /**
-     * Execute a Scilab script .sce/.sci
-     * This function is based on SendScilabJob from call_scilab
-     * @param job the script to execute
-     * @return if the operation is successful
-     */
-    public boolean exec(File scriptFilename) throws FileNotFoundException {
-        if (!scriptFilename.exists()) {
-            throw new FileNotFoundException("Could not find " + scriptFilename);
-        }
-        return this.exec("exec('" + scriptFilename + "');");
-    }
-
-
-    /**
-     * Detect if a variable (varname) exists in Scilab
-     * @param varname the variable to check
-     * @return if the variable exists or not
-     */
-    public boolean isExistingVariable(String varname) {
-        return true;
-    }
-
-
-    /**
-     * Shutdown Scilab
-     * This function is based on TerminateScilab from call_scilab
-     * @return if the operation is successful
-     */
-    public boolean close() {
+               return this.exec(scriptFilename);
+       }
+
+
+       /**
+        * Execute a single command in Scilab
+        * This function is based on SendScilabJob from call_scilab
+        * @param job the job to execute
+        * @return if the operation is successful
+        */
+       public boolean exec(String job) {
+               return (Call_Scilab.SendScilabJob(job) == 0);
+       }
+
+
+       /**
+        * Execute several commands in Scilab
+        * This function is based on SendScilabJob from call_scilab
+        * @param job the serie of job to execute
+        * @return if the operation is successful
+        */
+       public boolean exec(String jobs[]) {
+               return (Call_Scilab.SendScilabJobs(jobs, jobs.length) == 0);
+       }
+
+
+       /**
+        * Execute a Scilab script .sce/.sci
+        * This function is based on SendScilabJob from call_scilab
+        * @param job the script to execute
+        * @return if the operation is successful
+        */
+       public boolean exec(File scriptFilename) throws FileNotFoundException {
+               if (!scriptFilename.exists()) {
+                       throw new FileNotFoundException("Could not find " + scriptFilename);
+               }
+               return this.exec("exec('" + scriptFilename + "');");
+       }
+
+
+       /**
+        * Detect if a variable (varname) exists in Scilab
+        * @param varname the variable to check
+        * @return if the variable exists or not
+        */
+       public boolean isExistingVariable(String varname) {
+               return true;
+       }
+
+
+       /**
+        * Shutdown Scilab
+        * This function is based on TerminateScilab from call_scilab
+        * @return if the operation is successful
+        */
+       public boolean close() {
                boolean res = Call_Scilab.TerminateScilab(null);
-        return res;
-    }
-
-
-    /**
-     * Return the last error code
-     * @return the error code
-     * @todo Check what is actually this number
-     */
-    public int getLastErrorCode() {
-        return Call_Scilab.GetLastErrorCode();
-    }
-
-
-    /**
-     * Return the last error message
-     * @return the error message itself
-     */
-    public String getLastErrorMessage() {
-        return Call_Scilab.getLastErrorMessage();
-    }
-
-
-    /**
-     * Detect if a Scilab graphic window is still opened
-     * This function is based on ScilabHaveAGraph from call_scilab
-     * @return if the graphic is open or not
-     */
-    public boolean isGraphicOpened() {
-        return false;
-    }
-
-
-    /**
-     * Return the code type of a variable varname
-     * The int refers to a Java enum from ScilabType.types
-     * @param varname the name of the variable
-     * @return the type of the variable
-     * @todo check the enum here
-    */
-    public ScilabTypeEnum getVariableType(String varName) {
-        ScilabTypeEnum variableType = null;
-        try {
-            variableType = Call_Scilab.getVariableType(varName);
-        } catch (IllegalArgumentException e) {
-            System.err.println("Could not find variable type: " + e.getLocalizedMessage());
-        }
-
-        return variableType;
-
-    }
-
-
-
-
-    /**
-     * Returns a variable named varname
-     * Throws an exception if the datatype is not managed or if the variable is not available
-     * @param varname the name of the variable
-     * @return return the variable 
-    */
-    public ScilabType get(String varname) {
+               return res;
+       }
+
+
+       /**
+        * Return the last error code
+        * @return the error code
+        * @todo Check what is actually this number
+        */
+       public int getLastErrorCode() {
+               return Call_Scilab.GetLastErrorCode();
+       }
+
+
+       /**
+        * Return the last error message
+        * @return the error message itself
+        */
+       public String getLastErrorMessage() {
+               return Call_Scilab.getLastErrorMessage();
+       }
+
+
+       /**
+        * Detect if a Scilab graphic window is still opened
+        * This function is based on ScilabHaveAGraph from call_scilab
+        * @return if the graphic is open or not
+        */
+       public boolean isGraphicOpened() {
+               return false;
+       }
+
+
+       /**
+        * Return the code type of a variable varname
+        * The int refers to a Java enum from ScilabType.types
+        * @param varname the name of the variable
+        * @return the type of the variable
+        * @todo check the enum here
+       */
+       public ScilabTypeEnum getVariableType(String varName) {
+               ScilabTypeEnum variableType = null;
+               try {
+                       variableType = Call_Scilab.getVariableType(varName);
+               } catch (IllegalArgumentException e) {
+                       System.err.println("Could not find variable type: " + e.getLocalizedMessage());
+               }
+
+               return variableType;
+
+       }
+
+
+
+
+       /**
+        * Returns a variable named varname
+        * Throws an exception if the datatype is not managed or if the variable is not available
+        * @param varname the name of the variable
+        * @return return the variable 
+       */
+       public ScilabType get(String varname) {
                ScilabTypeEnum sciType = this.getVariableType(varname);
                switch (sciType) {
                        case sci_matrix:
-                if (!Call_Scilab.isComplex(varname)) {
-                    return new ScilabDouble(Call_Scilab.getDouble(varname));
-                } else {
-                    return new ScilabDouble(Call_Scilab.getDoubleComplexReal(varname), Call_Scilab.getDoubleComplexImg(varname));
-                }
+                               if (!Call_Scilab.isComplex(varname)) {
+                                       return new ScilabDouble(Call_Scilab.getDouble(varname));
+                               } else {
+                                       return new ScilabDouble(Call_Scilab.getDoubleComplexReal(varname), Call_Scilab.getDoubleComplexImg(varname));
+                               }
 
                        case sci_boolean:
                                return new ScilabBoolean(Call_Scilab.getBoolean(varname));
@@ -304,87 +304,84 @@ public class Scilab {
                //              throw new UnsupportedTypeException();
                }
                return null;
-    }
+       }
 
 
-    /**
-     * Send to Scilab a variable theVariable named varname
-     * Throws an exception if the datatype is not managed or if the variable is not available
-     * @param varname the name of the variable
-     * @param varname the variable itself
-     * @return if the operation is successful
-    */
-    public boolean put(String varname, ScilabType theVariable) {
+       /**
+        * Send to Scilab a variable theVariable named varname
+        * Throws an exception if the datatype is not managed or if the variable is not available
+        * @param varname the name of the variable
+        * @param varname the variable itself
+        * @return if the operation is successful
+       */
+       public boolean put(String varname, ScilabType theVariable) {
                int err = -999;
 
-        if (theVariable instanceof ScilabDouble) {
-            ScilabDouble sciDouble = (ScilabDouble)theVariable;
-            if (sciDouble.isReal()) {
+               if (theVariable instanceof ScilabDouble) {
+                       ScilabDouble sciDouble = (ScilabDouble)theVariable;
+                       if (sciDouble.isReal()) {
                                err = Call_Scilab.putDouble(varname, sciDouble.getRealPart());
-            } else {
+                       } else {
 // Special case. Serialize the matrix from Scilab same way Scilab stores them
 // (columns by columns)
 // plus the complex values at the second part of the array
-                err = Call_Scilab.putDoubleComplex(varname,sciDouble.getSerializedComplexMatrix(), sciDouble.getHeight(), sciDouble.getWidth());
-            }
+                               err = Call_Scilab.putDoubleComplex(varname,sciDouble.getSerializedComplexMatrix(), sciDouble.getHeight(), sciDouble.getWidth());
+                       }
                }
 
                if (theVariable instanceof ScilabInteger) {
-            ScilabInteger sciInteger = (ScilabInteger)theVariable;
+                       ScilabInteger sciInteger = (ScilabInteger)theVariable;
                        switch (sciInteger.getPrec()) {
-                               case TYPE8: // byte
-                                       if (sciInteger.isUnsigned()) {
-                                               err = Call_Scilab.putUnsignedByte(varname, sciInteger.getDataAsByte());
-                                       } else {
-                                               err = Call_Scilab.putByte(varname, sciInteger.getDataAsByte());
-                                       }
+                               case sci_uint8:
+                                       err = Call_Scilab.putUnsignedByte(varname, sciInteger.getDataAsByte());
+                                       break;
+                               case sci_int8:
+                                       err = Call_Scilab.putByte(varname, sciInteger.getDataAsByte());
+                                       break;
+                               case sci_uint16:
+                                       err = Call_Scilab.putUnsignedShort(varname, sciInteger.getDataAsShort());
                                        break;
-                               case TYPE16: // short 
-                                       if (sciInteger.isUnsigned()) {
-                                               err = Call_Scilab.putUnsignedShort(varname, sciInteger.getDataAsShort());
-                                       } else {
-                                               err = Call_Scilab.putShort(varname, sciInteger.getDataAsShort());
-                                       }
+                               case sci_int16:
+                                       err = Call_Scilab.putShort(varname, sciInteger.getDataAsShort());
                                        break;
-                               case TYPE32: // int
-                                       if (sciInteger.isUnsigned()) {
-                                               err = Call_Scilab.putUnsignedInt(varname, sciInteger.getDataAsInt());
-                                       } else {
-                                               err = Call_Scilab.putInt(varname, sciInteger.getDataAsInt());
-                                               }
+                               case sci_uint32:
+                                       err = Call_Scilab.putUnsignedInt(varname, sciInteger.getDataAsInt());
+                               case sci_int32:
+                                       err = Call_Scilab.putInt(varname, sciInteger.getDataAsInt());
                                        break;
-                               case TYPE64: // long. unsupported by Scilab 5.X
+                               case sci_uint64:
+//                                     err = Call_Scilab.putUnsignedLong(varname, sciInteger.getData_());
+                               case sci_int64:
+//                                     err = Call_Scilab.putLong(varname, sciInteger.getData_());
                                        break;
                        }
-
-                       //                      Call_Scilab.putLong(varname, sciInteger.getData_());
                }
 
                if (theVariable instanceof ScilabBoolean) {
-            ScilabBoolean sciBoolean = (ScilabBoolean)theVariable;
-            err = Call_Scilab.putBoolean(varname, sciBoolean.getData());
-        }
+                       ScilabBoolean sciBoolean = (ScilabBoolean)theVariable;
+                       err = Call_Scilab.putBoolean(varname, sciBoolean.getData());
+               }
 
                if (theVariable instanceof ScilabString) {
-            ScilabString sciString = (ScilabString)theVariable;
-            err = Call_Scilab.putString(varname, sciString.getData());
-        }
+                       ScilabString sciString = (ScilabString)theVariable;
+                       err = Call_Scilab.putString(varname, sciString.getData());
+               }
 
 
                //TODO: a remplacer par la bonne exception return new UnsupportedTypeException();
                //              throw new UnsupportedTypeException();
-        if (err == -999) {
-                // Exception a lancer
-
-            System.err.println("Type not managed: " + theVariable.getClass());
-        } else {
-            if (err != 0) {
-                // Exception a lancer
-                System.err.println("put failed");
-            }
-        }
+               if (err == -999) {
+                               // Exception a lancer
+
+                       System.err.println("Type not managed: " + theVariable.getClass());
+               } else {
+                       if (err != 0) {
+                               // Exception a lancer
+                               System.err.println("put failed");
+                       }
+               }
 
                return true;
-    }
+       }
 
 }