学习gRPC--建立一个解析器助手

如何引用别人的博客?

头文件:

#ifndef _POS_GRPC_PARSER_HELPER_H_
#define _POS_GRPC_PARSER_HELPER_H_

#include <string>
#include "study_grpc.grpc.pb.h"


const std::string g_strLocation = "\"location\":";
const std::string g_strLatitude = "\"latitude\":";
const std::string g_strLongitude = "\"longitude\":";
const std::string g_strName = "\"name\":";


namespace routeguide {

    class Parser {
    public:
        Parser(const std::string& strDb);

        bool Finished();

        bool TryParseOne(Feature* feature);

    private:

        bool SetFailedAndReturnFalse();

        bool Match(const std::string& prefix);

        void ReadLong(long* l);

    private:
        bool              m_bFailed ;
        std::string       m_strDb;
        size_t            m_szCurrent;
    
    };

}

#endif

#include "stdafx.h"
#include "pos_grpc_parser_help.h"
#include <list>
#include <iostream>
#include <exception>

namespace routeguide {

        Parser:: Parser(const std::string& strDb) : m_strDb(strDb),m_bFailed(false),m_szCurrent(0)
        {
            // Remove all spaces.
            m_strDb.erase(std::remove_if(m_strDb.begin(), m_strDb.end(), isspace),m_strDb.end());
            if (!Match("["))
            {
                SetFailedAndReturnFalse();
            }
        }

        bool Parser::Finished() 
        {
            return m_szCurrent >= m_strDb.size();
        }

        bool Parser::TryParseOne(Feature* feature) 
        {
            if (m_bFailed || Finished() || !Match("{"))
            {
                return SetFailedAndReturnFalse();
            }
            if (!Match(g_strLocation) || !Match("{") || !Match(g_strLatitude))
            {
                return SetFailedAndReturnFalse();
            }

            long temp = 0;
            ReadLong(&temp);
            feature->mutable_location()->set_latitude(temp);
            if (!Match(",") || !Match(g_strLongitude)) {
                return SetFailedAndReturnFalse();
            }
            ReadLong(&temp);
            feature->mutable_location()->set_longitude(temp);
            if (!Match("},") || !Match(g_strName) || !Match("\"")) {
                return SetFailedAndReturnFalse();
            }

            size_t name_start = m_szCurrent;

            while (m_szCurrent != m_strDb.size() &&  m_strDb[m_szCurrent++] != '"') 
            {
            }
            if (m_szCurrent == m_strDb.size())
            {
                return SetFailedAndReturnFalse();
            }

            feature->set_name(m_strDb.substr(name_start, m_szCurrent - name_start - 1));

            if (!Match("},")) 
            {
                if (m_strDb[m_szCurrent - 1] == ']' && m_szCurrent == m_strDb.size())
                {
                    return true;
                }
                return SetFailedAndReturnFalse();
            }
            return true;
        }

        bool Parser::SetFailedAndReturnFalse()
        {
            m_bFailed = true;
            return false;
        }

        bool Parser::Match(const std::string& strPrefix)
        {
            bool bEq = m_strDb.substr(m_szCurrent, strPrefix.size()) == strPrefix;
            m_szCurrent += strPrefix.size();
            return bEq;
        }

        void Parser::ReadLong(long* lResult) 
        {
            try
            {
                size_t start = m_szCurrent;
                while (m_szCurrent != m_strDb.size() && m_strDb[m_szCurrent] != ',' && m_strDb[m_szCurrent] != '}')
                {
                    m_szCurrent++;
                }
                // It will throw an exception if fails.
                *lResult = std::stol(m_strDb.substr(start, m_szCurrent - start));
            }
            catch (const std::exception& e)
            {
                std::cout << e.what() << std::endl;
            }
        
        
        }

}
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值