zoukankan      html  css  js  c++  java
  • Perl解析RC,RC2文件

    我们正在开发的一个软件是国际化软件,所以需要翻译成其他国家的语言。但是对每一次更新实际上需要改动的语言只是一部分,其他一大部分是可重用的。

    所以,为了避免代理商再次翻译,我们从原来的版本资源文件抽出了对应的字典文件,并进行替换,达到只翻译一部分改变了的字符串。

    这其他还希望用到模糊匹配,但是效果似乎不大,不过还是很有必要的。关于模糊匹配,我看到了《编程之美》里面的一个题目:“判断字符串的相似度”。与这个类似。 以下是部分Perl写的代码: [pl] #根据RC,RC2文件构造字典需要输入两个文件,利用控件ID来对应

      1 #!/usr/bin/perl -w
      2 use strict;
      3 use warnings;
      4 
      5 
      6 #根据RC,RC2文件构造字典需要输入两个文件,利用控件ID来对应
      7 
      8 ##################
      9 #解析.rc2文件
     10 sub readRc2File
     11 {
     12    my $currentLine = 0 ;####读到492多行,addcount=382时的时候竟然结束了!
     13    my $fileName = shift;
     14    my $hash_ref = shift ;
     15    my $encoding = shift ; 
     16    my $fd ; 
     17    my $addCount = 0 ;
     18    if( $encoding )
     19    {
     20       open $fd, "<:utf8", $fileName or die "openFile $fileName error!";
     21    }else{
     22       open $fd, "<:utf8", $fileName or die "openFile $fileName error!";
     23    }
     24    my $begin = 0 ;   #begin开始标志位
     25    my $mul_begin = 0 ; #/**/开始标志位
     26    my $str = "";
     27    while(<$fd>)
     28     {
     29       #$currentLine++;
     30       #print "$currentLine: $_"; 
     31       #过滤多行注释
     32       if( m#^\s*/\*# )#if begin with: /*  #这里会有问题!当字符串“”里面有注释的时候就会挂-_-!!!所以!必须得开头!
     33       {
     34            $mul_begin = 1 ; 
     35       }
     36       if($mul_begin)
     37       {
     38          if( m#.*\*/# ) #if end width */
     39          {
     40             #print "Current Line:$currentLine";#addCount = 1670出问题
     41             $mul_begin = 0 ; 
     42          }
     43       }
     44       else
     45       {
     46         #在BEGIN\n与END\n里面的就是我们需要的字符串  ##这里出要求BEGIN开头,避免字符串里面出现BEGIN
     47         if( m/^\s*BEGIN/ )
     48         {
     49             $begin = 1 ; 
     50         }
     51         else{
     52             if( $begin )
     53             {
     54                 if( /^END/ )
     55                 {
     56                     $begin = 0 ; 
     57                 }
     58                 else
     59                 {
     60                     #处理数据
     61                     #过滤单行注释 
     62                     if( !m=^\s*(//|#)= ) # if begin with // or #
     63                      {
     64                          #处理真正的数据
     65                          if( m#^[\s|\t]*(\w+)[\s|\t]+"(.+)"# )
     66                            {
     67                               if( !exists( $$hash_ref{$1} ) )
     68                               {
     69                                  $$hash_ref{$1} = $2 ; 
     70                                  $addCount++;
     71                               }
     72                            }
     73                          }
     74                      }
     75                 }
     76            }
     77        }
     78     }
     79 }
     80 
     81 ##################
     82 #解析.rc文件,.rc文件与.rc2文件不同的地方在于:控件ID在“字符串”后面 ,例如:"C&ounter_Clockwise",IDC_ADVANCE_CCW
     83 #rc与文件有点不同
     84 sub readRcFile
     85 {
     86    my $addCount = 0 ;
     87    my $fileName = shift;
     88    my $hash_ref = shift ;
     89    open my $fd, "< ", $fileName or die "openFile $fileName error!";
     90    my $begin = 0 ;
     91    my $mul_begin = 0 ; #/**/开始标志位
     92    my $str = "";
     93    while(<$fd>)
     94    {
     95       #过滤多行注释
     96       if( m#^\s*/\*# )#if begin with: /*  #这里会有问题!当字符串“”里面有注释的时候就会挂-_-!!!所以!必须得开头!
     97       {
     98            $mul_begin = 1 ; 
     99       }
    100       if($mul_begin)
    101       {
    102          if( m#.*\*/# ) #if end width */
    103          {
    104             #print "Current Line:$currentLine";#addCount = 1670出问题
    105             $mul_begin = 0 ; 
    106          }
    107       }
    108       else
    109       {
    110         #在BEGIN\n与END\n里面的就是我们需要的字符串  ##这里出要求BEGIN开头,避免字符串里面出现BEGIN
    111         if( m/^\s*BEGIN/ )
    112         {
    113             $begin = 1 ; 
    114         }
    115         else
    116         {
    117             if( $begin )
    118             {
    119                 if( /^END/ )
    120                 {
    121                     $begin = 0 ; 
    122                 }
    123                 else
    124                 {
    125                     #处理数据
    126                     #过滤单行注释 
    127                     if( !m=^\s*(//|#)= ) # if begin with // or #
    128                      {
    129                          #处理真正的数据
    130                          if( m="(.+?)"\s*,(\w+?),= )
    131                            {
    132                               if( !exists( $$hash_ref{$2} ) )
    133                               {
    134                                  $$hash_ref{$2} = $1 ; 
    135                                  $addCount++;
    136                               }
    137                            }
    138                      }
    139                 }
    140             }
    141          }
    142       }
    143    }
    144 }
    145 
    146 
    147 #####################
    148 #通过相同ID确定 英文与波兰文的键值关系
    149 sub getDict
    150 {
    151    my $hash_eng = shift ;
    152    my $hash_pol = shift ;
    153    my $dict = shift ; 
    154    while(my ($key, $val) = each(%$hash_eng) )
    155     {
    156         $$dict{$val} = $$hash_pol{$key};
    157     }
    158 }
    159 
    160 ######################
    161 sub main
    162 {
    163    my %hash_eng = () ;
    164    my %hash_pol = () ;
    165    my %dict = (); 
    166    
    167    print "========READING ENG FILE============\n";
    168    readRc2File("rc2/Addition_ENU.rc2",\%hash_eng);
    169    print "Hash Table Size of hash_eng:".dictSize(\%hash_eng)."\n";
    170    readRc2File("rc2/icad.rc2",\%hash_eng,1);
    171    print "Hash Table Size of hash_eng:".dictSize(\%hash_eng)."\n";
    172    
    173    #readRcFile("rc/IcadRes2_ENU.rc",\%hash_eng);
    174    #print "Hash Table Size of hash_eng:".dictSize(\%hash_eng)."\n";
    175    
    176    
    177    print "=======READING POL FILE============\n";
    178    readRc2File("rc2/AdditionPL.rc2",\%hash_pol);
    179    print "Hash Table Size of hash_pol:".dictSize(\%hash_pol)."\n";
    180    readRc2File("rc2/icad_POL.rc2",\%hash_pol,1);
    181    print "Hash Table Size of hash_pol:".dictSize(\%hash_pol)."\n";
    182    
    183    #readRcFile("rc/IcadRes2_POL.rc",\%hash_pol);
    184    #print "Hash Table Size of hash_pol:".dictSize(\%hash_pol)."\n";
    185    
    186    getDict(\%hash_eng,\%hash_pol,\%dict);
    187    writeDict(\%dict,"dict/rc_dict");
    188 }
    189 
    190 
    191 ######### 程序执行入口 ##################
    192 main();
    193 
    194 
    195 
    196 
    197 ###################tools for test###########################
    198 
    199 sub writeDict
    200 {
    201     my $dict_hash = shift ;
    202     my $dict_Name = shift ;
    203     open my $myfd, " >:utf8", $dict_Name;
    204     while(my ($key, $val) = each(%$dict_hash) )
    205     {
    206         if( $key ne $val )
    207         {
    208            print $myfd "$key=\n$val \n" ;
    209         }
    210     }  
    211 }
    212 
    213 sub dictSize
    214 {
    215    my $hash_ref = shift ;
    216    my $hash_size = keys %$hash_ref ;
    217    return $hash_size;
    218 }
    219 sub printDict
    220 {
    221     my $hash_ref = shift ;
    222     my $hash_size = keys %$hash_ref ;
    223     print "\n---------hash_size:$hash_size-----------\n";
    224     while(my ($key, $val) = each(%$hash_ref) )
    225     {
    226         print  "$key=\n$val\n" ;
    227     }  
    228 }

    由于RC文件过于复杂,还没有写出来...~>_<~+

  • 相关阅读:
    根据数据类型选择特征 select_dtypes(include=[]/exclude=[])
    quantile()
    concat()、merge()的区别
    json.dumps()和json.loads()
    Linux-top命令详解
    性能测试总结(一)---基础理论篇
    jmeter-常见问题及解决方法
    Jmeter之Bean shell使用(五)
    Jmeter-内存溢出原因及解决方法
    JMeter- JDBC Request
  • 原文地址:https://www.cnblogs.com/trying/p/2863830.html
Copyright © 2011-2022 走看看