📄 cfg.h
字号:
/************************************
REVISION LOG ENTRY
Revision By: Alex Turc
Revised on 6/15/00 1:53:25 PM
Comments: Configuration objects
************************************/
#ifndef __cfg_h__
#define __cfg_h__
#include <string>
#include <list>
#include <strstream>
#include <sstream>
using namespace std;
#include "mylib/_str.h"
#include "mylib/_exception.h"
using namespace extension;
/*
Configuration stream exception
*/
//##ModelId=3B79EDE600FE
class configuration_stream_exception :
public extended_exception
{
public:
// Function name : configuration_stream_exception
// Description : Exception raised when the configuration stream is invalid
// Return type :
// Argument : long nCode - exception code
// Argument : const char* pDescription = "Configuration stream exception" - description
// Argument : const char* pFile = "" - the file name where the exception has occured
// Argument : long nLine = -1 - the line number where the exception has occured
//##ModelId=3B79EDE60109
configuration_stream_exception( long nCode, const char* pDescription = "Configuration stream exception", const char* pFile = "", long nLine = -1 ) :
extended_exception( nCode, pDescription, pFile, nLine )
{
}
};
/*
Configuration structure for an tunnel
*/
//##ModelId=3B79EDE6014E
struct tunnel_cfg
{
//##ModelId=3B79EDE60159
int m_iSourcePort;
//##ModelId=3B79EDE6016D
string m_strProxyAddress;
//##ModelId=3B79EDE60176
int m_iProxyPort;
//##ModelId=3B79EDE6018B
string m_strDestinationAddress;
//##ModelId=3B79EDE6019E
int m_iDestinationPort;
// Function name : operator==
// Description : Compares two configuration
// Return type : bool
// Argument : const tunel_cfg& c
//##ModelId=3B79EDE601A8
bool operator==( const tunnel_cfg& c )
{
return m_iSourcePort == c.m_iSourcePort &&
m_strProxyAddress == c.m_strProxyAddress &&
m_iProxyPort == c.m_iProxyPort &&
m_strDestinationAddress == c.m_strDestinationAddress &&
m_iDestinationPort == c.m_iDestinationPort;
}
};
/*
Configuration structure
*/
//##ModelId=3B79EDE601C6
struct configuration
{
// List of tunnels configuration
//##ModelId=3B79EDE601F0
list< tunnel_cfg > m_lstTunnels;
};
// Function name : operator >>
// Description :
// Return type : inline istream&
// Argument : istream& is
// Argument : tunnel_cfg& otc
inline istream& operator >> ( istream& is, tunnel_cfg& t )
throw( configuration_stream_exception )
{
const int iBufferSize = 3 * 1024;
char pBuffer[iBufferSize];
is.getline( pBuffer, iBufferSize );
strstream ssTmp( pBuffer, iBufferSize );
ssTmp >> t.m_iSourcePort >> t.m_strDestinationAddress >> t.m_iDestinationPort;
if( ssTmp.fail() )
throw_exception( configuration_stream_exception, extended_exception::exception_code_generic, "Failed to read configuration for a tunnel" );
// Read proxy information
string strProxyAddress;
int iProxyPort;
ssTmp >> strProxyAddress >> iProxyPort;
if( !ssTmp.fail() )
{
t.m_strProxyAddress = strProxyAddress;
t.m_iProxyPort = iProxyPort;
}
return is;
};
// Function name : operator <<
// Description :
// Return type : inline ostream&
// Argument : ostream& os
// Argument : tunnel_cfg& otc
inline ostream& operator << ( ostream& os, tunnel_cfg& t )
{
os << t.m_iSourcePort << "\t\t" << t.m_strDestinationAddress << "\t\t\t" << t.m_iDestinationPort << "\t\t" << t.m_strProxyAddress << "\t\t\t" << t.m_iProxyPort << endl;
return os;
};
// Function name : operator >>
// Description :
// Return type : inline istream&
// Argument : istream& is
// Argument : configuration& c
inline istream& operator >> ( istream& is, configuration& c )
throw( configuration_stream_exception )
{
const int iBufferSize = 3 * 1024;
char pBuffer[iBufferSize];
while( true )
{
is.getline( pBuffer, iBufferSize );
if( is.fail() )
break;
string strLine = trim( pBuffer );
// Skip empty line
if( strLine.size() == 0 )
continue;
// Skip comment line
if( strLine[0] == '#' )
continue;
stringstream ssTmp( strLine );
// Read a configuration line
tunnel_cfg t;
ssTmp >> t;
c.m_lstTunnels.push_back( t );
}
return is;
};
// Function name : operator <<
// Description :
// Return type : inline ostream&
// Argument : ostream& os
// Argument : configuration& c
inline ostream& operator << ( ostream& os, configuration& c )
{
os << "# HTTP tunneling configuration file" << endl << endl;
list< tunnel_cfg >::iterator i = c.m_lstTunnels.begin();
while( i != c.m_lstTunnels.end() )
{
os << *i << endl;
i++;
}
return os;
};
#endif // __cfg_h__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -