Commit initial: GNUWorld servicii IRC cu README.md complet in limba romana - Analiza arhitecturala completa a proiectului - Documentatie ierarhie clase, bucla de evenimente, protocol P10 - Identificare probleme memorie si recomandari modernizare
This commit is contained in:
commit
fcb24f6de9
870 files changed
+311128
No files matched your search
@@ -0,0 +1,6 @@
|
||||
.deps
|
||||
.libs
|
||||
*.la
|
||||
*.lo
|
||||
Makefile
|
||||
.arch-ids
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Buffer.cc
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Buffer.cc,v 1.6 2003/07/20 14:13:15 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "Buffer.h"
|
||||
|
||||
const char rcsId[] = "$Id: Buffer.cc,v 1.6 2003/07/20 14:13:15 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::string ;
|
||||
|
||||
Buffer::Buffer( char delimiter )
|
||||
: delim( delimiter )
|
||||
{}
|
||||
|
||||
Buffer::~Buffer()
|
||||
{ /* No heap space allocated */ }
|
||||
|
||||
bool Buffer::ReadLine( string& retMe )
|
||||
{
|
||||
/* remove any leading new line characters */
|
||||
while( !empty() && ('\n' == buf[ 0 ]) )
|
||||
{
|
||||
/* just remove the first char */
|
||||
buf.erase( buf.begin() ) ;
|
||||
}
|
||||
|
||||
/* Check to see if there is anything left */
|
||||
if( empty() )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
/* find() returns the index of the character, or
|
||||
* npos if it was not found
|
||||
*/
|
||||
size_type pos = buf.find( delim ) ;
|
||||
|
||||
if( string::npos == pos )
|
||||
{
|
||||
// Unable to find the delimiter
|
||||
return false ;
|
||||
}
|
||||
|
||||
// else I need to add this substring into retMe
|
||||
// and remove it from buf
|
||||
retMe = buf.substr( 0, pos + 1 ) ;
|
||||
buf.erase( 0, pos + 1 ) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
// Delete numBytes bytes from the beginning
|
||||
// of the Buffer
|
||||
void Buffer::Delete( const size_type& numBytes )
|
||||
{
|
||||
if( numBytes >= size() )
|
||||
{
|
||||
// Clear the entire Buffer
|
||||
// buf.clear() ;
|
||||
buf.erase() ;
|
||||
return ;
|
||||
}
|
||||
|
||||
// Else just erase the number of bytes given
|
||||
buf.erase( 0, numBytes ) ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* Buffer.h
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
*
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Buffer.h,v 1.7 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __BUFFER_H
|
||||
#define __BUFFER_H "$Id: Buffer.h,v 1.7 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* This class represents a dynamic buffer capable or parsing out
|
||||
* (delimiter) delimited lines of text.
|
||||
* This is a very useful class for using as an input buffer because
|
||||
* of the ReadLine() method, and a useful class for ouput buffering
|
||||
* because it is unlimited in size and provides a Delete() method
|
||||
* to delete an arbitrary number of bytes.
|
||||
*/
|
||||
class Buffer
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* The type used to represent the string, could be useful
|
||||
* if this class is ever templated (again).
|
||||
*/
|
||||
typedef std::string stringType ;
|
||||
|
||||
/**
|
||||
* Initialize the Buffer with the given string and
|
||||
* delimiting character
|
||||
*/
|
||||
Buffer( char = '\n' ) ;
|
||||
|
||||
/**
|
||||
* Destroy this Buffer object. No internal allocation
|
||||
* is done, so this method is a no-op.
|
||||
*/
|
||||
virtual ~Buffer() ;
|
||||
|
||||
/**
|
||||
* Read a (delimiter) delimited token from the
|
||||
* buffer and write it into the string
|
||||
* passed as argument, erasing the line from
|
||||
* the Buffer once complete.
|
||||
*/
|
||||
virtual bool ReadLine( std::string& ) ;
|
||||
|
||||
/**
|
||||
* Define the type of the variable used to maintain
|
||||
* size (number of bytes) of Buffer.
|
||||
*/
|
||||
typedef std::string::size_type size_type ;
|
||||
|
||||
/**
|
||||
* Return the the number of characters currently in
|
||||
* the Buffer.
|
||||
*/
|
||||
virtual size_type size() const
|
||||
{ return buf.size() ; }
|
||||
|
||||
/**
|
||||
* Return the the number of characters currently in
|
||||
* the Buffer.
|
||||
*/
|
||||
virtual size_type length() const
|
||||
{ return buf.length() ; }
|
||||
|
||||
/**
|
||||
* Return true if the buffer is empty, false otherwise.
|
||||
*/
|
||||
virtual bool empty() const
|
||||
{ return buf.empty() ; }
|
||||
|
||||
/**
|
||||
* Clear all data from the Buffer.
|
||||
* For some reason, GNU gcc's implementation of std::string doesn't
|
||||
* include clear(), do it the old fashion way here.
|
||||
*/
|
||||
virtual void clear()
|
||||
{ buf.erase() ; }
|
||||
|
||||
/**
|
||||
* Remove the given number of bytes from the
|
||||
* beginning of the Buffer.
|
||||
*/
|
||||
virtual void Delete( const size_type& ) ;
|
||||
|
||||
/**
|
||||
* Return a substring of this Buffer, starting at (index),
|
||||
* and continuing (len) characters into the Buffer.
|
||||
* This method is const, and therefore does not alter the
|
||||
* Buffer.
|
||||
*/
|
||||
inline std::string substr( const size_type& index,
|
||||
const size_type& len ) const
|
||||
{ return buf.substr( index, len ) ; }
|
||||
|
||||
/**
|
||||
* Return a const reference to a C++ string
|
||||
* representation of this Buffer.
|
||||
*/
|
||||
inline const std::string& toString() const
|
||||
{ return buf ; }
|
||||
|
||||
/**
|
||||
* Return a const reference to a C++ string representation
|
||||
* of this Buffer.
|
||||
*/
|
||||
inline const std::string& operator*() const
|
||||
{ return buf ; }
|
||||
|
||||
/**
|
||||
* Return a pointer to the C NULL terminated character
|
||||
* array representation of this array. Note that the
|
||||
* data is const, and thus cannot be altered by the
|
||||
* pointer returned by this method.
|
||||
*/
|
||||
inline const char* c_str() const
|
||||
{ return buf.c_str() ; }
|
||||
|
||||
/**
|
||||
* Return a pointer to the character array representation
|
||||
* of this buffer.
|
||||
* Note that conforming to the C++ standard on std::string,
|
||||
* this data may NOT be null terminated.
|
||||
*/
|
||||
inline const char* data() const
|
||||
{ return buf.data() ; }
|
||||
|
||||
/**
|
||||
* Append a give number of bytes from appendMe to this
|
||||
* Buffer. This is useful for storing binary data in an
|
||||
* object of this class.
|
||||
*/
|
||||
inline void append( const char* appendMe, size_t numBytes )
|
||||
{ buf.append( appendMe, numBytes ) ; }
|
||||
|
||||
/**
|
||||
* Concatenate the given C++ string to the end of the
|
||||
* Buffer.
|
||||
*/
|
||||
inline Buffer& operator+=( const std::string& addMe )
|
||||
{ buf += addMe ; return *this ; }
|
||||
|
||||
/**
|
||||
* Concatenate the given Buffer onto the end of this
|
||||
* Buffer.
|
||||
*/
|
||||
inline Buffer& operator+=( const Buffer& addMe )
|
||||
{ buf += addMe.buf ; return *this ; }
|
||||
|
||||
/**
|
||||
* Assign to this Buffer the contents of the C++ string
|
||||
* passed as argument.
|
||||
*/
|
||||
inline Buffer& operator=( const std::string& replaceWithMe )
|
||||
{ buf = replaceWithMe ; return *this ; }
|
||||
|
||||
/**
|
||||
* Assign to this Buffer the contents of the Buffer passed
|
||||
* as argument.
|
||||
*/
|
||||
inline Buffer& operator=( const Buffer& replaceWithMe )
|
||||
{ buf = replaceWithMe.buf ; return *this ; }
|
||||
|
||||
/**
|
||||
* Debugging function for output of this Buffer to a C++
|
||||
* output stream.
|
||||
*/
|
||||
inline friend std::ostream& operator<<( std::ostream& o,
|
||||
const Buffer& b )
|
||||
{
|
||||
o << b.buf ;
|
||||
return o ;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* This is the internal representation of this Buffer.
|
||||
*/
|
||||
std::string buf ;
|
||||
|
||||
/**
|
||||
* This is the delimiting character.
|
||||
*/
|
||||
char delim ;
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif /* __BUFFER_H */
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Connection.cc
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Connection.cc,v 1.5 2004/01/07 18:33:42 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "Connection.h"
|
||||
#include "Buffer.h"
|
||||
|
||||
const char rcsId[] = "$Id: Connection.cc,v 1.5 2004/01/07 18:33:42 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
// Allocate these static variables in class Connection
|
||||
const Connection::flagType Connection::F_PENDING = 0x01 ;
|
||||
const Connection::flagType Connection::F_CONNECTED = 0x02 ;
|
||||
const Connection::flagType Connection::F_INCOMING = 0x04 ;
|
||||
const Connection::flagType Connection::F_LISTEN = 0x08 ;
|
||||
const Connection::flagType Connection::F_FILE = 0x10 ;
|
||||
const Connection::flagType Connection::F_FLUSH = 0x20 ;
|
||||
|
||||
using std::string ;
|
||||
|
||||
// Simply initialize the object
|
||||
Connection::Connection( const string& _hostname,
|
||||
const unsigned short int _remotePort,
|
||||
const char _delimiter )
|
||||
: hostname( _hostname ),
|
||||
localPort( 0 ),
|
||||
remotePort( _remotePort ),
|
||||
inputBuffer( _delimiter ),
|
||||
outputBuffer( _delimiter ),
|
||||
IP( string() ),
|
||||
sockFD( -1 ),
|
||||
flags( F_PENDING ),
|
||||
connectTime( 0 ),
|
||||
bytesRead( 0 ),
|
||||
bytesWritten( 0 )
|
||||
{
|
||||
memset( &addr, 0, sizeof( struct sockaddr_in ) ) ;
|
||||
}
|
||||
|
||||
Connection::Connection( const char _delimiter )
|
||||
: localPort( 0 ),
|
||||
remotePort( 0 ),
|
||||
inputBuffer( _delimiter ),
|
||||
outputBuffer( _delimiter ),
|
||||
sockFD( -1 ),
|
||||
flags( F_PENDING )
|
||||
{
|
||||
memset( &addr, 0, sizeof( struct sockaddr_in ) ) ;
|
||||
}
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
/* No work to be done, no heap space allocated */
|
||||
}
|
||||
|
||||
void Connection::Write( const string& writeMe )
|
||||
{
|
||||
outputBuffer += writeMe ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,491 @@
|
||||
/**
|
||||
* Connection.h
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Connection.h,v 1.7 2004/01/07 18:33:42 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __CONNECTION_H
|
||||
#define __CONNECTION_H "$Id: Connection.h,v 1.7 2004/01/07 18:33:42 dan_karrels Exp $"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h> // sockaddr
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "Buffer.h"
|
||||
#include "ELog.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/// Forward declaration of the manager class
|
||||
class ConnectionManager ;
|
||||
|
||||
/**
|
||||
* This class represents an individual connection to be used with
|
||||
* the ConnectionManager class.
|
||||
* It is basically just a data storage class.
|
||||
*/
|
||||
class Connection
|
||||
{
|
||||
|
||||
/// Allow the manager class to have access to this class's
|
||||
/// protected members
|
||||
friend class ConnectionManager ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* The destructor does nothing since this is a helper class
|
||||
* of ConnectionManager.
|
||||
*/
|
||||
virtual ~Connection() ;
|
||||
|
||||
/**
|
||||
* Return the hostname which this connection represents.
|
||||
* The hostname may be empty if it represents a listening
|
||||
* socket. Otherwise, the hostname will contain either
|
||||
* a valid hostname, or a string representation of the
|
||||
* Connection's IP address.
|
||||
*/
|
||||
inline const std::string& getHostname() const
|
||||
{ return hostname ; }
|
||||
|
||||
/**
|
||||
* Return the IP for this connection.
|
||||
* The IP may be empty if it represents a listening
|
||||
* socket.
|
||||
*/
|
||||
inline const std::string& getIP() const
|
||||
{ return IP ; }
|
||||
|
||||
/**
|
||||
* Return the local port number for this Connection.
|
||||
*/
|
||||
inline unsigned short int getLocalPort() const
|
||||
{ return localPort ; }
|
||||
|
||||
/**
|
||||
* Return the remote port number for this Connection.
|
||||
*/
|
||||
inline unsigned short int getRemotePort() const
|
||||
{ return remotePort ; }
|
||||
|
||||
/**
|
||||
* This type is used to represent flags of this connection,
|
||||
* both for this class and any subclasses.
|
||||
*/
|
||||
typedef unsigned int flagType ;
|
||||
|
||||
/**
|
||||
* This flag is true if the connection is currently fully
|
||||
* connected.
|
||||
*/
|
||||
static const flagType F_CONNECTED ;
|
||||
|
||||
/**
|
||||
* This flag is true if the connection is still pending,
|
||||
* not yet complete.
|
||||
*/
|
||||
static const flagType F_PENDING ;
|
||||
|
||||
/**
|
||||
* This flag is true if this Connection represents a
|
||||
* connection created by a remote host connecting to the
|
||||
* localhost.
|
||||
* The F_INCOMING flag exists throughout the life ot
|
||||
* the Connection.
|
||||
*/
|
||||
static const flagType F_INCOMING ;
|
||||
|
||||
/**
|
||||
* This flag is true if this Connection represents a
|
||||
* socket which is listening for incoming connections.
|
||||
* This flag exists for the life of this connection.
|
||||
* A listening Connection is never connected.
|
||||
*/
|
||||
static const flagType F_LISTEN ;
|
||||
|
||||
/**
|
||||
* This flag is true when the Connection represents a
|
||||
* connection to a file, rather than a network connection.
|
||||
*/
|
||||
static const flagType F_FILE ;
|
||||
|
||||
/**
|
||||
* This flag will be set if the next write is to flush all
|
||||
* data from the output buffer.
|
||||
* The flag will be reset after the send().
|
||||
*/
|
||||
static const flagType F_FLUSH ;
|
||||
|
||||
/**
|
||||
* Append a string to the Connection's output buffer.
|
||||
*/
|
||||
virtual void Write( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Return the flags of this connection.
|
||||
*/
|
||||
inline const flagType& getFlags() const
|
||||
{ return flags ; }
|
||||
|
||||
/**
|
||||
* Check if an individual flag is present on this connection.
|
||||
*/
|
||||
inline bool hasFlag( const flagType& whichFlag ) const
|
||||
{ return (whichFlag == (flags & whichFlag)) ; }
|
||||
|
||||
/**
|
||||
* Return true if this is object represents a fully-connected
|
||||
* socket, false otherwise.
|
||||
* Note that this flag is mutually exclusive with F_PENDING.
|
||||
*/
|
||||
inline bool isConnected() const
|
||||
{ return (flags & F_CONNECTED) ; }
|
||||
|
||||
/**
|
||||
* Return true if this connection is still pending.
|
||||
* Note that this flag is mutually exclusive with F_CONNECTED.
|
||||
*/
|
||||
inline bool isPending() const
|
||||
{ return (flags & F_PENDING) ; }
|
||||
|
||||
/**
|
||||
* Return true if this connection was received by a listening
|
||||
* Connection.
|
||||
* Note that this flag remains true for the life of the
|
||||
* connection, and is not present for connection established
|
||||
* with ConnectionManager::Connect().
|
||||
*/
|
||||
inline bool isIncoming() const
|
||||
{ return (flags & F_INCOMING) ; }
|
||||
|
||||
/**
|
||||
* Return true if this connection is listening for new
|
||||
* incoming connections, as a result of calling
|
||||
* ConnectionManager::Listen().
|
||||
* Note that this flag remains true for the life of the
|
||||
* connection.
|
||||
*/
|
||||
inline bool isListening() const
|
||||
{ return (flags & F_LISTEN ) ; }
|
||||
|
||||
/**
|
||||
* Return true if this Connection is a file connection, false
|
||||
* otherwise.
|
||||
*/
|
||||
inline bool isFile() const
|
||||
{ return (flags & F_FILE) ; }
|
||||
|
||||
/**
|
||||
* Return true if the flush flag is set, indicating that the
|
||||
* next send() should write all available data, blocking
|
||||
* if necessary.
|
||||
*/
|
||||
inline bool isFlush() const
|
||||
{ return (flags & F_FLUSH) ; }
|
||||
|
||||
/**
|
||||
* Return the total number of bytes read from this
|
||||
* Connection.
|
||||
*/
|
||||
inline size_t getBytesRead() const
|
||||
{ return bytesRead ; }
|
||||
|
||||
/**
|
||||
* Return the total number of bytes successfully written
|
||||
* to this Connection (does not count amount currently
|
||||
* in the output buffer).
|
||||
*/
|
||||
inline size_t getBytesWritten() const
|
||||
{ return bytesWritten ; }
|
||||
|
||||
/**
|
||||
* Return the time at which this Connection object established
|
||||
* connection. This value is 0 for listening sockets.
|
||||
*/
|
||||
inline time_t getConnectTime() const
|
||||
{ return connectTime ; }
|
||||
|
||||
/**
|
||||
* Return the number of bytes currently in the output
|
||||
* buffer.
|
||||
*/
|
||||
inline size_t getOutputBufferSize() const
|
||||
{ return outputBuffer.size() ; }
|
||||
|
||||
/**
|
||||
* Return the number of bytes currently in the input
|
||||
* buffer.
|
||||
*/
|
||||
inline size_t getInputBufferSize() const
|
||||
{ return inputBuffer.size() ; }
|
||||
|
||||
/**
|
||||
* Set the F_FLUSH flag to true.
|
||||
*/
|
||||
inline void Flush()
|
||||
{ setFlag( F_FLUSH ) ; }
|
||||
|
||||
/**
|
||||
* This friend operator allows for the easy output of a
|
||||
* Connection object to a given output stream.
|
||||
*/
|
||||
friend std::ostream& operator<<( std::ostream& out, const
|
||||
Connection& con )
|
||||
{
|
||||
out << "Host: " << con.getHostname()
|
||||
<< ", IP: " << con.getIP()
|
||||
<< ", localPort: " << con.getLocalPort()
|
||||
<< ", remotePort: " << con.getRemotePort()
|
||||
<< ", sockFD: " << con.getSockFD()
|
||||
<< ", state: "
|
||||
<< (con.isConnected() ? "connected" : "pending") ;
|
||||
if( con.isIncoming() )
|
||||
{
|
||||
out << ",incoming" ;
|
||||
}
|
||||
if( con.isListening() )
|
||||
{
|
||||
out << ",listening" ;
|
||||
}
|
||||
return out ;
|
||||
}
|
||||
|
||||
/**
|
||||
* This friend operator allows for the easy output of a
|
||||
* Connection object to a given ELog output stream.
|
||||
*/
|
||||
friend ELog& operator<<( ELog& out, const Connection& con )
|
||||
{
|
||||
out << "Host: " << con.getHostname()
|
||||
<< ", IP: " << con.getIP()
|
||||
<< ", localPort: " << con.getLocalPort()
|
||||
<< ", remotePort: " << con.getRemotePort()
|
||||
<< ", sockFD: " << con.getSockFD()
|
||||
<< ", state: "
|
||||
<< (con.isConnected() ? "connected" : "pending") ;
|
||||
if( con.isIncoming() )
|
||||
{
|
||||
out << ",incoming" ;
|
||||
}
|
||||
if( con.isListening() )
|
||||
{
|
||||
out << ",listening" ;
|
||||
}
|
||||
return out ;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Create a new instance of this class given the remote
|
||||
* host (may be empty), the IP, remote port number.
|
||||
*/
|
||||
Connection( const std::string& host,
|
||||
const unsigned short int remotePort,
|
||||
const char delimiter ) ;
|
||||
|
||||
/**
|
||||
* Create a new instance of this class, set all variables
|
||||
* to default initial state.
|
||||
*/
|
||||
Connection( const char delimiter ) ;
|
||||
|
||||
/**
|
||||
* Set an arbitrary flag for this connection
|
||||
*/
|
||||
inline void setFlag( const flagType& whichFlag )
|
||||
{ flags |= whichFlag ; }
|
||||
|
||||
/**
|
||||
* Remove an arbitrary flag for this connection
|
||||
*/
|
||||
inline void removeFlag( const flagType& whichFlag )
|
||||
{ flags &= ~whichFlag ; }
|
||||
|
||||
/**
|
||||
* Set this Connection to fully connected state, which also
|
||||
* removes the PENDING state
|
||||
*/
|
||||
inline void setConnected()
|
||||
{ removeFlag( F_PENDING ) ; setFlag( F_CONNECTED ) ; }
|
||||
|
||||
/**
|
||||
* Set this Connection to the pending state, and remove
|
||||
* the CONNECTED state
|
||||
*/
|
||||
inline void setPending()
|
||||
{ removeFlag( F_CONNECTED ) ; setFlag( F_PENDING ) ; }
|
||||
|
||||
/**
|
||||
* Mark that this Connection as an incoming connection
|
||||
*/
|
||||
inline void setIncoming()
|
||||
{ setFlag( F_INCOMING ) ; }
|
||||
|
||||
/**
|
||||
* Mark that this Connection is a listening for connections
|
||||
*/
|
||||
inline void setListen()
|
||||
{ setFlag( F_LISTEN ) ; }
|
||||
|
||||
/**
|
||||
* Mark that this Connection represents a file
|
||||
*/
|
||||
inline void setFile()
|
||||
{ setFlag( F_FILE ) ; }
|
||||
|
||||
/**
|
||||
* Set this connection's local port number
|
||||
*/
|
||||
inline void setLocalPort(
|
||||
const unsigned short int newLocalPort )
|
||||
{ localPort = newLocalPort ; }
|
||||
|
||||
/**
|
||||
* Set this connection's remote port number
|
||||
*/
|
||||
inline void setRemotePort(
|
||||
const unsigned short int newRemotePort )
|
||||
{ remotePort = newRemotePort ; }
|
||||
|
||||
/**
|
||||
* Return the socket (file) descriptor for this connection
|
||||
*/
|
||||
inline int getSockFD() const
|
||||
{ return sockFD ; }
|
||||
|
||||
/**
|
||||
* Return a pointer to the socket address structure
|
||||
*/
|
||||
inline struct sockaddr_in* getAddr()
|
||||
{ return &addr ; }
|
||||
|
||||
/**
|
||||
* Set the Connection's IP to the new IP
|
||||
*/
|
||||
inline void setIP( const std::string& newIP )
|
||||
{ IP = newIP ; }
|
||||
|
||||
/**
|
||||
* Set the Connection's hostname to newHost
|
||||
*/
|
||||
inline void setHostname( const std::string& newHost )
|
||||
{ hostname = newHost ; }
|
||||
|
||||
/**
|
||||
* Set the Connection's FD to the new FD
|
||||
*/
|
||||
inline void setSockFD( int newSockFD )
|
||||
{ sockFD = newSockFD ; }
|
||||
|
||||
/**
|
||||
* Set the Connection absTimeout to the new absTimeOut
|
||||
*/
|
||||
inline void setAbsTimeout( const time_t newAbsTimeout )
|
||||
{ absTimeout = newAbsTimeout ; }
|
||||
|
||||
/**
|
||||
* Return the time at which this connection attempt will
|
||||
* be terminated (its absolute timeout value).
|
||||
*/
|
||||
inline time_t getAbsTimeout() const
|
||||
{ return absTimeout ; }
|
||||
|
||||
/**
|
||||
* The remote hostname of this connection
|
||||
* This variable is empty() if this Connection is a listener
|
||||
*/
|
||||
std::string hostname ;
|
||||
|
||||
/**
|
||||
* The local port number of this connection
|
||||
*/
|
||||
unsigned short int localPort ;
|
||||
|
||||
/**
|
||||
* The remote port number of this connection
|
||||
*/
|
||||
unsigned short int remotePort ;
|
||||
|
||||
/**
|
||||
* The input buffer for this connection
|
||||
*/
|
||||
Buffer inputBuffer ;
|
||||
|
||||
/**
|
||||
* The output buffer for this connection
|
||||
*/
|
||||
Buffer outputBuffer ;
|
||||
|
||||
/**
|
||||
* The remote IP of this connection
|
||||
* This variable is empty() if this Connection is a listener
|
||||
*/
|
||||
std::string IP ;
|
||||
|
||||
/**
|
||||
* The socket (file) descriptor for the socket of this
|
||||
* connection.
|
||||
*/
|
||||
int sockFD ;
|
||||
|
||||
/**
|
||||
* The flags associated with this connection
|
||||
*/
|
||||
flagType flags ;
|
||||
|
||||
/**
|
||||
* The socket address structure for this connection
|
||||
*/
|
||||
struct sockaddr_in addr ;
|
||||
|
||||
/**
|
||||
* The time at which this connection attempt began
|
||||
*/
|
||||
time_t absTimeout ;
|
||||
|
||||
/**
|
||||
* The time at which this Connection completed connection,
|
||||
* 0 for listening socket.
|
||||
*/
|
||||
time_t connectTime ;
|
||||
|
||||
/**
|
||||
* The total number of bytes read
|
||||
*/
|
||||
size_t bytesRead ;
|
||||
|
||||
/**
|
||||
* The total number of bytes written
|
||||
*/
|
||||
size_t bytesWritten ;
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __CONNECTION_H
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* ConnectionHandler.cc
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: ConnectionHandler.cc,v 1.3 2003/08/05 01:59:14 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "ConnectionHandler.h"
|
||||
#include "Connection.h"
|
||||
|
||||
const char rcsId[] = "$Id: ConnectionHandler.cc,v 1.3 2003/08/05 01:59:14 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::string ;
|
||||
using std::cout ;
|
||||
using std::endl ;
|
||||
|
||||
/**
|
||||
* These methods are empty.
|
||||
* Simply output a little debugging message if uncommented.
|
||||
*/
|
||||
|
||||
ConnectionHandler::ConnectionHandler()
|
||||
{}
|
||||
|
||||
ConnectionHandler::~ConnectionHandler()
|
||||
{}
|
||||
|
||||
void ConnectionHandler::OnConnect( Connection* cPtr )
|
||||
{
|
||||
cout << "ConnectionHandler::OnConnect> "
|
||||
<< *cPtr
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
void ConnectionHandler::OnConnectFail( Connection* cPtr )
|
||||
{
|
||||
cout << "ConnectionHandler::OnConnectFail> "
|
||||
<< *cPtr
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
void ConnectionHandler::OnRead( Connection*, const string& )
|
||||
{
|
||||
//cout << "ConnectionHandler::OnRead> "
|
||||
// << *cPtr
|
||||
// << ", line: "
|
||||
// << line
|
||||
// << endl ;
|
||||
}
|
||||
|
||||
void ConnectionHandler::OnDisconnect( Connection* cPtr )
|
||||
{
|
||||
cout << "ConnectionHandler::OnDisconnect> "
|
||||
<< *cPtr
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
void ConnectionHandler::OnTimeout( Connection* cPtr )
|
||||
{
|
||||
cout << "ConnectionHandler::OnTimeout> "
|
||||
<< *cPtr
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* ConnectionHandler.h
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: ConnectionHandler.h,v 1.3 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __CONNECTIONHANDLER_H
|
||||
#define __CONNECTIONHANDLER_H "$Id: ConnectionHandler.h,v 1.3 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "Connection.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
class ConnectionManager ;
|
||||
|
||||
/**
|
||||
* The purpose of this class is to provide a concrete base class
|
||||
* for interfacing to a ConnectionManager instance. This class
|
||||
* is designed to receive events on Connections for which the
|
||||
* ConnectionHandler is responsible.
|
||||
* Clients should NOT call any of these methods directly.
|
||||
* All state information of ConnectionManager is updated internally.
|
||||
* These methods are information for the Clients of the ConnectionManager
|
||||
* class, and are meant to be one-way.
|
||||
*/
|
||||
class ConnectionHandler
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create a ConnectionHandler instance with default
|
||||
* arguments.
|
||||
*/
|
||||
ConnectionHandler() ;
|
||||
|
||||
/**
|
||||
* Destroy this ConnectionHandler instance.
|
||||
* This does *not* destroy any associated Connections
|
||||
* which may be in any associated ConnectionManager
|
||||
* objects.
|
||||
*/
|
||||
virtual ~ConnectionHandler() ;
|
||||
|
||||
/**
|
||||
* This method is called when a host connection succeeds.
|
||||
* This method is NOOP in the base class, and should NOT
|
||||
* be called by clients of this class.
|
||||
*/
|
||||
virtual void OnConnect( Connection* ) ;
|
||||
|
||||
/**
|
||||
* This method is called when the given connection attempt
|
||||
* fails. This could be for either an incoming or outgoing
|
||||
* attempt -- see the Connection's flags for determining
|
||||
* which it is.
|
||||
* The given Connection is no longer valid when this method
|
||||
* is called.
|
||||
*/
|
||||
virtual void OnConnectFail( Connection* ) ;
|
||||
|
||||
/**
|
||||
* This method is called when a string of data is available
|
||||
* from the remote connection referred to by the Connection.
|
||||
* The string of data (line) is passed as determined by
|
||||
* the delimiter passed to the constructor of the associated
|
||||
* ConnectionManager instance.
|
||||
*/
|
||||
virtual void OnRead( Connection*, const std::string& ) ;
|
||||
|
||||
/**
|
||||
* This is a handler method called when a connection is
|
||||
* established but is then closed by the remote end. Note
|
||||
* that connections terminated with Disconnect() are NOT
|
||||
* reflected by calling OnDisconnect(). Connections closed
|
||||
* using Disconnect() are closed without notification to
|
||||
* the handler.
|
||||
* This method is also called when a listening (incoming)
|
||||
* socket is no longer valid. If this occurs, the Connection
|
||||
* flags will have F_INCOMING set, and that further listening
|
||||
* on the given port will no longer proceed.
|
||||
* The given Connection is no longer valid when this method
|
||||
* is called.
|
||||
*/
|
||||
virtual void OnDisconnect( Connection* ) ;
|
||||
|
||||
/**
|
||||
* This method is called if a connection timeout occurs.
|
||||
* The given Connection is no longer valid when this method
|
||||
* is called.
|
||||
*/
|
||||
virtual void OnTimeout( Connection* ) ;
|
||||
|
||||
protected:
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __CONNECTIONHANDLER_H
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,428 @@
|
||||
/**
|
||||
* ConnectionManager.h
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: ConnectionManager.h,v 1.8 2005/01/12 04:36:43 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __CONNECTIONMANAGER_H
|
||||
#define __CONNECTIONMANAGER_H "$Id: ConnectionManager.h,v 1.8 2005/01/12 04:36:43 dan_karrels Exp $"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include "Connection.h"
|
||||
#include "ConnectionHandler.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* The purpose of this class it to manage multiple incoming and
|
||||
* outgoing connections on behalf of ConnectionHandler's.
|
||||
* This class is intended for systems which support multiple client
|
||||
* classes which might require one or more incoming and outgoing
|
||||
* connections, but may be used to manage a single connection
|
||||
* equally as well. Any client that wishes to create a connection
|
||||
* should subclass ConnectionHandler, and overload the method(s)
|
||||
* in which it is interested.
|
||||
* This class will support all connections for any number of
|
||||
* different ConnectionHandlers (note that a pointer to the
|
||||
* ConnectionHandler is passed as the first argument to many
|
||||
* methods). Each Connection is associated with a particular
|
||||
* ConnectionHandler, and no other ConnectionHandlers may view
|
||||
* or modify Connections which it does not own.
|
||||
* Any opening/closing of Connections is performed within this
|
||||
* class, so it is not necessary to perform any direct maintenance
|
||||
* of a particular Connection. Simply remember to call the
|
||||
* appropriate methods based on different situations (Disconnect(),
|
||||
* for example), and the ConnectionManager will update the state
|
||||
* of those Connections.
|
||||
*/
|
||||
class ConnectionManager
|
||||
{
|
||||
|
||||
/**
|
||||
* This type is used to store Connection's in a weak sorted
|
||||
* manner.
|
||||
* There is a single instance of a connectionMapType for
|
||||
* each handler which has one or more Connection's registered.
|
||||
*/
|
||||
typedef std::set< Connection* > connectionMapType ;
|
||||
|
||||
/**
|
||||
* This type is used as convenience to define an iterator
|
||||
* type for the connectionMap.
|
||||
*/
|
||||
typedef connectionMapType::iterator connectionMapIterator ;
|
||||
|
||||
/**
|
||||
* A const iterator used to iterate through a connectionMap.
|
||||
*/
|
||||
typedef connectionMapType::const_iterator constConnectionMapIterator ;
|
||||
|
||||
/**
|
||||
* This map stores connectionMapType's, keyed by the handler
|
||||
* which registered the connection(s).
|
||||
*/
|
||||
typedef std::map< ConnectionHandler*, connectionMapType >
|
||||
handlerMapType ;
|
||||
|
||||
/**
|
||||
* This type is used as convenience to define an iterator
|
||||
* type for the handlerMap.
|
||||
*/
|
||||
typedef handlerMapType::iterator handlerMapIterator ;
|
||||
|
||||
/**
|
||||
* A const iterator used to iterate through the handlerMap.
|
||||
*/
|
||||
typedef handlerMapType::const_iterator constHandlerMapIterator ;
|
||||
|
||||
/**
|
||||
* The type is used to store Connection objects to be erased.
|
||||
* This structure stores an iterator to each Connection
|
||||
* to be removed. Removal is synchronous to prevent
|
||||
* iterator invalidation of the handlerMap.
|
||||
* Because more than one item may be associated with any
|
||||
* given key, we need a multimap here. While that may
|
||||
* be a useful trait for handling more than one Connection
|
||||
* for any given ConnectionHandler, it also has the unfortunate
|
||||
* side effect of allowing a Connection to be scheduled for
|
||||
* closure more than once. The scheduleErasure() method is
|
||||
* used to prevent multiple entries of the same Connection
|
||||
* into the eraseMap.
|
||||
*/
|
||||
typedef std::multimap< ConnectionHandler*, connectionMapType::iterator >
|
||||
eraseMapType ;
|
||||
|
||||
/**
|
||||
* This type is used as convenience to define an iterator
|
||||
* type for the eraseMap.
|
||||
*/
|
||||
typedef eraseMapType::iterator eraseMapIterator ;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructor. The first argument which (may) be
|
||||
* supplied is the default timeout value for establishing
|
||||
* individual connections (in seconds). If the (outgoing)
|
||||
* connection is not established within that time period,
|
||||
* then the connection's handler is notified via OnTimeout(),
|
||||
* and the Connection scheduled for erasure. Note that
|
||||
* listening Connections do not have a timeout.
|
||||
* Delimiter is the line delimiter for determining when a
|
||||
* full line/command has been read from each connection.
|
||||
* Once this delimiter is encountered, the line/command is
|
||||
* passed to OnRead().
|
||||
*/
|
||||
ConnectionManager( const time_t defaultTimeout = 10,
|
||||
const char defaultDelimiter = '\n' ) ;
|
||||
|
||||
/**
|
||||
* The destructor will deallocate any memory and close all
|
||||
* connections.
|
||||
* Handlers for the given connections will *not* be
|
||||
* notified.
|
||||
*/
|
||||
virtual ~ConnectionManager() ;
|
||||
|
||||
/**
|
||||
* This method will set the timeout duraction for connection
|
||||
* attempts to the new value given to the method.
|
||||
*/
|
||||
void setTimeoutDuration( const time_t newTimeoutDuration )
|
||||
{ timeoutDuration = newTimeoutDuration ; }
|
||||
|
||||
/**
|
||||
* Return a string with (host)'s IP address in numbers
|
||||
* and dots format.
|
||||
* Returns an empty string if no IP address found.
|
||||
*/
|
||||
static std::string ipAddrOf( const std::string& host ) ;
|
||||
|
||||
/**
|
||||
* Return true if the given string is in the proper
|
||||
* IP format of numbers and dots.
|
||||
*/
|
||||
static bool isIpAddress( const std::string& host ) ;
|
||||
|
||||
/**
|
||||
* Connect() will attempt to establish a new connection to
|
||||
* the given host, on the given port. The (host) field may
|
||||
* be the canonical host name, or the IP in the form of
|
||||
* numbres and dots.
|
||||
* This method creates a non-blocking socket with which to
|
||||
* connect to the remote host, thus a return value of non-NULL
|
||||
* does not necessarily mean that the connection is valid,
|
||||
* just that the attempt is in progress.
|
||||
* A return value of NULL indicates that some part of the
|
||||
* connection process failed, and errno is set appropriately.
|
||||
* UDP sockets are currently not implemented fully, and will
|
||||
* not work properly.
|
||||
* The ConnectionHandler must be non-NULL.
|
||||
*/
|
||||
virtual Connection* Connect(
|
||||
ConnectionHandler*,
|
||||
const std::string& host,
|
||||
const unsigned short int remotePort ) ;
|
||||
|
||||
/**
|
||||
* Connect to a file instead of a network host. This method
|
||||
* has the same semantics as Connect(), except that OnConnect()
|
||||
* may be called during the call to ConnectToFile().
|
||||
*/
|
||||
virtual Connection* ConnectToFile( ConnectionHandler*,
|
||||
const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Attempt to establish a listening Connection on the
|
||||
* given port number. If successful, the Connection is
|
||||
* returned. The Connection returned is of no functional
|
||||
* use except to indicate success or failure. That
|
||||
* Connection is the actual listening socket, and cannot
|
||||
* be written to or read from directly.
|
||||
* The ConnectionHandler must be non-NULL.
|
||||
*/
|
||||
virtual Connection* Listen(
|
||||
ConnectionHandler*,
|
||||
const unsigned short int localPort ) ;
|
||||
|
||||
/**
|
||||
* DisConnect() forces the connection(s) associated with
|
||||
* hostname/ports to be disconnected and deallocated.
|
||||
* By specifying the optional localPort, only outgoing
|
||||
* connections which match all 3 search criteria will be
|
||||
* removed; not specifying a third parameter will cause all
|
||||
* connections which match the first two search criteria to
|
||||
* be removed.
|
||||
* OnDisconnect() is NOT called.
|
||||
* The ConnectionHandler must be non-NULL.
|
||||
* The Connection will not be removed if the ConnectionHandler
|
||||
* does not own the particular Connection.
|
||||
* This method schedules the given Connection to be disconnected
|
||||
* in the next call to Poll().
|
||||
* To close a listening Connection, pass an empty hostname, in
|
||||
* which case the remotePort will be ignored.
|
||||
*/
|
||||
virtual bool DisconnectByHost( ConnectionHandler*,
|
||||
const std::string& hostname,
|
||||
const unsigned short int remotePort,
|
||||
const unsigned short int localPort = 0 ) ;
|
||||
|
||||
/**
|
||||
* DisConnect() forces the connection(s) associated with
|
||||
* IP/ports to be disconnected and deallocated.
|
||||
* By specifying the optional localPort, only outgoing
|
||||
* connections which match all 3 search criteria will be
|
||||
* removed; not specifying a third parameter will cause all
|
||||
* connections which match the first two search criteria to
|
||||
* be removed.
|
||||
* OnDisconnect() is NOT called.
|
||||
* The ConnectionHandler must be non-NULL.
|
||||
* The Connection will not be removed if the ConnectionHandler
|
||||
* does not own the particular Connection.
|
||||
* This method schedules the given Connection to be disconnected
|
||||
* in the next call to Poll().
|
||||
* To close a listening Connection, pass an empty hostname, in
|
||||
* which case the remotePort will be ignored.
|
||||
*/
|
||||
virtual bool DisconnectByIP( ConnectionHandler*,
|
||||
const std::string& IP,
|
||||
const unsigned short int remotePort,
|
||||
const unsigned short int localPort = 0 ) ;
|
||||
|
||||
/**
|
||||
* Disconnect the given Connection from ConnectionHandler's
|
||||
* list of Connections.
|
||||
* The Connection will not be removed if the ConnectionHandler
|
||||
* does not own the particular Connection.
|
||||
* Return true if removal was successful, false if the
|
||||
* Connection was not found under ConnectionHandler's control.
|
||||
* Both the ConnectionHandler and the Connection must
|
||||
* be non-NULL.
|
||||
* This method schedules the given Connection to be disconnected
|
||||
* in the next call to Poll().
|
||||
* Passing NULL as the Connection* will remove ALL connections
|
||||
* belonging to the ConnectionHandler.
|
||||
*/
|
||||
virtual bool Disconnect( ConnectionHandler*,
|
||||
Connection* ) ;
|
||||
|
||||
/**
|
||||
* Return the number of outstanding connections for the given
|
||||
* ConnectionHandler.
|
||||
* If anyone figures out why gcc won't let me declare
|
||||
* the ConnectionHandler* as const here please let me know.
|
||||
*/
|
||||
virtual size_t numConnections( ConnectionHandler* ) const ;
|
||||
|
||||
/**
|
||||
* This method performs the actual read/write calls for all
|
||||
* sockets. This method will check for any
|
||||
* pending outgoing connections (and call OnConnect() for
|
||||
* all that succeed), check for read state on all sockets
|
||||
* which are currently connected (and call OnRead() if conditions
|
||||
* are met), check for dead connections (and call OnDisconnect()
|
||||
* for each), and check for connection time outs (and call
|
||||
* OnTimeout() for each).
|
||||
* (seconds) and (milliseconds) is how long to wait for a
|
||||
* state change on any of the Connections, in seconds and
|
||||
* milliseconds, respectively. The default is to return
|
||||
* immediately (poll).
|
||||
* If -1 is passed for seconds, then the process will block
|
||||
* indefinitely waiting for a state change.
|
||||
*/
|
||||
virtual void Poll( const long seconds = 0,
|
||||
const long milliseconds = 0 ) ;
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* The duration to wait for outgoing connections to be
|
||||
* established.
|
||||
*/
|
||||
time_t timeoutDuration ;
|
||||
|
||||
/**
|
||||
* The line delimiter, 0 if none
|
||||
*/
|
||||
char delimiter ;
|
||||
|
||||
/**
|
||||
* Stores connectionMap's, one for each handler (key)
|
||||
*/
|
||||
handlerMapType handlerMap ;
|
||||
|
||||
/**
|
||||
* Allow for asynchronous calls to Disconnect()
|
||||
* This structure contains Connections to be removed from
|
||||
* the connection tables.
|
||||
*/
|
||||
eraseMapType eraseMap ;
|
||||
|
||||
/**
|
||||
* The size of the scratch buffer
|
||||
*/
|
||||
size_t inputBufferSize ;
|
||||
|
||||
/**
|
||||
* Scratch buffer for reads
|
||||
*/
|
||||
char* inputBuffer ;
|
||||
|
||||
/**
|
||||
* Open a socket.
|
||||
* Return -1 on error
|
||||
*/
|
||||
virtual int openSocket() ;
|
||||
|
||||
/**
|
||||
* Close a socket
|
||||
*/
|
||||
virtual void closeSocket( int ) ;
|
||||
|
||||
/**
|
||||
* Set the options for a socket
|
||||
* Return true is all options were set properly, false
|
||||
* otherwise.
|
||||
*/
|
||||
virtual bool setSocketOptions( int ) ;
|
||||
|
||||
/**
|
||||
* Attempt to read()/recv() from the given Connection.
|
||||
* Post event(s) OnRead() or OnDisconnect().
|
||||
* Return true if the read was successful, false if
|
||||
* connection is no longer valid.
|
||||
*/
|
||||
virtual bool handleRead( ConnectionHandler*, Connection* ) ;
|
||||
|
||||
/**
|
||||
* Attempt to write()/send() to the given Connection.
|
||||
* Post event OnDisconnect() if necessary.
|
||||
* Return true if the write was successful, false if
|
||||
* the connection is no longer valid.
|
||||
*/
|
||||
virtual bool handleWrite( ConnectionHandler*, Connection* ) ;
|
||||
|
||||
/**
|
||||
* Attempt to flush all data from the given Connection.
|
||||
* Reset the F_FLUSH flag.
|
||||
* Post event OnDisconnect() if necessary.
|
||||
* Return true if the write(s) was successful, false if
|
||||
* the connection is no longer valid.
|
||||
*/
|
||||
virtual bool handleFlush( ConnectionHandler*, Connection* ) ;
|
||||
|
||||
/**
|
||||
* Attempt to complete the connection to the given Connection.
|
||||
* Post event(s) OnConnect() or OnConnectFail().
|
||||
* Return true if connect was successful, false if Connection
|
||||
* is invalid.
|
||||
*/
|
||||
virtual bool finishConnect( ConnectionHandler*, Connection* ) ;
|
||||
|
||||
/**
|
||||
* Attempt to complete an incoming connection creation.
|
||||
* The ConnectionHandler will be notified either by a call
|
||||
* to OnConnect() or OnConnectFail(), depending if the incoming
|
||||
* connection attempt was successful or unsuccessful,
|
||||
* respectively.
|
||||
* Return true if connection attempt was successful, false
|
||||
* otherwise.
|
||||
*/
|
||||
virtual bool finishAccept( ConnectionHandler*, Connection* ) ;
|
||||
|
||||
/**
|
||||
* Because the eraseMap is a multimap, it is possible to insert
|
||||
* more than one value for each key. This is useful for
|
||||
* removing more than a single connection for any single handler,
|
||||
* but it also permits the insertion of the same connection
|
||||
* more than once. This would create a problem when closing
|
||||
* connections, because the second (and third, fourth, etc)
|
||||
* instance of a particular iterator would be invalidated
|
||||
* once the iterator is removed once -- this would cause
|
||||
* a process crash.
|
||||
* This method performs a simple check to see if the connection
|
||||
* iterator is already present in the eraseMap. If so, just
|
||||
* return because the connection is already scheduled to be
|
||||
* removed, and this will occur in Poll(). Otherwise, add
|
||||
* the connection to be erased to the eraseMap.
|
||||
*/
|
||||
virtual void scheduleErasure( ConnectionHandler*,
|
||||
connectionMapType::iterator ) ;
|
||||
|
||||
/**
|
||||
* Call Disconnect() for each Connection owned by the
|
||||
* given ConnectionHandler.
|
||||
*/
|
||||
virtual bool disconnectAll( ConnectionHandler* ) ;
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __CONNECTIONMANAGER_H
|
||||
@@ -0,0 +1,472 @@
|
||||
/**
|
||||
* EConfig.cc
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: EConfig.cc,v 1.9 2005/10/01 13:13:55 kewlio Exp $
|
||||
*/
|
||||
|
||||
#include <unistd.h> // unlink()
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio> // rename()
|
||||
#include <cstring> // strerror()
|
||||
#include <cerrno>
|
||||
|
||||
#include "EConfig.h"
|
||||
#include "StringTokenizer.h"
|
||||
#include "ELog.h"
|
||||
#include "misc.h"
|
||||
|
||||
const char rcsId[] = "$Id: EConfig.cc,v 1.9 2005/10/01 13:13:55 kewlio Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::ifstream ;
|
||||
using std::ofstream ;
|
||||
using std::string ;
|
||||
using std::endl ;
|
||||
using std::map ;
|
||||
|
||||
EConfig::EConfig( const string& _configFileName )
|
||||
: configFileName( _configFileName ),
|
||||
error( false )
|
||||
{
|
||||
//elog << "EConfig> Opening configFile: "
|
||||
// << configFileName
|
||||
// << ", hasError(): "
|
||||
// << hasError()
|
||||
// << endl ;
|
||||
|
||||
ifstream configFile( configFileName.c_str() ) ;
|
||||
if( !configFile.is_open() )
|
||||
{
|
||||
elog << "EConfig> Unable to open file: "
|
||||
<< configFileName
|
||||
<< endl ;
|
||||
setError() ;
|
||||
}
|
||||
//else
|
||||
// {
|
||||
// elog << "EConfig> Successfully opened"
|
||||
// << endl ;
|
||||
// }
|
||||
|
||||
if( !readFile( configFile ) )
|
||||
{
|
||||
setError() ;
|
||||
}
|
||||
//else
|
||||
// {
|
||||
// elog << "EConfig> Successfully parsed"
|
||||
// << endl ;
|
||||
// }
|
||||
configFile.close() ;
|
||||
}
|
||||
|
||||
EConfig::~EConfig()
|
||||
{
|
||||
// No heap space allocated
|
||||
valueMap.clear() ;
|
||||
lineList.clear() ;
|
||||
}
|
||||
|
||||
EConfig::iterator EConfig::Find( const string& findMe )
|
||||
{
|
||||
return valueMap.find( findMe ) ;
|
||||
}
|
||||
|
||||
EConfig::const_iterator EConfig::Find( const string& findMe ) const
|
||||
{
|
||||
return valueMap.find( findMe ) ;
|
||||
}
|
||||
|
||||
EConfig::iterator EConfig::Require( const string& key )
|
||||
{
|
||||
// Attempt to find the key in the map
|
||||
iterator ptr = valueMap.find( key ) ;
|
||||
|
||||
// Was it found?
|
||||
if( ptr == valueMap.end() )
|
||||
{
|
||||
// Nope, this method is intended to "require" the config file
|
||||
// to have a certain key/value pair. Since this is not the
|
||||
// case, print out an error and quit.
|
||||
elog << "EConfig::Require> Configuration requires value "
|
||||
<< "for key \""
|
||||
<< key << "\""
|
||||
<< endl ;
|
||||
::exit( 0 ) ;
|
||||
}
|
||||
|
||||
// At least one key/value pair for this key exists; return it
|
||||
return ptr ;
|
||||
}
|
||||
|
||||
bool EConfig::readFile( ifstream& configFile )
|
||||
{
|
||||
size_t lineNumber = 0 ;
|
||||
string tmp ;
|
||||
|
||||
while( getline( configFile, tmp ) )
|
||||
{
|
||||
// Increment lineNumber before
|
||||
// any continue statements
|
||||
lineNumber++ ;
|
||||
|
||||
if( !removeSpaces( tmp ) )
|
||||
{
|
||||
// Parse error
|
||||
elog << "EConfig: Parse error at line: "
|
||||
<< lineNumber
|
||||
<< endl ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
if( tmp.empty() || '#' == tmp[ 0 ] )
|
||||
{
|
||||
lineInfo addMe( tmp ) ;
|
||||
lineList.push_back( addMe ) ;
|
||||
|
||||
// Ignore this line
|
||||
continue ;
|
||||
}
|
||||
|
||||
// The line should now be of the form:
|
||||
// variablename=value
|
||||
StringTokenizer st( tmp, '=' ) ;
|
||||
|
||||
if( st.size() < 2 )
|
||||
{
|
||||
elog << "EConfig: Improper number of fields "
|
||||
<< "at line: "
|
||||
<< lineNumber
|
||||
<< std::endl ;
|
||||
elog << "EConfig: Perhaps you meant for an empty "
|
||||
<< "value? Use: key = '' to specify empty values"
|
||||
<< std::endl ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Looks ok
|
||||
mapType::iterator mapItr =
|
||||
valueMap.insert( mapPairType( st[ 0 ],
|
||||
st.assemble( 1 ) ) ) ;
|
||||
|
||||
lineList.push_back( lineInfo( st[ 0 ], st.assemble( 1 ),
|
||||
mapItr ) ) ;
|
||||
}
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool EConfig::removeSpaces( string& line )
|
||||
{
|
||||
// If the first character is '#', just return true
|
||||
if( !line.empty() && ('#' == line[ 0 ]) )
|
||||
{
|
||||
return true ;
|
||||
}
|
||||
|
||||
string::iterator ptr = line.begin() ;
|
||||
|
||||
bool inValue = false ;
|
||||
|
||||
// Only continue up until the value field
|
||||
while( ptr != line.end() )
|
||||
{
|
||||
if( ('=' == *ptr) && !inValue )
|
||||
{
|
||||
// We've reached the value field
|
||||
// Only remove comments from this point
|
||||
// on.
|
||||
if( ((ptr + 1) != line.end()) && (*(ptr + 1) == ' ') )
|
||||
{
|
||||
ptr = line.erase( ptr + 1 ) ;
|
||||
}
|
||||
inValue = true ;
|
||||
}
|
||||
|
||||
// Remove any white space characters, so long
|
||||
// as we're not in the value field
|
||||
else if( (' ' == *ptr || '\t' == *ptr) && !inValue )
|
||||
{
|
||||
|
||||
// erase() invalidates the iterator
|
||||
ptr = line.erase( ptr ) ;
|
||||
|
||||
continue ;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
++ptr ;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove trailing whitespace
|
||||
while( !line.empty()
|
||||
&& ((' ' == line[ line.size() - 1 ])
|
||||
|| ('\t' == line[ line.size() - 1 ])) )
|
||||
{
|
||||
line.erase( line.size() - 1 ) ;
|
||||
}
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool EConfig::Add( const string& key, const string& value )
|
||||
{
|
||||
// TODO: Add timestamp info?
|
||||
// Update the valueMap, which is used by clients of this class
|
||||
iterator mapItr =
|
||||
valueMap.insert( mapType::value_type( key, value ) ) ;
|
||||
|
||||
// Update the fileMap, which is used to keep track of the
|
||||
// format of the config file
|
||||
lineInfo addMe( key, value, mapItr ) ;
|
||||
lineList.push_back( addMe ) ;
|
||||
|
||||
return writeFile() ;
|
||||
}
|
||||
|
||||
bool EConfig::writeFile()
|
||||
{
|
||||
// Move the old file to the /tmp directory to ensure we have
|
||||
// a backup.
|
||||
string backupFileName( configFileName ) ;
|
||||
|
||||
// Remove any leading directory path information
|
||||
string::size_type slashPos = backupFileName.find_last_of( '/' ) ;
|
||||
if( string::npos != slashPos )
|
||||
{
|
||||
// There is leading path information
|
||||
backupFileName = backupFileName.substr( slashPos, string::npos ) ;
|
||||
}
|
||||
backupFileName = string( "/tmp/" ) + backupFileName ;
|
||||
|
||||
//elog << "EConfig::writeFile> Renaming "
|
||||
// << configFileName
|
||||
// << " to "
|
||||
// << backupFileName
|
||||
// << endl ;
|
||||
|
||||
if( ::rename( configFileName.c_str(), backupFileName.c_str() ) < 0 )
|
||||
{
|
||||
elog << "EConfig::writeFile> Unable to rename "
|
||||
<< configFileName
|
||||
<< " to "
|
||||
<< backupFileName
|
||||
<< " because: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
// Ok to leave the file open per class conditions.
|
||||
return false ;
|
||||
}
|
||||
|
||||
std::ofstream configFile( configFileName.c_str() ) ;
|
||||
if( !configFile.is_open() )
|
||||
{
|
||||
elog << "EConfig::writeFile> Unable to open file: "
|
||||
<< configFileName
|
||||
<< ", because: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
if( ::rename( backupFileName.c_str(), configFileName.c_str() ) < 0 )
|
||||
{
|
||||
elog << "EConfig::writeFile> Unable to restore "
|
||||
<< "original file \""
|
||||
<< configFileName
|
||||
<< "\" from backup \""
|
||||
<< backupFileName
|
||||
<< "\" because: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
}
|
||||
return false ;
|
||||
}
|
||||
|
||||
for( lineListType::const_iterator fileItr = lineList.begin() ;
|
||||
fileItr != lineList.end() ; ++fileItr )
|
||||
{
|
||||
const lineInfo& theInfo = *fileItr ;
|
||||
if( theInfo.value.empty() )
|
||||
{
|
||||
// elog << "EConfig::writeFile> Writing: "
|
||||
// << theInfo.key
|
||||
// << endl ;
|
||||
|
||||
// comment line
|
||||
configFile << theInfo.key
|
||||
<< endl ;
|
||||
|
||||
continue ;
|
||||
}
|
||||
|
||||
// elog << "EConfig::writeFile> Writing: "
|
||||
// << theInfo.key
|
||||
// << " = "
|
||||
// << theInfo.value
|
||||
// << endl ;
|
||||
|
||||
configFile << theInfo.key
|
||||
<< " = "
|
||||
<< theInfo.value
|
||||
<< endl ;
|
||||
} // for()
|
||||
|
||||
configFile.close() ;
|
||||
|
||||
// Writing of new file succeeded, remove the backup
|
||||
if( ::unlink( backupFileName.c_str() ) < 0 )
|
||||
{
|
||||
elog << "EConfig::writeFile> Unable to unlink "
|
||||
<< "backup file "
|
||||
<< backupFileName
|
||||
<< " because: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
// This is not a critical failure
|
||||
// Allow the method to return true
|
||||
}
|
||||
|
||||
return true ;
|
||||
} // writeFile()
|
||||
|
||||
bool EConfig::Delete( const string& key )
|
||||
{
|
||||
iterator itr = valueMap.find( key ) ;
|
||||
if( itr == valueMap.end() )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
return Delete( itr ) ;
|
||||
}
|
||||
|
||||
bool EConfig::Delete( iterator itr )
|
||||
{
|
||||
if( itr == valueMap.end() )
|
||||
{
|
||||
elog << "EConfig::Delete> Attempting to delete "
|
||||
<< "valueMap.end()"
|
||||
<< endl ;
|
||||
return false ;
|
||||
}
|
||||
valueMap.erase( itr ) ;
|
||||
|
||||
bool foundIt = false ;
|
||||
for( lineListType::iterator lineItr = lineList.begin() ;
|
||||
lineItr != lineList.end() ; ++lineItr )
|
||||
{
|
||||
if( itr == (*lineItr).mapItr )
|
||||
{
|
||||
// Found it
|
||||
foundIt = true ;
|
||||
lineList.erase( lineItr ) ;
|
||||
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
if( !foundIt )
|
||||
{
|
||||
elog << "EConfig::Delete> Unable to find lineList "
|
||||
<< "entry corresponding to iterator: "
|
||||
<< itr->first
|
||||
<< '/'
|
||||
<< itr->second
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
return (foundIt ? writeFile() : false) ;
|
||||
}
|
||||
|
||||
bool EConfig::Replace( iterator itr, const string& newValue )
|
||||
{
|
||||
if( newValue.empty() )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
if( itr == valueMap.end() )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
valueMap.erase( itr ) ;
|
||||
|
||||
bool foundIt = false ;
|
||||
|
||||
// Replacing the value in-place here should maintain the
|
||||
// relative location of the key/value pair in the file.
|
||||
for( lineListType::iterator listItr = lineList.begin() ;
|
||||
listItr != lineList.end() ; ++listItr )
|
||||
{
|
||||
if( (*listItr).mapItr == itr )
|
||||
{
|
||||
// Found it
|
||||
foundIt = true ;
|
||||
(*listItr).value = newValue ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
if( !foundIt )
|
||||
{
|
||||
elog << "EConfig::Replace> Unable to find lineList "
|
||||
<< "entry corresponding to iterator: "
|
||||
<< itr->first
|
||||
<< '/'
|
||||
<< itr->second
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
valueMap.insert( mapType::value_type( itr->first, newValue ) ) ;
|
||||
|
||||
return writeFile() ;
|
||||
}
|
||||
|
||||
bool EConfig::AddComment( const string& commentData )
|
||||
{
|
||||
string newComment( commentData ) ;
|
||||
|
||||
// If this command is not empty and does not begin with the
|
||||
// proper comment delimiter, then add the delimiter.
|
||||
if( !newComment.empty() && ('#' != newComment[ 0 ]) )
|
||||
{
|
||||
newComment = string( "# " ) + newComment ;
|
||||
}
|
||||
|
||||
// For some reason the compiler won't let me use the 3 argument
|
||||
// constructor for lineInfo here -- dan
|
||||
lineInfo addMe ;
|
||||
addMe.key = newComment ;
|
||||
lineList.push_back( addMe ) ;
|
||||
|
||||
return writeFile() ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,341 @@
|
||||
/**
|
||||
* EConfig.h
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: EConfig.h,v 1.7 2004/01/16 00:59:47 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __ECONFIG_H
|
||||
#define __ECONFIG_H "$Id: EConfig.h,v 1.7 2004/01/16 00:59:47 dan_karrels Exp $"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
#include "ELog.h"
|
||||
#include "misc.h" // noCaseCompare
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* A simple configuration file parser.
|
||||
*
|
||||
* Format of config file:
|
||||
* - Any line *beginning* with '#' is a comment
|
||||
*
|
||||
* Fields are of the form:
|
||||
* - variablename = value
|
||||
* - variablename must be one word
|
||||
* - value may be any number of words, but may
|
||||
* NOT continue onto the next line
|
||||
* - value fields may NOT have a '=' character in it
|
||||
*/
|
||||
class EConfig
|
||||
{
|
||||
|
||||
/**
|
||||
* The type of the map used to store the key/value pairs.
|
||||
*/
|
||||
typedef std::multimap< std::string, std::string, noCaseCompare >
|
||||
mapType ;
|
||||
|
||||
/**
|
||||
* This struct is used to store information about an entry
|
||||
* into the physical configuration file.
|
||||
*/
|
||||
struct lineInfo
|
||||
{
|
||||
/**
|
||||
* The key for the key/value pair.
|
||||
*/
|
||||
std::string key ;
|
||||
|
||||
/**
|
||||
* The value for the key/value pair.
|
||||
* An empty value string indicates that this line
|
||||
* is a comment, and key contains the comment
|
||||
* (including preceeding '#' where necessary.
|
||||
*/
|
||||
std::string value ;
|
||||
|
||||
/**
|
||||
* An iterator into the key/value map for this line,
|
||||
* so that modifications to this key/value pair can
|
||||
* can be placed in the correct location in the file.
|
||||
*/
|
||||
mapType::iterator mapItr ;
|
||||
|
||||
/**
|
||||
* Default constructor relies upon the default constructors
|
||||
* of the member variables.
|
||||
*/
|
||||
lineInfo()
|
||||
{}
|
||||
|
||||
/**
|
||||
* This constructor is called when there is a comment
|
||||
* line. Even though there are no value tokens, be sure
|
||||
* to record whatever comment is present.
|
||||
*/
|
||||
lineInfo( const std::string& key )
|
||||
: key( key )
|
||||
{}
|
||||
|
||||
/**
|
||||
* Copy constructor, used in assignment (as in the key/value map).
|
||||
*/
|
||||
lineInfo( const lineInfo& rhs )
|
||||
: key( rhs.key ), value( rhs.value ), mapItr( rhs.mapItr )
|
||||
{}
|
||||
|
||||
/**
|
||||
* Constructor receives the key, value, and map iterator.
|
||||
*/
|
||||
lineInfo( const std::string& key, const std::string& value,
|
||||
const mapType::iterator& mapItr )
|
||||
: key( key ), value( value ), mapItr( mapItr )
|
||||
{}
|
||||
|
||||
/**
|
||||
* Permit assignment.
|
||||
*/
|
||||
lineInfo& operator=( const lineInfo& rhs )
|
||||
{
|
||||
key = rhs.key ;
|
||||
value = rhs.value ;
|
||||
mapItr = rhs.mapItr ;
|
||||
return *this ;
|
||||
}
|
||||
|
||||
} ;
|
||||
|
||||
/**
|
||||
* Type used to store configuration file format.
|
||||
*/
|
||||
typedef std::list< lineInfo > lineListType ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* The pair type for the mapType, it holds the key/value pairs.
|
||||
*/
|
||||
typedef mapType::value_type mapPairType ;
|
||||
|
||||
/**
|
||||
* The type used to store the number of significant key/value
|
||||
* pairs stored.
|
||||
*/
|
||||
typedef mapType::size_type size_type ;
|
||||
|
||||
/**
|
||||
* A const iterator that users of this class may use
|
||||
* to safely walk through the structure.
|
||||
*/
|
||||
typedef mapType::const_iterator const_iterator ;
|
||||
|
||||
/**
|
||||
* A non-const iterator for walking through this
|
||||
* structure.
|
||||
*/
|
||||
typedef mapType::iterator iterator ;
|
||||
|
||||
/**
|
||||
* Create a new config parser for the given config file name.
|
||||
*/
|
||||
EConfig( const std::string& fileName ) ;
|
||||
|
||||
/**
|
||||
* Destroy the EConfig object. This will also close the
|
||||
* input file.
|
||||
*/
|
||||
virtual ~EConfig() ;
|
||||
|
||||
/**
|
||||
* Obtain a const iterator to the beginning of the map
|
||||
* of key/value pairs.
|
||||
*/
|
||||
inline const_iterator begin() const
|
||||
{ return valueMap.begin() ; }
|
||||
|
||||
/**
|
||||
* Obtain a const iterator to the end of the map of
|
||||
* key/value pairs.
|
||||
*/
|
||||
inline const_iterator end() const
|
||||
{ return valueMap.end() ; }
|
||||
|
||||
/**
|
||||
* Obtain a iterator to the beginning of the map
|
||||
* of key/value pairs.
|
||||
*/
|
||||
inline iterator begin()
|
||||
{ return valueMap.begin() ; }
|
||||
|
||||
/**
|
||||
* Obtain a iterator to the end of the map of
|
||||
* key/value pairs.
|
||||
*/
|
||||
inline iterator end()
|
||||
{ return valueMap.end() ; }
|
||||
|
||||
/**
|
||||
* Return the number of significant key/value pairs stored.
|
||||
*/
|
||||
inline size_type size() const
|
||||
{ return valueMap.size() ; }
|
||||
|
||||
/**
|
||||
* Return true if there was an error in processing
|
||||
* the file.
|
||||
*/
|
||||
inline bool hasError() const
|
||||
{ return error ; }
|
||||
|
||||
/**
|
||||
* Find the first key/value pair for the given key.
|
||||
*/
|
||||
virtual iterator Find( const std::string& findMe ) ;
|
||||
|
||||
/**
|
||||
* Find the first key/value pair for the given key.
|
||||
*/
|
||||
virtual const_iterator Find( const std::string& findMe ) const ;
|
||||
|
||||
/**
|
||||
* Call this method to retrieve a const_iterator to the first
|
||||
* value associated with the key (findMe). If the key is
|
||||
* not found, then an error message will be output and the
|
||||
* program will terminate.
|
||||
*/
|
||||
virtual iterator Require( const std::string& findMe ) ;
|
||||
|
||||
/**
|
||||
* Add a key/value pair to the config file.
|
||||
* Note that this will not delete any existing entry, but
|
||||
* will add a duplicate, which is supported by the EConfig
|
||||
* class.
|
||||
*/
|
||||
virtual bool Add( const std::string& key,
|
||||
const std::string& value ) ;
|
||||
|
||||
/**
|
||||
* Add a comment to the end of the file.
|
||||
* This comment line may be empty.
|
||||
*/
|
||||
virtual bool AddComment( const std::string& newComment ) ;
|
||||
|
||||
/**
|
||||
* Delete a key/value pair by key. Only one pair whose key
|
||||
* matches the given key will be deleted, and no guarantee
|
||||
* is made about which one.
|
||||
* If you have duplicates, and would like to remove a specific
|
||||
* entry, use the other form of Delete().
|
||||
*/
|
||||
virtual bool Delete( const std::string& key ) ;
|
||||
|
||||
/**
|
||||
* Remove a key value pair given its iterator.
|
||||
*/
|
||||
virtual bool Delete( iterator itr ) ;
|
||||
|
||||
/**
|
||||
* Replace the value of a given key/value pair.
|
||||
* It is important to use this method rather than just
|
||||
* modifying the iterator itself.
|
||||
*/
|
||||
virtual bool Replace( iterator itr,
|
||||
const std::string& newValue ) ;
|
||||
|
||||
/**
|
||||
* Debugging function for outputting the entire map to
|
||||
* an output stream.
|
||||
*/
|
||||
friend ELog& operator<<( ELog& out, const EConfig& conf )
|
||||
{
|
||||
EConfig::const_iterator ptr = conf.begin() ;
|
||||
while( ptr != conf.end() )
|
||||
{
|
||||
out << "Key: " << ptr->first << ", "
|
||||
<< "Value: " << ptr->second << std::endl ;
|
||||
++ptr ;
|
||||
}
|
||||
return out ;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Disable copying, this method is declared but NOT defined.
|
||||
*/
|
||||
EConfig( const EConfig& ) ;
|
||||
|
||||
/**
|
||||
* Disable assignment, this method is declared but NOT defined.
|
||||
*/
|
||||
EConfig operator=( const EConfig& ) ;
|
||||
|
||||
/**
|
||||
* Remove blank spaces from the line of text.
|
||||
*/
|
||||
virtual bool removeSpaces( std::string& ) ;
|
||||
|
||||
/**
|
||||
* Parse the input file.
|
||||
*/
|
||||
virtual bool readFile( std::ifstream& ) ;
|
||||
|
||||
/**
|
||||
* Write the current memory configuration to disk.
|
||||
*/
|
||||
virtual bool writeFile() ;
|
||||
|
||||
/**
|
||||
* Record that an error has occured.
|
||||
*/
|
||||
inline void setError()
|
||||
{ error = true ; }
|
||||
|
||||
/**
|
||||
* The name of the configuration file.
|
||||
*/
|
||||
std::string configFileName ;
|
||||
|
||||
/**
|
||||
* The error status field.
|
||||
*/
|
||||
bool error ;
|
||||
|
||||
/**
|
||||
* The map used to store the file's key/value pairs.
|
||||
*/
|
||||
mapType valueMap ;
|
||||
|
||||
/**
|
||||
* The DS used to store the actual format of the config
|
||||
* file.
|
||||
*/
|
||||
lineListType lineList ;
|
||||
|
||||
} ; // class EConfig
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __ECONFIG_H
|
||||
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* ELog.cc
|
||||
* Author: Daniel Karrels (dan@karrels.com)
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: ELog.cc,v 1.8 2005/02/20 15:49:21 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "ELog.h"
|
||||
|
||||
const char rcsId[] = "$Id: ELog.cc,v 1.8 2005/02/20 15:49:21 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::stringstream ;
|
||||
using std::string ;
|
||||
using std::endl ;
|
||||
|
||||
// Instantiate the global instance
|
||||
ELog elog ;
|
||||
|
||||
ELog::ELog()
|
||||
: outStream( 0 ),
|
||||
logFile( false )
|
||||
{}
|
||||
|
||||
ELog::ELog( const string& fileName )
|
||||
: outStream( 0 ),
|
||||
logFile( false )
|
||||
{
|
||||
openFile( fileName ) ;
|
||||
}
|
||||
|
||||
ELog::~ELog()
|
||||
{
|
||||
if( logFile )
|
||||
{
|
||||
closeFile() ;
|
||||
}
|
||||
}
|
||||
|
||||
bool ELog::openFile( const string& fileName )
|
||||
{
|
||||
if( isOpen() )
|
||||
{
|
||||
closeFile() ;
|
||||
}
|
||||
logFile = true ;
|
||||
outFile.open( fileName.c_str(), std::ios::out | std::ios::trunc ) ;
|
||||
|
||||
if( !isOpen() )
|
||||
{
|
||||
logFile = false ;
|
||||
}
|
||||
return isOpen() ;
|
||||
}
|
||||
|
||||
void ELog::closeFile()
|
||||
{
|
||||
if( isOpen() )
|
||||
{
|
||||
outFile << endl ;
|
||||
outFile.close() ;
|
||||
}
|
||||
}
|
||||
|
||||
ELog& ELog::operator<<( __E_omanip var )
|
||||
{
|
||||
if( logFile )
|
||||
{
|
||||
outFile << var ;
|
||||
}
|
||||
if( outStream ) *outStream << var ;
|
||||
return *this ;
|
||||
}
|
||||
|
||||
ELog& ELog::operator<<( __E_manip var )
|
||||
{
|
||||
if( logFile )
|
||||
{
|
||||
outFile << var ;
|
||||
}
|
||||
if( outStream ) *outStream << var ;
|
||||
return *this ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* ELog.h
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: ELog.h,v 1.8 2005/02/20 15:49:21 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __ELOG_H
|
||||
#define __ELOG_H "$Id: ELog.h,v 1.8 2005/02/20 15:49:21 dan_karrels Exp $"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* This class handles logging for GNUWorld. It maintains a pointer
|
||||
* to a C++ output stream. If this pointer is non-NULL, all messages
|
||||
* are logged to this stream. All messages are also logged to the
|
||||
* file whose name is specified in the constructor.
|
||||
*/
|
||||
class ELog
|
||||
{
|
||||
|
||||
protected:
|
||||
|
||||
typedef std::ostream& (*__E_omanip)( std::ostream& ) ;
|
||||
typedef std::ostream& (*__E_manip)( std::ios& ) ;
|
||||
|
||||
/**
|
||||
* A pointer to a C++ output stream for logging. If this
|
||||
* stream is non-NULL, then all messages will be logged
|
||||
* to this stream.
|
||||
*/
|
||||
std::ostream *outStream ;
|
||||
|
||||
/**
|
||||
* The file output stream to which to log all messages.
|
||||
*/
|
||||
std::ofstream outFile ;
|
||||
|
||||
/**
|
||||
* True if logging to an output file.
|
||||
*/
|
||||
bool logFile ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Instantiate an instance of this class. No output file
|
||||
* is opened, and no output stream is specified for
|
||||
* logging.
|
||||
*/
|
||||
ELog() ;
|
||||
|
||||
/**
|
||||
* Instantiate an instance of this class, specifying the
|
||||
* name of the file to which to log messages. This log file
|
||||
* will be created if it does not already exist. If it
|
||||
* exists, it will be truncated.
|
||||
*/
|
||||
ELog( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Destroy an instance of this class. This method will
|
||||
* close the output file if it is open.
|
||||
*/
|
||||
virtual ~ELog() ;
|
||||
|
||||
/**
|
||||
* Open an output file for logging messages. If an output
|
||||
* file is currently open, it will be flushed and closed.
|
||||
* This method will create the file if it does not already
|
||||
* exist, otherwise the existing file will be truncated.
|
||||
* This method returns true on success, false otherwise.
|
||||
* Keep in mind that even if this method return false,
|
||||
* any existing output file will be closed.
|
||||
*/
|
||||
bool openFile( const std::string& fileName ) ;
|
||||
|
||||
/**
|
||||
* Close the output log file, if it is open.
|
||||
*/
|
||||
void closeFile() ;
|
||||
|
||||
/**
|
||||
* Return true if an output log file is open, false otherwise.
|
||||
* For some reason, std::fstream::is_open() is not const.
|
||||
*/
|
||||
inline bool isOpen()
|
||||
{ return (logFile && outFile.is_open()) ; }
|
||||
|
||||
/**
|
||||
* Use this method to specify which stream to which
|
||||
* to log messages. Specifying NULL will stop all
|
||||
* output logging to any stream (except for the output file,
|
||||
* if it exists).
|
||||
*/
|
||||
inline void setStream( std::ostream* newStream )
|
||||
{ outStream = newStream ; }
|
||||
|
||||
/**
|
||||
* Output the endl function.
|
||||
*/
|
||||
ELog& operator<<( __E_omanip func ) ;
|
||||
|
||||
/**
|
||||
* Output the endl function.
|
||||
*/
|
||||
ELog& operator<<( __E_manip func ) ;
|
||||
|
||||
/**
|
||||
* Output any other type supported by std::ostream.
|
||||
*/
|
||||
template< typename T >
|
||||
ELog& operator<<( const T& vari )
|
||||
{
|
||||
if( logFile ) outFile << vari ;
|
||||
if( outStream ) *outStream << vari ;
|
||||
return *this ;
|
||||
}
|
||||
|
||||
} ;
|
||||
|
||||
/// The global logging instance.
|
||||
extern ELog elog ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __ELOG_H
|
||||
@@ -0,0 +1,786 @@
|
||||
/**
|
||||
* MTrie.cc
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: MTrie.cc,v 1.14 2004/05/18 16:50:57 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "MTrie.h"
|
||||
#include "StringTokenizer.h"
|
||||
#include "match.h"
|
||||
|
||||
// TODO: allow comparison functor (case insensitive)
|
||||
|
||||
using std::map ;
|
||||
using std::clog ;
|
||||
using std::endl ;
|
||||
using std::list ;
|
||||
using std::string ;
|
||||
using std::ostream ;
|
||||
using gnuworld::StringTokenizer ;
|
||||
|
||||
template< typename _valueT >
|
||||
MTrie< _valueT >::MTrie( const char delimiter )
|
||||
: numElements( 0 ),
|
||||
parent( 0 ),
|
||||
delimiter( delimiter )
|
||||
{}
|
||||
|
||||
template< typename _valueT >
|
||||
MTrie< _valueT >::~MTrie()
|
||||
{
|
||||
for( nodes_iterator itr = nodesMap.begin() ;
|
||||
itr != nodesMap.end() ; ++itr )
|
||||
{
|
||||
delete itr->second ;
|
||||
}
|
||||
nodesMap.clear() ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
bool MTrie< _valueT >::insert( const string& key, const _valueT& value )
|
||||
{
|
||||
// Ensure there are no wildcard characters in the key.
|
||||
if( key.find( '*' ) != string::npos )
|
||||
{
|
||||
// clog << "MTrie::insert> Found \'*\' in key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
if( key.find( '?' ) != string::npos )
|
||||
{
|
||||
// clog << "MTrie::insert> Found \'?\' in key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Tokenize the key
|
||||
// The key is of the form "www.yahoo.com" or "ns1.switch.abc.net"
|
||||
StringTokenizer st( key, delimiter ) ;
|
||||
|
||||
MTrie< _valueT >* currentNode = this ;
|
||||
|
||||
// The purpose of this loop is to iterate to the level in which
|
||||
// to place the new item.
|
||||
// The proper level is found when the tokenItr is no longer valid,
|
||||
// i.e., an empty token is found (works for top level also).
|
||||
for( StringTokenizer::const_reverse_iterator tokenItr = st.rbegin() ;
|
||||
tokenItr != st.rend() ; ++tokenItr )
|
||||
{
|
||||
if( currentNode->nodesMap.find( *tokenItr ) ==
|
||||
currentNode->nodesMap.end() )
|
||||
{
|
||||
// This node does not exist
|
||||
MTrie< _valueT >* newNode =
|
||||
new (std::nothrow) MTrie< _valueT > ;
|
||||
assert( newNode != 0 ) ;
|
||||
|
||||
currentNode->nodesMap[ *tokenItr ] = newNode ;
|
||||
newNode->parent = currentNode ;
|
||||
|
||||
// clog << "MTrie::insert> Added node for key: "
|
||||
// << *tokenItr
|
||||
// << endl ;
|
||||
|
||||
currentNode = newNode ;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentNode = currentNode->nodesMap[ *tokenItr ] ;
|
||||
}
|
||||
}
|
||||
|
||||
// currentNode now points to the level at which to add this item
|
||||
currentNode->valuesList.push_back( value ) ;
|
||||
|
||||
//clog << "MTrie::insert> Added key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
++numElements ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
list< typename MTrie< _valueT >::value_type >
|
||||
MTrie< _valueT >::find( const string& key ) const
|
||||
{
|
||||
//clog << "MTrie::find> Searching for key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
// base tracks the keys used to iterate to the different levels.
|
||||
// It is used when matching for '*', since '*' may cross
|
||||
// levels.
|
||||
base.clear() ;
|
||||
returnMe.clear() ;
|
||||
origKey = key ;
|
||||
|
||||
StringTokenizer tokens( key, delimiter ) ;
|
||||
StringTokenizer::const_reverse_iterator tokenItr = tokens.rbegin() ;
|
||||
|
||||
// key may have wildcards, which is trickier than it may sound.
|
||||
// A key may be "w*w.yahoo.com" which matches to both
|
||||
// "www.yahoo.com" and "www.wwwww.yahoo.com"
|
||||
// The recursive find will handle all cases of '?', '*', and
|
||||
// normal non-wildcard keys.
|
||||
find( this, tokens, tokenItr ) ;
|
||||
|
||||
return returnMe ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::find(
|
||||
const MTrie< _valueT >* currentNode,
|
||||
const StringTokenizer& tokens,
|
||||
StringTokenizer::const_reverse_iterator tokenItr ) const
|
||||
{
|
||||
// If the key is empty, that means that we have reached the
|
||||
// final node. All values at this node are matches.
|
||||
// This happens when the left most token has a '?' in it:
|
||||
// "n?ws.abs.net"
|
||||
if( tokenItr == tokens.rend() )
|
||||
{
|
||||
// clog << "MTrie::find> Found end of tokens, numValues: "
|
||||
// << currentNode->valuesList.size()
|
||||
// << endl ;
|
||||
|
||||
for( const_values_iterator vItr = currentNode->valuesList.begin(),
|
||||
vEndItr = currentNode->valuesList.end() ;
|
||||
vItr != vEndItr ; ++vItr )
|
||||
{
|
||||
returnMe.push_back( value_type( getBase(), *vItr ) ) ;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
//clog << "MTrie::find> tokens: "
|
||||
// << tokens
|
||||
// << ", tokenItr: "
|
||||
// << *tokenItr
|
||||
// << endl ;
|
||||
|
||||
bool foundQuestionMark = false ;
|
||||
|
||||
// Iterate as far as possible
|
||||
for( ; tokenItr != tokens.rend() ; ++tokenItr )
|
||||
{
|
||||
if( string::npos != (*tokenItr).find( '*' ) )
|
||||
{
|
||||
// Wildcard
|
||||
break ;
|
||||
}
|
||||
|
||||
// Make sure this is after the search for '*'
|
||||
if( string::npos != (*tokenItr).find( '?') )
|
||||
{
|
||||
// clog << "MTrie::find> Found question mark in token: "
|
||||
// << *tokenItr
|
||||
// << endl ;
|
||||
foundQuestionMark = true ;
|
||||
break ;
|
||||
}
|
||||
|
||||
// No wildcard, continue to next level
|
||||
const_nodes_iterator nItr = currentNode->nodesMap.find( *tokenItr ) ;
|
||||
if( currentNode->nodesMap.end() == nItr )
|
||||
{
|
||||
// This node does not exist
|
||||
|
||||
// clog << "MTrie::find> Unable to find key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
return ;
|
||||
}
|
||||
currentNode = nItr->second ;
|
||||
|
||||
// Add to base
|
||||
base.push_front( *tokenItr ) ;
|
||||
} // for( tokenItr )
|
||||
|
||||
if( tokenItr == tokens.rend() )
|
||||
{
|
||||
// We are at a matching node
|
||||
for( const_values_iterator vItr = currentNode->valuesList.begin(),
|
||||
vEndItr = currentNode->valuesList.end() ;
|
||||
vItr != vEndItr ; ++vItr )
|
||||
{
|
||||
returnMe.push_back( value_type( getBase(), *vItr ) ) ;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
//clog << "MTrie::find> *tokenItr: "
|
||||
// << *tokenItr
|
||||
// << endl ;
|
||||
|
||||
// Because of the precondition, the only way to exit the above
|
||||
// loop is by finding a wildcard, tokenItr must be valid
|
||||
// tokenItr now points to a token of the form "w*w" or "*w*w*"
|
||||
// We can prevent some unnecessary searching by creating a
|
||||
// search token with as many real characters as possible.
|
||||
// In the above cases, it would be "w" and "*", respectively.
|
||||
// That is, if we are searching for "w*w", there is no reason
|
||||
// to recursively check the currentNode's nodesMap element whose
|
||||
// key is "moo".
|
||||
// Construct the most known token.
|
||||
string localKey( *tokenItr ) ;
|
||||
|
||||
//clog << "MTrie::find> localKey: "
|
||||
// << localKey
|
||||
// << endl ;
|
||||
|
||||
// Move to the next token for calls to find()
|
||||
++tokenItr ;
|
||||
|
||||
// This variable is true if the localKey determined below
|
||||
// is '*' alone.
|
||||
bool loneStar = false ;
|
||||
|
||||
// Prepare localKey if a '*' was found
|
||||
if( !foundQuestionMark )
|
||||
{
|
||||
// '*' was found in localKey
|
||||
|
||||
// localKey starts out as "n*ws"
|
||||
// Setup localKey to "*ws"
|
||||
// In the case of a key like "*adsl*", there is no
|
||||
// sense in trimming down localKey just to perform
|
||||
// a match(), since all nodes match.
|
||||
|
||||
// starPos is the index of the right-most '*'
|
||||
const string::size_type starPos = localKey.rfind( '*' ) ;
|
||||
|
||||
if( starPos == (localKey.size() - 1) )
|
||||
{
|
||||
// The '*' is the right-most character, "*adsl*"
|
||||
// cout << "find> Found lonestar"
|
||||
// << endl ;
|
||||
loneStar = true ;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not a generic '*' search, it's the case of "n*ws"
|
||||
// Trim localKey to look like "*ws"
|
||||
localKey.erase( 0, starPos ) ;
|
||||
}
|
||||
} // if( !foundQuestionMark )
|
||||
|
||||
// Everything is set, begin recursion
|
||||
// Match localKey against all nodes
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin(),
|
||||
nEndItr = currentNode->nodesMap.end() ;
|
||||
nItr != nEndItr ; ++nItr )
|
||||
{
|
||||
// clog << "MTrie::find> match( "
|
||||
// << localKey
|
||||
// << ", "
|
||||
// << nItr->first
|
||||
// << " ): " ;
|
||||
|
||||
if( foundQuestionMark )
|
||||
{
|
||||
if( !gnuworld::match( localKey, nItr->first ) )
|
||||
{
|
||||
// clog << "true" << endl ;
|
||||
base.push_front( nItr->first ) ;
|
||||
|
||||
// Question mark only, no '*' in the token.
|
||||
find( nItr->second,
|
||||
tokens,
|
||||
tokenItr ) ;
|
||||
|
||||
base.pop_front() ;
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// clog << "false" << endl ;
|
||||
// }
|
||||
continue ;
|
||||
}
|
||||
|
||||
// '*'
|
||||
if( loneStar )
|
||||
{
|
||||
/* NO-OP, fall through, all nodes match */
|
||||
}
|
||||
else if( gnuworld::match( localKey, nItr->first ) )
|
||||
{
|
||||
// Not a match, do not recursiveFind() on this node
|
||||
continue ;
|
||||
}
|
||||
|
||||
// clog << "true" << endl ;
|
||||
|
||||
base.push_front( nItr->first ) ;
|
||||
recursiveFind( nItr->second ) ;
|
||||
base.pop_front() ;
|
||||
} // for( nItr )
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::recursiveFind(
|
||||
const MTrie< _valueT >* currentNode,
|
||||
bool blindRecursion ) const
|
||||
{
|
||||
// blindRecursion:
|
||||
// If we encounter a '*' as the first character of origKey (the original
|
||||
// search string), and the current node matches that key, then by
|
||||
// definition all nodes under the current node must also match.
|
||||
// In this case, use a "blind recursion," simply adding all values
|
||||
// under this node without performing a match() on any of them.
|
||||
|
||||
//clog << "MTrie::recursiveFind> key: "
|
||||
// << origKey
|
||||
// << ", base: "
|
||||
// << getBase()
|
||||
// << endl ;
|
||||
|
||||
// '*'
|
||||
// No need to create a new string and perform a match
|
||||
// if this node has no values
|
||||
if( !currentNode->valuesList.empty() )
|
||||
{
|
||||
// We need the stringBase either way
|
||||
string stringBase = getBase() ;
|
||||
|
||||
if( blindRecursion || !gnuworld::match( origKey, stringBase ) )
|
||||
{
|
||||
// clog << "MTrie::recursiveFind> match"
|
||||
// << endl ;
|
||||
|
||||
if( !blindRecursion && '*' == origKey[ 0 ] )
|
||||
{
|
||||
// Found a match starting at this node
|
||||
blindRecursion = true ;
|
||||
}
|
||||
|
||||
// This node matches
|
||||
for( const_values_iterator vItr =
|
||||
currentNode->valuesList.begin(),
|
||||
vEndItr = currentNode->valuesList.end() ;
|
||||
vItr != vEndItr ; ++vItr )
|
||||
{
|
||||
// clog << "MTrie::recursiveFind> vItr: "
|
||||
// << *vItr
|
||||
// << endl ;
|
||||
value_type addMe( stringBase, *vItr ) ;
|
||||
returnMe.push_back( addMe ) ;
|
||||
} // for( vItr )
|
||||
} // if( !match() )
|
||||
} // if( !empty() )
|
||||
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin(),
|
||||
nEndItr = currentNode->nodesMap.end() ;
|
||||
nItr != nEndItr ; ++nItr )
|
||||
{
|
||||
// match() is not important here, since we are doing
|
||||
// a blind '*' search, check every node from here down
|
||||
base.push_front( nItr->first ) ;
|
||||
recursiveFind( nItr->second, blindRecursion ) ;
|
||||
base.pop_front() ;
|
||||
} // for( nItr )
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
string MTrie< _valueT >::getBase() const
|
||||
{
|
||||
string retMe ;
|
||||
retMe.reserve( 128 ) ;
|
||||
bool doneALoop = false ;
|
||||
|
||||
for( list< string >::const_iterator rtItr = base.begin(),
|
||||
rtEndItr = base.end() ; rtItr != rtEndItr ; ++rtItr )
|
||||
{
|
||||
if( doneALoop )
|
||||
{
|
||||
retMe += delimiter ;
|
||||
}
|
||||
retMe += *rtItr ;
|
||||
doneALoop = true ;
|
||||
}
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
typename MTrie< _valueT >::size_type
|
||||
MTrie< _valueT >::erase( const string& key )
|
||||
{
|
||||
//clog << "MTrie::erase> Erasing key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
// key may have wildcards, which is trickier than it may sound.
|
||||
// A key may be "w*w.yahoo.com" which matches to both
|
||||
// "www.yahoo.com" and "www.wwwww.yahoo.com"
|
||||
|
||||
list< string > base ;
|
||||
return erase( this, base, key, key ) ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
typename MTrie< _valueT >::size_type
|
||||
MTrie< _valueT >::erase(
|
||||
MTrie< _valueT >* currentNode,
|
||||
list< string >& base,
|
||||
const string& origKey,
|
||||
const string& key )
|
||||
{
|
||||
size_type eraseCount = 0 ;
|
||||
if( key.empty() )
|
||||
{
|
||||
// Everything on this level is a match
|
||||
eraseCount = currentNode->valuesList.size() ;
|
||||
currentNode->valuesList.clear() ;
|
||||
|
||||
return eraseCount ;
|
||||
}
|
||||
|
||||
//clog << "MTrie::erase> Erasing key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
StringTokenizer tokens( key, delimiter ) ;
|
||||
StringTokenizer::const_reverse_iterator tokenItr = tokens.rbegin() ;
|
||||
|
||||
bool foundQuestionMark = false ;
|
||||
|
||||
// Iterate as far as possible before beginning wild card match()
|
||||
// searches.
|
||||
for( ; tokenItr != tokens.rend() ; ++tokenItr )
|
||||
{
|
||||
if( string::npos != (*tokenItr).find( '*' ) )
|
||||
{
|
||||
// Wildcard
|
||||
break ;
|
||||
}
|
||||
|
||||
// Make sure this is after the search for '*'
|
||||
if( string::npos != (*tokenItr).find( '?') )
|
||||
{
|
||||
foundQuestionMark = true ;
|
||||
break ;
|
||||
}
|
||||
|
||||
// No wildcard, continue to next level
|
||||
nodes_iterator nItr = currentNode->nodesMap.find( *tokenItr ) ;
|
||||
if( currentNode->nodesMap.end() == nItr )
|
||||
{
|
||||
// This node does not exist
|
||||
// Since we are not at the last token,
|
||||
// this key does not exist
|
||||
// clog << "MTrie::erase> Unable to find key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
currentNode = nItr->second ;
|
||||
|
||||
// Add to base
|
||||
base.push_front( *tokenItr ) ;
|
||||
} // for( tokenItr )
|
||||
|
||||
if( tokenItr == tokens.rend() )
|
||||
{
|
||||
// Everything on this level is a match
|
||||
eraseCount = currentNode->valuesList.size() ;
|
||||
currentNode->valuesList.clear() ;
|
||||
|
||||
return eraseCount ;
|
||||
}
|
||||
|
||||
//clog << "MTrie::erase> *tokenItr: "
|
||||
// << *tokenItr
|
||||
// << endl ;
|
||||
|
||||
// Because of the precondition, the only way to exit the above
|
||||
// loop is by finding a wildcard, tokenItr must be valid
|
||||
// tokenItr now points to a token of the form "w*w" or "*w*w*"
|
||||
// We can prevent some unnecessary searching by creating a
|
||||
// search token with as many real characters as possible.
|
||||
// In the above cases, it would be "w" and "*", respectively.
|
||||
// That is, if we are searching for "w*w", there is no reason
|
||||
// to recursively check the currentNode's nodesMap element whose
|
||||
// key is "moo".
|
||||
// Construct the most known token.
|
||||
string localKey( *tokenItr ) ;
|
||||
|
||||
// starPos is the index of the '*'
|
||||
string::size_type starPos = localKey.rfind( '*' ) ;
|
||||
|
||||
// Here we must:
|
||||
// setup localKey to be the star and everything to its right
|
||||
// setup the rest of the string to be everything left of
|
||||
// and including the star
|
||||
|
||||
if( !foundQuestionMark )
|
||||
{
|
||||
// localKey starts out as "n*ws"
|
||||
// Setup localKey to "*ws"
|
||||
localKey.erase( 0, starPos ) ;
|
||||
}
|
||||
|
||||
//clog << "MTrie::erase> starPos: "
|
||||
// << starPos
|
||||
// << ", localKey: "
|
||||
// << localKey
|
||||
// << ", base: "
|
||||
// << getBase()
|
||||
// << endl ;
|
||||
|
||||
// Everything is set, begin recursion
|
||||
// Match localKey against all nodes
|
||||
for( nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
// clog << "MTrie::erase> match( "
|
||||
// << localKey
|
||||
// << ", "
|
||||
// << nItr->first
|
||||
// << " ): " ;
|
||||
|
||||
if( gnuworld::match( localKey, nItr->first ) )
|
||||
{
|
||||
// clog << "false" << endl ;
|
||||
continue ;
|
||||
}
|
||||
|
||||
// clog << "true" << endl ;
|
||||
|
||||
base.push_front( nItr->first ) ;
|
||||
if( foundQuestionMark )
|
||||
{
|
||||
// Question mark only, no '*' in the
|
||||
// token
|
||||
// Reassemble the key
|
||||
string newKey ;
|
||||
for( ++tokenItr; tokenItr != tokens.rend() ;
|
||||
++tokenItr )
|
||||
{
|
||||
if( !newKey.empty() )
|
||||
{
|
||||
newKey.insert( newKey.begin(),
|
||||
delimiter ) ;
|
||||
}
|
||||
newKey.insert( 0, *tokenItr ) ;
|
||||
}
|
||||
|
||||
// clog << "MTrie::erase> (?) newKey: "
|
||||
// << newKey
|
||||
// << endl ;
|
||||
|
||||
eraseCount += erase( nItr->second,
|
||||
base,
|
||||
origKey,
|
||||
newKey ) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
eraseCount += recursiveErase( nItr->second, // MTrie*
|
||||
base,
|
||||
origKey ) ;
|
||||
} // if( questionMarkFound )
|
||||
base.pop_front() ;
|
||||
|
||||
if( currentNode->valuesList.empty() &&
|
||||
currentNode->nodesMap.empty() )
|
||||
{
|
||||
// clog << "MTrie::erase> Erasing empty node"
|
||||
// << endl ;
|
||||
|
||||
// This does NOT invalidate the iterator
|
||||
delete nItr->second ;
|
||||
currentNode->nodesMap.erase( nItr ) ;
|
||||
}
|
||||
} // for( nItr )
|
||||
return eraseCount ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
typename MTrie< _valueT >::size_type
|
||||
MTrie< _valueT >::recursiveErase(
|
||||
MTrie< _valueT >* currentNode,
|
||||
list< string >& base,
|
||||
const string& key )
|
||||
{
|
||||
//clog << "MTrie::recursiveErase(*)> base: "
|
||||
// << getBase()
|
||||
// << ", key: "
|
||||
// << key
|
||||
// << endl ;
|
||||
|
||||
size_type eraseCount = 0 ;
|
||||
|
||||
// '*'
|
||||
string stringBase = getBase() ;
|
||||
if( !gnuworld::match( key, stringBase ) )
|
||||
{
|
||||
// This node matches
|
||||
eraseCount = currentNode->valuesList.size() ;
|
||||
currentNode->valuesList.clear() ;
|
||||
} // if( !match() )
|
||||
|
||||
for( nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
// match() is not important here, since we are doing
|
||||
// a blind '*' search, check every node from here down
|
||||
base.push_front( nItr->first ) ;
|
||||
eraseCount += recursiveErase( nItr->second, // MTrie*
|
||||
base,
|
||||
key ) ;
|
||||
base.pop_front() ;
|
||||
|
||||
if( currentNode->valuesList.empty() &&
|
||||
currentNode->nodesMap.empty() )
|
||||
{
|
||||
// clog << "MTrie::recursiveErase> Erasing empty node"
|
||||
// << endl ;
|
||||
|
||||
// This does NOT invalidate the iterator
|
||||
delete nItr->second ;
|
||||
currentNode->nodesMap.erase( nItr ) ;
|
||||
}
|
||||
} // for( nItr )
|
||||
return eraseCount ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::dumpDebug( ostream& out ) const
|
||||
{
|
||||
recursiveDebug( out, 1, this ) ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::recursiveDebug( ostream& out,
|
||||
size_t levelNum,
|
||||
const MTrie< _valueT >* currentNode ) const
|
||||
{
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
recursiveDebug( out, levelNum + 1, nItr->second ) ;
|
||||
}
|
||||
out << levelNum << ' '
|
||||
<< currentNode->nodesMap.size() << ' '
|
||||
<< currentNode->valuesList.size() << ' '
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::levelDebug( ostream& out, size_t searchLevel )
|
||||
const
|
||||
{
|
||||
out << "Searching for level "
|
||||
<< searchLevel
|
||||
<< endl ;
|
||||
levelDebug( out, 0, searchLevel, this ) ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void MTrie< _valueT >::levelDebug( ostream& out, size_t currentLevel,
|
||||
size_t searchLevel, const MTrie< _valueT >* currentNode ) const
|
||||
{
|
||||
if( (currentLevel + 1) == searchLevel )
|
||||
{
|
||||
// Next level is the winner
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
out << "Key: "
|
||||
<< nItr->first
|
||||
<< ", number of values: "
|
||||
<< value_size( nItr->second )
|
||||
<< endl ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
levelDebug( out, currentLevel + 1, searchLevel,
|
||||
currentNode ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
size_t MTrie< _valueT >::value_size( const MTrie< _valueT >* currentNode )
|
||||
const
|
||||
{
|
||||
size_t returnMe = currentNode->valuesList.size() ;
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
returnMe += value_size( nItr->second ) ;
|
||||
}
|
||||
return returnMe ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
list< string >
|
||||
MTrie< _valueT >::findMinLength( size_t minLength ) const
|
||||
{
|
||||
base.clear() ;
|
||||
list< string > retMe ;
|
||||
|
||||
findMinLength( minLength, retMe, this ) ;
|
||||
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
template< typename _valueT >
|
||||
void
|
||||
MTrie< _valueT >::findMinLength( size_t minLength,
|
||||
list< string >& retMe,
|
||||
const MTrie< _valueT >* currentNode ) const
|
||||
{
|
||||
if( (base.size() >= minLength) && !currentNode->valuesList.empty() )
|
||||
{
|
||||
// We are at or past the min length, and this node
|
||||
// has at least one value to add
|
||||
// Only one entry into returnMe is necessary
|
||||
retMe.push_back( getBase() ) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Continue to all subnodes
|
||||
for( const_nodes_iterator nItr = currentNode->nodesMap.begin() ;
|
||||
nItr != currentNode->nodesMap.end() ; ++nItr )
|
||||
{
|
||||
base.push_front( nItr->first ) ;
|
||||
findMinLength( minLength,
|
||||
retMe,
|
||||
nItr->second ) ;
|
||||
base.pop_front() ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
* MTrie.h
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: MTrie.h,v 1.13 2004/05/18 16:50:57 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __MTRIE_H
|
||||
#define __MTRIE_H
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "StringTokenizer.h"
|
||||
|
||||
using gnuworld::StringTokenizer ;
|
||||
|
||||
/**
|
||||
* This class is used to store elements keyed by a decimalized string,
|
||||
* for example a hostname like www.yahoo.com
|
||||
* The key advantage is that this class is designed to support
|
||||
* fast wildcard matching of these hosts.
|
||||
* For example, clients of this class can search for all elements
|
||||
* whose keys match 'ab*ef.foo.ba?.net'.
|
||||
*/
|
||||
template< typename _valueT >
|
||||
class MTrie
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initialize an MTrie.
|
||||
*/
|
||||
MTrie( const char delimiter = '.' ) ;
|
||||
|
||||
/**
|
||||
* Destructor will deallocate all internally allocated
|
||||
* memory, but will not touch data stored here on behalf
|
||||
* the client.
|
||||
*/
|
||||
virtual ~MTrie() ;
|
||||
|
||||
/**
|
||||
* The type being stored in this structure
|
||||
*/
|
||||
typedef _valueT data_type ;
|
||||
|
||||
/**
|
||||
* The key/value pair type
|
||||
*/
|
||||
typedef std::pair< std::string, _valueT > value_type ;
|
||||
|
||||
/**
|
||||
* The type used to represent the number of elements
|
||||
* in this MTrie
|
||||
*/
|
||||
typedef size_t size_type ;
|
||||
|
||||
/**
|
||||
* Return the current number of elements being stored
|
||||
* in this MTrie.
|
||||
*/
|
||||
inline size_type size() const
|
||||
{ return numElements ; }
|
||||
|
||||
/**
|
||||
* Insert a key/value pair. The key must NOT have any
|
||||
* wildcard characters.
|
||||
*/
|
||||
bool insert( const std::string& key, const data_type& value ) ;
|
||||
|
||||
/**
|
||||
* Return a list of the key/value pairs that match the given
|
||||
* wildcard key. The key may contain '?' and '*', but need
|
||||
* not do so. In general, the most specific searches will
|
||||
* be fastest.
|
||||
*/
|
||||
virtual std::list< value_type > find( const std::string& key ) const ;
|
||||
|
||||
/**
|
||||
* Remove all values associated with key's that match the given
|
||||
* key. The key may have wildcards '?' and '*', and follow
|
||||
* the same semantics as find(). Be sure to deallocate the
|
||||
* values if necessary before calling erase(), this method will
|
||||
* not deallocate the items stored in this structure.
|
||||
* The number of elements erased is returned.
|
||||
*/
|
||||
virtual size_type erase( const std::string& key ) ;
|
||||
|
||||
/**
|
||||
* Produce output about for each node in the trie in the
|
||||
* following format:
|
||||
* <level number> <nodesmap size> <valueslist size>
|
||||
* This method is used for statistical analysis.
|
||||
*/
|
||||
virtual void dumpDebug( std::ostream& ) const ;
|
||||
|
||||
/**
|
||||
* Send the key of each node at the given level to the
|
||||
* output stream. Also send the total number of values
|
||||
* under each subtree at those nodes.
|
||||
*/
|
||||
virtual void levelDebug( std::ostream&, size_t searchLevel )
|
||||
const ;
|
||||
|
||||
/**
|
||||
* Return all keys that have at least as many tokens as
|
||||
* minLength.
|
||||
*/
|
||||
virtual std::list< std::string > findMinLength( size_t minLength )
|
||||
const ;
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Recursive helper method to the public levelDebug()
|
||||
*/
|
||||
virtual void levelDebug( std::ostream&, size_t currentLevel,
|
||||
size_t searchLevel,
|
||||
const MTrie< data_type >* ) const ;
|
||||
|
||||
/**
|
||||
* Return the total number of values under the given node
|
||||
*/
|
||||
virtual size_t value_size( const MTrie< data_type >* ) const ;
|
||||
|
||||
/**
|
||||
* Recursive helper method to the public findMinLength()
|
||||
*/
|
||||
virtual void findMinLength( size_t minLength,
|
||||
std::list< std::string >& retMe,
|
||||
const MTrie< data_type >* ) const ;
|
||||
|
||||
/**
|
||||
* Recursive find method that handles all the hard work
|
||||
* matching across multiple levels.
|
||||
*/
|
||||
virtual void find( const MTrie< data_type >*,
|
||||
const StringTokenizer&,
|
||||
StringTokenizer::const_reverse_iterator )
|
||||
const ;
|
||||
|
||||
/**
|
||||
* Recursive erase method that handles all the hard work
|
||||
* matching across multiple levels.
|
||||
*/
|
||||
virtual size_type erase( MTrie< data_type >*,
|
||||
std::list< std::string >&,
|
||||
const std::string& origKey,
|
||||
const std::string& key ) ;
|
||||
|
||||
/**
|
||||
* Convenience method that puts together a list of
|
||||
* string tokens into a single string.
|
||||
*/
|
||||
std::string getBase() const ;
|
||||
|
||||
/**
|
||||
* Recursive method used only for searching for '*' matched
|
||||
* strings.
|
||||
*/
|
||||
virtual void recursiveFind( const MTrie< data_type >*,
|
||||
bool blindRecursion = false )
|
||||
const ;
|
||||
|
||||
/**
|
||||
* Recursive method used only for erasing '*' matched
|
||||
* strings.
|
||||
*/
|
||||
virtual size_type recursiveErase( MTrie< data_type >*,
|
||||
std::list< std::string >& base,
|
||||
const std::string& key ) ;
|
||||
|
||||
/**
|
||||
* A recursive method used to output some debugging information.
|
||||
*/
|
||||
virtual void recursiveDebug( std::ostream&,
|
||||
size_t levelNum,
|
||||
const MTrie< data_type >* ) const ;
|
||||
|
||||
/**
|
||||
* The number of elements stored in this MTrie
|
||||
*/
|
||||
size_type numElements ;
|
||||
|
||||
/**
|
||||
* The parent node to this node (NULL if root)
|
||||
*/
|
||||
MTrie< data_type >* parent ;
|
||||
|
||||
/**
|
||||
* The string delimiter.
|
||||
*/
|
||||
char delimiter ;
|
||||
|
||||
/**
|
||||
* The type used to store values
|
||||
*/
|
||||
typedef std::list< data_type > valuesListType ;
|
||||
|
||||
/**
|
||||
* An iterator for iterating the valuesList
|
||||
*/
|
||||
typedef typename valuesListType::iterator values_iterator ;
|
||||
|
||||
/**
|
||||
* A const iterator for iterating the valuesList
|
||||
*/
|
||||
typedef typename valuesListType::const_iterator const_values_iterator ;
|
||||
|
||||
/**
|
||||
* The structure used to store values
|
||||
*/
|
||||
valuesListType valuesList ;
|
||||
|
||||
/**
|
||||
* The type used to store pointers to levels in the trie
|
||||
*/
|
||||
typedef std::map< std::string, MTrie< _valueT >* > nodesMapType ;
|
||||
|
||||
/**
|
||||
* An iterator to nodes in the trie
|
||||
*/
|
||||
typedef typename nodesMapType::iterator nodes_iterator ;
|
||||
|
||||
/**
|
||||
* A const iterator to nodes in the trie
|
||||
*/
|
||||
typedef typename nodesMapType::const_iterator const_nodes_iterator ;
|
||||
|
||||
/**
|
||||
* The structure used to store levels in the trie
|
||||
*/
|
||||
nodesMapType nodesMap ;
|
||||
|
||||
/**
|
||||
* This variable is used in find() and erase() to reduce the
|
||||
* number of arguments passed to the internal recursive methods.
|
||||
* It stores the current key associated with the node
|
||||
* being examined.
|
||||
*/
|
||||
mutable std::list< std::string > base ;
|
||||
|
||||
/**
|
||||
* This variable is used in find() and erase() to reduce the
|
||||
* number of arguments passed to the internal recursive methods.
|
||||
* It stores the values to be returned from the methods.
|
||||
*/
|
||||
mutable std::list< value_type > returnMe ;
|
||||
|
||||
/**
|
||||
* This variable is used in find() and erase() to reduce the
|
||||
* number of arguments passed to the internal recursive methods.
|
||||
* It stores the key being searched for in the overall
|
||||
* find/erase.
|
||||
*/
|
||||
mutable std::string origKey ;
|
||||
|
||||
/**
|
||||
* The tokens object used in find() and erase().
|
||||
*/
|
||||
StringTokenizer tokens ;
|
||||
|
||||
} ;
|
||||
|
||||
// This is a template class, cannot compile, so include the source
|
||||
// inline.
|
||||
#include "MTrie.cc"
|
||||
|
||||
#endif // __MTRIE_H
|
||||
@@ -0,0 +1,36 @@
|
||||
## Process this file with automake to produce Makefile.in
|
||||
## $Id: Makefile.am,v 1.9 2004/01/07 20:23:20 dan_karrels Exp $
|
||||
|
||||
noinst_LTLIBRARIES = libgnuworld.la
|
||||
libgnuworld_la_SOURCES = Buffer.cc \
|
||||
Connection.cc \
|
||||
ConnectionHandler.cc \
|
||||
ConnectionManager.cc \
|
||||
EConfig.cc \
|
||||
ELog.cc \
|
||||
Signal.cc \
|
||||
StringTokenizer.cc \
|
||||
gThread.cc \
|
||||
match.cc \
|
||||
md5hash.cc \
|
||||
misc.cc
|
||||
libgnuworld_la_LDFLAGS = $(threadLib)
|
||||
libgnuworld_la_CXXFLAGS = -I${top_srcdir}/include
|
||||
|
||||
EXTRA_DIST = Buffer.h \
|
||||
Connection.h \
|
||||
ConnectionHandler.h \
|
||||
ConnectionManager.h \
|
||||
EConfig.h \
|
||||
ELog.h \
|
||||
gThread.h \
|
||||
ircd_chattr.h \
|
||||
match.h \
|
||||
match_table.h \
|
||||
md5hash.h \
|
||||
misc.h \
|
||||
MTrie.h \
|
||||
MTrie.cc \
|
||||
Signal.h \
|
||||
StringTokenizer.h \
|
||||
xparameters.h
|
||||
@@ -0,0 +1,622 @@
|
||||
# Makefile.in generated by automake 1.11.1 from Makefile.am.
|
||||
# @configure_input@
|
||||
|
||||
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
|
||||
# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation,
|
||||
# Inc.
|
||||
# This Makefile.in is free software; the Free Software Foundation
|
||||
# gives unlimited permission to copy and/or distribute it,
|
||||
# with or without modifications, as long as this notice is preserved.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
|
||||
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
# PARTICULAR PURPOSE.
|
||||
|
||||
@SET_MAKE@
|
||||
|
||||
VPATH = @srcdir@
|
||||
pkgdatadir = $(datadir)/@PACKAGE@
|
||||
pkgincludedir = $(includedir)/@PACKAGE@
|
||||
pkglibdir = $(libdir)/@PACKAGE@
|
||||
pkglibexecdir = $(libexecdir)/@PACKAGE@
|
||||
am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
|
||||
install_sh_DATA = $(install_sh) -c -m 644
|
||||
install_sh_PROGRAM = $(install_sh) -c
|
||||
install_sh_SCRIPT = $(install_sh) -c
|
||||
INSTALL_HEADER = $(INSTALL_DATA)
|
||||
transform = $(program_transform_name)
|
||||
NORMAL_INSTALL = :
|
||||
PRE_INSTALL = :
|
||||
POST_INSTALL = :
|
||||
NORMAL_UNINSTALL = :
|
||||
PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
subdir = libgnuworld
|
||||
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
am__aclocal_m4_deps = $(top_srcdir)/ac_cxx_namespaces.m4 \
|
||||
$(top_srcdir)/ax_boost_thread.m4 $(top_srcdir)/configure.ac
|
||||
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
|
||||
$(ACLOCAL_M4)
|
||||
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
|
||||
CONFIG_HEADER = $(top_builddir)/include/defs.h
|
||||
CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
LTLIBRARIES = $(noinst_LTLIBRARIES)
|
||||
libgnuworld_la_LIBADD =
|
||||
am_libgnuworld_la_OBJECTS = libgnuworld_la-Buffer.lo \
|
||||
libgnuworld_la-Connection.lo \
|
||||
libgnuworld_la-ConnectionHandler.lo \
|
||||
libgnuworld_la-ConnectionManager.lo libgnuworld_la-EConfig.lo \
|
||||
libgnuworld_la-ELog.lo libgnuworld_la-Signal.lo \
|
||||
libgnuworld_la-StringTokenizer.lo libgnuworld_la-gThread.lo \
|
||||
libgnuworld_la-match.lo libgnuworld_la-md5hash.lo \
|
||||
libgnuworld_la-misc.lo
|
||||
libgnuworld_la_OBJECTS = $(am_libgnuworld_la_OBJECTS)
|
||||
libgnuworld_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CXXLD) \
|
||||
$(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) \
|
||||
$(libgnuworld_la_LDFLAGS) $(LDFLAGS) -o $@
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include
|
||||
depcomp = $(SHELL) $(top_srcdir)/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
am__mv = mv -f
|
||||
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
LTCXXCOMPILE = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
CXXLD = $(CXX)
|
||||
CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(libgnuworld_la_SOURCES)
|
||||
DIST_SOURCES = $(libgnuworld_la_SOURCES)
|
||||
ETAGS = etags
|
||||
CTAGS = ctags
|
||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||
ACLOCAL = @ACLOCAL@
|
||||
AMTAR = @AMTAR@
|
||||
AR = @AR@
|
||||
AUTOCONF = @AUTOCONF@
|
||||
AUTOHEADER = @AUTOHEADER@
|
||||
AUTOMAKE = @AUTOMAKE@
|
||||
AWK = @AWK@
|
||||
BOOST_THREAD_LIB = @BOOST_THREAD_LIB@
|
||||
CC = @CC@
|
||||
CCDEPMODE = @CCDEPMODE@
|
||||
CFLAGS = @CFLAGS@
|
||||
CPP = @CPP@
|
||||
CPPFLAGS = @CPPFLAGS@
|
||||
CXX = @CXX@
|
||||
CXXCPP = @CXXCPP@
|
||||
CXXDEPMODE = @CXXDEPMODE@
|
||||
CXXFLAGS = @CXXFLAGS@
|
||||
CYGPATH_W = @CYGPATH_W@
|
||||
DEFS = @DEFS@
|
||||
DEPDIR = @DEPDIR@
|
||||
DLLTOOL = @DLLTOOL@
|
||||
DSYMUTIL = @DSYMUTIL@
|
||||
DUMPBIN = @DUMPBIN@
|
||||
ECHO_C = @ECHO_C@
|
||||
ECHO_N = @ECHO_N@
|
||||
ECHO_T = @ECHO_T@
|
||||
EGREP = @EGREP@
|
||||
EXEEXT = @EXEEXT@
|
||||
FGREP = @FGREP@
|
||||
GREP = @GREP@
|
||||
INCLTDL = @INCLTDL@
|
||||
INSTALL = @INSTALL@
|
||||
INSTALL_DATA = @INSTALL_DATA@
|
||||
INSTALL_PROGRAM = @INSTALL_PROGRAM@
|
||||
INSTALL_SCRIPT = @INSTALL_SCRIPT@
|
||||
INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
|
||||
LD = @LD@
|
||||
LDFLAGS = @LDFLAGS@
|
||||
LIBGNUWORLD = @LIBGNUWORLD@
|
||||
LIBLTDL = @LIBLTDL@
|
||||
LIBOBJS = @LIBOBJS@
|
||||
LIBS = @LIBS@
|
||||
LIBTOOL = @LIBTOOL@
|
||||
LIBTOOL_DEPS = @LIBTOOL_DEPS@
|
||||
LIPO = @LIPO@
|
||||
LN_S = @LN_S@
|
||||
LTDLDEPS = @LTDLDEPS@
|
||||
LTDLINCL = @LTDLINCL@
|
||||
LTLIBOBJS = @LTLIBOBJS@
|
||||
MAINT = @MAINT@
|
||||
MAKEINFO = @MAKEINFO@
|
||||
MANIFEST_TOOL = @MANIFEST_TOOL@
|
||||
MKDIR_P = @MKDIR_P@
|
||||
NEW_MODS = @NEW_MODS@
|
||||
NM = @NM@
|
||||
NMEDIT = @NMEDIT@
|
||||
OBJDUMP = @OBJDUMP@
|
||||
OBJEXT = @OBJEXT@
|
||||
OTOOL = @OTOOL@
|
||||
OTOOL64 = @OTOOL64@
|
||||
PACKAGE = @PACKAGE@
|
||||
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
|
||||
PACKAGE_NAME = @PACKAGE_NAME@
|
||||
PACKAGE_STRING = @PACKAGE_STRING@
|
||||
PACKAGE_TARNAME = @PACKAGE_TARNAME@
|
||||
PACKAGE_URL = @PACKAGE_URL@
|
||||
PACKAGE_VERSION = @PACKAGE_VERSION@
|
||||
PATH_SEPARATOR = @PATH_SEPARATOR@
|
||||
RANLIB = @RANLIB@
|
||||
SED = @SED@
|
||||
SET_MAKE = @SET_MAKE@
|
||||
SHELL = @SHELL@
|
||||
STRIP = @STRIP@
|
||||
VERSION = @VERSION@
|
||||
abs_builddir = @abs_builddir@
|
||||
abs_srcdir = @abs_srcdir@
|
||||
abs_top_builddir = @abs_top_builddir@
|
||||
abs_top_srcdir = @abs_top_srcdir@
|
||||
ac_ct_AR = @ac_ct_AR@
|
||||
ac_ct_CC = @ac_ct_CC@
|
||||
ac_ct_CXX = @ac_ct_CXX@
|
||||
ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
|
||||
am__include = @am__include@
|
||||
am__leading_dot = @am__leading_dot@
|
||||
am__quote = @am__quote@
|
||||
am__tar = @am__tar@
|
||||
am__untar = @am__untar@
|
||||
bindir = @bindir@
|
||||
build = @build@
|
||||
build_alias = @build_alias@
|
||||
build_cpu = @build_cpu@
|
||||
build_os = @build_os@
|
||||
build_vendor = @build_vendor@
|
||||
builddir = @builddir@
|
||||
datadir = @datadir@
|
||||
datarootdir = @datarootdir@
|
||||
docdir = @docdir@
|
||||
dvidir = @dvidir@
|
||||
exec_prefix = @exec_prefix@
|
||||
host = @host@
|
||||
host_alias = @host_alias@
|
||||
host_cpu = @host_cpu@
|
||||
host_os = @host_os@
|
||||
host_vendor = @host_vendor@
|
||||
htmldir = @htmldir@
|
||||
includedir = @includedir@
|
||||
infodir = @infodir@
|
||||
install_sh = @install_sh@
|
||||
libdir = @libdir@
|
||||
libexecdir = @libexecdir@
|
||||
localedir = @localedir@
|
||||
localstatedir = @localstatedir@
|
||||
mandir = @mandir@
|
||||
mkdir_p = @mkdir_p@
|
||||
oldincludedir = @oldincludedir@
|
||||
pdfdir = @pdfdir@
|
||||
pgsql_includedir = @pgsql_includedir@
|
||||
pgsql_libdir = @pgsql_libdir@
|
||||
prefix = @prefix@
|
||||
program_transform_name = @program_transform_name@
|
||||
psdir = @psdir@
|
||||
sbindir = @sbindir@
|
||||
sharedstatedir = @sharedstatedir@
|
||||
srcdir = @srcdir@
|
||||
subdirs = @subdirs@
|
||||
sysconfdir = @sysconfdir@
|
||||
target_alias = @target_alias@
|
||||
threadLib = @threadLib@
|
||||
top_build_prefix = @top_build_prefix@
|
||||
top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
noinst_LTLIBRARIES = libgnuworld.la
|
||||
libgnuworld_la_SOURCES = Buffer.cc \
|
||||
Connection.cc \
|
||||
ConnectionHandler.cc \
|
||||
ConnectionManager.cc \
|
||||
EConfig.cc \
|
||||
ELog.cc \
|
||||
Signal.cc \
|
||||
StringTokenizer.cc \
|
||||
gThread.cc \
|
||||
match.cc \
|
||||
md5hash.cc \
|
||||
misc.cc
|
||||
|
||||
libgnuworld_la_LDFLAGS = $(threadLib)
|
||||
libgnuworld_la_CXXFLAGS = -I${top_srcdir}/include
|
||||
EXTRA_DIST = Buffer.h \
|
||||
Connection.h \
|
||||
ConnectionHandler.h \
|
||||
ConnectionManager.h \
|
||||
EConfig.h \
|
||||
ELog.h \
|
||||
gThread.h \
|
||||
ircd_chattr.h \
|
||||
match.h \
|
||||
match_table.h \
|
||||
md5hash.h \
|
||||
misc.h \
|
||||
MTrie.h \
|
||||
MTrie.cc \
|
||||
Signal.h \
|
||||
StringTokenizer.h \
|
||||
xparameters.h
|
||||
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .cc .lo .o .obj
|
||||
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps)
|
||||
@for dep in $?; do \
|
||||
case '$(am__configure_deps)' in \
|
||||
*$$dep*) \
|
||||
( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
|
||||
&& { if test -f $@; then exit 0; else break; fi; }; \
|
||||
exit 1;; \
|
||||
esac; \
|
||||
done; \
|
||||
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu libgnuworld/Makefile'; \
|
||||
$(am__cd) $(top_srcdir) && \
|
||||
$(AUTOMAKE) --gnu libgnuworld/Makefile
|
||||
.PRECIOUS: Makefile
|
||||
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
|
||||
@case '$?' in \
|
||||
*config.status*) \
|
||||
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
|
||||
*) \
|
||||
echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \
|
||||
cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \
|
||||
esac;
|
||||
|
||||
$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
|
||||
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
|
||||
|
||||
$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps)
|
||||
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
|
||||
$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps)
|
||||
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
|
||||
$(am__aclocal_m4_deps):
|
||||
|
||||
clean-noinstLTLIBRARIES:
|
||||
-test -z "$(noinst_LTLIBRARIES)" || rm -f $(noinst_LTLIBRARIES)
|
||||
@list='$(noinst_LTLIBRARIES)'; for p in $$list; do \
|
||||
dir="`echo $$p | sed -e 's|/[^/]*$$||'`"; \
|
||||
test "$$dir" != "$$p" || dir=.; \
|
||||
echo "rm -f \"$${dir}/so_locations\""; \
|
||||
rm -f "$${dir}/so_locations"; \
|
||||
done
|
||||
libgnuworld.la: $(libgnuworld_la_OBJECTS) $(libgnuworld_la_DEPENDENCIES)
|
||||
$(libgnuworld_la_LINK) $(libgnuworld_la_OBJECTS) $(libgnuworld_la_LIBADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-Buffer.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-Connection.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-ConnectionHandler.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-ConnectionManager.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-EConfig.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-ELog.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-Signal.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-StringTokenizer.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-gThread.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-match.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-md5hash.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworld_la-misc.Plo@am__quote@
|
||||
|
||||
.cc.o:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
.cc.obj:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
|
||||
.cc.lo:
|
||||
@am__fastdepCXX_TRUE@ $(LTCXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $<
|
||||
|
||||
libgnuworld_la-Buffer.lo: Buffer.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-Buffer.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-Buffer.Tpo -c -o libgnuworld_la-Buffer.lo `test -f 'Buffer.cc' || echo '$(srcdir)/'`Buffer.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-Buffer.Tpo $(DEPDIR)/libgnuworld_la-Buffer.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Buffer.cc' object='libgnuworld_la-Buffer.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-Buffer.lo `test -f 'Buffer.cc' || echo '$(srcdir)/'`Buffer.cc
|
||||
|
||||
libgnuworld_la-Connection.lo: Connection.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-Connection.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-Connection.Tpo -c -o libgnuworld_la-Connection.lo `test -f 'Connection.cc' || echo '$(srcdir)/'`Connection.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-Connection.Tpo $(DEPDIR)/libgnuworld_la-Connection.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Connection.cc' object='libgnuworld_la-Connection.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-Connection.lo `test -f 'Connection.cc' || echo '$(srcdir)/'`Connection.cc
|
||||
|
||||
libgnuworld_la-ConnectionHandler.lo: ConnectionHandler.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-ConnectionHandler.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-ConnectionHandler.Tpo -c -o libgnuworld_la-ConnectionHandler.lo `test -f 'ConnectionHandler.cc' || echo '$(srcdir)/'`ConnectionHandler.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-ConnectionHandler.Tpo $(DEPDIR)/libgnuworld_la-ConnectionHandler.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ConnectionHandler.cc' object='libgnuworld_la-ConnectionHandler.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-ConnectionHandler.lo `test -f 'ConnectionHandler.cc' || echo '$(srcdir)/'`ConnectionHandler.cc
|
||||
|
||||
libgnuworld_la-ConnectionManager.lo: ConnectionManager.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-ConnectionManager.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-ConnectionManager.Tpo -c -o libgnuworld_la-ConnectionManager.lo `test -f 'ConnectionManager.cc' || echo '$(srcdir)/'`ConnectionManager.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-ConnectionManager.Tpo $(DEPDIR)/libgnuworld_la-ConnectionManager.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ConnectionManager.cc' object='libgnuworld_la-ConnectionManager.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-ConnectionManager.lo `test -f 'ConnectionManager.cc' || echo '$(srcdir)/'`ConnectionManager.cc
|
||||
|
||||
libgnuworld_la-EConfig.lo: EConfig.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-EConfig.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-EConfig.Tpo -c -o libgnuworld_la-EConfig.lo `test -f 'EConfig.cc' || echo '$(srcdir)/'`EConfig.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-EConfig.Tpo $(DEPDIR)/libgnuworld_la-EConfig.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='EConfig.cc' object='libgnuworld_la-EConfig.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-EConfig.lo `test -f 'EConfig.cc' || echo '$(srcdir)/'`EConfig.cc
|
||||
|
||||
libgnuworld_la-ELog.lo: ELog.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-ELog.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-ELog.Tpo -c -o libgnuworld_la-ELog.lo `test -f 'ELog.cc' || echo '$(srcdir)/'`ELog.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-ELog.Tpo $(DEPDIR)/libgnuworld_la-ELog.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ELog.cc' object='libgnuworld_la-ELog.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-ELog.lo `test -f 'ELog.cc' || echo '$(srcdir)/'`ELog.cc
|
||||
|
||||
libgnuworld_la-Signal.lo: Signal.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-Signal.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-Signal.Tpo -c -o libgnuworld_la-Signal.lo `test -f 'Signal.cc' || echo '$(srcdir)/'`Signal.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-Signal.Tpo $(DEPDIR)/libgnuworld_la-Signal.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Signal.cc' object='libgnuworld_la-Signal.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-Signal.lo `test -f 'Signal.cc' || echo '$(srcdir)/'`Signal.cc
|
||||
|
||||
libgnuworld_la-StringTokenizer.lo: StringTokenizer.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-StringTokenizer.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-StringTokenizer.Tpo -c -o libgnuworld_la-StringTokenizer.lo `test -f 'StringTokenizer.cc' || echo '$(srcdir)/'`StringTokenizer.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-StringTokenizer.Tpo $(DEPDIR)/libgnuworld_la-StringTokenizer.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='StringTokenizer.cc' object='libgnuworld_la-StringTokenizer.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-StringTokenizer.lo `test -f 'StringTokenizer.cc' || echo '$(srcdir)/'`StringTokenizer.cc
|
||||
|
||||
libgnuworld_la-gThread.lo: gThread.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-gThread.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-gThread.Tpo -c -o libgnuworld_la-gThread.lo `test -f 'gThread.cc' || echo '$(srcdir)/'`gThread.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-gThread.Tpo $(DEPDIR)/libgnuworld_la-gThread.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='gThread.cc' object='libgnuworld_la-gThread.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-gThread.lo `test -f 'gThread.cc' || echo '$(srcdir)/'`gThread.cc
|
||||
|
||||
libgnuworld_la-match.lo: match.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-match.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-match.Tpo -c -o libgnuworld_la-match.lo `test -f 'match.cc' || echo '$(srcdir)/'`match.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-match.Tpo $(DEPDIR)/libgnuworld_la-match.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='match.cc' object='libgnuworld_la-match.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-match.lo `test -f 'match.cc' || echo '$(srcdir)/'`match.cc
|
||||
|
||||
libgnuworld_la-md5hash.lo: md5hash.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-md5hash.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-md5hash.Tpo -c -o libgnuworld_la-md5hash.lo `test -f 'md5hash.cc' || echo '$(srcdir)/'`md5hash.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-md5hash.Tpo $(DEPDIR)/libgnuworld_la-md5hash.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='md5hash.cc' object='libgnuworld_la-md5hash.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-md5hash.lo `test -f 'md5hash.cc' || echo '$(srcdir)/'`md5hash.cc
|
||||
|
||||
libgnuworld_la-misc.lo: misc.cc
|
||||
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworld_la-misc.lo -MD -MP -MF $(DEPDIR)/libgnuworld_la-misc.Tpo -c -o libgnuworld_la-misc.lo `test -f 'misc.cc' || echo '$(srcdir)/'`misc.cc
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/libgnuworld_la-misc.Tpo $(DEPDIR)/libgnuworld_la-misc.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='misc.cc' object='libgnuworld_la-misc.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworld_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworld_la-misc.lo `test -f 'misc.cc' || echo '$(srcdir)/'`misc.cc
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
|
||||
clean-libtool:
|
||||
-rm -rf .libs _libs
|
||||
|
||||
ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
|
||||
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
|
||||
unique=`for i in $$list; do \
|
||||
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
|
||||
done | \
|
||||
$(AWK) '{ files[$$0] = 1; nonempty = 1; } \
|
||||
END { if (nonempty) { for (i in files) print i; }; }'`; \
|
||||
mkid -fID $$unique
|
||||
tags: TAGS
|
||||
|
||||
TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
|
||||
$(TAGS_FILES) $(LISP)
|
||||
set x; \
|
||||
here=`pwd`; \
|
||||
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
|
||||
unique=`for i in $$list; do \
|
||||
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
|
||||
done | \
|
||||
$(AWK) '{ files[$$0] = 1; nonempty = 1; } \
|
||||
END { if (nonempty) { for (i in files) print i; }; }'`; \
|
||||
shift; \
|
||||
if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \
|
||||
test -n "$$unique" || unique=$$empty_fix; \
|
||||
if test $$# -gt 0; then \
|
||||
$(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
|
||||
"$$@" $$unique; \
|
||||
else \
|
||||
$(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
|
||||
$$unique; \
|
||||
fi; \
|
||||
fi
|
||||
ctags: CTAGS
|
||||
CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
|
||||
$(TAGS_FILES) $(LISP)
|
||||
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
|
||||
unique=`for i in $$list; do \
|
||||
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
|
||||
done | \
|
||||
$(AWK) '{ files[$$0] = 1; nonempty = 1; } \
|
||||
END { if (nonempty) { for (i in files) print i; }; }'`; \
|
||||
test -z "$(CTAGS_ARGS)$$unique" \
|
||||
|| $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
|
||||
$$unique
|
||||
|
||||
GTAGS:
|
||||
here=`$(am__cd) $(top_builddir) && pwd` \
|
||||
&& $(am__cd) $(top_srcdir) \
|
||||
&& gtags -i $(GTAGS_ARGS) "$$here"
|
||||
|
||||
distclean-tags:
|
||||
-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
|
||||
|
||||
distdir: $(DISTFILES)
|
||||
@srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
|
||||
topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
|
||||
list='$(DISTFILES)'; \
|
||||
dist_files=`for file in $$list; do echo $$file; done | \
|
||||
sed -e "s|^$$srcdirstrip/||;t" \
|
||||
-e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
|
||||
case $$dist_files in \
|
||||
*/*) $(MKDIR_P) `echo "$$dist_files" | \
|
||||
sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
|
||||
sort -u` ;; \
|
||||
esac; \
|
||||
for file in $$dist_files; do \
|
||||
if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
|
||||
if test -d $$d/$$file; then \
|
||||
dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
|
||||
if test -d "$(distdir)/$$file"; then \
|
||||
find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
|
||||
fi; \
|
||||
if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
|
||||
cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
|
||||
find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
|
||||
fi; \
|
||||
cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
|
||||
else \
|
||||
test -f "$(distdir)/$$file" \
|
||||
|| cp -p $$d/$$file "$(distdir)/$$file" \
|
||||
|| exit 1; \
|
||||
fi; \
|
||||
done
|
||||
check-am: all-am
|
||||
check: check-am
|
||||
all-am: Makefile $(LTLIBRARIES)
|
||||
installdirs:
|
||||
install: install-am
|
||||
install-exec: install-exec-am
|
||||
install-data: install-data-am
|
||||
uninstall: uninstall-am
|
||||
|
||||
install-am: all-am
|
||||
@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
|
||||
|
||||
installcheck: installcheck-am
|
||||
install-strip:
|
||||
$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
|
||||
install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
|
||||
`test -z '$(STRIP)' || \
|
||||
echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
|
||||
mostlyclean-generic:
|
||||
|
||||
clean-generic:
|
||||
|
||||
distclean-generic:
|
||||
-test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
|
||||
-test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
|
||||
|
||||
maintainer-clean-generic:
|
||||
@echo "This command is intended for maintainers to use"
|
||||
@echo "it deletes files that may require special tools to rebuild."
|
||||
clean: clean-am
|
||||
|
||||
clean-am: clean-generic clean-libtool clean-noinstLTLIBRARIES \
|
||||
mostlyclean-am
|
||||
|
||||
distclean: distclean-am
|
||||
-rm -rf ./$(DEPDIR)
|
||||
-rm -f Makefile
|
||||
distclean-am: clean-am distclean-compile distclean-generic \
|
||||
distclean-tags
|
||||
|
||||
dvi: dvi-am
|
||||
|
||||
dvi-am:
|
||||
|
||||
html: html-am
|
||||
|
||||
html-am:
|
||||
|
||||
info: info-am
|
||||
|
||||
info-am:
|
||||
|
||||
install-data-am:
|
||||
|
||||
install-dvi: install-dvi-am
|
||||
|
||||
install-dvi-am:
|
||||
|
||||
install-exec-am:
|
||||
|
||||
install-html: install-html-am
|
||||
|
||||
install-html-am:
|
||||
|
||||
install-info: install-info-am
|
||||
|
||||
install-info-am:
|
||||
|
||||
install-man:
|
||||
|
||||
install-pdf: install-pdf-am
|
||||
|
||||
install-pdf-am:
|
||||
|
||||
install-ps: install-ps-am
|
||||
|
||||
install-ps-am:
|
||||
|
||||
installcheck-am:
|
||||
|
||||
maintainer-clean: maintainer-clean-am
|
||||
-rm -rf ./$(DEPDIR)
|
||||
-rm -f Makefile
|
||||
maintainer-clean-am: distclean-am maintainer-clean-generic
|
||||
|
||||
mostlyclean: mostlyclean-am
|
||||
|
||||
mostlyclean-am: mostlyclean-compile mostlyclean-generic \
|
||||
mostlyclean-libtool
|
||||
|
||||
pdf: pdf-am
|
||||
|
||||
pdf-am:
|
||||
|
||||
ps: ps-am
|
||||
|
||||
ps-am:
|
||||
|
||||
uninstall-am:
|
||||
|
||||
.MAKE: install-am install-strip
|
||||
|
||||
.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \
|
||||
clean-libtool clean-noinstLTLIBRARIES ctags distclean \
|
||||
distclean-compile distclean-generic distclean-libtool \
|
||||
distclean-tags distdir dvi dvi-am html html-am info info-am \
|
||||
install install-am install-data install-data-am install-dvi \
|
||||
install-dvi-am install-exec install-exec-am install-html \
|
||||
install-html-am install-info install-info-am install-man \
|
||||
install-pdf install-pdf-am install-ps install-ps-am \
|
||||
install-strip installcheck installcheck-am installdirs \
|
||||
maintainer-clean maintainer-clean-generic mostlyclean \
|
||||
mostlyclean-compile mostlyclean-generic mostlyclean-libtool \
|
||||
pdf pdf-am ps ps-am tags uninstall uninstall-am
|
||||
|
||||
|
||||
# Tell versions [3.59,3.63) of GNU make to not export all variables.
|
||||
# Otherwise a system limit (for SysV at least) may be exceeded.
|
||||
.NOEXPORT:
|
||||
@@ -0,0 +1,295 @@
|
||||
/**
|
||||
* Signal.cc
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
* Copyright (C) 2003 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Signal.cc,v 1.11 2007/09/13 02:00:45 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "Signal.h"
|
||||
#include "ELog.h"
|
||||
|
||||
const char rcsId[] = "$Id: Signal.cc,v 1.11 2007/09/13 02:00:45 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
using std::queue ;
|
||||
using std::cout ;
|
||||
using std::endl ;
|
||||
|
||||
// This will need to be changed to work properly in some code.
|
||||
//Signal sig ;
|
||||
|
||||
bool Signal::signalError = false ;
|
||||
int Signal::readFD = -1 ;
|
||||
int Signal::writeFD = -1 ;
|
||||
|
||||
Signal* Signal::theInstance = 0 ;
|
||||
|
||||
pthread_mutex_t Signal::singletonMutex = PTHREAD_MUTEX_INITIALIZER ;
|
||||
pthread_mutex_t Signal::pipeMutex = PTHREAD_MUTEX_INITIALIZER ;
|
||||
|
||||
Signal::Signal()
|
||||
{
|
||||
// No need to initialize signalError here since it is only initialized
|
||||
// directly above, and because this class models the Singleton
|
||||
// pattern.
|
||||
if( !openPipes() )
|
||||
{
|
||||
elog << "Signal> Failed to open FIFO pipes"
|
||||
<< endl ;
|
||||
// Failure to initialize pipes on startup is a critical
|
||||
// failure.
|
||||
::exit( 0 ) ;
|
||||
}
|
||||
|
||||
// Catch some signals
|
||||
#ifdef SIGINT
|
||||
::signal( SIGINT, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGHUP
|
||||
::signal( SIGHUP, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGPIPE
|
||||
::signal( SIGPIPE, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGTERM
|
||||
::signal( SIGTERM, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGPOLL
|
||||
::signal( SIGPOLL, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGUSR1
|
||||
::signal( SIGUSR1, AddSignal ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SIGUSR2
|
||||
::signal( SIGUSR2, AddSignal ) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
Signal::~Signal()
|
||||
{
|
||||
delete theInstance ; theInstance = 0 ;
|
||||
closePipes() ;
|
||||
::pthread_mutex_destroy( &singletonMutex ) ;
|
||||
::pthread_mutex_destroy( &pipeMutex ) ;
|
||||
}
|
||||
|
||||
bool Signal::isError()
|
||||
{
|
||||
return ((-1 == readFD) || (-1 == writeFD) || signalError) ;
|
||||
}
|
||||
|
||||
void Signal::closePipes()
|
||||
{
|
||||
::pthread_mutex_lock( &pipeMutex ) ;
|
||||
::close( readFD ) ;
|
||||
::close( writeFD ) ;
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
|
||||
signalError = true ;
|
||||
}
|
||||
|
||||
bool Signal::openPipes()
|
||||
{
|
||||
int rwFD[ 2 ] = { 0, 0 } ;
|
||||
|
||||
// Create the pipe
|
||||
::pthread_mutex_lock( &pipeMutex ) ;
|
||||
int pipeRet = ::pipe( rwFD ) ;
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
|
||||
if( pipeRet < 0 )
|
||||
{
|
||||
elog << "Signal::openPipes> pipe() failed: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
signalError = true ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Set the fd's to non-blocking
|
||||
::pthread_mutex_lock( &pipeMutex ) ;
|
||||
|
||||
for( size_t i = 0 ; i < 2 ; ++i )
|
||||
{
|
||||
// Get current flags
|
||||
int flags = ::fcntl( rwFD[ i ], F_GETFL ) ;
|
||||
if( flags < 0 )
|
||||
{
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
elog << "Signal::openPipes> Failed to get flags "
|
||||
<< "for pipe fd: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
closePipes() ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Update flags to indicate non-blocking
|
||||
flags |= O_NONBLOCK ;
|
||||
|
||||
// Set new flags
|
||||
if( ::fcntl( rwFD[ i ], F_SETFL, flags ) < 0 )
|
||||
{
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
elog << "Signal::openPipes> Failed to set flags "
|
||||
<< "on pipe fd: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
closePipes() ;
|
||||
return false ;
|
||||
}
|
||||
} // for()
|
||||
|
||||
// All is well
|
||||
readFD = rwFD[ 0 ] ;
|
||||
writeFD = rwFD[ 1 ] ;
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
|
||||
signalError = false ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
/**
|
||||
* AddSignal() will not modify the open/close states of the pipes.
|
||||
* If any error is detected, then signalError will be set to true
|
||||
* and GetSignal() must handle the rest.
|
||||
*/
|
||||
void Signal::AddSignal( int whichSig )
|
||||
{
|
||||
errno = 0 ;
|
||||
|
||||
// Attempt to write this signal to the pipe.
|
||||
if( ::write( writeFD, &whichSig, sizeof( int ) ) < 0 )
|
||||
{
|
||||
// Check for non-blocking type errors.
|
||||
if( (EINTR != errno) && (EWOULDBLOCK != errno) &&
|
||||
(EINTR != EAGAIN) )
|
||||
{
|
||||
// Critical error occured, readFD no longer valid
|
||||
signalError = true ;
|
||||
|
||||
// Do not close the pipes here, the readFD may be
|
||||
// in use. Let getSignal() handle updating the pipes.
|
||||
|
||||
// Do not attempt to create new pipes inside
|
||||
// of the signal handler.
|
||||
// The getSignal() below will attempt to reopen
|
||||
// the pipes if it detects that one is closed.
|
||||
return ;
|
||||
} // if( errno )
|
||||
} // if( ::read() )
|
||||
|
||||
// In general, outputting data during a signal handler is a very
|
||||
// bad idea.
|
||||
// This is for testing purposes only.
|
||||
//std::cerr << "Signal::AddSignal> Successfully sent signal: "
|
||||
// << whichSig
|
||||
// << endl ;
|
||||
} // AddSignal()
|
||||
|
||||
/**
|
||||
* The semantics of the return statement are a little backwards here:
|
||||
* - true indicates that the caller should check the value of theSignal
|
||||
* for either a new signal or an error state (-1)
|
||||
* - false indicates that no error and no signal are ready
|
||||
*/
|
||||
bool Signal::getSignal( int& theSignal )
|
||||
{
|
||||
errno = 0 ;
|
||||
|
||||
// Attempt to read the next signal from the pipe
|
||||
::pthread_mutex_lock( &pipeMutex ) ;
|
||||
int readResult = ::read( readFD, &theSignal, sizeof( int ) ) ;
|
||||
::pthread_mutex_unlock( &pipeMutex ) ;
|
||||
|
||||
if( readResult < 0 )
|
||||
{
|
||||
// Check for non-blocking type errors.
|
||||
if( (EINTR != errno) && (EWOULDBLOCK != errno) &&
|
||||
(EINTR != EAGAIN) )
|
||||
{
|
||||
// Critical error occured, readFD no longer valid
|
||||
|
||||
// closePipes() will set signalError to true.
|
||||
closePipes() ;
|
||||
|
||||
// Attempt to re-open the pipes
|
||||
openPipes() ;
|
||||
|
||||
theSignal = -1 ;
|
||||
return true ;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No signal detected
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
if( readResult != sizeof( int ) )
|
||||
{
|
||||
elog << "Signal::getSignal> Somehow read "
|
||||
<< readResult
|
||||
<< " bytes, where sizeof(int) is: "
|
||||
<< sizeof( int )
|
||||
<< endl ;
|
||||
}
|
||||
|
||||
// Signal detected, read() returned ok
|
||||
return true ;
|
||||
}
|
||||
|
||||
Signal* Signal::getInstance()
|
||||
{
|
||||
::pthread_mutex_lock( &singletonMutex ) ;
|
||||
if( 0 == theInstance )
|
||||
{
|
||||
theInstance = new Signal ;
|
||||
}
|
||||
::pthread_mutex_unlock( &singletonMutex ) ;
|
||||
|
||||
return theInstance ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* Signal.h
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
* Copyright (C) 2003 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: Signal.h,v 1.5 2003/12/17 18:21:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __SIGNAL_H
|
||||
#define __SIGNAL_H "$Id: Signal.h,v 1.5 2003/12/17 18:21:36 dan_karrels Exp $"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* A class used to safely handle asynchronous (non-realtime) signals.
|
||||
* It uses a nonblocking pipe (rather than other solutions, see
|
||||
* my thesis) to solve the multiple consumer, single nonblocking
|
||||
* producer p/c problem.
|
||||
*/
|
||||
class Signal
|
||||
{
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* This variable is true if there exists an uncoverable error.
|
||||
*/
|
||||
static bool signalError ;
|
||||
|
||||
/**
|
||||
* The FD for the read side of the pipe.
|
||||
*/
|
||||
static int readFD ;
|
||||
|
||||
/**
|
||||
* The FD for the write side of the pipe.
|
||||
*/
|
||||
static int writeFD ;
|
||||
|
||||
/**
|
||||
* A mutex to guard access to the Singleton.
|
||||
*/
|
||||
static pthread_mutex_t singletonMutex ;
|
||||
|
||||
/**
|
||||
* The Singleton instance.
|
||||
*/
|
||||
static Signal* theInstance ;
|
||||
|
||||
/**
|
||||
* This mutex guards from multiple threads performing a get()
|
||||
*/
|
||||
static pthread_mutex_t pipeMutex ;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Release resources associated with this class.
|
||||
*/
|
||||
virtual ~Signal() ;
|
||||
|
||||
/**
|
||||
* Retrieve the Singleton instance of this class, creating
|
||||
* it if necessary.
|
||||
* Once an instance is created, all signals currently
|
||||
* supported by this class will be remapped to this subsystem.
|
||||
*/
|
||||
static Signal* getInstance() ;
|
||||
|
||||
/**
|
||||
* Add a signal to the signal queue.
|
||||
* This method is meant to be called *only* from the
|
||||
* asynchronous signal handler.
|
||||
*/
|
||||
static void AddSignal( int whichSig ) ;
|
||||
|
||||
/**
|
||||
* The semantics of the return statement are a little backwards here:
|
||||
* - true indicates that the caller should check the value of
|
||||
* theSignal for either a new signal or an error state (-1)
|
||||
* - false indicates that no error and no signal are ready
|
||||
*/
|
||||
static bool getSignal( int& theSignal ) ;
|
||||
|
||||
/**
|
||||
* This method will return true if there is a non-recoverable
|
||||
* error in the signal handler subsystem.
|
||||
*/
|
||||
static bool isError() ;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Private constructor, make this class a Singleton.
|
||||
*/
|
||||
Signal() ;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Convenience method to close the pipes when a critical
|
||||
* error occurs. In this case, signalError will remain
|
||||
* set, and both pipes are invalid, no signal delivery
|
||||
* will occur.
|
||||
*/
|
||||
static void closePipes() ;
|
||||
|
||||
/**
|
||||
* Opens both pipes, and configures in nonblocking mode.
|
||||
*/
|
||||
static bool openPipes() ;
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __SIGNAL_H
|
||||
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* StringTokenizer.cpp
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: StringTokenizer.cc,v 1.7 2005/02/20 15:49:21 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <new>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <cassert>
|
||||
#include "StringTokenizer.h"
|
||||
|
||||
const char rcsId[] = "$Id: StringTokenizer.cc,v 1.7 2005/02/20 15:49:21 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::string ;
|
||||
using std::vector ;
|
||||
|
||||
/**
|
||||
* StringTokenizer()
|
||||
* Arguments --
|
||||
* buf: the string which we will be tokenizing
|
||||
* delimiter: The specified delimiter by which
|
||||
* we will tokenize the string
|
||||
*/
|
||||
StringTokenizer::StringTokenizer( const string& buf, char _delimiter )
|
||||
: delimiter( _delimiter )
|
||||
{
|
||||
Tokenize( buf ) ;
|
||||
}
|
||||
|
||||
StringTokenizer::~StringTokenizer()
|
||||
{ /* No heap space allocated */ }
|
||||
|
||||
/**
|
||||
* getToken()
|
||||
* Return an individual token to the calling
|
||||
* method.
|
||||
* This method will deliberately drop core in debug
|
||||
* mode if sub is out of range.
|
||||
*/
|
||||
const string& StringTokenizer::getToken( const size_type& sub ) const
|
||||
{
|
||||
// Dump core
|
||||
assert( validSubscript( sub ) ) ;
|
||||
return array[ sub ] ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize()
|
||||
* Break the string into elements delimited
|
||||
* by the given delimiter.
|
||||
* Fill the array with individual tokens.
|
||||
* Rewritten on 5/09/00, much faster now, fewer
|
||||
* function calls, and more linear algorithm
|
||||
* complexity (though still not quite linear).
|
||||
*/
|
||||
void StringTokenizer::Tokenize( const string& buf )
|
||||
{
|
||||
// Make sure that there is something
|
||||
// worth tokenizing
|
||||
if( buf.empty() )
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
// addMe is the string which will be added to the vector
|
||||
// Initialize it to buf.size() empty characters to speed
|
||||
// up addition of characters onto the string
|
||||
// This method is only called once for the life of this object,
|
||||
// so no need for memory of any kind here.
|
||||
char* addMe = new (std::nothrow) char[ buf.size() + 1 ] ;
|
||||
assert( addMe != 0 ) ;
|
||||
|
||||
addMe[ 0 ] = 0 ;
|
||||
|
||||
// (addMePtr) is used to walk down the addMe buffer, adding new
|
||||
// characters to the end.
|
||||
// (addMePtr) always points to the next location in the addMe ptr
|
||||
// in which a character may be placed.
|
||||
char* addMePtr = addMe ;
|
||||
|
||||
// currentPtr is the current character pointer
|
||||
string::const_iterator currentPtr = buf.begin() ;
|
||||
|
||||
// endPtr is the end of the string, currentPtr is valid on
|
||||
// [buf.begin(), buf.end())
|
||||
string::const_iterator endPtr = buf.end() ;
|
||||
|
||||
// Iterate through the entire string
|
||||
for( ; currentPtr != endPtr ; ++currentPtr )
|
||||
{
|
||||
|
||||
// Is this the delimiter for which we are searching?
|
||||
if( delimiter == *currentPtr )
|
||||
{
|
||||
// We have reached a delimiter
|
||||
// Null terminate the token
|
||||
*addMePtr = 0 ;
|
||||
|
||||
// Is this an empty token?
|
||||
if( addMe[ 0 ] != 0 )
|
||||
{
|
||||
// Nope, go ahead and add it to the vector
|
||||
array.push_back( addMe ) ;
|
||||
|
||||
addMePtr = addMe ;
|
||||
*addMePtr = 0 ;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, no delimiter was found...just add this
|
||||
// character to addMe
|
||||
else
|
||||
{
|
||||
*addMePtr = *currentPtr ;
|
||||
++addMePtr ;
|
||||
}
|
||||
}
|
||||
|
||||
// Null terminate, if necessary
|
||||
*addMePtr = 0 ;
|
||||
|
||||
// currentPtr == endPtr
|
||||
// Make sure to check for last token
|
||||
if( addMe[ 0 ] != 0 )
|
||||
{
|
||||
array.push_back( addMe ) ;
|
||||
}
|
||||
|
||||
// This point is always reached, excluding a crash
|
||||
delete[] addMe ;
|
||||
}
|
||||
|
||||
/**
|
||||
* assemble()
|
||||
* Assemble into a string all tokens starting from index
|
||||
* (start).
|
||||
*/
|
||||
string StringTokenizer::assemble( const size_type& start, int end ) const
|
||||
{
|
||||
|
||||
// check if the beginning index is valid
|
||||
if( !validSubscript( start ) )
|
||||
{
|
||||
return string() ;
|
||||
}
|
||||
|
||||
if(end < 0)
|
||||
{
|
||||
end = size();
|
||||
}
|
||||
else if(!validSubscript(end-1))
|
||||
{
|
||||
return string();
|
||||
}
|
||||
|
||||
// retMe will be returned at the end of the method
|
||||
string retMe ;
|
||||
retMe.reserve( totalChars() ) ;
|
||||
|
||||
// continue while there are more tokens to concatenate
|
||||
for( size_type i = start ; i < end ; i++ )
|
||||
{
|
||||
// Add this token to the returning string
|
||||
retMe += array[ i ] ;
|
||||
|
||||
// If there is another token, put a delimiter here
|
||||
if( (i + 1) < end )
|
||||
{
|
||||
retMe += delimiter ;
|
||||
}
|
||||
|
||||
} // close for
|
||||
|
||||
// Return the assembled string
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
StringTokenizer::size_type StringTokenizer::totalChars() const
|
||||
{
|
||||
size_type numChars = 0 ;
|
||||
|
||||
for( const_iterator currentPtr = begin() ; currentPtr != end() ;
|
||||
++currentPtr )
|
||||
{
|
||||
numChars += (*currentPtr).size() ;
|
||||
}
|
||||
|
||||
// Account for delimiters
|
||||
numChars += size() - 1 ;
|
||||
|
||||
return numChars ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* StringTokenizer.h
|
||||
* Author: Daniel Karrels dan@karrels.com
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: StringTokenizer.h,v 1.6 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __STRINGTOKENIZER_H
|
||||
#define __STRINGTOKENIZER_H "$Id: StringTokenizer.h,v 1.6 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* This class provides a clean mechanism for parsing
|
||||
* C++ strings based on a given delimiter.
|
||||
* It overloads operator[], and does not affect the
|
||||
* original string passed as argument to the constructor.
|
||||
* The tokens maintained by this class are zero indexed.
|
||||
* This class is immutable.
|
||||
*/
|
||||
class StringTokenizer
|
||||
{
|
||||
|
||||
private:
|
||||
/**
|
||||
* This is the type that will be used to store
|
||||
* the tokens in the StringTokenizer object.
|
||||
*/
|
||||
typedef std::vector< std::string > vectorType ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor receives the string to be
|
||||
* tokenized, and the delimiter by which
|
||||
* tokens will be generated.
|
||||
*/
|
||||
StringTokenizer( const std::string& = std::string(), char = ' ' ) ;
|
||||
|
||||
/**
|
||||
* The destructor is a NOOP because no streams have been
|
||||
* opened, and no memory dynamically explicitly allocated.
|
||||
*/
|
||||
virtual ~StringTokenizer() ;
|
||||
|
||||
/**
|
||||
* This is the type of the variable used for
|
||||
* representing the size (number of tokens) of
|
||||
* the StringTokenizer object.
|
||||
*/
|
||||
typedef vectorType::size_type size_type ;
|
||||
|
||||
/**
|
||||
* Retrieve a const reference to a given token, zero-indexed.
|
||||
* This method will assert(false) if the requested index is
|
||||
* out of bounds. This is for debugging, and may be conditionally
|
||||
* compiled at a later time to throw an exception while
|
||||
* in a production environment.
|
||||
*/
|
||||
const std::string& getToken( const size_type& ) const ;
|
||||
|
||||
/**
|
||||
* This method allows StringTokenizer objects to be used like
|
||||
* arrays. This method just calls the getToken() method, and
|
||||
* its semantics are the same.
|
||||
*/
|
||||
inline const std::string& operator[]( const size_type& sub ) const
|
||||
{ return getToken( sub ) ; }
|
||||
|
||||
/**
|
||||
* Return a const reference to the original C++ string before
|
||||
* tokenization.
|
||||
* A copy of the original string is *not* kept because, in
|
||||
* the setting in which this class was designed, getOriginal()
|
||||
* is never called. This will free up some processing time
|
||||
* spent copying the original string.
|
||||
*/
|
||||
inline const std::string getOriginal() const
|
||||
{ return assemble() ; }
|
||||
|
||||
/**
|
||||
* Return the number of tokens in this StringTokenizer object.
|
||||
*/
|
||||
inline size_type size() const
|
||||
{ return array.size() ; }
|
||||
|
||||
/**
|
||||
* Return true if the StringTokenizer holds no tokens,
|
||||
* false otherwise.
|
||||
* This is equivalent to (size() == 0).
|
||||
*/
|
||||
inline bool empty() const
|
||||
{ return array.empty() ; }
|
||||
|
||||
/**
|
||||
* Determine if the subscript argument is within the
|
||||
* bounds [0,size()).
|
||||
* Return true if so, false otherwise.
|
||||
* This method works also even if the StringTokenizer is empty.
|
||||
*/
|
||||
inline bool validSubscript( const size_type& sub ) const
|
||||
{ return (sub < size()) ; }
|
||||
|
||||
/**
|
||||
* This method builds and returns a C++ string starting at the given
|
||||
* index, and continuing until the last token, placing the
|
||||
* appropriate delimiter between each token.
|
||||
* With no argument supplied, assemble() will return the entire
|
||||
* original string, delimiters included.
|
||||
*/
|
||||
std::string assemble( const size_type& = 0, int = -1 ) const ;
|
||||
|
||||
/**
|
||||
* The immutable iterator type to use for walking through
|
||||
* this object's tokens.
|
||||
*/
|
||||
typedef vectorType::const_iterator const_iterator ;
|
||||
|
||||
/**
|
||||
* The immutable reverse iterator type to use for walking through
|
||||
* this object's tokens, in reverse.
|
||||
*/
|
||||
typedef vectorType::const_reverse_iterator const_reverse_iterator ;
|
||||
|
||||
/**
|
||||
* Retrieve an immutable iterator to the beginning of this
|
||||
* object's token structure.
|
||||
*/
|
||||
inline const_iterator begin() const
|
||||
{ return array.begin() ; }
|
||||
|
||||
/**
|
||||
* Retrieve an immutable iterator to the end of this object's
|
||||
* token structure.
|
||||
*/
|
||||
inline const_iterator end() const
|
||||
{ return array.end() ; }
|
||||
|
||||
/**
|
||||
* Retrieve an immutable reverse iterator to the
|
||||
* reverse beginning of this object's token structure.
|
||||
*/
|
||||
inline const_reverse_iterator rbegin() const
|
||||
{ return array.rbegin() ; }
|
||||
|
||||
/**
|
||||
* Retrieve an immutable reverse iterator to the reverse
|
||||
* end of this object's token structure.
|
||||
*/
|
||||
inline const_reverse_iterator rend() const
|
||||
{ return array.rend() ; }
|
||||
|
||||
/**
|
||||
* Return the total number of characters for all tokens,
|
||||
* including the delimiters.
|
||||
*/
|
||||
inline size_type totalChars() const ;
|
||||
|
||||
/**
|
||||
* Convenience method for debugging purposes.
|
||||
*/
|
||||
friend std::ostream& operator<<( std::ostream& out,
|
||||
const StringTokenizer& rhs )
|
||||
{
|
||||
for( size_type i = 0, end = rhs.size() ; i < end ; ++i )
|
||||
{
|
||||
out << rhs.array[ i ] ;
|
||||
if( i < (rhs.size() - 1) )
|
||||
{
|
||||
out << rhs.delimiter ;
|
||||
}
|
||||
}
|
||||
return out ;
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Protected method called internally by the constructor once
|
||||
* at object instantiation to tokenize the given C++ string.
|
||||
*/
|
||||
virtual void Tokenize( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* The delimiter by which the (original) string is tokenized.
|
||||
*/
|
||||
char delimiter ;
|
||||
|
||||
/**
|
||||
* The structure for holding the tokens.
|
||||
*/
|
||||
vectorType array ;
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif /* __STRINGTOKENIZER_H */
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* gThread.cc
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: gThread.cc,v 1.4 2003/06/17 15:13:53 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <cassert>
|
||||
|
||||
#include "gThread.h"
|
||||
#include "ELog.h"
|
||||
|
||||
const char rcsId[] = "$Id: gThread.cc,v 1.4 2003/06/17 15:13:53 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::string ;
|
||||
using std::cout ;
|
||||
using std::endl ;
|
||||
|
||||
/**
|
||||
* This function issues the call back to the gThread subclass, and
|
||||
* is executed in a separate thread of execution.
|
||||
*/
|
||||
void* stub( void* arg )
|
||||
{
|
||||
assert( arg != 0 ) ;
|
||||
|
||||
gThread* theThread = reinterpret_cast< gThread* >( arg ) ;
|
||||
theThread->Exec() ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
gThread::gThread()
|
||||
{
|
||||
keepRunning = true ;
|
||||
isShutdownComplete = false ;
|
||||
threadID = 0 ;
|
||||
|
||||
if( ::pthread_mutex_init( &waitMutex, 0 ) != 0 )
|
||||
{
|
||||
elog << "gThread> Unable to initialize waitMutex: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
::exit( -1 ) ;
|
||||
}
|
||||
}
|
||||
|
||||
gThread::~gThread()
|
||||
{
|
||||
Join() ;
|
||||
DestroyAllMutexes() ;
|
||||
// destroy mutexes
|
||||
if( !isShutdownComplete )
|
||||
{
|
||||
Join() ;
|
||||
}
|
||||
|
||||
::pthread_mutex_destroy( &waitMutex ) ;
|
||||
}
|
||||
|
||||
bool gThread::CreateMutex( const string& mutexName )
|
||||
{
|
||||
if( mutexName.empty() )
|
||||
{
|
||||
// Invalid name
|
||||
return false ;
|
||||
}
|
||||
|
||||
if( mutexMap.find( mutexName ) != mutexMap.end() )
|
||||
{
|
||||
// Mutex already exists for that name
|
||||
return false ;
|
||||
}
|
||||
|
||||
pthread_mutex_t newMutex ;
|
||||
::pthread_mutex_init( &newMutex, 0 ) ;
|
||||
|
||||
if( !mutexMap.insert(
|
||||
mutexMapType::value_type( mutexName, newMutex ) ).second )
|
||||
{
|
||||
// Insertion into map failed
|
||||
::pthread_mutex_destroy( &newMutex ) ;
|
||||
|
||||
return false ;
|
||||
}
|
||||
|
||||
// All is well
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool gThread::DestroyMutex( const string& mutexName )
|
||||
{
|
||||
mutexIterator mItr = mutexMap.find( mutexName ) ;
|
||||
if( mItr == mutexMap.end() )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
bool retMe = true ;
|
||||
if( ::pthread_mutex_destroy( &(mItr->second) ) != 0 )
|
||||
{
|
||||
elog << "gThread::DestroyMutex> Unable to destroy mutex: "
|
||||
<< mutexName
|
||||
<< " because: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
retMe = false ;
|
||||
}
|
||||
|
||||
// Remove it from the map either way
|
||||
mutexMap.erase( mItr ) ;
|
||||
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
void gThread::DestroyAllMutexes()
|
||||
{
|
||||
for( mutexIterator mItr = mutexMap.begin() ; mItr != mutexMap.end() ;
|
||||
++mItr )
|
||||
{
|
||||
if( ::pthread_mutex_destroy( &(mItr->second) ) != 0 )
|
||||
{
|
||||
elog << "gThread::DestroyAllMutexes> "
|
||||
<< "pthread_mutex_destroy() failed: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
}
|
||||
}
|
||||
mutexMap.clear() ;
|
||||
}
|
||||
|
||||
void gThread::LockMutex( const string& mutexName )
|
||||
{
|
||||
mutexIterator mItr = mutexMap.find( mutexName ) ;
|
||||
if( mItr == mutexMap.end() )
|
||||
{
|
||||
elog << "gThread::LockMutex> Unable to locate mutex: "
|
||||
<< mutexName
|
||||
<< endl ;
|
||||
return ;
|
||||
}
|
||||
|
||||
::pthread_mutex_lock( &(mItr->second) ) ;
|
||||
}
|
||||
|
||||
void gThread::UnLockMutex( const string& mutexName )
|
||||
{
|
||||
mutexIterator mItr = mutexMap.find( mutexName ) ;
|
||||
if( mItr == mutexMap.end() )
|
||||
{
|
||||
elog << "gThread::UnLockMutex> Unable to locate mutex: "
|
||||
<< mutexName
|
||||
<< endl ;
|
||||
return ;
|
||||
}
|
||||
|
||||
::pthread_mutex_unlock( &(mItr->second) ) ;
|
||||
}
|
||||
|
||||
bool gThread::Start()
|
||||
{
|
||||
if( ::pthread_create( &threadID, 0, stub,
|
||||
reinterpret_cast< void* >( this ) ) != 0 )
|
||||
{
|
||||
elog << "gThread::Start> pthread_create() failed: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
|
||||
threadID = 0 ;
|
||||
return false ;
|
||||
}
|
||||
return true ;
|
||||
}
|
||||
|
||||
void gThread::Join()
|
||||
{
|
||||
if( isShutdownComplete || (0 == threadID) )
|
||||
{
|
||||
// Not running, or never started
|
||||
return ;
|
||||
}
|
||||
|
||||
if( ::pthread_join( threadID, 0 ) != 0 )
|
||||
{
|
||||
elog << "gThread::Join> join() failed: "
|
||||
<< strerror( errno )
|
||||
<< endl ;
|
||||
}
|
||||
isShutdownComplete = true ;
|
||||
}
|
||||
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* gThread.h
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: gThread.h,v 1.5 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __GTHREAD_H
|
||||
#define __GTHREAD_H "$Id: gThread.h,v 1.5 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* The base class for creating a thread.
|
||||
* Simply subclass this class and overload the Exec()
|
||||
* method, and call Start() to have a running thread.
|
||||
* Once started, each thread is responsible for monitoring
|
||||
* this object's 'keepRunning' variable. It should
|
||||
* poll this variable (note: do not block indefinitely)
|
||||
* and return from its Exec() method when keepRunning
|
||||
* is set to false; this indicates that a Stop() request
|
||||
* has been issued.
|
||||
*/
|
||||
class gThread
|
||||
{
|
||||
|
||||
protected:
|
||||
/**
|
||||
* The type used to store mutexes.
|
||||
*/
|
||||
typedef std::map< std::string, pthread_mutex_t, noCaseCompare >
|
||||
mutexMapType ;
|
||||
|
||||
/**
|
||||
* The iterator type to the mutex map.
|
||||
*/
|
||||
typedef mutexMapType::iterator mutexIterator ;
|
||||
|
||||
/**
|
||||
* The const_iterator type to the mutex map.
|
||||
*/
|
||||
typedef mutexMapType::const_iterator constMutexIterator ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
gThread() ;
|
||||
|
||||
/**
|
||||
* The destructor will stop the thread, issue a join(), and
|
||||
* destroy any mutexes.
|
||||
*/
|
||||
virtual ~gThread() ;
|
||||
|
||||
/**
|
||||
* Invoke this method to begin the Exec() method in a new
|
||||
* thread.
|
||||
*/
|
||||
virtual bool Start() ;
|
||||
|
||||
/**
|
||||
* Return true if this thread is running, false otherwise.
|
||||
*/
|
||||
virtual bool isRunning() const
|
||||
{ return (keepRunning && (threadID != 0)) ; }
|
||||
|
||||
/**
|
||||
* Request that this thread stop processing. This could take
|
||||
* an arbitrary length of time to fully stop, depending on how
|
||||
* well the thread subclass follows directions (see above).
|
||||
*/
|
||||
virtual void Stop()
|
||||
{ keepRunning = false ; }
|
||||
|
||||
/**
|
||||
* Issue a pthread_join() on this (already stopped) thread to
|
||||
* cleanup.
|
||||
*/
|
||||
virtual void Join() ;
|
||||
|
||||
/**
|
||||
* Create a mutex with the given case insensitive name.
|
||||
* True is returned if successfull, false otherwise.
|
||||
*/
|
||||
virtual bool CreateMutex( const std::string& mutexName ) ;
|
||||
|
||||
/**
|
||||
* Destroy a mutex with the given case insensitive name.
|
||||
* True is returned if successfull, false otherwise.
|
||||
*/
|
||||
virtual bool DestroyMutex( const std::string& mutexName ) ;
|
||||
|
||||
/**
|
||||
* Destroy all active mutexes.
|
||||
*/
|
||||
virtual void DestroyAllMutexes() ;
|
||||
|
||||
/**
|
||||
* Locks the mutex specified by mutexName.
|
||||
* This method blocks indefinitely until the lock is obtained.
|
||||
*/
|
||||
virtual void LockMutex( const std::string& mutexName )
|
||||
;
|
||||
|
||||
/**
|
||||
* Unlocks the mutex specified by mutexName.
|
||||
* This method will return immediately.
|
||||
*/
|
||||
virtual void UnLockMutex( const std::string& mutexName ) ;
|
||||
|
||||
/**
|
||||
* This method is invoked in a new thread when the Start()
|
||||
* method is invoked. Subclasses must overload this method
|
||||
* to perform the meaningful duty of the thread proper.
|
||||
* This method must halt when keepRunning is set to false.
|
||||
*/
|
||||
virtual void Exec() {}
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* The running status of the thread. Subclasses must honor
|
||||
* the value of this variable.
|
||||
*/
|
||||
bool keepRunning ;
|
||||
|
||||
/**
|
||||
* True if shutdown has completed (keepRunning request has
|
||||
* been honored), and it is clear to issue a join(), false
|
||||
* otherwise.
|
||||
*/
|
||||
bool isShutdownComplete ;
|
||||
|
||||
/**
|
||||
* The thread ID for this thread, 0 if not running.
|
||||
*/
|
||||
pthread_t threadID ;
|
||||
|
||||
/**
|
||||
* The container used to store the active mutexes.
|
||||
*/
|
||||
mutexMapType mutexMap ;
|
||||
|
||||
/**
|
||||
* A general purpose mutex on which to wait.
|
||||
*/
|
||||
pthread_mutex_t waitMutex ;
|
||||
|
||||
} ; // class gThread
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __GTHREAD_H
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* IRC - Internet Relay Chat, include/ircd_chattr.h
|
||||
* Copyright (C) 1998 Andrea Cocito
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 1, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
* $Id: ircd_chattr.h,v 1.1 2002/08/07 20:28:06 dan_karrels Exp $
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* All the code in common.h/common.c is taken from the NTL
|
||||
* (Nemesi's Tools Library), adapted for ircu's character set
|
||||
* and thereafter released under GNU GPL, from there comes the
|
||||
* NTL_ prefix of all macro and object names.
|
||||
* Removed isXdigit() to leave space to other char sets in the
|
||||
* bitmap, should give the same results as isxdigit() on any
|
||||
* implementation and isn't used in IRC anyway.
|
||||
*/
|
||||
#ifndef INCLUDED_ircd_chattr_h
|
||||
#define INCLUDED_ircd_chattr_h
|
||||
#ifndef INCLUDED_limits_h
|
||||
#include <limits.h>
|
||||
#define INCLUDED_limits_h
|
||||
#endif
|
||||
#ifndef INCLUDED_sys_types_h
|
||||
#include <sys/types.h>
|
||||
#define INCLUDED_sys_types_h
|
||||
#endif
|
||||
|
||||
#include "match_table.h"
|
||||
|
||||
/*
|
||||
* Character attribute macros
|
||||
*/
|
||||
#define NTL_ALNUM 0x0001 /* (NTL_ALPHA | NTL_DIGIT) */
|
||||
#define NTL_ALPHA 0x0002 /* (NTL_LOWER | NTL_UPPER) */
|
||||
#define NTL_CNTRL 0x0004 /* \000 - \037 == 0x00 - 0x1F */
|
||||
#define NTL_DIGIT 0x0008 /* 0123456789 */
|
||||
#define NTL_GRAPH 0x0010 /* (NTL_ALNUM | NTL_PUNCT) */
|
||||
#define NTL_LOWER 0x0020 /* abcdefghijklmnopqrstuvwxyz{|}~ */
|
||||
#define NTL_PRINT 0x0040 /* (NTL_GRAPH | ' ') */
|
||||
#define NTL_PUNCT 0x0080 /* !"#$%&'()*+,-./:;<=>?@_` */
|
||||
#define NTL_SPACE 0x0100 /* \011\012\013\014\015\040 */
|
||||
#define NTL_UPPER 0x0200 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^ */
|
||||
#define NTL_IRCCH 0x0400 /* Channel's names charset */
|
||||
#define NTL_IRCCL 0x0800 /* Force toLower() in ch-name */
|
||||
#define NTL_IRCNK 0x1000 /* Nick names charset, aka isvalid() */
|
||||
#define NTL_IRCUI 0x2000 /* UserIDs charset, IRCHN plus tilde */
|
||||
#define NTL_IRCHN 0x4000 /* Hostnames charset (weak, RFC 1033) */
|
||||
#define NTL_IRCIP 0x8000 /* Numeric IPs charset (DIGIT and .) */
|
||||
#define NTL_EOL 0x10000 /* \r\n */
|
||||
#define NTL_KTIME 0x20000 /* Valid character for a k:line time */
|
||||
#define NTL_CHPFX 0x40000 /* channel prefix char # & + */
|
||||
|
||||
/*
|
||||
* Translation macros for channel name case translation
|
||||
* NOTE: Channel names are supposed to be lower case insensitive for
|
||||
* ISO 8859-1 character sets.
|
||||
*/
|
||||
#define ToLower(c) (ToLowerTab_8859_1[(c) - CHAR_MIN])
|
||||
#define ToUpper(c) (ToUpperTab_8859_1[(c) - CHAR_MIN])
|
||||
|
||||
/*
|
||||
* Character classification macros
|
||||
* NOTE: The IsUpper and IsLower macros do not apply to the complete
|
||||
* ISO 8859-1 character set, unlike the ToUpper and ToLower macros above.
|
||||
* IsUpper and IsLower only apply for comparisons of the US ASCII subset.
|
||||
*/
|
||||
#define IsAlnum(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_ALNUM)
|
||||
#define IsAlpha(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_ALPHA)
|
||||
#define IsDigit(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_DIGIT)
|
||||
#define IsLower(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_LOWER)
|
||||
#define IsSpace(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_SPACE)
|
||||
#define IsUpper(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_UPPER)
|
||||
#define IsCntrl(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_CNTRL)
|
||||
|
||||
#define IsChannelChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCCH)
|
||||
#define IsChannelLower(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCCL)
|
||||
#define IsChannelPrefix(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_CHPFX)
|
||||
#define IsNickChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCNK)
|
||||
#define IsUserChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCUI)
|
||||
#define IsHostChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCHN)
|
||||
#define IsIPChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_IRCIP)
|
||||
#define IsEol(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_EOL)
|
||||
#define IsKTimeChar(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_KTIME)
|
||||
|
||||
#endif /* INCLUDED_ircd_chattr_h */
|
||||
@@ -0,0 +1,979 @@
|
||||
/*
|
||||
* IRC - Internet Relay Chat, common/match.c
|
||||
* Copyright (C) 1990 Jarkko Oikarinen
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 1, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
* $Id: match.cc,v 1.10 2007/09/13 02:00:45 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "match.h"
|
||||
#include "ConnectionManager.h"
|
||||
|
||||
const char rcsId[] = "$Id: match.cc,v 1.10 2007/09/13 02:00:45 dan_karrels Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
int match( const std::string& s1, const std::string& s2 )
|
||||
{
|
||||
if( s1.empty() || s2.empty() )
|
||||
{
|
||||
return 1 ;
|
||||
}
|
||||
return match( s1.c_str(), s2.c_str() ) ;
|
||||
}
|
||||
|
||||
int casematch( const std::string& s1, const std::string& s2 )
|
||||
{
|
||||
if( s1.empty() || s2.empty() )
|
||||
{
|
||||
return 1 ;
|
||||
}
|
||||
return casematch( s1.c_str(), s2.c_str() ) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare if a given string (name) matches the given
|
||||
* mask (which can contain wild cards: '*' - match any
|
||||
* number of chars, '?' - match any single character.
|
||||
*
|
||||
* return 0, if match
|
||||
* 1, if no match
|
||||
*/
|
||||
|
||||
/*
|
||||
* match
|
||||
*
|
||||
* Rewritten by Andrea Cocito (Nemesi), November 1998.
|
||||
*
|
||||
*/
|
||||
|
||||
/****************** Nemesi's match() ***************/
|
||||
|
||||
int match(const char *mask, const char *string)
|
||||
{
|
||||
const char *m = mask, *s = string;
|
||||
char ch;
|
||||
const char *bm, *bs; /* Will be reg anyway on a decent CPU/compiler */
|
||||
bool isIP = true, isCIDR = false;
|
||||
char CIDRip[16];
|
||||
int i = 0, CIDRmask = 0, dots = 0;
|
||||
int client_addr[4] = { 0 };
|
||||
unsigned long mask_ip, client_ip;
|
||||
|
||||
/* check if the mask is a CIDR mask (also if it's an IP) */
|
||||
while ((ch = *m++))
|
||||
{
|
||||
if (ch == '.')
|
||||
{
|
||||
dots++;
|
||||
if (dots > 3)
|
||||
{
|
||||
isIP = false; /* more than 3 dots, can't be an IP */
|
||||
break; /* no point continuing the check as we have the info we want */
|
||||
}
|
||||
}
|
||||
if (ch == '/')
|
||||
{
|
||||
isCIDR = true;
|
||||
break; /* break, preserving location of the slash in m */
|
||||
}
|
||||
if (i <= 15)
|
||||
{
|
||||
CIDRip[i] = ch;
|
||||
i++;
|
||||
}
|
||||
if (isIP && ((ch > '9') || (ch < '0')) && (ch != '.'))
|
||||
{
|
||||
/* not an IP */
|
||||
isIP = false;
|
||||
isCIDR = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isIP)
|
||||
isCIDR = false; /* if it's not an IP, it can't be a CIDR mask! */
|
||||
|
||||
if (isCIDR)
|
||||
{
|
||||
/* we have a CIDR mask, deal with it as such */
|
||||
/* we can only match CIDR masks against IPs - check for an IP */
|
||||
if (ConnectionManager::isIpAddress(string))
|
||||
{
|
||||
/* ok, it's an IP - compute masks etc */
|
||||
CIDRip[i] = '\0';
|
||||
CIDRmask = atoi(m);
|
||||
/* convert IP into integer */
|
||||
i = sscanf(CIDRip, "%d.%d.%d.%d", &client_addr[0], &client_addr[1], &client_addr[2], &client_addr[3]);
|
||||
mask_ip = ntohl((client_addr[0]) | (client_addr[1] << 8) | (client_addr[2] << 16) | (client_addr[3] << 24));
|
||||
i = sscanf(string, "%d.%d.%d.%d", &client_addr[0], &client_addr[1], &client_addr[2], &client_addr[3]);
|
||||
client_ip = ntohl((client_addr[0]) | (client_addr[1] << 8) | (client_addr[2] << 16) | (client_addr[3] << 24));
|
||||
/* time to compare them */
|
||||
for (i = 0; i < (32 - CIDRmask); i++)
|
||||
{
|
||||
/* right shift to drop off the insignificant bits */
|
||||
mask_ip >>= 1;
|
||||
client_ip >>= 1;
|
||||
}
|
||||
for (i = 0; i < (32 - CIDRmask); i++)
|
||||
{
|
||||
/* left shift to restore the ip, but with insignificant bits = 0 */
|
||||
mask_ip <<= 1;
|
||||
client_ip <<= 1;
|
||||
}
|
||||
if (client_ip == mask_ip)
|
||||
{
|
||||
/* cidr matches */
|
||||
return 0;
|
||||
} else {
|
||||
/* cidr doesnt match */
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
m = mask;
|
||||
|
||||
/* Process the "head" of the mask, if any */
|
||||
while ((ch = *m++) && (ch != '*'))
|
||||
switch (ch)
|
||||
{
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
if (ToLower(*s) != ToLower(ch))
|
||||
return 1;
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
};
|
||||
if (!ch)
|
||||
return *s;
|
||||
|
||||
/* We got a star: quickly find if/where we match the next char */
|
||||
got_star:
|
||||
bm = m; /* Next try rollback here */
|
||||
while ((ch = *m++))
|
||||
switch (ch)
|
||||
{
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
case '*':
|
||||
bm = m;
|
||||
continue; /* while */
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
goto break_while; /* C is structured ? */
|
||||
};
|
||||
break_while:
|
||||
if (!ch)
|
||||
return 0; /* mask ends with '*', we got it */
|
||||
ch = ToLower(ch);
|
||||
while (ToLower(*s++) != ch)
|
||||
if (!*s)
|
||||
return 1;
|
||||
bs = s; /* Next try start from here */
|
||||
|
||||
/* Check the rest of the "chunk" */
|
||||
while ((ch = *m++))
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '*':
|
||||
goto got_star;
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
if (ToLower(*s) != ToLower(ch))
|
||||
{
|
||||
m = bm;
|
||||
s = bs;
|
||||
goto got_star;
|
||||
};
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
if (*s)
|
||||
{
|
||||
m = bm;
|
||||
s = bs;
|
||||
goto got_star;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
/****************** Nemesi's match() ***************/
|
||||
|
||||
/* casematch method, I simply removed all ToLower's ... (Seven) */
|
||||
int casematch(const char *mask, const char *string)
|
||||
{
|
||||
const char *m = mask, *s = string;
|
||||
char ch;
|
||||
const char *bm, *bs; /* Will be reg anyway on a decent CPU/compiler */
|
||||
bool isIP = true, isCIDR = false;
|
||||
char CIDRip[16];
|
||||
int i = 0, CIDRmask = 0, dots = 0;
|
||||
int client_addr[4] = { 0 };
|
||||
unsigned long mask_ip, client_ip;
|
||||
|
||||
/* check if the mask is a CIDR mask (also if it's an IP) */
|
||||
while ((ch = *m++))
|
||||
{
|
||||
if (ch == '.')
|
||||
{
|
||||
dots++;
|
||||
if (dots > 3)
|
||||
{
|
||||
isIP = false; /* more than 3 dots, can't be an IP */
|
||||
break; /* no point continuing the check as we have the info we want */
|
||||
}
|
||||
}
|
||||
if (ch == '/')
|
||||
{
|
||||
isCIDR = true;
|
||||
break; /* break, preserving location of the slash in m */
|
||||
}
|
||||
if (i <= 15)
|
||||
{
|
||||
CIDRip[i] = ch;
|
||||
i++;
|
||||
}
|
||||
if (isIP && ((ch > '9') || (ch < '0')) && (ch != '.'))
|
||||
{
|
||||
/* not an IP */
|
||||
isIP = false;
|
||||
isCIDR = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isIP)
|
||||
isCIDR = false; /* if it's not an IP, it can't be a CIDR mask! */
|
||||
|
||||
if (isCIDR)
|
||||
{
|
||||
/* we have a CIDR mask, deal with it as such */
|
||||
/* we can only match CIDR masks against IPs - check for an IP */
|
||||
if (ConnectionManager::isIpAddress(string))
|
||||
{
|
||||
/* ok, it's an IP - compute masks etc */
|
||||
CIDRip[i] = '\0';
|
||||
CIDRmask = atoi(m);
|
||||
/* convert IP into integer */
|
||||
i = sscanf(CIDRip, "%d.%d.%d.%d", &client_addr[0], &client_addr[1], &client_addr[2], &client_addr[3]);
|
||||
mask_ip = ntohl((client_addr[0]) | (client_addr[1] << 8) | (client_addr[2] << 16) | (client_addr[3] << 24));
|
||||
i = sscanf(string, "%d.%d.%d.%d", &client_addr[0], &client_addr[1], &client_addr[2], &client_addr[3]);
|
||||
client_ip = ntohl((client_addr[0]) | (client_addr[1] << 8) | (client_addr[2] << 16) | (client_addr[3] << 24));
|
||||
/* time to compare them */
|
||||
for (i = 0; i < (32 - CIDRmask); i++)
|
||||
{
|
||||
/* right shift to drop off the insignificant bits */
|
||||
mask_ip >>= 1;
|
||||
client_ip >>= 1;
|
||||
}
|
||||
for (i = 0; i < (32 - CIDRmask); i++)
|
||||
{
|
||||
/* left shift to restore the ip, but with insignificant bits = 0 */
|
||||
mask_ip <<= 1;
|
||||
client_ip <<= 1;
|
||||
}
|
||||
if (client_ip == mask_ip)
|
||||
{
|
||||
/* cidr matches */
|
||||
return 0;
|
||||
} else {
|
||||
/* cidr doesnt match */
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
m = mask;
|
||||
|
||||
/* Process the "head" of the mask, if any */
|
||||
while ((ch = *m++) && (ch != '*'))
|
||||
switch (ch)
|
||||
{
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
if ((*s) != (ch))
|
||||
return 1;
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
};
|
||||
if (!ch)
|
||||
return *s;
|
||||
|
||||
/* We got a star: quickly find if/where we match the next char */
|
||||
got_star:
|
||||
bm = m; /* Next try rollback here */
|
||||
while ((ch = *m++))
|
||||
switch (ch)
|
||||
{
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
case '*':
|
||||
bm = m;
|
||||
continue; /* while */
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
goto break_while; /* C is structured ? */
|
||||
};
|
||||
break_while:
|
||||
if (!ch)
|
||||
return 0; /* mask ends with '*', we got it */
|
||||
//ch = ToLower(ch);
|
||||
while ((*s++) != ch)
|
||||
if (!*s)
|
||||
return 1;
|
||||
bs = s; /* Next try start from here */
|
||||
|
||||
/* Check the rest of the "chunk" */
|
||||
while ((ch = *m++))
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '*':
|
||||
goto got_star;
|
||||
case '\\':
|
||||
if (*m == '?' || *m == '*')
|
||||
ch = *m++;
|
||||
default:
|
||||
if ((*s) != (ch))
|
||||
{
|
||||
m = bm;
|
||||
s = bs;
|
||||
goto got_star;
|
||||
};
|
||||
case '?':
|
||||
if (!*s++)
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
if (*s)
|
||||
{
|
||||
m = bm;
|
||||
s = bs;
|
||||
goto got_star;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* collapse()
|
||||
* Collapse a pattern string into minimal components.
|
||||
* This particular version is "in place", so that it changes the pattern
|
||||
* which is to be reduced to a "minimal" size.
|
||||
*
|
||||
* (C) Carlo Wood - 6 Oct 1998
|
||||
* Speedup rewrite by Andrea Cocito, December 1998.
|
||||
* Note that this new optimized alghoritm can *only* work in place.
|
||||
*/
|
||||
|
||||
char *collapse(char *mask)
|
||||
{
|
||||
bool star = false ;
|
||||
char *m = mask;
|
||||
char *b;
|
||||
|
||||
if (m)
|
||||
{
|
||||
do
|
||||
{
|
||||
if ((*m == '*') && ((m[1] == '*') || (m[1] == '?')))
|
||||
{
|
||||
b = m;
|
||||
do
|
||||
{
|
||||
if (*m == '*')
|
||||
star = true;
|
||||
else
|
||||
{
|
||||
if (star && (*m != '?'))
|
||||
{
|
||||
*b++ = '*';
|
||||
star = false;
|
||||
};
|
||||
*b++ = *m;
|
||||
if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
|
||||
*b++ = *++m;
|
||||
};
|
||||
}
|
||||
while (*m++);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
|
||||
m++;
|
||||
};
|
||||
}
|
||||
while (*m++);
|
||||
};
|
||||
return mask;
|
||||
}
|
||||
|
||||
/*
|
||||
***************** Nemesi's matchcomp() / matchexec() **************
|
||||
*/
|
||||
|
||||
/* These functions allow the use of "compiled" masks, you compile a mask
|
||||
* by means of matchcomp() that gets the plain text mask as input and writes
|
||||
* its result in the memory locations addressed by the 3 parameters:
|
||||
* - *cmask will contain the text of the compiled mask
|
||||
* - *minlen will contain the lenght of the shortest string that can match
|
||||
* the mask
|
||||
* - *charset will contain the minimal set of chars needed to match the mask
|
||||
* You can pass NULL as *charset and it will be simply not returned, but you
|
||||
* MUST pass valid pointers for *minlen and *cmask (wich must be big enough
|
||||
* to contain the compiled mask text that is in the worst case as long as the
|
||||
* text of the mask itself in plaintext format) and the return value of
|
||||
* matchcomp() will be the number of chars actually written there (excluded
|
||||
* the trailing zero). cmask can be == mask, matchcomp() can work in place.
|
||||
* The {cmask, minlen} couple of values make the real compiled mask and
|
||||
* need to be passed to the functions that use the compiled mask, if you pass
|
||||
* the wrong minlen or something wrong in cmask to one of these expect a
|
||||
* coredump. This means that when you record a compiled mask you must store
|
||||
* *both* these values.
|
||||
* Once compiled the mask can be used to match a string by means of
|
||||
* matchexec(), it can be printed back to human-readable format by means
|
||||
* of sprintmatch() or it can be compared to another compiled mask by means
|
||||
* of mmexec() that will tell if it completely overrides that mask (a lot like
|
||||
* what mmatch() does for plain text masks).
|
||||
* You can gain a lot of speed in many situations avoiding to matchexec() when:
|
||||
* - The maximum lenght of the field you are about to match() the mask to is
|
||||
* shorter than minlen, in example when matching abc*def*ghil with a nick:
|
||||
* It just cannot match since a nick is at most 9 chars long and the mask
|
||||
* needs at least 10 chars (10 will be the value returned in minlen).
|
||||
* - The charset allowed for the field you are about to match to doesn't
|
||||
* "contain" the charset returned by matchcomp(), in example when you
|
||||
* have *.* as mask it makes no sense to try to match it against a nick
|
||||
* because, again, a nick can't contain a '.', you can check this with
|
||||
* a simple (charset & NTL_IRCNK) in this case.
|
||||
* - As a special case, since compiled masks are forced to lowercase,
|
||||
* it would make no sense to use the NTL_LOWER and NTL_UPPER on a compiled
|
||||
* mask, thus they are reused as follows: if the NTL_LOWER bit of charset
|
||||
* is set it means that the mask contains only non-wilds chars (i.e. you can
|
||||
* use strCasecmp() to match it or a direct hash lookup), if the NTL_UPPER
|
||||
* bit is set it means that it contains only wild chars (and you can
|
||||
* match it with strlen(field)>=minlen).
|
||||
* Do these optimizations ONLY when the data you are about to pass to
|
||||
* matchexec() are *known* to be invalid in advance, using strChattr()
|
||||
* or strlen() on the text would be slower than calling matchexec() directly
|
||||
* and let it fail.
|
||||
* Internally a compiled mask contain in the *cmask area the text of
|
||||
* the plain text form of the mask itself with applied the following hacks:
|
||||
* - All characters are forced to lowercase (so that uppercase letters and
|
||||
* specifically the symbols 'A' and 'Z' are reserved for special use)
|
||||
* - All non-escaped stars '*' are replaced by the letter 'Z'
|
||||
* - All non-escaped question marks '?' are replaced by the letter 'A'
|
||||
* - All escape characters are removed, the wilds escaped by them are
|
||||
* then passed by without the escape since they don't collide anymore
|
||||
* with the real wilds (encoded as A/Z)
|
||||
* - Finally the part of the mask that follows the last asterisk is
|
||||
* reversed (byte order mirroring) and moved right after the first
|
||||
* asterisk.
|
||||
* After all this a mask like: Head*CHUNK1*chu\*nK2*ch??k3*TaIl
|
||||
* .... becomes: headZliatZchunk1Zchu*nk2ZchAAk3
|
||||
* This can still be printed on a console, more or less understood by an
|
||||
* human and handled with the usual str*() library functions.
|
||||
* When you store somewhere the compiled mask you can avoid storing the
|
||||
* textform of it since it can be "decompiled" by means of sprintmatch(),
|
||||
* but at that time the following things are changed in the mask:
|
||||
* - All chars have been forced to lowercase.
|
||||
* - The mask is collapsed.
|
||||
* The balance point of using compiled masks in terms of CPU is when you expect
|
||||
* to use matchexec() instead of match() at least 20 times on the same mask
|
||||
* or when you expect to use mmexec() instead of mmatch() 3 times.
|
||||
*/
|
||||
|
||||
/*
|
||||
* matchcomp()
|
||||
*
|
||||
* Compiles a mask into a form suitable for using in matchexec().
|
||||
*/
|
||||
|
||||
int matchcomp(char *cmask, int *minlen, int *charset, const char *mask)
|
||||
{
|
||||
const char *m = mask;
|
||||
char *b = cmask;
|
||||
char *fs = 0;
|
||||
char *ls = 0;
|
||||
char *x1, *x2;
|
||||
int l1, l2, lmin, loop, sign;
|
||||
bool star = false;
|
||||
int cnt = 0;
|
||||
char ch;
|
||||
int chset = ~0;
|
||||
int chset2 = (NTL_LOWER | NTL_UPPER);
|
||||
|
||||
if (m)
|
||||
while ((ch = *m++))
|
||||
switch (ch)
|
||||
{
|
||||
case '*':
|
||||
star = true;
|
||||
break;
|
||||
case '?':
|
||||
cnt++;
|
||||
*b++ = 'A';
|
||||
chset2 &= ~NTL_LOWER;
|
||||
break;
|
||||
case '\\':
|
||||
if ((*m == '?') || (*m == '*'))
|
||||
ch = *m++;
|
||||
default:
|
||||
if (star)
|
||||
{
|
||||
ls = b;
|
||||
fs = fs ? fs : b;
|
||||
*b++ = 'Z';
|
||||
chset2 &= ~NTL_LOWER;
|
||||
star = false;
|
||||
};
|
||||
cnt++;
|
||||
*b = ToLower(ch);
|
||||
chset &= IRCD_CharAttrTab[*b++ - CHAR_MIN];
|
||||
chset2 &= ~NTL_UPPER;
|
||||
};
|
||||
|
||||
if (charset)
|
||||
*charset = (chset | chset2);
|
||||
|
||||
if (star)
|
||||
{
|
||||
ls = b;
|
||||
fs = (fs ? fs : b);
|
||||
*b++ = 'Z';
|
||||
};
|
||||
|
||||
if (ls)
|
||||
{
|
||||
for (x1 = ls + 1, x2 = (b - 1); x1 < x2; x1++, x2--)
|
||||
{
|
||||
ch = *x1;
|
||||
*x1 = *x2;
|
||||
*x2 = ch;
|
||||
};
|
||||
l1 = (ls - fs);
|
||||
l2 = (b - ls);
|
||||
x1 = fs;
|
||||
while ((lmin = (l1 < l2) ? l1 : l2))
|
||||
{
|
||||
x2 = x1 + l1;
|
||||
for (loop = 0; loop < lmin; loop++)
|
||||
{
|
||||
ch = x1[loop];
|
||||
x1[loop] = x2[loop];
|
||||
x2[loop] = ch;
|
||||
};
|
||||
x1 += lmin;
|
||||
sign = l1 - l2;
|
||||
l1 -= (sign < 0) ? 0 : lmin;
|
||||
l2 -= (sign > 0) ? 0 : lmin;
|
||||
};
|
||||
};
|
||||
|
||||
*b = '\0';
|
||||
*minlen = cnt;
|
||||
return (b - cmask);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* matchexec()
|
||||
*
|
||||
* Executes a match with a mask previosuly compiled with matchcomp()
|
||||
* Note 1: If the mask isn't correctly produced by matchcomp() I will core
|
||||
* Note 2: 'min' MUST be the value returned by matchcomp on that mask,
|
||||
* or.... I will core even faster :-)
|
||||
* Note 3: This piece of code is not intended to be nice but efficient.
|
||||
*/
|
||||
|
||||
int matchexec(const char *string, const char *cmask, int minlen)
|
||||
{
|
||||
const char *s = string - 1;
|
||||
const char *b = cmask - 1;
|
||||
int trash;
|
||||
const char *bb, *bs;
|
||||
char ch;
|
||||
|
||||
tryhead:
|
||||
while ((ToLower(*++s) == *++b) && *s) { /* noop */ };
|
||||
if (!*s)
|
||||
return ((*b != '\0') && ((*b++ != 'Z') || (*b != '\0')));
|
||||
if (*b != 'Z')
|
||||
{
|
||||
if (*b == 'A')
|
||||
goto tryhead;
|
||||
return 1;
|
||||
};
|
||||
|
||||
bs = s;
|
||||
while (*++s) { /* noop */ };
|
||||
|
||||
if ((trash = (s - string - minlen)) < 0)
|
||||
return 2;
|
||||
|
||||
trytail:
|
||||
while ((ToLower(*--s) == *++b) && *b && (ToLower(*--s) == *++b) && *b
|
||||
&& (ToLower(*--s) == *++b) && *b && (ToLower(*--s) == *++b) && *b) { /* noop */ };
|
||||
if (*b != 'Z')
|
||||
{
|
||||
if (*b == 'A')
|
||||
goto trytail;
|
||||
return (*b != '\0');
|
||||
};
|
||||
|
||||
s = --bs;
|
||||
bb = b;
|
||||
|
||||
while ((ch = *++b))
|
||||
{
|
||||
while ((ToLower(*++s) != ch))
|
||||
if (--trash < 0)
|
||||
return 4;
|
||||
bs = s;
|
||||
|
||||
trychunk:
|
||||
while ((ToLower(*++s) == *++b) && *b) { /* noop */ };
|
||||
if (!*b)
|
||||
return 0;
|
||||
if (*b == 'Z')
|
||||
{
|
||||
bs = --s;
|
||||
bb = b;
|
||||
continue;
|
||||
};
|
||||
if (*b == 'A')
|
||||
goto trychunk;
|
||||
|
||||
b = bb;
|
||||
s = bs;
|
||||
if (--trash < 0)
|
||||
return 5;
|
||||
};
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* matchdecomp()
|
||||
* Prints the human readable version of *cmask into *mask, (decompiles
|
||||
* cmask).
|
||||
* The area pointed by *mask MUST be big enough (the mask might be up to
|
||||
* twice the size of its compiled form if it's made all of \? or \*, and
|
||||
* this function can NOT work in place since it might enflate the mask)
|
||||
* The printed mask is not identical to the one that was compiled to cmask,
|
||||
* infact it is 1) forced to all lowercase, 2) collapsed, both things
|
||||
* are supposed to NOT change it's meaning.
|
||||
* It returns the number of chars actually written to *mask;
|
||||
*/
|
||||
|
||||
int matchdecomp(char *mask, const char *cmask)
|
||||
{
|
||||
char *rtb = mask;
|
||||
const char *rcm = cmask;
|
||||
const char *begtail, *endtail;
|
||||
|
||||
if (rtb ==0)
|
||||
return (-1);
|
||||
|
||||
if (rcm == 0)
|
||||
return (-2);
|
||||
|
||||
for (; (*rcm != 'Z'); rcm++, rtb++)
|
||||
{
|
||||
if ((*rcm == '?') || (*rcm == '*'))
|
||||
*rtb++ = '\\';
|
||||
if (!((*rtb = ((*rcm == 'A') ? '?' : *rcm))))
|
||||
return (rtb - mask);
|
||||
};
|
||||
|
||||
begtail = rcm++;
|
||||
*rtb++ = '*';
|
||||
|
||||
while (*rcm && (*rcm != 'Z'))
|
||||
rcm++;
|
||||
|
||||
endtail = rcm;
|
||||
|
||||
if (*rcm)
|
||||
{
|
||||
while (*++rcm)
|
||||
switch (*rcm)
|
||||
{
|
||||
case 'A':
|
||||
*rtb++ = '?';
|
||||
break;
|
||||
case 'Z':
|
||||
*rtb++ = '*';
|
||||
break;
|
||||
case '*':
|
||||
case '?':
|
||||
*rtb++ = '\\';
|
||||
default:
|
||||
*rtb++ = *rcm;
|
||||
};
|
||||
*rtb++ = '*';
|
||||
};
|
||||
|
||||
for (rcm = endtail; (--rcm) > begtail; *rtb++ = ((*rcm == 'A') ? '?' : *rcm))
|
||||
if ((*rcm == '?') || (*rcm == '*'))
|
||||
*rtb++ = '\\';
|
||||
|
||||
*rtb = '\0';
|
||||
return (rtb - mask);
|
||||
}
|
||||
|
||||
/*
|
||||
* mmexec()
|
||||
* Checks if a wider compiled mask (wcm/wminlen) completely overrides
|
||||
* a more restrict one (rcm/rminlen), basically what mmatch() does for
|
||||
* non-compiled masks, returns 0 if the override is true (like mmatch()).
|
||||
* "the wider overrides the restrict" means that any string that matches
|
||||
* the restrict one _will_ also match the wider one, always.
|
||||
* In this we behave differently from mmatch() because in example we return
|
||||
* true for " a?*cd overrides a*bcd " for wich the override happens for how
|
||||
* we literally defined it, here mmatch() would have returned false.
|
||||
* The original concepts and the base alghoritm are copied from mmatch()
|
||||
* written by Run (Carlo Wood), this function is written by
|
||||
* Nemesi (Andrea Cocito)
|
||||
*/
|
||||
|
||||
int mmexec(const char *wcm, int wminlen, const char *rcm, int rminlen)
|
||||
{
|
||||
const char *w, *r, *br, *bw, *rx, *rz;
|
||||
int eat, trash;
|
||||
|
||||
/* First of all rm must have enough non-stars to 'contain' wm */
|
||||
if ((trash = rminlen - wminlen) < 0)
|
||||
return 1;
|
||||
w = wcm;
|
||||
r = rcm;
|
||||
eat = 0;
|
||||
|
||||
/* Let's start the game, remember that '*' is mapped to 'Z', '?'
|
||||
is mapped to 'A' and that head?*??*?chunk*???*tail becomes
|
||||
headAAAAZliatAAAZchunk for compiled masks */
|
||||
|
||||
/* Match the head of wm with the head of rm */
|
||||
for (; (*r) && (*r != 'Z') && ((*w == *r) || (*w == 'A')); r++, w++) { /* noop */ };
|
||||
if (*r == 'Z')
|
||||
while (*w == 'A') /* Eat extra '?' before '*' in wm if got '*' in rm */
|
||||
w++, eat++;
|
||||
if (*w != 'Z') /* head1<any>.. can't match head2<any>.. */
|
||||
return ((*w) || (*r)) ? 1 : 0; /* and head<nul> matches only head<nul> */
|
||||
if (!*++w)
|
||||
return 0; /* headZ<nul> matches head<anything> */
|
||||
|
||||
/* Does rm have any stars in it ? let's check */
|
||||
for (rx = r; *r && (*r != 'Z'); r++) { /* noop */ };
|
||||
if (!*r)
|
||||
{
|
||||
/* rm has no stars and thus isn't a mask but it's just a flat
|
||||
string: special handling occurs here, note that eat must be 0 here */
|
||||
|
||||
/* match the tail */
|
||||
if (*w != 'Z')
|
||||
{
|
||||
for (; r--, (*w) && ((*w == *r) || (*w == 'A')); w++) { /* noop */ };
|
||||
if (*w != 'Z') /* headZliat1<any> fails on head<any>2tail */
|
||||
return (*w) ? 1 : 0; /* but headZliat<nul> matches head<any>tail */
|
||||
}
|
||||
|
||||
/* match the chunks */
|
||||
while (1)
|
||||
{ /* This loop can't break but only return */
|
||||
|
||||
for (bw = w++; (*w != *rx); rx++) /* Seek the 1st char of the chunk */
|
||||
if (--trash < 0) /* See if we can trash one more char of rm */
|
||||
return 1; /* If not we can only fail of course */
|
||||
for (r = ++rx, w++; (*w) && ((*w == *r) || (*w == 'A')); r++, w++) { /* noop */ };
|
||||
if (!*w) /* Did last loop match the rest of chunk ? */
|
||||
return 0; /* ... Yes, end of wm, matched ! */
|
||||
if (*w != 'Z')
|
||||
{ /* ... No, hitted non-star */
|
||||
w = bw; /* Rollback at beginning of chunk */
|
||||
if (--trash < 0) /* Trashed the char where this try started */
|
||||
return 1; /* if we can't trash more chars fail */
|
||||
}
|
||||
else
|
||||
{
|
||||
rx = r; /* Successfully matched a chunk, move rx */
|
||||
} /* and go on with the next one */
|
||||
}
|
||||
}
|
||||
|
||||
/* rm has at least one '*' and thus is a 'real' mask */
|
||||
rz = r++; /* rx = unused of head, rz = beg-tail */
|
||||
|
||||
/* Match the tail of wm (if any) against the tail of rm */
|
||||
if (*w != 'Z')
|
||||
{
|
||||
for (; (*w) && (*r != 'Z') && ((*w == *r) || (*w == 'A')); w++, r++) { /* noop */ };
|
||||
if (*r == 'Z') /* extra '?' before tail are fluff, just flush 'em */
|
||||
while (*w == 'A')
|
||||
w++;
|
||||
if (*w != 'Z') /* We aren't matching a chunk, can't rollback */
|
||||
return (*w) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* Match the chunks of wm against what remains of the head of rm */
|
||||
while (1)
|
||||
{
|
||||
bw = w;
|
||||
for (bw++; (rx < rz) && (*bw != *rx); rx++) /* Seek the first */
|
||||
if (--trash < 0) /* waste some trash reserve */
|
||||
return 1;
|
||||
if (!(rx < rz)) /* head finished */
|
||||
break;
|
||||
for (bw++, (br = ++rx);
|
||||
(br < rz) && (*bw) && ((*bw == *br) || (*bw == 'A')); br++, bw++) { /* noop */ };
|
||||
if (!(br < rz)) /* Note that we didn't use any 'eat' char yet, if */
|
||||
while (*bw == 'A') /* there were eat-en chars the head would be over */
|
||||
bw++, eat++; /* Happens only at end of head, and eat is still 0 */
|
||||
if (!*bw)
|
||||
return 0;
|
||||
if (*bw != 'Z')
|
||||
{
|
||||
eat = 0;
|
||||
if (!(br < rz))
|
||||
{ /* If we failed because we got the end of head */
|
||||
trash -= (br - rx); /* it makes no sense to rollback, just trash */
|
||||
if (--trash < 0) /* all the rest of the head wich isn't long */
|
||||
return 1; /* enough for this chunk and go out of this */
|
||||
break; /* loop, then we try with the chunks of rm */
|
||||
};
|
||||
if (--trash < 0)
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
w = bw;
|
||||
rx = br;
|
||||
}
|
||||
}
|
||||
|
||||
/* Match the unused chunks of wm against the chunks of rm */
|
||||
rx = r;
|
||||
for (; *r && (*r != 'Z'); r++) { /* noop */ };
|
||||
rz = r;
|
||||
if (*r++)
|
||||
{
|
||||
while (*r)
|
||||
{
|
||||
bw = w;
|
||||
while (eat && *r) /* the '?' we had eated make us skip as many chars */
|
||||
if (*r++ != 'Z') /* here, but can't skip stars or trailing zero */
|
||||
eat--;
|
||||
for (bw++; (*r) && (*bw != *r); r++)
|
||||
if ((*r != 'Z') && (--trash < 0))
|
||||
return 1;
|
||||
if (!*r)
|
||||
break;
|
||||
for ((br = ++r), bw++;
|
||||
(*br) && (*br != 'Z') && ((*bw == *br) || (*bw == 'A')); br++, bw++) { /* noop */ };
|
||||
if (*br == 'Z')
|
||||
while (*bw == 'A')
|
||||
bw++, eat++;
|
||||
if (!*bw)
|
||||
return 0;
|
||||
if (*bw != 'Z')
|
||||
{
|
||||
eat = 0;
|
||||
if ((!*br) || (*r == 'Z'))
|
||||
{ /* If we hit the end of rm or a star in it */
|
||||
trash -= (br - r); /* makes no sense to rollback within this */
|
||||
if (trash < 0) /* same chunk of br, skip it all and then */
|
||||
return 1; /* either rollback or break this loop if */
|
||||
if (!*br) /* it was the end of rm */
|
||||
break;
|
||||
r = br;
|
||||
}
|
||||
if (--trash < 0)
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = br;
|
||||
w = bw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* match the remaining chunks of wm against what remains of the tail of rm */
|
||||
r = rz - eat - 1; /* can't have <nul> or 'Z'within the tail, so just move r */
|
||||
while (r >= rx)
|
||||
{
|
||||
bw = w;
|
||||
for (bw++; (*bw != *r); r--)
|
||||
if (--trash < 0)
|
||||
return 1;
|
||||
if (!(r >= rx))
|
||||
return 1;
|
||||
for ((br = --r), bw++;
|
||||
(*bw) && (br >= rx) && ((*bw == *br) || (*bw == 'A')); br--, bw++) { /* noop */ };
|
||||
if (!*bw)
|
||||
return 0;
|
||||
if (!(br >= rx))
|
||||
return 1;
|
||||
if (*bw != 'Z')
|
||||
{
|
||||
if (--trash < 0)
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = br;
|
||||
w = bw;
|
||||
}
|
||||
}
|
||||
return 1; /* Auch... something left out ? Fail */
|
||||
}
|
||||
|
||||
int matchall(const std::string& mask)
|
||||
{
|
||||
for (unsigned int i = 0; i < mask.length(); i++)
|
||||
{
|
||||
char c = mask[i];
|
||||
if ((c != '*') && (c != '?') && (c != '.') && (c != '!') && (c != '@') && (c != '~'))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* match.h
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: match.h,v 1.4 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __MATCH_H
|
||||
#define __MATCH_H "$Id: match.h,v 1.4 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "match_table.h"
|
||||
#include "ircd_chattr.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
/*
|
||||
* Prototypes
|
||||
*/
|
||||
|
||||
/*
|
||||
* XXX - match returns 0 if a match is found. Smelly interface
|
||||
* needs to be fixed. --Bleep
|
||||
*/
|
||||
int match( const std::string&, const std::string& ) ;
|
||||
int casematch( const std::string&, const std::string& ) ;
|
||||
//int mmatch( const char *old_mask, const char *new_mask);
|
||||
int match( const char *ma, const char *na);
|
||||
int casematch( const char *ma, const char *na);
|
||||
char *collapse(char *pattern);
|
||||
|
||||
int matchcomp(char *cmask, int *minlen, int *charset, const char *mask);
|
||||
int matchexec(const char *string, const char *cmask, int minlen);
|
||||
int matchdecomp(char *mask, const char *cmask);
|
||||
int mmexec(const char *wcm, int wminlen, const char *rcm, int rminlen);
|
||||
//Given mask matches with every mask
|
||||
int matchall(const std::string&);
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __MATCH_H
|
||||
@@ -0,0 +1,179 @@
|
||||
#ifndef __MATCH_TABLE_H
|
||||
#define __MATCH_TABLE_H
|
||||
|
||||
/*
|
||||
* Automatically Generated Tables - DO NOT EDIT
|
||||
*/
|
||||
#include <limits.h>
|
||||
const char ToLowerTab_8859_1[] = {
|
||||
#if (CHAR_MIN<0)
|
||||
/* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
|
||||
/* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
|
||||
/* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
|
||||
/* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
|
||||
/* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
|
||||
/* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
|
||||
/* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
|
||||
/* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
|
||||
/* xc0-xc7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
|
||||
/* xc8-xcf */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
|
||||
/* xd0-xd7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xd7',
|
||||
/* xd8-xdf */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xdf',
|
||||
/* xe0-xe7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
|
||||
/* xe8-xef */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
|
||||
/* xf0-xf7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
|
||||
/* xf8-xff */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff'
|
||||
,
|
||||
#endif /* (CHAR_MIN<0) */
|
||||
/* x00-x07 */ '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
|
||||
/* x08-x0f */ '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
|
||||
/* x10-x17 */ '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
|
||||
/* x18-x1f */ '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
|
||||
/* ' '-x27 */ ' ', '!', '"', '#', '$', '%', '&', '\x27',
|
||||
/* '('-'/' */ '(', ')', '*', '+', ',', '-', '.', '/',
|
||||
/* '0'-'7' */ '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
/* '8'-'?' */ '8', '9', ':', ';', '<', '=', '>', '?',
|
||||
/* '@'-'G' */ '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
||||
/* 'H'-'O' */ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
||||
/* 'P'-'W' */ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
||||
/* 'X'-'_' */ 'x', 'y', 'z', '{', '|', '}', '~', '_',
|
||||
/* '`'-'g' */ '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
||||
/* 'h'-'o' */ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
||||
/* 'p'-'w' */ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
||||
/* 'x'-x7f */ 'x', 'y', 'z', '{', '|', '}', '~', '\x7f'
|
||||
#if (!(CHAR_MIN<0))
|
||||
,
|
||||
/* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
|
||||
/* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
|
||||
/* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
|
||||
/* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
|
||||
/* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
|
||||
/* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
|
||||
/* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
|
||||
/* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
|
||||
/* xc0-xc7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
|
||||
/* xc8-xcf */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
|
||||
/* xd0-xd7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xd7',
|
||||
/* xd8-xdf */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xdf',
|
||||
/* xe0-xe7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
|
||||
/* xe8-xef */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
|
||||
/* xf0-xf7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
|
||||
/* xf8-xff */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff'
|
||||
#endif /* (!(CHAR_MIN<0)) */
|
||||
};
|
||||
|
||||
const char ToUpperTab_8859_1[] = {
|
||||
#if (CHAR_MIN<0)
|
||||
/* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
|
||||
/* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
|
||||
/* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
|
||||
/* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
|
||||
/* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
|
||||
/* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
|
||||
/* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
|
||||
/* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
|
||||
/* xc0-xc7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
|
||||
/* xc8-xcf */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
|
||||
/* xd0-xd7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
|
||||
/* xd8-xdf */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
|
||||
/* xe0-xe7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
|
||||
/* xe8-xef */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
|
||||
/* xf0-xf7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xf7',
|
||||
/* xf8-xff */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xff'
|
||||
,
|
||||
#endif /* (CHAR_MIN<0) */
|
||||
/* x00-x07 */ '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
|
||||
/* x08-x0f */ '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
|
||||
/* x10-x17 */ '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
|
||||
/* x18-x1f */ '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
|
||||
/* ' '-x27 */ ' ', '!', '"', '#', '$', '%', '&', '\x27',
|
||||
/* '('-'/' */ '(', ')', '*', '+', ',', '-', '.', '/',
|
||||
/* '0'-'7' */ '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
/* '8'-'?' */ '8', '9', ':', ';', '<', '=', '>', '?',
|
||||
/* '@'-'G' */ '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
/* 'H'-'O' */ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
||||
/* 'P'-'W' */ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
||||
/* 'X'-'_' */ 'X', 'Y', 'Z', '[', '\x5c', ']', '^', '_',
|
||||
/* '`'-'g' */ '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
/* 'h'-'o' */ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
||||
/* 'p'-'w' */ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
||||
/* 'x'-x7f */ 'X', 'Y', 'Z', '[', '\x5c', ']', '^', '\x7f'
|
||||
#if (!(CHAR_MIN<0))
|
||||
,
|
||||
/* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
|
||||
/* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
|
||||
/* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
|
||||
/* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
|
||||
/* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
|
||||
/* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
|
||||
/* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
|
||||
/* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
|
||||
/* xc0-xc7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
|
||||
/* xc8-xcf */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
|
||||
/* xd0-xd7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
|
||||
/* xd8-xdf */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
|
||||
/* xe0-xe7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
|
||||
/* xe8-xef */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
|
||||
/* xf0-xf7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xf7',
|
||||
/* xf8-xff */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xff'
|
||||
#endif /* (!(CHAR_MIN<0)) */
|
||||
};
|
||||
|
||||
const unsigned int IRCD_CharAttrTab[] = {
|
||||
#if (CHAR_MIN<0)
|
||||
/* x80-x87 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x88-x8f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x90-x97 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x98-x9f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xa0-xa7 */ 0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xa8-xaf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xb0-xb7 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xb8-xbf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xc0-xc7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
|
||||
/* xc8-xcf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
|
||||
/* xd0-xd7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
|
||||
/* xd8-xdf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
|
||||
/* xe0-xe7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
|
||||
/* xe8-xef */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
|
||||
/* xf0-xf7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400,
|
||||
/* xf8-xff */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400
|
||||
,
|
||||
#endif /* (CHAR_MIN<0) */
|
||||
/* x00-x07 */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0004,
|
||||
/* x08-x0f */ 0x0404, 0x0504, 0x10504, 0x0504, 0x0504, 0x10504, 0x0404, 0x0404,
|
||||
/* x10-x17 */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
|
||||
/* x18-x1f */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
|
||||
/* ' '-x27 */ 0x20140, 0x04d0, 0x04d0, 0x404d0, 0x04d0, 0x04d0, 0x404d0, 0x24d0,
|
||||
/* '('-'/' */ 0x04d0, 0x04d0, 0x04d0, 0x404d0, 0x200d0, 0x274d0, 0xe4d0, 0x04d0,
|
||||
/* '0'-'7' */ 0x2f459, 0x2f459, 0x2f459, 0x2f459, 0x2f459, 0x2f459, 0x2f459, 0x2f459,
|
||||
/* '8'-'?' */ 0x2f459, 0x2f459, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0,
|
||||
/* '@'-'G' */ 0x04d0, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
|
||||
/* 'H'-'O' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
|
||||
/* 'P'-'W' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
|
||||
/* 'X'-'_' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x74d0,
|
||||
/* '`'-'g' */ 0x34d0, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
|
||||
/* 'h'-'o' */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
|
||||
/* 'p'-'w' */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
|
||||
/* 'x'-x7f */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x0400
|
||||
#if (!(CHAR_MIN<0))
|
||||
,
|
||||
/* x80-x87 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x88-x8f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x90-x97 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* x98-x9f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xa0-xa7 */ 0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xa8-xaf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xb0-xb7 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xb8-xbf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
|
||||
/* xc0-xc7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
|
||||
/* xc8-xcf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
|
||||
/* xd0-xd7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
|
||||
/* xd8-xdf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
|
||||
/* xe0-xe7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
|
||||
/* xe8-xef */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
|
||||
/* xf0-xf7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400,
|
||||
/* xf8-xff */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400
|
||||
#endif /* (!(CHAR_MIN<0)) */
|
||||
};
|
||||
|
||||
#endif // __MATCH_TABLE_H
|
||||
@@ -0,0 +1,429 @@
|
||||
/*************************************************************************
|
||||
* $Workfile: MD5.CPP $
|
||||
* $Revision: 1.3 $
|
||||
* $Modtime: 1/08/97 6:35p $
|
||||
*
|
||||
* PURPOSE:
|
||||
* MD5 Message digest class derived from the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm as published in RFC 1321, April 1992.
|
||||
*
|
||||
* COPYRIGHT:
|
||||
* Copyright (c) 1995, 1996, 1997 Tree Frog Software, All rights reserved.
|
||||
* This source code and the binaries that result may be freely distributed,
|
||||
* used and modified as long as the above copyright notice remains intact.
|
||||
*
|
||||
* WARRANTY:
|
||||
* The author of md5.cpp (hereafter referred to as "the author") makes no
|
||||
* warranty of any kind, expressed or implied, including without limitation,
|
||||
* any warranties of merchantability and/or fitness for a particular purpose.
|
||||
* The author shall not be liable for any damages, whether direct, indirect,
|
||||
* special, or consequential arising from a failure of this program to
|
||||
* operate in the manner desired by the user. The author shall not be liable
|
||||
* for any damage to data or property which may be caused directly or
|
||||
* indirectly by use of the program.
|
||||
*
|
||||
* In no event will the author be liable to the user for any damages,
|
||||
* including any lost profits, lost savings, or other incidental or
|
||||
* consequential damages arising out of the use or inability to use the
|
||||
* program, or for any claim by any other party.
|
||||
*
|
||||
* -------------------- MD5 RSA copyright follows: -------------------------
|
||||
*
|
||||
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||
* rights reserved.
|
||||
*
|
||||
* License to copy and use this software is granted provided that it
|
||||
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
||||
* Algorithm" in all material mentioning or referencing this software
|
||||
* or this function.
|
||||
*
|
||||
* License is also granted to make and use derivative works provided
|
||||
* that such works are identified as "derived from the RSA Data
|
||||
* Security, Inc. MD5 Message-Digest Algorithm" in all material
|
||||
* mentioning or referencing the derived work.
|
||||
*
|
||||
* RSA Data Security, Inc. makes no representations concerning either
|
||||
* the merchantability of this software or the suitability of this
|
||||
* software for any particular purpose. It is provided "as is"
|
||||
* without express or implied warranty of any kind.
|
||||
*
|
||||
* These notices must be retained in any copies of any part of this
|
||||
* documentation and/or software.
|
||||
*
|
||||
* So there!
|
||||
************************************************************************/
|
||||
#include <iostream> // Needed for ostream and istream.
|
||||
#include <cstring> // Needed for memcpy() and memset().
|
||||
#include <inttypes.h>
|
||||
#include "md5hash.h"
|
||||
|
||||
const char rcsId[] = "$Id: md5hash.cc,v 1.3 2005/11/16 21:40:37 kewlio Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
// F, G, H and I are basic MD5 functions.
|
||||
inline uint32_t F( uint32_t xx, uint32_t yy, uint32_t zz )
|
||||
{ return (( xx & yy ) | (~xx & zz )); }
|
||||
|
||||
inline uint32_t G( uint32_t xx, uint32_t yy, uint32_t zz )
|
||||
{ return (( xx & zz ) | ( yy & ~zz )); }
|
||||
|
||||
inline uint32_t H( uint32_t xx, uint32_t yy, uint32_t zz )
|
||||
{ return xx ^ yy ^ zz; }
|
||||
|
||||
inline uint32_t I( uint32_t xx, uint32_t yy, uint32_t zz )
|
||||
{ return yy ^ ( xx | ~zz ); }
|
||||
|
||||
// ROTATE_LEFT rotates x left n bits.
|
||||
inline uint32_t ROTATE_LEFT( uint32_t xx, int32_t nn )
|
||||
{ return ( xx << nn ) | ( xx >> (32-nn )); }
|
||||
|
||||
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
||||
// Rotation is separate from addition to prevent recomputation.
|
||||
inline void
|
||||
FF(uint32_t &aa, uint32_t bb, uint32_t cc, uint32_t dd, uint32_t xx, int32_t ss, uint32_t ac){
|
||||
aa += F( bb, cc, dd ) + xx + ac;
|
||||
aa = ROTATE_LEFT( aa, ss );
|
||||
aa += bb;
|
||||
return;
|
||||
}
|
||||
|
||||
inline void
|
||||
GG(uint32_t &aa, uint32_t bb, uint32_t cc, uint32_t dd, uint32_t xx, int32_t ss, uint32_t ac){
|
||||
aa += G( bb, cc, dd ) + xx + ac;
|
||||
aa = ROTATE_LEFT( aa, ss );
|
||||
aa += bb;
|
||||
return;
|
||||
}
|
||||
|
||||
inline void
|
||||
HH(uint32_t &aa, uint32_t bb, uint32_t cc, uint32_t dd, uint32_t xx, int32_t ss, uint32_t ac){
|
||||
aa += H( bb, cc, dd ) + xx + ac;
|
||||
aa = ROTATE_LEFT( aa, ss );
|
||||
aa += bb;
|
||||
return;
|
||||
}
|
||||
|
||||
inline void
|
||||
II(uint32_t &aa, uint32_t bb, uint32_t cc, uint32_t dd, uint32_t xx, int32_t ss, uint32_t ac){
|
||||
aa += I( bb, cc, dd ) + xx + ac;
|
||||
aa = ROTATE_LEFT( aa, ss );
|
||||
aa += bb;
|
||||
return;
|
||||
}
|
||||
|
||||
md5Digest::md5Digest( void )
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
md5Digest::~md5Digest( void )
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void
|
||||
md5Digest::clear( void )
|
||||
{
|
||||
memset( m_data, 0, sizeof( m_data ));
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char&
|
||||
md5Digest::operator[]( size_t ii ) // lvalue operator[].
|
||||
{
|
||||
static unsigned char val = 0;
|
||||
if( ii >= MD5_DIGEST_LENGTH ) { // Note that size_t is unsigned.
|
||||
val = 0;
|
||||
return val; // Benign, don't throw an exception.
|
||||
}
|
||||
return m_data[ ii ];
|
||||
}
|
||||
|
||||
unsigned char
|
||||
md5Digest::operator[]( size_t ii ) const // rvalue operator[].
|
||||
{
|
||||
if( ii >= MD5_DIGEST_LENGTH ) { // Note that size_t is unsigned.
|
||||
return 0; // Benign, don't throw an exception.
|
||||
}
|
||||
return m_data[ ii ];
|
||||
}
|
||||
|
||||
int32_t
|
||||
operator==( const md5Digest &lhs, const md5Digest &rhs )
|
||||
{
|
||||
int32_t result = 1; // Assume true to begin with.
|
||||
for( int32_t ii = 0; result && ii < MD5_DIGEST_LENGTH; ii++ ) {
|
||||
result = lhs.m_data[ii] == rhs.m_data[ii];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t
|
||||
operator!=( const md5Digest &lhs, const md5Digest &rhs )
|
||||
{
|
||||
return !( lhs == rhs );
|
||||
}
|
||||
|
||||
std::istream&
|
||||
operator>>( std::istream& stream, md5Digest& digest )
|
||||
{
|
||||
stream.read( reinterpret_cast< char* >( digest.m_data ),
|
||||
MD5_DIGEST_LENGTH );
|
||||
// for( int32_t ii = 0; ii < MD5_DIGEST_LENGTH; ii++ ) {
|
||||
// stream >> digest.m_data[ii];
|
||||
// }
|
||||
return stream;
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
operator<<( std::ostream& stream, const md5Digest& digest )
|
||||
{
|
||||
stream.write( reinterpret_cast< const char* >( digest.m_data ),
|
||||
MD5_DIGEST_LENGTH );
|
||||
// for( int32_t ii = 0; ii < MD5_DIGEST_LENGTH; ii++ ) {
|
||||
// stream << digest.m_data[ii];
|
||||
// }
|
||||
return stream;
|
||||
}
|
||||
|
||||
md5::md5( void )
|
||||
{
|
||||
init(); // Set up the initial state values.
|
||||
}
|
||||
|
||||
md5::~md5( void )
|
||||
{
|
||||
clear(); // Wipe the class data clean.
|
||||
}
|
||||
|
||||
void
|
||||
md5::init( void )
|
||||
{ // Load magic initialization constants.
|
||||
m_state[0] = 0x67452301L; // These values are only used here.
|
||||
m_state[1] = 0xefcdab89L;
|
||||
m_state[2] = 0x98badcfeL;
|
||||
m_state[3] = 0x10325476L;
|
||||
m_count[0] = m_count[1] = 0;
|
||||
memset( m_buffer, 0, sizeof( m_buffer ));
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::clear( void )
|
||||
{ // Zero the data memebers of this class.
|
||||
m_state[0] = m_state[1] = m_state[2] = m_state[3] = 0;
|
||||
m_count[0] = m_count[1] = 0;
|
||||
memset( m_buffer, 0, sizeof( m_buffer ));
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::update( const unsigned char *input, size_t inputLen )
|
||||
{ // MD5 block update operation. Continues an MD5 message-digest
|
||||
// operation, processing another message block, and updating the
|
||||
// context.
|
||||
|
||||
// Compute number of bytes mod 64.
|
||||
size_t index = (size_t)((m_count[0] >> 3) & 0x3F);
|
||||
|
||||
// Update number of bits.
|
||||
if(( m_count[0] += ((uint32_t)inputLen << 3)) < ((uint32_t)inputLen << 3)) {
|
||||
m_count[1]++;
|
||||
}
|
||||
m_count[1] += ((uint32_t)inputLen >> 29);
|
||||
|
||||
size_t partLen = 64 - index;
|
||||
|
||||
// Transform as many times as possible.
|
||||
size_t ii = 0;
|
||||
if( inputLen >= partLen ) {
|
||||
memcpy( &m_buffer[index], input, partLen);
|
||||
transform( m_buffer );
|
||||
|
||||
for( ii = partLen; ii + 63 < inputLen; ii += 64 ) {
|
||||
transform( &input[ii] );
|
||||
}
|
||||
index = 0;
|
||||
}
|
||||
if( inputLen - ii ) {
|
||||
// Buffer remaining input.
|
||||
memcpy( &m_buffer[index],&input[ii],inputLen-ii );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::report( md5Digest &digest )
|
||||
{ // MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||
// the message digest and zeroizing the context.
|
||||
static unsigned char padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
unsigned char bits[8];
|
||||
|
||||
encode( bits, m_count, 8 ); // Save number of bits
|
||||
|
||||
// Pad out to 56 mod 64.
|
||||
size_t index = (size_t)((m_count[0] >> 3) & 0x3f);
|
||||
size_t padLen = (index < 56) ? (56 - index) : (120 - index);
|
||||
|
||||
update( padding, padLen );
|
||||
update( bits, 8 ); // Append length (before padding).
|
||||
|
||||
encode( digest ); // Store state in digest.
|
||||
clear(); // Zero sensitive information.
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::transform( const unsigned char block[64] )
|
||||
{ // MD5 basic transformation. Transforms state based on block.
|
||||
const int32_t S11 = 7;
|
||||
const int32_t S12 = 12;
|
||||
const int32_t S13 = 17;
|
||||
const int32_t S14 = 22;
|
||||
const int32_t S21 = 5;
|
||||
const int32_t S22 = 9;
|
||||
const int32_t S23 = 14;
|
||||
const int32_t S24 = 20;
|
||||
const int32_t S31 = 4;
|
||||
const int32_t S32 = 11;
|
||||
const int32_t S33 = 16;
|
||||
const int32_t S34 = 23;
|
||||
const int32_t S41 = 6;
|
||||
const int32_t S42 = 10;
|
||||
const int32_t S43 = 15;
|
||||
const int32_t S44 = 21;
|
||||
|
||||
uint32_t aa = m_state[0], bb = m_state[1], cc = m_state[2], dd = m_state[3];
|
||||
uint32_t xx[16];
|
||||
|
||||
decode( xx, block, 64 );
|
||||
|
||||
/* Round 1 */
|
||||
FF( aa, bb, cc, dd, xx[ 0], S11, 0xd76aa478L); /* 1 */
|
||||
FF( dd, aa, bb, cc, xx[ 1], S12, 0xe8c7b756L); /* 2 */
|
||||
FF( cc, dd, aa, bb, xx[ 2], S13, 0x242070dbL); /* 3 */
|
||||
FF( bb, cc, dd, aa, xx[ 3], S14, 0xc1bdceeeL); /* 4 */
|
||||
FF( aa, bb, cc, dd, xx[ 4], S11, 0xf57c0fafL); /* 5 */
|
||||
FF( dd, aa, bb, cc, xx[ 5], S12, 0x4787c62aL); /* 6 */
|
||||
FF( cc, dd, aa, bb, xx[ 6], S13, 0xa8304613L); /* 7 */
|
||||
FF( bb, cc, dd, aa, xx[ 7], S14, 0xfd469501L); /* 8 */
|
||||
FF( aa, bb, cc, dd, xx[ 8], S11, 0x698098d8L); /* 9 */
|
||||
FF( dd, aa, bb, cc, xx[ 9], S12, 0x8b44f7afL); /* 10 */
|
||||
FF( cc, dd, aa, bb, xx[10], S13, 0xffff5bb1L); /* 11 */
|
||||
FF( bb, cc, dd, aa, xx[11], S14, 0x895cd7beL); /* 12 */
|
||||
FF( aa, bb, cc, dd, xx[12], S11, 0x6b901122L); /* 13 */
|
||||
FF( dd, aa, bb, cc, xx[13], S12, 0xfd987193L); /* 14 */
|
||||
FF( cc, dd, aa, bb, xx[14], S13, 0xa679438eL); /* 15 */
|
||||
FF( bb, cc, dd, aa, xx[15], S14, 0x49b40821L); /* 16 */
|
||||
|
||||
/* Round 2 */
|
||||
GG( aa, bb, cc, dd, xx[ 1], S21, 0xf61e2562L); /* 17 */
|
||||
GG( dd, aa, bb, cc, xx[ 6], S22, 0xc040b340L); /* 18 */
|
||||
GG( cc, dd, aa, bb, xx[11], S23, 0x265e5a51L); /* 19 */
|
||||
GG( bb, cc, dd, aa, xx[ 0], S24, 0xe9b6c7aaL); /* 20 */
|
||||
GG( aa, bb, cc, dd, xx[ 5], S21, 0xd62f105dL); /* 21 */
|
||||
GG( dd, aa, bb, cc, xx[10], S22, 0x02441453L); /* 22 */
|
||||
GG( cc, dd, aa, bb, xx[15], S23, 0xd8a1e681L); /* 23 */
|
||||
GG( bb, cc, dd, aa, xx[ 4], S24, 0xe7d3fbc8L); /* 24 */
|
||||
GG( aa, bb, cc, dd, xx[ 9], S21, 0x21e1cde6L); /* 25 */
|
||||
GG( dd, aa, bb, cc, xx[14], S22, 0xc33707d6L); /* 26 */
|
||||
GG( cc, dd, aa, bb, xx[ 3], S23, 0xf4d50d87L); /* 27 */
|
||||
GG( bb, cc, dd, aa, xx[ 8], S24, 0x455a14edL); /* 28 */
|
||||
GG( aa, bb, cc, dd, xx[13], S21, 0xa9e3e905L); /* 29 */
|
||||
GG( dd, aa, bb, cc, xx[ 2], S22, 0xfcefa3f8L); /* 30 */
|
||||
GG( cc, dd, aa, bb, xx[ 7], S23, 0x676f02d9L); /* 31 */
|
||||
GG( bb, cc, dd, aa, xx[12], S24, 0x8d2a4c8aL); /* 32 */
|
||||
|
||||
/* Round 3 */
|
||||
HH( aa, bb, cc, dd, xx[ 5], S31, 0xfffa3942L); /* 33 */
|
||||
HH( dd, aa, bb, cc, xx[ 8], S32, 0x8771f681L); /* 34 */
|
||||
HH( cc, dd, aa, bb, xx[11], S33, 0x6d9d6122L); /* 35 */
|
||||
HH( bb, cc, dd, aa, xx[14], S34, 0xfde5380cL); /* 36 */
|
||||
HH( aa, bb, cc, dd, xx[ 1], S31, 0xa4beea44L); /* 37 */
|
||||
HH( dd, aa, bb, cc, xx[ 4], S32, 0x4bdecfa9L); /* 38 */
|
||||
HH( cc, dd, aa, bb, xx[ 7], S33, 0xf6bb4b60L); /* 39 */
|
||||
HH( bb, cc, dd, aa, xx[10], S34, 0xbebfbc70L); /* 40 */
|
||||
HH( aa, bb, cc, dd, xx[13], S31, 0x289b7ec6L); /* 41 */
|
||||
HH( dd, aa, bb, cc, xx[ 0], S32, 0xeaa127faL); /* 42 */
|
||||
HH( cc, dd, aa, bb, xx[ 3], S33, 0xd4ef3085L); /* 43 */
|
||||
HH( bb, cc, dd, aa, xx[ 6], S34, 0x04881d05L); /* 44 */
|
||||
HH( aa, bb, cc, dd, xx[ 9], S31, 0xd9d4d039L); /* 45 */
|
||||
HH( dd, aa, bb, cc, xx[12], S32, 0xe6db99e5L); /* 46 */
|
||||
HH( cc, dd, aa, bb, xx[15], S33, 0x1fa27cf8L); /* 47 */
|
||||
HH( bb, cc, dd, aa, xx[ 2], S34, 0xc4ac5665L); /* 48 */
|
||||
|
||||
/* Round 4 */
|
||||
II( aa, bb, cc, dd, xx[ 0], S41, 0xf4292244L); /* 49 */
|
||||
II( dd, aa, bb, cc, xx[ 7], S42, 0x432aff97L); /* 50 */
|
||||
II( cc, dd, aa, bb, xx[14], S43, 0xab9423a7L); /* 51 */
|
||||
II( bb, cc, dd, aa, xx[ 5], S44, 0xfc93a039L); /* 52 */
|
||||
II( aa, bb, cc, dd, xx[12], S41, 0x655b59c3L); /* 53 */
|
||||
II( dd, aa, bb, cc, xx[ 3], S42, 0x8f0ccc92L); /* 54 */
|
||||
II( cc, dd, aa, bb, xx[10], S43, 0xffeff47dL); /* 55 */
|
||||
II( bb, cc, dd, aa, xx[ 1], S44, 0x85845dd1L); /* 56 */
|
||||
II( aa, bb, cc, dd, xx[ 8], S41, 0x6fa87e4fL); /* 57 */
|
||||
II( dd, aa, bb, cc, xx[15], S42, 0xfe2ce6e0L); /* 58 */
|
||||
II( cc, dd, aa, bb, xx[ 6], S43, 0xa3014314L); /* 59 */
|
||||
II( bb, cc, dd, aa, xx[13], S44, 0x4e0811a1L); /* 60 */
|
||||
II( aa, bb, cc, dd, xx[ 4], S41, 0xf7537e82L); /* 61 */
|
||||
II( dd, aa, bb, cc, xx[11], S42, 0xbd3af235L); /* 62 */
|
||||
II( cc, dd, aa, bb, xx[ 2], S43, 0x2ad7d2bbL); /* 63 */
|
||||
II( bb, cc, dd, aa, xx[ 9], S44, 0xeb86d391L); /* 64 */
|
||||
|
||||
m_state[0] += aa;
|
||||
m_state[1] += bb;
|
||||
m_state[2] += cc;
|
||||
m_state[3] += dd;
|
||||
|
||||
memset((unsigned char*)xx, 0, sizeof (xx)); // Clear sensitive information.
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::encode( unsigned char *output, const uint32_t *input, int32_t len )
|
||||
{ // Encodes input (uint32_t) into output (unsigned char).
|
||||
// Assumes len is a multiple of 4.
|
||||
int32_t ii, jj;
|
||||
for( ii = 0, jj = 0; jj < len; ii++, jj += 4 ) {
|
||||
output[jj] = (unsigned char)( input[ii] & 0xFF );
|
||||
output[jj+1] = (unsigned char)(( input[ii] >> 8 ) & 0xFF );
|
||||
output[jj+2] = (unsigned char)(( input[ii] >> 16 ) & 0xFF );
|
||||
output[jj+3] = (unsigned char)(( input[ii] >> 24 ) & 0xFF );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::encode( md5Digest &digest )
|
||||
{ // Encodes m_state into digest.
|
||||
int32_t ii, jj;
|
||||
for( ii = 0, jj = 0; jj < MD5_DIGEST_LENGTH; ii++, jj += 4 ) {
|
||||
digest.m_data[jj] = (unsigned char)( m_state[ii] & 0xFF );
|
||||
digest.m_data[jj+1] = (unsigned char)(( m_state[ii] >> 8 ) & 0xFF );
|
||||
digest.m_data[jj+2] = (unsigned char)(( m_state[ii] >> 16 ) & 0xFF );
|
||||
digest.m_data[jj+3] = (unsigned char)(( m_state[ii] >> 24 ) & 0xFF );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
md5::decode( uint32_t *output, const unsigned char *input, int32_t len )
|
||||
{ // Decodes input (unsigned char) into output (uint32_t).
|
||||
// Assumes len is a multiple of 4.
|
||||
int32_t ii, jj;
|
||||
for( ii = 0, jj = 0; jj < len; ii++, jj += 4 ) {
|
||||
output[ii] = ((uint32_t)input[jj]) |
|
||||
(((uint32_t)input[jj+1]) << 8 ) |
|
||||
(((uint32_t)input[jj+2]) << 16 ) |
|
||||
(((uint32_t)input[jj+3]) << 24 );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
} // Namespace
|
||||
@@ -0,0 +1,114 @@
|
||||
/**********************************************************************
|
||||
* $Workfile: MD5.H $
|
||||
* $Revision: 1.4 $
|
||||
* $Modtime: 1/08/97 6:35p $
|
||||
*
|
||||
* PURPOSE:
|
||||
* MD5 Message-Digest class derived from the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm as published in RFC 1321, April 1992.
|
||||
*
|
||||
* ASSUMES:
|
||||
* size_t as unsigned.
|
||||
*
|
||||
* NOTE:
|
||||
* Currently the md5::update length is limited to unsigned int, which may
|
||||
* be less than unit32_t.
|
||||
*
|
||||
* COPYRIGHT:
|
||||
* Copyright (c) 1995, 1996, 1997 Tree Frog Software, All rights reserved.
|
||||
* This source code and the binaries that result may be freely distributed,
|
||||
* used and modified as long as the above copyright notice remains intact.
|
||||
*
|
||||
* WARRANTY:
|
||||
* The author of md5.h (hereafter referred to as "the author") makes no
|
||||
* warranty of any kind, expressed or implied, including without limitation,
|
||||
* any warranties of merchantability and/or fitness for a particular purpose.
|
||||
* The author shall not be liable for any damages, whether direct, indirect,
|
||||
* special, or consequential arising from a failure of this program to
|
||||
* operate in the manner desired by the user. The author shall not be liable
|
||||
* for any damage to data or property which may be caused directly or
|
||||
* indirectly by use of the program.
|
||||
*
|
||||
* In no event will the author be liable to the user for any damages,
|
||||
* including any lost profits, lost savings, or other incidental or
|
||||
* consequential damages arising out of the use or inability to use the
|
||||
* program, or for any claim by any other party.
|
||||
*
|
||||
* -------------------- MD5 RSA copyright follows: -------------------------
|
||||
*
|
||||
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||
* rights reserved.
|
||||
*
|
||||
* License to copy and use this software is granted provided that it
|
||||
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
||||
* Algorithm" in all material mentioning or referencing this software
|
||||
* or this function.
|
||||
*
|
||||
* License is also granted to make and use derivative works provided
|
||||
* that such works are identified as "derived from the RSA Data
|
||||
* Security, Inc. MD5 Message-Digest Algorithm" in all material
|
||||
* mentioning or referencing the derived work.
|
||||
*
|
||||
* RSA Data Security, Inc. makes no representations concerning either
|
||||
* the merchantability of this software or the suitability of this
|
||||
* software for any particular purpose. It is provided "as is"
|
||||
* without express or implied warranty of any kind.
|
||||
* These notices must be retained in any copies of any part of this
|
||||
* documentation and/or software.
|
||||
*
|
||||
* So there!
|
||||
**********************************************************************/
|
||||
#ifndef MD5_H
|
||||
#define MD5_H "$Id: md5hash.h,v 1.4 2005/11/16 21:40:37 kewlio Exp $"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
class md5Digest; // Forward declaration.
|
||||
|
||||
class md5 {
|
||||
public:
|
||||
md5( void ); // Automatically calls md5::init().
|
||||
virtual ~md5( void ); // Automatically calls md5::clear().
|
||||
void update( const unsigned char *input, size_t length ); // Process data.
|
||||
void report( md5Digest &digest ); // Used to generate the digest.
|
||||
void init( void ); // Used for reinitialization.
|
||||
void clear( void ); // Used to wipe clean interal data.
|
||||
protected:
|
||||
uint32_t m_state[4]; // State (ABCD)
|
||||
uint32_t m_count[2]; // Number of bits, modulo 2^64 (lsb first).
|
||||
unsigned char m_buffer[64]; // Input buffer.
|
||||
void transform( const unsigned char block[64] );
|
||||
void encode( unsigned char *output, const uint32_t *input, int32_t len );
|
||||
void encode( md5Digest &digest );
|
||||
void decode( uint32_t *output, const unsigned char *input, int32_t len );
|
||||
};
|
||||
|
||||
class md5Digest {
|
||||
friend class md5 ;
|
||||
public:
|
||||
md5Digest( void ); // Automatically calls md5Digest::clear().
|
||||
~md5Digest( void ); // Automatically calls md5Digest::clear().
|
||||
unsigned char &operator[]( size_t ii ); // lvalue.
|
||||
unsigned char operator[]( size_t ii ) const; // rvalue.
|
||||
void clear( void );
|
||||
|
||||
private:
|
||||
unsigned char m_data[ MD5_DIGEST_LENGTH ];
|
||||
|
||||
friend int32_t operator==( const md5Digest &lhs, const md5Digest &rhs );
|
||||
friend int32_t operator!=( const md5Digest &lhs, const md5Digest &rhs );
|
||||
friend std::istream& operator>>( std::istream& stream,
|
||||
md5Digest& digest );
|
||||
friend std::ostream& operator<<( std::ostream& stream,
|
||||
const md5Digest& digest );
|
||||
// friend void md5::encode( md5Digest &digest );
|
||||
};
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // MD5_H
|
||||
@@ -0,0 +1,368 @@
|
||||
/**
|
||||
* misc.cc
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: misc.cc,v 1.6 2007/12/27 20:45:15 kewlio Exp $
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "misc.h"
|
||||
#include "StringTokenizer.h"
|
||||
|
||||
const char rcsId[] = "$Id: misc.cc,v 1.6 2007/12/27 20:45:15 kewlio Exp $" ;
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
using std::string ;
|
||||
|
||||
/**
|
||||
* Create a string and copy into it (Key), but convert to all
|
||||
* lower case.
|
||||
*/
|
||||
string string_lower( const string& Key )
|
||||
{
|
||||
// This variable will be the string to be returned
|
||||
// Make a copy of the original string to speed up
|
||||
// allocation.
|
||||
string retMe( Key ) ;
|
||||
|
||||
// Iterate through the new string, converting each character
|
||||
// to lower case
|
||||
for( string::size_type i = 0, end = retMe.size() ; i < end ; ++i )
|
||||
{
|
||||
retMe[ i ] = tolower( retMe[ i ] ) ;
|
||||
}
|
||||
|
||||
// Return by value the new string (this will create a copy of
|
||||
// the new string)
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a string and copy into it (Key), but convert to all
|
||||
* upper case.
|
||||
*/
|
||||
string string_upper( const string& Key )
|
||||
{
|
||||
|
||||
// This variable will be the string to be returned
|
||||
// Make a copy of the original string to speed up
|
||||
// allocation.
|
||||
string retMe( Key ) ;
|
||||
|
||||
// Iterate through the new string, converting each character
|
||||
// to upper case
|
||||
for( string::size_type i = 0, end = retMe.size() ; i < end ; i++ )
|
||||
{
|
||||
retMe[ i ] = toupper( retMe[ i ] ) ;
|
||||
}
|
||||
|
||||
// Return by value the new string (this will create a copy of
|
||||
// the new string)
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given string to all lowercase. This method
|
||||
* mutates the original string, yet is much faster than the
|
||||
* equivalent method which receives the string by const
|
||||
* reference.
|
||||
*/
|
||||
void string_tolower( string& Key )
|
||||
{
|
||||
for( string::size_type i = 0, end = Key.size() ; i < end ; i++ )
|
||||
{
|
||||
Key[ i ] = tolower( Key[ i ] ) ;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given string to all uppercase. This method
|
||||
* mutates the original string, yet is much faster than the
|
||||
* equivalent method which receives the string by const
|
||||
* reference.
|
||||
*/
|
||||
void string_toupper( string& Key )
|
||||
{
|
||||
for( string::size_type i = 0, end = Key.size() ; i < end ; i++ )
|
||||
{
|
||||
Key[ i ] = toupper( Key[ i ] ) ;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this string consists of all numerical
|
||||
* [0,9] characters.
|
||||
* Return false otherwise. */
|
||||
bool IsNumeric( const string& s )
|
||||
{
|
||||
for( string::const_iterator ptr = s.begin(), endPtr = s.end() ;
|
||||
ptr != endPtr ; ++ptr )
|
||||
{
|
||||
if( !isdigit( *ptr ) )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
return true ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this string consists of a time specification
|
||||
* [0123456789SsMmHhDd] - it also must begin with a digit.
|
||||
*/
|
||||
bool IsTimeSpec( const string& s )
|
||||
{
|
||||
char c;
|
||||
int count = 0;
|
||||
|
||||
for ( string::const_iterator ptr = s.begin(), endPtr = s.end() ;
|
||||
ptr != endPtr ; ++ptr )
|
||||
{
|
||||
c = tolower(*ptr);
|
||||
/* if this is the start, it must be a digit */
|
||||
if ((ptr == s.begin()) && (!isdigit(c)))
|
||||
return false;
|
||||
/* check for valid characters (digits, smhd) */
|
||||
if (!isdigit(c) && c != 's' && c != 'm' && c != 'h' && c != 'd')
|
||||
return false;
|
||||
/* keep a count of non-numeric characters */
|
||||
if (!isdigit(c))
|
||||
count++;
|
||||
/* maximum of 1 is allowed */
|
||||
if (count > 1)
|
||||
return false;
|
||||
}
|
||||
/* if we reach here, this is a valid time specification */
|
||||
return true;
|
||||
}
|
||||
|
||||
int strcasecmp( const string& s1, const string& s2 )
|
||||
{
|
||||
return ::strcasecmp( s1.c_str(), s2.c_str() ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time which is given as #<d/h/m/s> as seconds */
|
||||
|
||||
time_t extractTime( string Length, unsigned int defaultUnits )
|
||||
{
|
||||
unsigned int Units;
|
||||
|
||||
if (defaultUnits == 0)
|
||||
Units = 1;
|
||||
else
|
||||
Units = defaultUnits;
|
||||
|
||||
if (!strcasecmp(Length.substr(Length.length()-1).c_str(),"d"))
|
||||
{
|
||||
Units = (24*3600);
|
||||
Length.resize(Length.length()-1);
|
||||
}
|
||||
else if(!strcasecmp(Length.substr(Length.length()-1).c_str(),"h"))
|
||||
{
|
||||
Units = 3600;
|
||||
Length.resize(Length.length()-1);
|
||||
}
|
||||
else if(!strcasecmp(Length.substr(Length.length()-1).c_str(),"m"))
|
||||
{
|
||||
Units = 60;
|
||||
Length.resize(Length.length()-1);
|
||||
}
|
||||
else if(!strcasecmp(Length.substr(Length.length()-1).c_str(),"s"))
|
||||
{
|
||||
Units = 1;
|
||||
Length.resize(Length.length()-1);
|
||||
}
|
||||
|
||||
return atoi(Length.c_str()) * Units;
|
||||
}
|
||||
|
||||
int atoi( const string& val )
|
||||
{
|
||||
return ::atoi( val.c_str() ) ;
|
||||
}
|
||||
|
||||
string itoa(int n)
|
||||
{
|
||||
string Result;
|
||||
std::ostringstream convert;
|
||||
convert << n;
|
||||
Result = convert.str();
|
||||
return Result;
|
||||
}
|
||||
|
||||
string extractNick(const string& NickUserHostIP)
|
||||
{
|
||||
StringTokenizer st( NickUserHostIP, '!' ) ;
|
||||
|
||||
// Make sure there are exactly two tokens
|
||||
if( st.size() != 2 )
|
||||
return string("");
|
||||
|
||||
return st[0];
|
||||
}
|
||||
|
||||
string extractUser(const string& NickUserHostIP)
|
||||
{
|
||||
string NickUser = extractNickUser(NickUserHostIP);
|
||||
|
||||
StringTokenizer st( NickUser, '!' ) ;
|
||||
|
||||
if( st.size() != 2 )
|
||||
return string("");
|
||||
|
||||
return st[1];
|
||||
}
|
||||
|
||||
string extractNickUser(const std::string& NickUserHostIP)
|
||||
{
|
||||
StringTokenizer st( NickUserHostIP, '@' ) ;
|
||||
|
||||
if( st.size() != 2 )
|
||||
return string("");
|
||||
|
||||
return st[0];
|
||||
}
|
||||
|
||||
string extractHostIP(const string& NickUserHostIP)
|
||||
{
|
||||
StringTokenizer st( NickUserHostIP, '@' ) ;
|
||||
|
||||
if( st.size() != 2 )
|
||||
return string("");
|
||||
|
||||
return st[1];
|
||||
}
|
||||
|
||||
bool validUserMask(const string& userMask)
|
||||
{
|
||||
// Check that a '@' exists
|
||||
StringTokenizer st( userMask, '@' ) ;
|
||||
|
||||
if (st.size() != 2)
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Check that a '!' exists
|
||||
StringTokenizer st1( st[0], '!' ) ;
|
||||
if (st1.size() != 2)
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Be sure that the hostname is no more than 255 characters
|
||||
if( st[ 1 ].size() > 255 )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
|
||||
// Tests have passed
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool checkAllValidChars(const string& theString)
|
||||
{
|
||||
const char validChars[]
|
||||
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-";
|
||||
for( string::const_iterator ptr = theString.begin() ;
|
||||
ptr != theString.end() ; ++ptr )
|
||||
{
|
||||
/*
|
||||
* 62 entries in the table. 26 + 26 + 2 + 10 digits.
|
||||
*/
|
||||
bool found = false;
|
||||
for (int f = 0; f < 64; f++)
|
||||
{
|
||||
if(*ptr == validChars[f])
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool validHostName(const string& hostname)
|
||||
{
|
||||
string _hostname = string_lower(hostname);
|
||||
StringTokenizer st( _hostname, '.' ) ;
|
||||
if (st.size() == 1) //no dot found ...
|
||||
return false;
|
||||
//Domains are at least 2 characters long ...
|
||||
if (string(st[st.size()-1]).length() < 2)
|
||||
return false;
|
||||
if (hostname[0] == '.') //hostname cannot start with dot
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
const string prettyDuration( int duration )
|
||||
{
|
||||
// Pretty format a 'duration' in seconds to
|
||||
// x day(s), xx:xx:xx.
|
||||
char tmpBuf[ 64 ] = {0};
|
||||
|
||||
if (duration == 0)
|
||||
{
|
||||
sprintf(tmpBuf, "Never");
|
||||
return string( tmpBuf ) ;
|
||||
}
|
||||
|
||||
int res = ::time(NULL) - duration,
|
||||
secs = res % 60,
|
||||
mins = (res / 60) % 60,
|
||||
hours = (res / 3600) % 24,
|
||||
days = (res / 86400) ;
|
||||
|
||||
sprintf(tmpBuf, "%i day%s, %02d:%02d:%02d",
|
||||
days,
|
||||
(days == 1 ? "" : "s"),
|
||||
hours,
|
||||
mins,
|
||||
secs );
|
||||
|
||||
return string( tmpBuf ) ;
|
||||
}
|
||||
|
||||
const string TokenStringsParams(const char* format,...)
|
||||
{
|
||||
char buf[ 1024 ] = { 0 } ;
|
||||
va_list _list ;
|
||||
|
||||
va_start( _list, format ) ;
|
||||
vsnprintf( buf, 1024, format, _list ) ;
|
||||
va_end( _list ) ;
|
||||
return string(buf);
|
||||
}
|
||||
} // namespace gnuworld
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* misc.h
|
||||
* Author: Daniel Karrels (dan@karrels.com
|
||||
* Purpose: This file contains a few miscellaneous methods.
|
||||
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: misc.h,v 1.9 2007/12/27 20:45:15 kewlio Exp $
|
||||
*/
|
||||
|
||||
#ifndef __MISC_H
|
||||
#define __MISC_H "$Id: misc.h,v 1.9 2007/12/27 20:45:15 kewlio Exp $"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "match.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* Return 0 if the two strings are equivalent, according to
|
||||
* case insensitive searches.
|
||||
* Otherwise, it returns the comparison between
|
||||
* s1 and s2.
|
||||
*/
|
||||
int strcasecmp( const std::string&, const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Case insensitive comparison struct for use by STL structures/algorithms.
|
||||
*/
|
||||
struct noCaseCompare
|
||||
{
|
||||
inline bool operator()( const std::string& lhs, const std::string& rhs ) const
|
||||
{
|
||||
return (strcasecmp( lhs, rhs ) < 0) ;
|
||||
}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* A case insensitive binary predicate comparator for two
|
||||
* std::string's.
|
||||
*/
|
||||
struct eqstr
|
||||
{
|
||||
inline bool operator()( const std::string& s1, const std::string& s2 ) const
|
||||
{
|
||||
return (0 == strcasecmp( s1, s2 )) ;
|
||||
}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* A hashing operator for the system hash tables.
|
||||
* This is not used for now, since hash_map has been removed
|
||||
* from gnuworld completely.
|
||||
*/
|
||||
struct eHash
|
||||
{
|
||||
inline size_t operator()( const std::string& s ) const
|
||||
{
|
||||
if( s.empty() )
|
||||
{
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
register size_t __h = 0 ;
|
||||
for ( register const char* ptr = s.c_str() ; *ptr ; ++ptr )
|
||||
{
|
||||
__h = (5 * __h) + tolower( *ptr ) ;
|
||||
}
|
||||
return __h ;
|
||||
}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* A functor suitable for using in STL style containers which provides
|
||||
* wildcard matching routine.
|
||||
*/
|
||||
struct Match
|
||||
{
|
||||
inline bool operator()( const std::string& lhs, const std::string& rhs ) const
|
||||
{
|
||||
return (match( lhs, rhs ) < 0) ;
|
||||
}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* Return a copy of a given C++ string, whose characters
|
||||
* are all lower case.
|
||||
*/
|
||||
std::string string_lower( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Return a copy of a given C++ string, whose
|
||||
* characters are all upper case.
|
||||
*/
|
||||
std::string string_upper( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Convert all characters of a given C++ string to
|
||||
* lower case.
|
||||
*/
|
||||
void string_tolower( std::string& ) ;
|
||||
|
||||
/**
|
||||
* Convert all characters of a given C++ string to
|
||||
* upper case.
|
||||
*/
|
||||
void string_toupper( std::string& ) ;
|
||||
|
||||
/**
|
||||
* Examine a given C++ string and return true if it contains
|
||||
* a time specification, return false otherwise.
|
||||
*/
|
||||
bool IsTimeSpec( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Examine a given C++ string and return true if it contains
|
||||
* all numeric characters, return false otherwise.
|
||||
*/
|
||||
bool IsNumeric( const std::string& ) ;
|
||||
|
||||
/**
|
||||
* Returns the time which is given as #<d/h/m/s> as seconds
|
||||
*/
|
||||
time_t extractTime( std::string Length, unsigned int defaultUnits ) ;
|
||||
|
||||
int atoi( const std::string& ) ;
|
||||
|
||||
std::string itoa(int) ;
|
||||
|
||||
/**
|
||||
* Extract the parts of a *valid* nick!user@hostip address
|
||||
*/
|
||||
std::string extractNick( const std::string& ) ;
|
||||
|
||||
std::string extractUser( const std::string& ) ;
|
||||
|
||||
std::string extractNickUser( const std::string& ) ;
|
||||
|
||||
std::string extractHostIP( const std::string& ) ;
|
||||
|
||||
// Check for valid hostmask.
|
||||
bool validUserMask(const std::string& );
|
||||
|
||||
bool checkAllValidChars(const std::string& );
|
||||
|
||||
bool validHostName(const std::string& );
|
||||
|
||||
/* Formats a timestamp into a "X Days, XX:XX:XX" from 'Now'. */
|
||||
const std::string prettyDuration( int ) ;
|
||||
|
||||
const std::string TokenStringsParams(const char*,...);
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif /* __MISC__ */
|
||||
@@ -0,0 +1,221 @@
|
||||
/**
|
||||
* xparameters.h
|
||||
* This class originally written by Orlando Bassotto.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
* USA.
|
||||
*
|
||||
* $Id: xparameters.h,v 1.4 2003/12/29 23:59:36 dan_karrels Exp $
|
||||
*/
|
||||
|
||||
#ifndef __XPARAMETERS_H
|
||||
#define __XPARAMETERS_H "$Id: xparameters.h,v 1.4 2003/12/29 23:59:36 dan_karrels Exp $"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "ELog.h"
|
||||
|
||||
namespace gnuworld
|
||||
{
|
||||
|
||||
/**
|
||||
* This class is similar to StringTokenizer, except that it accepts
|
||||
* pointers to already tokenized C character arrays. This class
|
||||
* will not attempt to copy the arrays. This is done for efficiency.
|
||||
* The tokens kept by instances of this class are indexed starting
|
||||
* at zero.
|
||||
* This class is mutable.
|
||||
*/
|
||||
class xParameters
|
||||
{
|
||||
|
||||
private:
|
||||
/**
|
||||
* The structure type for storing the tokens internally.
|
||||
*/
|
||||
typedef std::vector< char* > vectorType ;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* The variable type used to represent this class's
|
||||
* size (number of tokens in the object).
|
||||
*/
|
||||
typedef vectorType::size_type size_type ;
|
||||
|
||||
/**
|
||||
* Construct an xParameters object.
|
||||
*/
|
||||
xParameters() {}
|
||||
|
||||
/**
|
||||
* Destroy an xParameters object.
|
||||
* No streams have been opened, and no memory explicitly
|
||||
* dynamically allocated, so this method is a NOOP.
|
||||
*/
|
||||
~xParameters() {}
|
||||
|
||||
/**
|
||||
* This type is used to perform a read only iteration
|
||||
* of this objects array of strings.
|
||||
*/
|
||||
typedef vectorType::const_iterator const_iterator ;
|
||||
|
||||
/**
|
||||
* Return a const_iterator to the beginning of the array
|
||||
* of strings.
|
||||
*/
|
||||
inline const_iterator begin() const
|
||||
{ return myVector.begin() ; }
|
||||
|
||||
/**
|
||||
* Return a const_iterator to the end of the array
|
||||
* of strings.
|
||||
*/
|
||||
inline const_iterator end() const
|
||||
{ return myVector.end() ; }
|
||||
|
||||
/**
|
||||
* Insert a pointer to a character array (token) into the
|
||||
* xParameters instance.
|
||||
*/
|
||||
inline xParameters& operator<<( char* Parameter )
|
||||
{ myVector.push_back( Parameter ) ; return *this ; }
|
||||
|
||||
/**
|
||||
* Return a pointer to a mutable character array token.
|
||||
* The tokens are indexed beginning at zero.
|
||||
* This method will assert false if the requested index
|
||||
* is out of bounds, according to validSubscript.
|
||||
*/
|
||||
inline char* operator[]( const size_type& pos ) const
|
||||
{ assert( validSubscript( pos ) ) ; return myVector[ pos ] ; }
|
||||
|
||||
/**
|
||||
* Set a particular element to a new value, NULL is
|
||||
* permitted.
|
||||
*/
|
||||
void setValue( const size_t pos, char* newVal )
|
||||
{ assert( validSubscript( pos ) ) ; myVector[ pos ] = newVal ; }
|
||||
|
||||
/**
|
||||
* Remove all tokens, if any, from this xParameters object.
|
||||
*/
|
||||
inline void Clear()
|
||||
{ myVector.clear() ; }
|
||||
|
||||
/**
|
||||
* Return the number of tokens held in this xParameters object.
|
||||
*/
|
||||
inline size_type Count() const
|
||||
{ return myVector.size() ; }
|
||||
|
||||
/**
|
||||
* Return the number of tokens held in this xParameters object.
|
||||
*/
|
||||
inline size_type size() const
|
||||
{ return myVector.size() ; }
|
||||
|
||||
/**
|
||||
* Return true if there exist no tokens represented by this
|
||||
* class.
|
||||
*/
|
||||
inline bool empty() const
|
||||
{ return myVector.empty() ; }
|
||||
|
||||
/**
|
||||
* Return true if the given variable (i) is a valid index
|
||||
* into the table of tokens.
|
||||
* Return false otherwise.
|
||||
*/
|
||||
inline bool validSubscript( const size_type& i ) const
|
||||
{ return (i < myVector.size()) ; }
|
||||
|
||||
/**
|
||||
* Return a string containing all tokens beginning
|
||||
* with the zero based index beginIndex. A ' ' will
|
||||
* be placed between token in the string returned.
|
||||
*/
|
||||
inline std::string assemble( const size_type& beginIndex ) const
|
||||
{
|
||||
assert( validSubscript( beginIndex ) ) ;
|
||||
if( myVector.empty() )
|
||||
{
|
||||
return std::string() ;
|
||||
}
|
||||
std::string retMe( "" ) ;
|
||||
for( vectorType::size_type i = beginIndex ;
|
||||
i < myVector.size() ; ++i )
|
||||
{
|
||||
retMe += myVector[ i ] ;
|
||||
if( (i + 1) < myVector.size() )
|
||||
{
|
||||
retMe += ' ' ;
|
||||
}
|
||||
}
|
||||
return retMe ;
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple operator to output an xParameters object to
|
||||
* an ELog stream.
|
||||
*/
|
||||
friend ELog& operator<<( ELog& out, const xParameters& param )
|
||||
{
|
||||
// Iterate through to each element
|
||||
for( size_type i = 0 ; i < param.size() ; ++i )
|
||||
{
|
||||
// Place this element into the ELog stream
|
||||
out << param[ i ] ;
|
||||
|
||||
// If there is at least one more token left,
|
||||
// place a space character into the stream
|
||||
if( (i + 1) < param.size() )
|
||||
{
|
||||
out << ' ' ;
|
||||
}
|
||||
}
|
||||
// Return the ELog stream so that it may be used
|
||||
// for further pipelining of output
|
||||
return out ;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Disable copy constructing.
|
||||
* This method is declared, but NOT defined.
|
||||
*/
|
||||
xParameters( const xParameters& ) ;
|
||||
|
||||
/**
|
||||
* Disable default assignment.
|
||||
* This method is declared but NOT defined.
|
||||
*/
|
||||
xParameters operator=( const xParameters& ) ;
|
||||
|
||||
/**
|
||||
* This is the structure used to store tokens internally.
|
||||
*/
|
||||
vectorType myVector ;
|
||||
|
||||
} ;
|
||||
|
||||
} // namespace gnuworld
|
||||
|
||||
#endif // __XPARAMETERS_H
|
||||
Reference in new issue
Block a user