Laxenade
Laxenade

Reputation: 9

Parsing c struct to java interface

Are there any tools that I can use to parse c struct to java interface automatically? an example would be:

typedef struct C {
    int x;
    byte y;
}C;

//Java
interface C {
    public int x();
    public byte y();
    public void x(int val);
    public void y(byte val);
}

Upvotes: 0

Views: 1196

Answers (2)

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

Yes, try regular expression replaces. In Java for example:

String javaCode = cCode.replaceAll("(\\w+) +(\\w+);", "public void $1 $2();\n    public $1 x($1 $2);");

Demo here: http://regexr.com?388h8

Now, just a minimal effort is needed to change the syntax of the struct definition itself.

Upvotes: 1

Jack
Jack

Reputation: 133567

Javolution is a library that provides this kind of functionality in an easy way, example taken from the documentation:

public enum Gender { MALE, FEMALE };
public static class Date extends Struct {
  public final Unsigned16 year = new Unsigned16();
  public final Unsigned8 month = new Unsigned8();
  public final Unsigned8 day   = new Unsigned8();
}
public static class Student extends Struct {
  public final Enum32<Gender>       gender = new Enum32<Gender>(Gender.values());
  public final UTF8String           name   = new UTF8String(64);
  public final Date                 birth  = inner(new Date());
  public final Float32[]            grades = array(new Float32[10]);
  public final Reference32<Student> next   =  new Reference32<Student>();
}

Upvotes: 0

Related Questions