PERL能为我做什么(1)

PERL能为我做什么?


一个有关Qt反射机制的编程要求:

1. 利用CIME格式(一种电力系统模型ASCII码格式)文件,生成满足QT要求的C++头文件,使得这些头文件中的定义的类具有反射机制(refection);

2. CIME格式具有规格化的表达方式,如下:


@Num    mRID    name    Parent    p    q  eng_code
//序号   标识    区域名    父区域标识    总有功    总无功  英文编码
# 1 1@650 test 1@600 0 0 test
# 2 4001@650 贵阳供电局 4001@600 0 0 贵阳供电局


@ Num mRID name nomkV
//序号 标识 基准电压名 基准电压
# 1 20@662 6kV 6
# 2 21@662 220kV 220
# 3 22@662 110kV 110
# 4 23@662 35kV 35
# 5 24@662 10kV 10
# 6 25@662 66kV 66
# 7 30@662 中性点 1

3. 满足QT反射机制的C++头文件

示例如下:

#ifndef _BASEVOLTAGE_H_

#define _BASEVOLTAGE_H_


#include


class CimeBaseVoltage : public QObject
{
Q_OBJECT
Q_PROPERTY(QString mRID READ mRID WRITE setmRID)
Q_PROPERTY(QString name READ name WRITE setname)
Q_PROPERTY(QString nomkV READ nomkV WRITE setnomkV)
Q_PROPERTY(QString Region_id READ Region_id WRITE setRegion_id)


public:
CimeBaseVoltage(QObject *parent=NULL) {}
signals:
public slots:
public:
QString mRID() const {return m_mRID; }
QString name() const {return m_name; }
QString nomkV() const {return m_nomkV; }
QString Region_id() const {return m_Region_id; }


void setmRID(const QString &input ) { m_mRID = input; }
void setname(const QString &input ) { m_name = input; }
void setnomkV(const QString &input ) { m_nomkV = input; }
void setRegion_id(const QString &input ) { m_Region_id = input; }
public:
QString m_mRID;
QString m_name;
QString m_nomkV;
QString m_Region_id;
};
#endif



大家知道perl具有对字符处理超强的能力,以上需求perl代码如下:


#! /usr/bin/perl


open INFILE, 'TEST.CIME' or die "$!";
my $className = 'defaut_name';
my $filename  = lc($className);
my $ucName    = uc($className);
while ()
{
    if (/<(\w+):/)
    {
        $className = $1;
        print "className=", $1, "\n";
    }


    if (/^\@Num/)
    {
        my @line = split /\s+/, $_;    #Seperated with space
        shift @line;


        #print "matched line=", @line,"\n";
        #print "open file...", $className;
        $filename = lc($className);
        open QFILE, '>', $filename . ".h" or die "$!";
        $ucName = uc($className);
        print QFILE "#ifndef _$ucName", "_H_\n";
        print QFILE "#define _$ucName", "_H_\n\n";
        print QFILE "#include \n\n";
        print QFILE "class $className : public QObject\n";
        print QFILE "{\n\tQ_OBJECT\n";


        foreach $item (@line)
        {
            print QFILE "\tQ_PROPERTY(QString $item READ $item WRITE $item)\n";
        }


        print QFILE "\npublic:\n";
        print QFILE "\t$className", "(QObject *parent=NULL) {}\n";
        print QFILE "signals:\npublic slots:\npublic:\n";


        #
        foreach $item (@line)
        {
            print QFILE "\tQString $item", "() const {return m_", "$item; }\n";
        }


        #
        print QFILE "\n";
        foreach $item (@line)
        {
            print QFILE "\tvoid set$item", "(const QString &input ) { m_", "$item = input; }\n";
        }


        print QFILE "public:\n";
        foreach $item (@line)
        {
            print QFILE "\tQString m_", "$item;\n";
        }
        print QFILE "};\n";
        print QFILE "#endif\n";
        close QFILE;
    }
    else
    {
        next;
    }
}


close INFILE;












你可能感兴趣的:(PERL编程)