Example #1
0
string Jce2Php::generatePHP(const StructPtr &pPtr, const string& namespaceId) const
{
    ostringstream s;

	vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();

    s << TAB << "class " << pPtr->getId() << " extends c_struct" << endl;
    s << TAB << "{" << endl;
    INC_TAB;

    //变量声明
    for(size_t k = 0;k < member.size();k++)
    {
        s<< TAB << "public $" <<member[k]->getId()<<";"<<endl;
    }
    s<<endl;
    s << TAB << "public function __clone()" << endl;
    s << TAB << "{" << endl;
    INC_TAB;
    for(size_t k = 0;k < member.size();k++)
    {
        s<< TAB << "$this->" <<member[k]->getId()<<" = clone $this->"<<member[k]->getId()<<";"<<endl;
    }
    DEL_TAB;
    s << TAB << "}" << endl;
    s <<endl;

    s << TAB << "public function __construct()" << endl;
    s << TAB << "{" << endl;
    INC_TAB;
    for(size_t k = 0;k < member.size();k++)
    {
        s<< TAB << "$this->" <<member[k]->getId()<<" = new  "<< tostr(member[k]->getTypePtr())<<";"<<endl;
    }
    DEL_TAB;
    s << TAB << "}" << endl;
    s <<endl;
    ////////////////////////////////////////////////////////////
    s << TAB << "public function get_class_name()" << endl;
    s << TAB << "{" << endl;
    INC_TAB;
    s << TAB << "return " << "\"" << namespaceId << "." << pPtr->getId() << "\"" << ";" << endl;
    DEL_TAB;
    s << TAB << "}" << endl;
    s<<endl;

	//write begin
	if(m_bPDU)
	{
	    s << TAB << "public function writeTo(&$_out,$tag)" << endl;
	    s << TAB << "{" << endl;
	    INC_TAB;
	    //s << TAB << "jce_header::_pack_header($_out,'c_struct_begin',$tag);" << endl;
	}
	else
	{
		s << TAB << "public function write(&$_out,$tag)" << endl;
		s << TAB << "{" << endl;
		INC_TAB;
		s << TAB << "jce_header::_pack_header($_out,'c_struct_begin',$tag);" << endl;
	}

    for(size_t j = 0; j < member.size(); j++)
    {
        s << writeTo(member[j]);
    }

    if(m_bPDU)
    {
        //s << TAB << "jce_header::_pack_header($_out,'c_struct_end',0);" << endl;
    }
	else
	{
		 s << TAB << "jce_header::_pack_header($_out,'c_struct_end',0);" << endl;
	}

    DEL_TAB;
    s << TAB << "}" << endl;
	//write end


	///read begin
	if(m_bPDU)
	{
		s << TAB << "public function readFrom(&$_in,$tag,$isRequire = true)" << endl;
		s << TAB << "{" << endl;

		INC_TAB;
#if 0

		s << TAB << "jce_header::_peek_header($_in,$type,$this_tag);"<<endl;

		s << TAB << "if($tag != $this_tag)" <<endl;
		s << TAB << "{" << endl;
		INC_TAB;
		s << TAB << "if($isRequire == true){"<<endl;
		s << TAB << "throw new JceException(__LINE__ ,TAG_NOT_MATCH);"<<endl;
		s << TAB << "}else{" <<endl;
		INC_TAB;
		s << TAB << "return;"<<endl;
		DEL_TAB;
		s << TAB << "}" <<endl;
		DEL_TAB;
		s << TAB << "}" <<endl;

		s << TAB << "if($type != 'c_struct_begin')"<<endl;
		INC_TAB;
		s << TAB << "throw new JceException(__LINE__ ,TYPE_NOT_MATCH);"<<endl;
		DEL_TAB;

		s<< TAB << "jce_header::_unpack_header($_in,$type,$this_tag);"<<endl;
#endif
	}
	else
	{
		s << TAB << "public function read(&$_in,$tag,$isRequire = true)" << endl;
		s << TAB << "{" << endl;

		INC_TAB;

		s << TAB << "jce_header::_check_struct($_in,$type,$tag,$isRequire);"<<endl;
		s << TAB << "jce_header::_unpack_header($_in,$type,$this_tag);"<<endl;

	}
    for(size_t j = 0; j < member.size(); j++)
    {
        s << readFrom(member[j]);
    }

	if(!m_bPDU)
	{
    	s<< TAB << "$this->_skip_struct($_in);" << endl;
	}

    DEL_TAB;
    s << TAB << "}" << endl;
	//read end

    DEL_TAB;
    s << TAB << "}" << endl;
    return s.str();
}
Example #2
0
int TarsAnalyzer::readStruct(const StructPtr & pPtr, const NamespacePtr & nPtr)
{
    vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();

    AnalyzerDataHead h;
    size_t hLen = h.peekFrom(*this);
    if (AnalyzerDataHead::eStructBegin == h.getType()) // 检查结构开始
    {
        cout << endl;
        incTab();
        cout << getTab()<<  "struct " << pPtr->getId() << " {"  << endl;
        skip(hLen);
    }

    //解析struct成员变量
    for (size_t i = 0; i < member.size(); i++)
    {
        cout<< getTab() <<"expect tag ";
        cout.setf(ios::right);
        cout.width(CHAR_WIDTH_TAG);
        cout<< member[i]->getTag();
        cout.width(CHAR_WIDTH_ID); // 成员名12字符宽
        cout<< member[i]->getId();

        //获取tup版本号
        if(isTup() && (pPtr->getId() == "RequestPacket"))
        {
            if(member[i]->getTag() == 1)
            {
                char iVer = 2;
                this->peekBuf(&iVer, sizeof(iVer),1);
                _tupVersion = iVer;
            }
            else if(member[i]->getTag() == 7)///解析sbuffer
            {
                _tup_sbuffer_pos = _cur;
                _cur +=2; //7D 00

                int n;
                uint8_t  tag = 0;
                int  realType;
                read(n,  tag,  realType, true ); //sbuffer的长度

                if(_tupVersion == 2)
                {
                    read(_tup2_helper, 7);
                    _bIsDumpTup?dumpTup(_tup2_helper):(void(0));
                }
                else///3
                {
                    read(_tup3_helper, 7);
                    _bIsDumpTup?dumpTup(_tup3_helper):(void(0));
                }

                _cur = _tup_sbuffer_pos;
            }
        }

        //读取内置基本类型
        BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr());
        if (bPtr)
        {
            cout.width(CHAR_WIDTH_TYPE);
            cout<<tostrBuiltin(bPtr);

            cout<<getSep();
            readBuiltin(bPtr, member[i]->getTag(), member[i]->isRequire(), NULL);
        }

        VectorPtr vPtr = VectorPtr::dynamicCast(member[i]->getTypePtr());
        if (vPtr)
        {
            cout.width(CHAR_WIDTH_TYPE);
            cout<<tostrVector(vPtr);
            cout<<getSep();
            readVector(vPtr, member[i]->getTag(),member[i]->isRequire(), NULL);
        }

        MapPtr mPtr   = MapPtr::dynamicCast(member[i]->getTypePtr());
        if (mPtr)
        {
            cout.width(CHAR_WIDTH_TYPE);
            cout<<tostrMap(mPtr);
            cout<<getSep();
            readMap(mPtr, member[i]->getTag(), member[i]->isRequire());
        }

        StructPtr sPtr = StructPtr::dynamicCast(member[i]->getTypePtr());
        if (sPtr)
        {
            cout.width(CHAR_WIDTH_TYPE);
            cout<< tostrStruct(sPtr);
            cout<<getSep();
            readStruct(sPtr, NULL);
        }

        EnumPtr ePtr = EnumPtr::dynamicCast(member[i]->getTypePtr());
        if (ePtr)
        {

            cout.width(CHAR_WIDTH_TYPE);
            cout<< tostrEnum(ePtr);
            cout<<getSep();
            readEnum(ePtr , member[i]->getTag(), member[i]->isRequire());
        }

    }

     if (!_bTup)
     {
        hLen = h.peekFrom(*this); //buf中没有结构结束标志,也不会抛异常,因为setBuffer的buf_len有1个保护字节
        if (AnalyzerDataHead::eStructEnd == h.getType() || _cur >= _buf_len-chars_buf_reserve) // 检查结构结束
        {
            cout << getTab() <<  "} //struct " << pPtr->getId() <<  endl;
            decTab();
            skip(hLen);
            if (pPtr->getId() == _StructName)
            {
                cout << "analyze " << _streamFileName << " with struct " << _StructName << " ok!" << endl;
            }
        }
     }
    return 0;

}