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

	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;
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为毛呀

非常感谢你对我的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值