您的位置:首页 > 编程语言 > C语言/C++

把C语言中的enum转成java里面的整型常量

2012-05-29 11:27 239 查看
public static void enum_c_2_java(String de_filename, String sr_filename)
{
try {
FileReader fr = new FileReader(sr_filename);//创建FileReader对象,用来读取字符流
BufferedReader br = new BufferedReader(fr);    //缓冲指定文件的输入
FileWriter fw = new FileWriter(de_filename);//创建FileWriter对象,用来写入字符流
BufferedWriter bw = new BufferedWriter(fw);    //将缓冲对文件的输出
String myreadline;    //定义一个String类型的变量,用来每次读取一行
boolean bfind = false;
int num = -1;
boolean isStr = false;
String intStr = null;

while (br.ready()) {
myreadline = removeSpecilChar(br.readLine());//读取一行
if (myreadline.indexOf("enum") > -1)
{
bfind = true;
continue;
}
if (bfind && myreadline.indexOf("{") > -1)
{
continue;
}
if (bfind && myreadline.indexOf("}") > -1)
{
bw.write("\r\n\r\n"); //写入文件
bfind = false;
num = -1;
isStr = false;
intStr = null;
continue;
}

if (bfind) // 找到enum成员
{
int nset = 0;
if (myreadline.startsWith("//") || myreadline.length()==0)
{
bw.write(myreadline); //写入文件
}
else
{
if ((nset = myreadline.indexOf("=")) > -1)
{
intStr = myreadline.substring(nset + 1, myreadline.indexOf(","));
try {
if(intStr.startsWith("0x") || intStr.startsWith("0X")) {
intStr = intStr.substring(2);
num = Integer.valueOf(intStr,16);
intStr = null;
} else if(intStr.startsWith("0") && intStr.length() > 1) {
intStr = intStr.substring(1);
num = Integer.valueOf(intStr,8);
intStr = null;
}else if((intStr.charAt(0)>='A'
&&(intStr.charAt(0)<='Z')
|| (intStr.charAt(0)>='a'&&intStr.charAt(0)<='z'))){
isStr = true;
}else {
num = Integer.valueOf(intStr);
intStr = null;
}

bw.write(get_str(myreadline.substring(0, nset), intStr, num));

if (isStr)
num = 0;
/*
bw.write(myreadline.substring(0, nset));
bw.write(" = "+num + ";"); //写入文件
*/
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
/*
isStr = true;
num = -1;
bw.write(get_str(myreadline.substring(0, myreadline.length() - 2), intStr, num));
num = 0;
*/
/*
bw.write(myreadline.substring(0, myreadline.length() - 2)); //写入文件
bw.write(";");
*/
}

}
else if ( myreadline.indexOf(",") > -1)
{
++num;
/*
bw.write(myreadline.substring(0, myreadline.length() - 1)); //写入文件
if(isStr)
bw.write(" = " + (intStr + "+" + num) + ";");
else
bw.write(" = "+num + ";");
*/
bw.write(get_str(myreadline.substring(0, myreadline.length() - 1), intStr, num));
}
else
{
/*
bw.write(myreadline); //写入文件
if(isStr)
bw.write(" = " + (intStr + "+" + num) + ";");
else
bw.write(" = "+num + ";");
*/
++num;
bw.write(get_str(myreadline, intStr, num));
}
}

bw.newLine();
bw.flush();
//System.out.println(myreadline);//在屏幕上输出
}
}
bw.flush();    //刷新该流的缓冲
bw.close();
br.close();
fw.close();
br.close();
fr.close();

} catch (IOException e) {
e.printStackTrace();
}
}
public static String get_str(String str, String instr, int num)
{
if (str == null)
return null;

if (instr != null)
str = str+" = "+instr;

if (num >= 0 && instr != null)
str = str + " + " + num;
else if(num >= 0)
str = str + " = " + num;

str = str + ";";
System.out.println(str);//在屏幕上输出
return "public final static int	"+str;
}

public static String removeSpecilChar(String str){
String result = "";
if(null != str){
Pattern pat = Pattern.compile("\\s*|\n|\r|\t");
Matcher mat = pat.matcher(str);
result = mat.replaceAll("");
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: