FileDocCategorySizeDatePackage
C2J++.javaAPI DocExample6309Sat Nov 25 11:54:28 GMT 2000None

C2J

public class C2J extends Object
Purpose: Encapsulates all the stages of translating C++ to Java
version
1.0
author
Ilya Tilevich

Fields Summary
private String
m_strHeaderFile
private String
m_strCppFile
private String
m_strTempFile
private String
m_strOutputFile
private TextOutputStream
m_tos
private FileOutputStream
m_fosFileOut
private Parser
m_Parser
static boolean
m_bDefnOnly
Constructors Summary
public C2J(String strHeaderFile, String strCppFile)
Purpose: Constructor

param
strHeaderFile - an *.h file
param
strCppFile - a *.cpp file
exception
FileNotFoundException if either file is not found

	                      	 
	    
		
		   m_strHeaderFile = new String(strHeaderFile); 
	       m_strCppFile = new String(strCppFile); 
		   m_strTempFile = new String("__temp__"); 
		   m_strOutputFile = ConstructOutputFileName(strCppFile); 
		   
		   
		   try{
				m_fosFileOut  = new FileOutputStream(m_strTempFile);
				m_tos = new TextOutputStream(m_fosFileOut);
		   }
		   catch(FileNotFoundException e){
				System.out.println("File Not Found" + e);
				return;
	       }
			
		   catch(Throwable e) {
				System.out.println("Error in opening file" + e);
				return;
		   }

		   m_Parser = new Parser(false); //create a parser to parse a header file
   
	
Methods Summary
private java.lang.StringConstructOutputFileName(java.lang.String strCppFile)

	   	
		String strOutputFile = new String(strCppFile); 
	    
	   	//System.out.println(strOutputFile);
		int iIndex = strOutputFile.indexOf(".");

	   
	   if(iIndex == -1){
   		  System.out.println("Invalid name for a cpp file : " + strCppFile);
		  System.exit(1);
	   }

	   strOutputFile = strOutputFile.substring(0, iIndex) + ".java";
	   return strOutputFile;

	
public java.lang.StringGetOutputFileName()
Purpose: Returns the output file name

return
String

 return m_strOutputFile; 
public voidMatchAndReplace()
Purpose: Reads an output file and replaces found strings as specified Note: similar to sed utility

		StringPair stPair1 = new StringPair("char * ","String ");
		StringPair stPair2 = new StringPair("char* ","String ");
		StringPair stPair3 = new StringPair("const char * " ,"String ");
		StringPair stPair4 = new StringPair(" & ", " ");
		StringPair stPair5 = new StringPair("*", " ");
		StringPair stPair6 = new StringPair("->", ".");
		StringPair stPair7 = new StringPair("unsigned int " ,"int ");
		StringPair stPair8 = new StringPair("unsigned long ","int ");		
		StringPair stPair9 = new StringPair("unsigned char ", "char ");
		StringPair stPair10 = new StringPair("unsigned ","int ");
		StringPair stPair11 = new StringPair("const " , "static final ");
		StringPair stPair12 = new StringPair("BOOL ", "boolean ");	
        StringPair stPair13 = new StringPair("NULL", "null");
		
			
		Vector v = new Vector();
		v.addElement(stPair1);
		v.addElement(stPair2);
		v.addElement(stPair3);
		v.addElement(stPair4);
		v.addElement(stPair5);
		v.addElement(stPair6);
		v.addElement(stPair7);
		v.addElement(stPair8);
		v.addElement(stPair9);
		v.addElement(stPair10);
		v.addElement(stPair11);
		v.addElement(stPair12);
		v.addElement(stPair13);

		Replacer rep = new Replacer(m_strOutputFile, v);
		rep.MatchAndReplace();
		
		(new File(m_strTempFile)).delete();
	
public voidParseCppFile()
Purpose: parses a cpp file containig the implementation of the class defined in header file

exception
IOException


	   m_Parser.OpenInputFile(m_strCppFile);
	   m_Parser.SetParseMethod(true);

	   while(m_Parser.readClass(m_bDefnOnly));

	   //m_Parser.m_membersArray.WriteOut();
	   m_Parser.addGlobals();
	   
	   m_Parser.CloseAllFiles();
	   
	   m_tos.close();
	   m_fosFileOut.close();
	 
		   
	   		   	
	
public voidParseHeaderFile()
Purpose: parses a header file containing class definitions Note: Should be called first

exception
IOException

	
	    boolean bFirstClass = true;
	    m_Parser.SetParseMethod(false);
	    m_Parser.OpenInputFile(m_strHeaderFile);

	    while(m_Parser.readClass(m_bDefnOnly));		
		
	    for(ClassRepresentation c = m_Parser.class_info; c!=null; c = c.GetNext()){
			c.print(m_tos, bFirstClass);      
	   		if(bFirstClass) bFirstClass = false; 
	    }		
	
public voidWeaveItTogether()
Purpose: Calls the preprocessor

	    //m_Parser.m_membersArray.WriteOut();
		PrimitivePreprocessor prep = new PrimitivePreprocessor(new String(m_strTempFile), new String(m_strOutputFile), 
		m_Parser.m_membersArray);
	    prep.ProcessFile();
	
public static voidmain(java.lang.String[] args)


	String strHeaderFile = new String();
	String strCppFile = new String();
	
	
	if(args.length >= 2){
	   strHeaderFile = new String(args[0]);
	   strCppFile = new String(args[1]);
	
	   if(args.length == 3 &&  args[2].equalsIgnoreCase("-DefnOnly"))
	      C2J.m_bDefnOnly = true;
		
	}
	else{
		System.out.println("Usage: Java C2J  Source.h  Source.cpp [-DefnOnly]");
		System.exit(1);
	}
   
	
	
	C2J trans = new C2J(strHeaderFile,  strCppFile);
	trans.ParseHeaderFile();
	trans.ParseCppFile();
	trans.WeaveItTogether();
	trans.MatchAndReplace();

	
			
			

	System.out.println(trans.GetOutputFileName() + " is created");
	System.out.println("Enjoy!");
    
	
	//Runtime run = Runtime.getRuntime(); //works only under Windows NT
	//run.exec("write " + trans.GetOutputFileName());