Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modification of Makefile and Utils.
[mapping.git] / src / and / Mapping / Utils.java
index 57a87e2..25cd4a7 100644 (file)
@@ -65,7 +65,7 @@ public class Utils
                        InetAddress addr = InetAddress.getLocalHost() ;
                        name = new String( addr.getCanonicalHostName() ) ;
                } catch( final Exception e ) {
-                       System.err.println( "Unalbe to retrieve host name !" ) ;
+                       System.err.println( "Unalbe to retrieve host's name !" ) ;
                        e.printStackTrace();
                        System.exit( 1 ) ;
                } 
@@ -173,7 +173,13 @@ public class Utils
         */
        public static void writeGrid( Grid _gl, String _file, String _path )
        {
-               if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+               if( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return ;
+               }
+               
+               if ( ! _file.endsWith( ".xml" ) )
                {
                        _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
                }
@@ -217,7 +223,13 @@ public class Utils
         */
        public static void writeGraph( Graph _gr, String _file, String _path )
        {
-               if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+               if( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return ;
+               }
+               
+               if ( ! _file.endsWith( ".xml" ) )
                {
                        _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
                }
@@ -257,28 +269,43 @@ public class Utils
        /**
         * Read an application Graph from a file.
         * @param _file File's name
+        * @param _path File's path
         * @return The application Graph read
         */
-       public static Graph readGraph( String _file )
+       public static Graph readGraph( String _path, String _file )
        {
-               if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+               if ( _file.equals( "" ) )
                {
-                       System.err.println( "Bad file !\n" ) ;
+                       System.err.println( "No file's name !\n" ) ;
                        return null ;
                }       
                
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+               }
+               
+               String path = "" ;
+               
+               if( _path.length() != 0 )
+               {
+                       path = _path+"/"+_file ;
+               } else {
+                       path = new String( "./" + _file ) ;
+               }       
+                       
                Graph gr = null ;
                
                XStream xstream = new XStream( new DomDriver() ) ;
                
                try {
-                       gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
+                       gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
                } catch( FileNotFoundException e ) {
                        System.err.println( "File not found !\n" ) ;
                        e.printStackTrace();
                        return null ;
                } catch( ClassCastException e ) {
-                       System.err.println( "The file does not contain a Graph" ) ;
+                       System.err.println( "The file does not contain a valid Graph" ) ;
                        e.printStackTrace() ;
                        return null ;
                }
@@ -290,28 +317,43 @@ public class Utils
        /**
         * Read a Grid graph from a file.
         * @param _file File's name
+        * @param _path File's path
         * @return The Grid graph read
         */
-       public static Grid readGrid( String _file )
-       {
-               if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+       public static Grid readGrid( String _path, String _file )
+       {       
+               if ( _file.equals( "" ) )
                {
-                       System.err.println( "Bad file !\n" ) ;
+                       System.err.println( "No file's name !\n" ) ;
                        return null ;
                }       
                
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+               }
+
+               String path = "" ;
+               
+               if( _path.length() != 0 )
+               {
+                       path = _path+"/"+_file ;
+               } else {
+                       path = new String( "./" + _file ) ;
+               }       
+               
                Grid gr = null ;
                
                XStream xstream = new XStream( new DomDriver() ) ;
                
                try {
-                       gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
+                       gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
                } catch( FileNotFoundException e ) {
                        System.err.println( "File not found !\n" ) ;
                        e.printStackTrace();
                        return null ;
                } catch( ClassCastException e ) {
-                       System.err.println( "The file does not contain a Grid" ) ;
+                       System.err.println( "The file does not contain a valid Grid" ) ;
                        e.printStackTrace() ;
                        return null ;
                }