planelement.cxx

来自「vovida的软交换」· CXX 代码 · 共 270 行

CXX
270
字号
/* ==================================================================== * The Vovida Software License, Version 1.0  *  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. *  * 3. The names "VOCAL", "Vovida Open Communication Application Library", *    and "Vovida Open Communication Application Library (VOCAL)" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor *    may "VOCAL" appear in their name, without prior written *    permission of Vovida Networks, Inc. *  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *  * ==================================================================== *  * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */#include "PlanElement.hxx"#include "cpLog.h"static const char* const PlanElement_cxx_Version =    "$Id: PlanElement.cxx,v 1.1 2003/05/07 09:28:26 suhac Exp $";/** * Constructor  */PlanElement::PlanElement(){    // regExpKey = 0;	// needsReplacement = 0;}/** * Constructor * @param  key  Regular expression * @param  contacts  List of contacts */PlanElement::PlanElement( const Data& key, const vector < Data > & contacts )        :        textKey(key){     contactList = contacts;	 needsReplacement = 0;    /// convert the key into regular expression format    LocalScopeAllocator lo;    if (regcomp(&regExpKey, key.getData(lo), REG_EXTENDED))        cpLog(LOG_ERR, "Failed to compile [%s] into regular expression format", key.logData());	/// Look into the contacts and see if regular expression grouping is	/// is used or not.	vector < Data >::const_iterator it = contacts.begin() ;	for( ; it != contacts.end(); it ++ ) {		const char *sz = (*it).getData(lo) ;		int ndx = 0 ;		while( ( sz[ndx] != '\0' ) && ( sz[ndx] != '\n' ) ) {			  if( ( sz[ndx] == '$' ) &&				  ( sz[ndx+1] > 0x30 ) &&				  ( sz[ndx+1] < 0x3A ) ) {				  cpLog(LOG_DEBUG,"Dial Plan[%s] has replacement string",key.logData() );				  needsReplacement = 1 ; 				  break ;			  }			  else {				 ndx ++ ;			  }		} /// end of while(){}		/// If atleast one contact exists which needs replacment, then		/// it has to be executed regarless.		if( needsReplacement )			break ;	} /// end of for( iterator ){}}/** * Copy Constructor * @param  rhs  Element to be copied */PlanElement::PlanElement( const PlanElement& rhs ){    if ( this != &rhs )    {        copyRhsToThis( rhs );    }}/** * Set element equal to specified element * @param  rhs  Element containing data to copy. * @return This element  */PlanElement&PlanElement::operator=( const PlanElement& rhs ){    if ( this != &rhs )        copyRhsToThis( rhs );    return *this;}/** * Copy data from specified element into this one. * @param  rhs  Element containing data to copy. */voidPlanElement::copyRhsToThis( const PlanElement& rhs ){    textKey = rhs.textKey;    /// convert the key into regular expression format    LocalScopeAllocator lo;    if (regcomp(&regExpKey, textKey.getData(lo), REG_EXTENDED))        cpLog(LOG_ERR, "Failed to compile [%s] into regular expression format", textKey.logData());        contactList = rhs.contactList;	needsReplacement = rhs.needsReplacement ;}/** * Destructor */PlanElement::~PlanElement(){    regfree(&regExpKey);}/** * Retrieve the regular expression in text form * @return Regular expression for this element. */const Data&PlanElement::getTextKey( ) const{    return (textKey);}/** * Retrieve the regular expression  * @return Regular expression for this element. */const regex_t&PlanElement::getKey( ) const{    return (regExpKey);}/** * Retrieve the regular expression Match Register * @return Regular expression Match Register for this element. */const regmatch_t *PlanElement::getMatch( ) const{    return (regMatch);}/** * Retrieve the contact list  * @return Contact list for this element. */const vector < Data > &PlanElement::getContacts( ) const{    return contactList;}/** * Check and see if the contact needs to be replaced using regular expressions */boolPlanElement::isToBeReplaced( ) const{	return( needsReplacement );}/** * Generate a new modified contact list based on  *   the provided matched "key"  *   the existing contact in the plan(using it as the replacement string) * @param  key  String that matched against the regular expressions in the table. * @param  elementFound List of contacts corresponding to expression matched. */voidPlanElement::replace( const Data& key , vector < Data > & elementFound){	vector < Data >::iterator contactIter = elementFound.begin() ;	/// For each of the contact in the contact List        LocalScopeAllocator lo, lo2;	for( ; contactIter != elementFound.end() ; contactIter ++ ) {		int szIndex = 0, newSzIndex = 0 ;		const char *repSz = (*contactIter).getData(lo) ;		const char *sz = key.getData(lo2) ;		char newSz[256] ;		while( ( repSz[szIndex] != '\0' ) && 			   ( repSz[szIndex] != '\n' ) ) {			   /// look for a pattern $[0-9]			   if( (repSz[szIndex] == '$') && 			   	   (repSz[szIndex+1] > 0x30) && 				   (repSz[szIndex+1] < 0x3A) ) {				   /// Start filling in the replacement characters				   /// Convert the digit from ascii.				   int j = regMatch[(repSz[szIndex+1] - 0x30)].rm_so ;				   while( j < regMatch[(repSz[szIndex+1] - 0x30)].rm_eo ) {				       newSz[newSzIndex++] = sz[j++] ;				   }				   /// Skip $[0-9]				   szIndex += 2 ;			   				} /// end of if{}				else {					newSz[newSzIndex++] = repSz[szIndex++] ;				}		} /* End of while() */		/// Null-terminate the new string		newSz[newSzIndex] = '\0' ;				cpLog( LOG_DEBUG,"Old Contact: %s, New Contact: %s",						(*contactIter).logData(), newSz ) ;		/// Replace the contact with the modified contact.		Data newContact( newSz ) ;		*contactIter = newContact ;	} /// end of for{}} /// end of replace()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?