gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2986 - in freeway: native src/org/gnu/freeway/cwrappers/ut


From: mdonoughe
Subject: [GNUnet-SVN] r2986 - in freeway: native src/org/gnu/freeway/cwrappers/util src/org/gnu/freeway/services src/org/gnu/freeway/services/impl
Date: Thu, 8 Jun 2006 15:39:28 -0700 (PDT)

Author: mdonoughe
Date: 2006-06-08 15:39:23 -0700 (Thu, 08 Jun 2006)
New Revision: 2986

Added:
   freeway/src/org/gnu/freeway/cwrappers/util/InterfaceImplementor.java
Modified:
   freeway/native/switch-table.c
   freeway/native/switch-table.h
   freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
   freeway/src/org/gnu/freeway/services/StatsService.java
   freeway/src/org/gnu/freeway/services/impl/StatsService.java
Log:
Started work on changing SwitchTableGenerator to use the Java parser. 
Currently only reads the imports from the interfaces.


Modified: freeway/native/switch-table.c
===================================================================
--- freeway/native/switch-table.c       2006-06-08 21:59:44 UTC (rev 2985)
+++ freeway/native/switch-table.c       2006-06-08 22:39:23 UTC (rev 2986)
@@ -10,27 +10,6 @@
   }
 }
 switch (functionType) {
-  case 13: {
-    void * carg0 = convObjectToPtr(jargs[0], env);
-    int cret = ((FunctionType13) 
((void**)m->modulePtr)[functionOffset])(carg0);
-    updateObjectFromPtr(jargs[0], carg0, env);
-    oret = convIntToCInt(cret, env);
-  }
-  case 60: {
-    int carg0 = convCIntToInt(jargs[0], env);
-    long long carg1 = convCLongToLong(jargs[1], env);
-    ((FunctionType60) ((void**)m->modulePtr)[functionOffset])(carg0, carg1);
-  }
-  case 42: {
-    int carg0 = convCIntToInt(jargs[0], env);
-    int carg1 = convCIntToInt(jargs[1], env);
-    ((FunctionType42) ((void**)m->modulePtr)[functionOffset])(carg0, carg1);
-  }
-  case 10: {
-    int carg0 = convCIntToInt(jargs[0], env);
-    long long cret = ((FunctionType10) 
((void**)m->modulePtr)[functionOffset])(carg0);
-    oret = convLongToCLong(cret, env);
-  }
   default:
     GNUNET_ASSERT(0);
 }

Modified: freeway/native/switch-table.h
===================================================================
--- freeway/native/switch-table.h       2006-06-08 21:59:44 UTC (rev 2985)
+++ freeway/native/switch-table.h       2006-06-08 22:39:23 UTC (rev 2986)
@@ -1,5 +1 @@
 // This file was autogenerated by SwitchTableGenerator
-typedef int (*FunctionType13)(void * arg0);
-typedef void (*FunctionType60)(int arg0, long long arg1);
-typedef void (*FunctionType42)(int arg0, int arg1);
-typedef long long (*FunctionType10)(int arg0);

