Does not parse the configuration content, reads only the file content, cuts off the annotation and the trailing space after writes the cache: Vector<string>. Used by other methods.
The code is written in an MFC gadget.
ReadProtocol.h
Copy Code code as follows:
/**
* Read the protocol content write cache from the file
* For external method use
* Alex Liu, 2014
*/
#pragma once
#include <vector>
#include <map>
#include <list>
#include <string>
using namespace Std;
#define MAX_FILEPATH 512
#define COMMENT_FLG ' # '
#define SECTION_BEGIN_FLG ' ['
#define SECTION_END_FLG '] '
Class Readprotocol {
Public
Readprotocol (char* FilePath);
~readprotocol ();
/**
* Return value of ErrMsg address for easy chained call
* The default return to "Success"
*/
char* Geterrinfo (char* errmsg, int errNo = 0);
/**
* Read the contents of the file row by line write M_strvect
* Use vector::p ush_back () write
* Return 0 Success < 0 failure can be based on the returned value Geterrinfo
*/
int Readinifile ();
/**
* Get one from the directory
* Use vector::p ush_back () write
* Return 0 Success < 0 failure can be based on the returned value Geterrinfo
*/
int getonesection (string section, list<string> &protocol);
Private
void Pushbacktovector (string oneline);
Private
Char M_inifile[max_filepath];
String M_errpos;
map<string, unsigned int> m_sectionmap;
Vector<string> M_strvect;
};
ReadProtocol.cpp
Copy Code code as follows:
//
#include "stdafx.h"
#include <fstream>
#include "ReadProtocol.h"
Remove the trailing space of the string
static string Strtrim (String aStr)
{
string s = aStr;
unsigned int-A, last;
if (String::npos!= (")" (The "s.find_first_not_of")
s = S.substr (A, s.length ()-first);
if (String::npos!= (last = s.find_last_not_of (')))
s = s.substr (0, last+1);
return s;
}
///=====================================================================================
Readprotocol::readprotocol (char* FilePath)
{
int Ilen = (strlen (FilePath) > Max_filepath)? Max_filepath:strlen (FILEPATH);
memset (m_inifile, 0, Max_filepath);
memcpy (M_inifile, FilePath, Ilen);
}
Readprotocol::~readprotocol ()
{
M_sectionmap.clear ();
M_strvect.clear ();
}
int readprotocol::getonesection (string section, List<string> &protocol)
{
unsigned int Start = 0;
Note that the [] operator cannot be used here
map<string, unsigned int>::iterator ITR = m_sectionmap.find (section);
if (m_sectionmap.end () = = ITR)
{
M_errpos = section;
return-5; Unknown section!!
}
Else
{
Start = itr->second;
}
Vector<string>::iterator it = m_strvect.begin () + Start + 1;
for (; It!=m_strvect.end (); ++it)
{
unsigned int-A, last;
A-it->find_first_of (SECTION_BEGIN_FLG);
Last = it->find_last_of (SECTION_END_FLG);
Stop when the next section
if (string::npos!= && string::npos!= last)
{
Break
}
Protocol.push_back (*it);
}
return (int) protocol.size ();
}
int Readprotocol::readinifile ()
{
Ifstream fin (m_inifile);
if (!fin.is_open ())
{
return-1; Can ' Topen file
}
String StrLine;
unsigned int last;
while (Std::getline (Fin, StrLine). Good ())
{
if (String::npos!= (last = strline.find_last_not_of (' \ R '))
{
Delete \ r
StrLine = Strline.substr (0, last + 1);
}
Pushbacktovector (StrLine);
}
Fin.close ();
if (M_strvect.empty ())
{
Return-2; Get noting from file
}
return 0;
}
void Readprotocol::P ushbacktovector (String oneline)
{
unsigned int upos;
Remove end of line comment
if (String::npos!= (Upos = oneline.find_first_of (COMMENT_FLG)))
{
Oneline = oneline.substr (0, Upos + 1);
}
Go to the end of the space
Oneline = Strtrim (oneline);
if (Oneline.empty () | | | oneline.length () < 2) return;
Only one record in a row
unsigned int-A, last;
A-oneline.find_first_of (SECTION_BEGIN_FLG);
Last = oneline.find_last_of (SECTION_END_FLG);
is section
if (string::npos!= && string::npos!= last)
{
m_sectionmap[oneline.substr (1, last-first-1)] = M_strvect.size ();
}
M_strvect.push_back (oneline);
}
char* Readprotocol::geterrinfo (char* errmsg, int errNo)
{
String Errinfo;
Switch (errNo)
{
Case 0:
{
Errinfo = "success!";
Break
}
Case-1:
{
Char path[1024] = {0};
int plength = 1024;
GetCurrentDirectory (Plength, Path);
Errinfo.append ("Can" T open file.) The file name is:==>\ "");
Errinfo.append (M_inifile);
Errinfo.append ("\" \r\nmaybe no such file in Path: ");
Errinfo.append (Path);
Break
}
Case-2:
{
Errinfo = "Get noting from file:";
Errinfo.append (M_inifile);
Break
}
Case-3:
{
Errinfo = "Analyze file failed. In ==> ";
Errinfo.append (M_errpos);
Break
}
Case-5:
{
Errinfo = "\r\nunknown section!! ==> \ "[";
Errinfo.append (M_errpos);
Errinfo.append ("]\" \ r \ n) Please check the configuration file for any omissions. ");
Break
}
Default
{
Errinfo = "Please follow the correct steps";
}
}
memcpy (ErrMsg, Errinfo.c_str (), errinfo.length ());
return errmsg;
}