如何引用别人的博客?
头文件:
#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;
}
}
}