Added: freeway/src/org/gnu/freeway/cwrappers/util/InterfaceImplementor.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/InterfaceImplementor.java        
2006-06-08 21:59:44 UTC (rev 2985)
+++ freeway/src/org/gnu/freeway/cwrappers/util/InterfaceImplementor.java        
2006-06-08 22:39:23 UTC (rev 2986)
@@ -0,0 +1,86 @@
+ /*
+      This file is part of Freeway
+
+      Freeway is free software; you can redistribute it and/or modify
+      it under the terms of the GNU General Public License as published
+      by the Free Software Foundation; either version 2, or (at your
+      option) any later version.
+
+      Freeway is distributed in the hope that it will be useful, but
+      WITHOUT ANY WARRANTY; without even the implied warranty of
+      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+      General Public License for more details.
+
+      You should have received a copy of the GNU General Public License
+      along with Freeway; see the file COPYING.  If not, write to the
+      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+      Boston, MA 02111-1307, USA.
+ */
+
+package org.gnu.freeway.cwrappers.util;
+
+import org.gnu.freeway.support.javaparser.visitors.Visitor;
+import org.gnu.freeway.support.javaparser.visitors.DepthFirstVisitor;
+import java.io.InputStream;
+import java.io.Writer;
+import java.io.IOException;
+import java.util.Set;
+import org.gnu.freeway.support.javaparser.*;
+
+/**
+ * @file InterfaceImplementor.java
+ * @brief 
+ * @author mdonoughe
+ */
+public class InterfaceImplementor extends DepthFirstVisitor implements Visitor 
{
+       //alias the constructor since an instance of this class is useless to 
others
+       public static void implement(InputStream in, Writer out, Set 
functionTypes) throws IOException, ParseException {
+               new InterfaceImplementor(in, out, functionTypes);
+       }
+       
+       private InputStream in;
+       private Writer out;
+       private Set functionTypes;
+       private StringBuffer output;
+       
+       private static final String[] INDENT = {"", "  ", "    ", "      "};
+
+       protected InterfaceImplementor(InputStream in, Writer out, Set 
functionTypes) throws IOException, ParseException {
+               this.in = in;
+               this.out = out;
+               this.functionTypes = functionTypes;
+               output = new StringBuffer();
+               JavaParser parser = new JavaParser(in);
+               CompilationUnit cu = parser.CompilationUnit();
+               output.append("//This class was autogenerated by 
InterfaceImplentor\n");
+               cu.accept(this);
+               out.write(output.toString());
+       }
+       
+       //start overriding DepthFirstVisitor
+       
+       public void visit(PackageDeclaration n) {
+               output.append(n.f0.tokenImage + " " + n.f1.f0.tokenImage);
+               for(int i = 0; i < n.f1.f1.size(); i++) {
+                       output.append("." + ((NodeToken) ((NodeSequence) 
n.f1.f1.elementAt(i)).nodes.get(1)).tokenImage);
+               }
+               output.append(".impl" + n.f2.tokenImage + "\n\n");
+               n.f0.accept(this);
+               n.f1.accept(this);
+               n.f2.accept(this);
+       }
+       
+       public void visit(ImportDeclaration n) {
+               output.append(n.f0.tokenImage + " " + n.f1.f0.tokenImage);
+               for(int i = 0; i < n.f1.f1.size(); i++) {
+                       output.append("." + ((NodeToken) ((NodeSequence) 
n.f1.f1.elementAt(i)).nodes.get(1)).tokenImage);
+               }
+               if(n.f2.present())
+                       output.append("." + ((NodeSequence) 
n.f2.node).nodes.get(1));
+               output.append(n.f3.tokenImage + "\n");
+               n.f0.accept(this);
+               n.f1.accept(this);
+               n.f2.accept(this);
+               n.f3.accept(this);
+       }
+}

Modified: freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-06-08 21:59:44 UTC (rev 2985)
+++ freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-06-08 22:39:23 UTC (rev 2986)
@@ -32,6 +32,11 @@
 import java.io.FileWriter;
 import java.io.Writer;
 import java.util.HashSet;
+import org.gnu.freeway.support.javaparser.JavaParser;
+import java.io.FileInputStream;
+import java.io.BufferedInputStream;
+import org.gnu.freeway.support.javaparser.ParseException;
+import org.gnu.freeway.support.javaparser.visitors.DepthFirstVisitor;
 
 /**
  * @file SwitchTableGenerator.java
@@ -57,7 +62,7 @@
         * @param args
         */
        public static void main(String[] args) {
-               StringBuffer clsPrefix = new StringBuffer();
+               //StringBuffer clsPrefix = new StringBuffer();
                StringBuffer srcPrefix = new StringBuffer();
                StringBuffer natPrefix = new StringBuffer();
                HashSet functionTypeSet = new HashSet();
@@ -69,9 +74,9 @@
                for(i = 0; i < args.length; i++)
                        if(args[i].startsWith("-"))
                                if(currentArg == null)
-                                       if(args[i].equals("-c") && 
clsPrefix.length() == 0)
-                                               currentArg = clsPrefix;
-                                       else if(args[i].equals("-s") && 
srcPrefix.length() == 0)
+//                                     if(args[i].equals("-c") && 
clsPrefix.length() == 0)
+//                                             currentArg = clsPrefix;
+                                       if(args[i].equals("-s") && 
srcPrefix.length() == 0)
                                                currentArg = srcPrefix;
                                        else if(args[i].equals("-n") && 
natPrefix.length() == 0)
                                                currentArg = natPrefix;
@@ -85,8 +90,8 @@
                        } else
                                break;
                //use defaults
-               if(clsPrefix.length() == 0)
-                       clsPrefix.append("build");
+               //if(clsPrefix.length() == 0)
+               //      clsPrefix.append("build");
                if(srcPrefix.length() == 0)
                        srcPrefix.append("src");
                if(natPrefix.length() == 0)
@@ -97,34 +102,29 @@
                        System.err.println("a source code generator for 
Freeway");
                        System.err.println();
                        System.err.println("usage: SwitchTableGenerator [-c 
<directory>] [-s <directory>] [-n <directory>]");
-                       System.err.println(" -c    path to class files 
(default: build)");
+                       //System.err.println(" -c    path to class files 
(default: build)");
                        System.err.println(" -s    path to java source files 
(default: src)");
                        System.err.println(" -n    path to c source files 
(default: native)");
                        return;
                }
                
-               if(clsPrefix.length() == 0)
-                       clsPrefix.append("build");
-               if(srcPrefix.length() == 0)
-                       srcPrefix.append("src");
-               if(natPrefix.length() == 0)
-                       natPrefix.append("native");
-               
-               if(!clsPrefix.toString().endsWith(File.separator))
-                       clsPrefix.append(File.separatorChar);
+               //if(!clsPrefix.toString().endsWith(File.separator))
+               //      clsPrefix.append(File.separatorChar);
                if(!srcPrefix.toString().endsWith(File.separator))
                        srcPrefix.append(File.separatorChar);
                if(!natPrefix.toString().endsWith(File.separator))
                        natPrefix.append(File.separatorChar);
                
                String servicesSubpath = "org" + File.separator + "gnu" + 
File.separator + "freeway" + File.separator + "services";
-               File classesDir = new File(clsPrefix + servicesSubpath);
-               File sourcesDir = new File(srcPrefix + servicesSubpath + 
File.separator + "impl");
+               //File classesDir = new File(clsPrefix + servicesSubpath);
+               File sourcesDir = new File(srcPrefix + servicesSubpath);
                File nativeCDir = new File(natPrefix.toString());
                
-               //create sources folder
-               if(!sourcesDir.exists())
-                       if(!sourcesDir.mkdirs()) {
+               File implDir = new File(sourcesDir, "impl");
+               
+               //create impl folder
+               if(!implDir.exists())
+                       if(!implDir.mkdirs()) {
                                try {
                                        System.err.println("Could not create 
nonexistant sources folder \"" + sourcesDir.getCanonicalPath() + "\".");
                                } catch(IOException ee) {
@@ -132,6 +132,113 @@
                                }
                        }
                
+               //process every interface
+               File inputFiles[] = sourcesDir.listFiles(new 
SourceFilesFilter());
+               for(i = 0; i < inputFiles.length; i++) {
+                       //get an input stream for the interface file
+                       BufferedInputStream inputSourceStream = null;
+                       try {
+                               inputSourceStream = new BufferedInputStream(new 
FileInputStream(inputFiles[i]));
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not read 
source file \"" + inputFiles[i].getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not read 
source file \"" + inputFiles[i].getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       //create empty output files
+                       File outputSource = new File(implDir, 
inputFiles[i].getName());
+                       try {
+                               if(!outputSource.createNewFile()) {
+                                       if(!outputSource.delete()) {
+                                               try {
+                                                       
System.err.println("Could not delete existing source file \"" + 
outputSource.getCanonicalPath() + "\".");
+                                               } catch(IOException ee) {
+                                                       
System.err.println("Could not delete existing source file \"" + 
outputSource.getAbsolutePath() + "\".");
+                                               }
+                                               continue;
+                                       }
+                                       if(!outputSource.createNewFile()) {
+                                               try {
+                                                       
System.err.println("Could not create source file \"" + 
outputSource.getCanonicalPath() + "\".");
+                                               } catch(IOException ee) {
+                                                       
System.err.println("Could not create source file \"" + 
outputSource.getAbsolutePath() + "\".");
+                                               }
+                                               continue;
+                                       }
+                               }
+                       } catch(Exception e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not create 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not create 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       //get a writer
+                       BufferedWriter outputSourceWriter = null;
+                       try {
+                               outputSourceWriter = new BufferedWriter(new 
FileWriter(outputSource));
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not open 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not open 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       //write code
+                       try {
+                               
InterfaceImplementor.implement(inputSourceStream, outputSourceWriter, 
functionTypeSet);
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not write 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not write 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       } catch(ParseException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not parse 
source file \"" + inputFiles[i].getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not parse 
source file \"" + inputFiles[i].getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       } finally {
+                               try {
+                                       outputSourceWriter.flush();
+                               } catch(IOException e) {
+                                       e.printStackTrace();
+                               }
+                               try {
+                                       outputSourceWriter.close();
+                               } catch(IOException e) {
+                                       e.printStackTrace();
+                               }
+                       }
+               }
+               
+               
+               //the old reflection way that got all the stuff backwards
+               /*//check for classes folder
+               if(!classesDir.exists()) {
+                       try {
+                               System.err.println("Could not find the classes 
folder \"" + classesDir.getCanonicalPath() + "\".");
+                       } catch(IOException ee) {
+                               System.err.println("Could not find the classes 
folder \"" + classesDir.getAbsolutePath() + "\".");
+                       }
+                       return;
+               }
+               
                //process every class
                File inputClasses[] = classesDir.listFiles(new 
ClassFilesFilter());
                for(i = 0; i < inputClasses.length; i++) {
@@ -149,8 +256,47 @@
                                continue;
                        }
                        
+                       //check for the interface file
+                       File inputSource = new File(sourcesDir, 
stripPackage(inputClass.getName()) + ".java");
+                       if(!inputSource.exists()) {
+                               try {
+                                       System.err.println("Could not find 
source file \"" + inputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not find 
source file \"" + inputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       //get an input stream for the interface file
+                       BufferedInputStream inputSourceStream = null;
+                       try {
+                               inputSourceStream = new BufferedInputStream(new 
FileInputStream(inputSource));
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not read 
source file \"" + inputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not read 
source file \"" + inputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       //run the parser
+                       JavaParser interfaceParser = new 
JavaParser(inputSourceStream);
+                       try{
+                               interfaceParser.CompilationUnit().accept(new 
DepthFirstVisitor());
+                       } catch(ParseException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not parse 
source file \"" + inputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not parse 
source file \"" + inputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
                        //create empty output files
-                       File outputSource = new File(sourcesDir, 
stripPackage(inputClass.getName()) + ".java");
+                       File outputSource = new File(implDir, 
stripPackage(inputClass.getName()) + ".java");
                        try {
                                if(!outputSource.createNewFile()) {
                                        if(!outputSource.delete()) {
@@ -219,7 +365,7 @@
                                        e.printStackTrace();
                                }
                        }
-               }
+               }*/
                
                //begin working on the C side
                //create native folder
@@ -621,14 +767,25 @@
                throw new NoSuchFieldException(name);
        }
        
-       private static class ClassFilesFilter implements FilenameFilter {
+       private static class SourceFilesFilter implements FilenameFilter {
                public boolean accept(File dir, String name) {
+                       if(name == null || !name.endsWith(".java"))
+                               return false;
+                       return true;
+               }
+               public SourceFilesFilter() {
+                       
+               }
+       }
+
+       /*private static class ClassFilesFilter implements FilenameFilter {
+               public boolean accept(File dir, String name) {
                        if(name == null || !name.endsWith(".class"))
                                return false;
                        return true;
                }
                public ClassFilesFilter() {
-                       
+       
                }
-       }
+       }*/
 }

Modified: freeway/src/org/gnu/freeway/services/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/StatsService.java      2006-06-08 
21:59:44 UTC (rev 2985)
+++ freeway/src/org/gnu/freeway/services/StatsService.java      2006-06-08 
22:39:23 UTC (rev 2986)
@@ -23,6 +23,7 @@
 import org.gnu.freeway.cwrappers.ConstCLong;
 import org.gnu.freeway.cwrappers.CLong;
 import org.gnu.freeway.cwrappers.CInt;
+import org.gnu.freeway.cwrappers.*;
 
 /**
  * @file StatsService.java

Modified: freeway/src/org/gnu/freeway/services/impl/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-08 
21:59:44 UTC (rev 2985)
+++ freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-08 
22:39:23 UTC (rev 2986)
@@ -1,39 +1,9 @@
-// This class was autogenerated by SwitchTableGenerator
+//This class was autogenerated by InterfaceImplentor
 package org.gnu.freeway.services.impl;
 
-import org.gnu.freeway.cwrappers.CInt;
+import org.gnu.freeway.cwrappers.ConstCString;
+import org.gnu.freeway.cwrappers.ConstCInt;
 import org.gnu.freeway.cwrappers.ConstCLong;
 import org.gnu.freeway.cwrappers.CLong;
-import org.gnu.freeway.cwrappers.ConstCInt;
-import org.gnu.freeway.cwrappers.ConstCString;
-import org.gnu.freeway.server.CPluginLoader;
-
-public class StatsService implements org.gnu.freeway.services.StatsService {
-       private CPluginLoader loader;
-       private CPluginLoader.Handle handle;
-
-       public void change(ConstCInt arg0, ConstCInt arg1) {
-               loader.callC(handle, "change", this, new Object[] {arg0, arg1});
-       }
-
-       public CLong get(ConstCInt arg0) {
-               return (CLong) loader.callC(handle, "get", this, new Object[] 
{arg0});
-       }
-
-       public void set(ConstCInt arg0, ConstCLong arg1) {
-               loader.callC(handle, "set", this, new Object[] {arg0, arg1});
-       }
-
-       public CInt create(ConstCString arg0) {
-               return (CInt) loader.callC(handle, "create", this, new Object[] 
{arg0});
-       }
-
-       public StatsService(CPluginLoader loader) {
-               this.loader = loader;
-               handle = loader.loadService("module_stats");
-       }
-
-       protected void finalize() {
-               loader.unloadService(handle);
-       }
-}
+import org.gnu.freeway.cwrappers.CInt;
+import org.gnu.freeway.cwrappers.*;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]