NewData.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:14k
- /* ====================================================================
- * 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/>.
- *
- */
- static const char* const NewData_cxx_Version =
- "$Id: NewData.cxx,v 1.16 2001/06/29 03:56:00 bko Exp $";
- //Authors: Sunitha Kumar, Cullen Jennings
- //#include <cstdio>
- //#include <ctype.h>
- #include "cpLog.h"
- #include "NewData.hxx"
- #include "DataException.hxx"
- #if USE_HASH_MAP
- #if (defined(__GNUC__) && (__GNUC__ < 3))
- #include <hash_map>
- #else
- #if defined(__GNUC__)
- #include <ext/hash_map>
- #endif
- #endif
- #endif
- NewData::NewData()
- {
- const char* str = "";
- buflen = boundary(strlen(str) + 1);
- buf = new char[buflen];
- strcpy(buf, str);
- }
- NewData::NewData( const char* str, int length )
- {
- str = str ? str : "";
- buflen = boundary(strlen(str) + 1);
- buf = new char[buflen];
- strcpy(buf, str);
- }
- NewData::NewData( const char* str )
- {
- str = str ? str : "";
- buflen = boundary(strlen(str) + 1);
- buf = new char[buflen];
- strcpy(buf, str);
- }
- NewData::NewData( const string& str)
- {
- buflen = boundary(str.length() + 1);
- buf = new char[buflen];
- strcpy(buf, str.c_str());
- }
- NewData::NewData( const mstring& mstr)
- {
- buflen = boundary(mstr.length() + 1);
- buf = new char[buflen];
- strcpy(buf, mstr.c_str());
- }
- NewData::NewData( int value)
- {
- char str[256];
- sprintf(str, "%d", value);
- buflen = boundary(strlen(str) + 1);
- buf = new char[buflen];
- strcpy(buf, str);
- }
- NewData::NewData( const NewData& data )
- {
- buflen = boundary(strlen(data.buf) + 1);
- buf = new char[buflen];
- strcpy(buf, data.buf);
- }
- NewData&
- NewData::operator=( const char* str )
- {
- int reqlen = strlen(str) + 1;
- if (reqlen < buflen)
- {
- strcpy(buf, str);
- }
- else
- {
- buflen = boundary(reqlen);
- delete [] buf;
- buf = new char[buflen];
- strcpy(buf, str);
- }
- return (*this);
- }
- NewData&
- NewData::operator=( const NewData& data )
- {
- if (this != &data)
- {
- int reqlen = strlen(data.buf) + 1;
- if (reqlen < buflen)
- {
- strcpy(buf, data.buf);
- }
- else
- {
- buflen = boundary(reqlen);
- delete [] buf;
- buf = new char[buflen];
- strcpy(buf, data.buf);
- }
- }
- return (*this);
- }
- const char*
- NewData::getData() const
- {
- return buf;
- }
- const char*
- NewData::getDataBuf() const
- {
- return buf;
- }
- int
- NewData::boundary(int len) const
- {
- const int step = 64;
- int res = (len / step);
- if (res == 0)
- return step;
- if (res > 0)
- {
- int count = 1;
- do
- {
- count = count * 2;
- if (res < count)
- return (count * step);
- }
- while (res >= count);
- return step;
- }
- return step;
- }
- char
- NewData::getChar(int i) const
- {
- if (( i < 0 ) || (i > static_cast < int > (strlen(buf)) ) )
- {
- cpLog(LOG_ERR, "NewData:getchar: i is out of range.");
- throw DataException(
- "i is out of range",
- __FILE__,
- __LINE__
- );
- return ' ';
- }
- return buf[i];
- }
- void NewData::setchar(int i, char c)
- {
- if (( i < 0) || (i > static_cast < int > (strlen(buf)) ) )
- {
- cpLog(LOG_ERR, "NewData:setchar: i is out of range.");
- throw DataException(
- "i is out of range",
- __FILE__,
- __LINE__
- );
- }
- else
- {
- buf[i] = c;
- }
- }
- char
- NewData::operator[](int i)
- {
- char ch;
- ch = buf[i];
- return ch;
- }
- int
- NewData::length() const
- {
- return strlen(buf);
- }
- bool NewData::operator==(const char* str) const
- {
- #if 0
- char* ptr = bitsPtr;
- int leftSize = size;
- while (leftSize && rightSize)
- {
- if (*ptr < *str)
- {
- return -1;
- }
- if (*ptr > *str)
- {
- return 1;
- }
- leftSize--;
- rightSize--;
- ptr++;
- str++;
- }
- if (leftSize < rightSize)
- {
- return -1;
- }
- else if (leftSize > rightSize)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- #endif
- return (strcmp(buf, str) == 0);
- }
- bool NewData::operator==( const NewData& data) const
- {
- int comparison = strcmp(buf, data.buf);
- #if 1
- return ( comparison == 0 );
- #else
- if (data.hashfn() == hashfn())
- {
- return ( comparison == 0 );
- }
- else
- {
- return false;
- }
- #endif
- }
- bool NewData::operator!=(const char* str) const
- {
- int comparison = strcmp(buf, str);
- return ( comparison != 0 );
- }
- bool NewData::operator!=(const NewData& data) const
- {
- int comparison = strcmp(buf, data.buf);
- return ( comparison != 0 );
- #if 0
- if (data.hashfn() == hashfn())
- {
- return ( comparison != 0 );
- }
- else
- {
- return true;
- }
- #endif
- }
- bool
- NewData::operator>(const NewData& data) const
- {
- int comparison = strcmp(buf, data.buf);
- return ( comparison > 0 );
- }
- bool
- NewData::operator<(const NewData& data) const
- {
- int comparison = strcmp(buf, data.buf);
- return ( comparison < 0 );
- }
- NewData
- NewData::operator+(const NewData& data) const
- {
- //char newbuf[1];
-
- char* newbuf = new char[strlen(buf) + strlen(data.buf) + 1];
- strcpy(newbuf, buf);
- strcat(newbuf, data.buf);
- NewData temp(newbuf);
- delete [] newbuf;
- return temp;
- }
- NewData
- NewData::operator+(const char* str) const
- {
- char* newbuf = new char[strlen(buf) + strlen(str) + 1];
- strcpy(newbuf, buf);
- strcat(newbuf, str);
- NewData temp(newbuf);
- delete [] newbuf;
- return temp;
- }
- /*
- void
- Data::replace(int startpos, int replaceLen, const char* replaceStr)
- {
- char* newbuf = new char[strlen(buf) + 1];
- string str = string(buf);
- str.replace(startpos, replaceLen, replaceStr);
- strcpy (newbuf, str.c_str());
- delete [] buf;
- buf = newbuf;
- }
- */
- void
- NewData::replace(int startpos, int endpos, const NewData& replaceStr)
- {
- string str = string(buf);
- str.string::replace(startpos, endpos, replaceStr.operator string());
- int reqlen = str.length() + 1;
- if (reqlen < buflen)
- {
- strcpy(buf, str.c_str());
- }
- else
- {
- buflen = boundary(reqlen);
- char* newbuf = new char[buflen];
- strcpy (newbuf, str.c_str());
- delete [] buf;
- buf = newbuf;
- }
- }
- void
- NewData::operator+=(const NewData& data)
- {
- *this = *this + data;
- }
- void
- NewData::operator+=(const char* str)
- {
- *this = *this + str;
- }
- NewData::~NewData()
- {
- delete [] buf;
- }
- void
- NewData::erase()
- {
- strcpy (buf, "");
- }
- NewData::operator string() const
- {
- return string(buf);
- }
- NewData::operator const char*() const
- {
- return buf;
- }
- NewData::operator mstring() const
- {
- return mstring(buf);
- }
- NewData::operator int() const
- {
- return atoi( buf );
- }
- int NewData::match( const char* match,
- NewData* retModifiedNewData,
- bool doReplace,
- NewData replaceWith)
- {
- #if 1
- int retVal;
- string::size_type pos = (string(buf)).find(match);
- if (pos == string::npos)
- {
- cpLog(LOG_DEBUG_STACK, "Match not found");
- return NOT_FOUND;
- }
- string::size_type replacePos = pos + strlen(match);
- retVal = FIRST;
- if (retModifiedNewData)
- {
- (*retModifiedNewData) = (string(buf)).substr(0, pos);
- if (retModifiedNewData->length()) retVal = FOUND;
- }
- if (doReplace)
- {
- if (replacePos <= strlen(buf) )
- {
- replace(0, replacePos, replaceWith.getData());
- }
- else
- {
- printf("buf =<%s> match =<%s>n", buf , match );
- printf("pos=%d match.len=%d replacePos=%d buf.size()= %dn",
- pos,
- strlen(match),
- replacePos,
- strlen(buf) );
- }
- }
- return retVal;
- #else
- int pos = static_cast < int > ((string(buf)).find(match.getData()));
- if (pos == static_cast < int > (string::npos))
- {
- cpLog(LOG_DEBUG_STACK, "Match not found");
- pos = NOT_FOUND;
- }
- else
- {
- if (data)
- {
- (*data) = (string(buf)).substr(0, pos);
- }
- if (doReplace)
- {
- replace(0, (pos + match.length()), replaceWith.getData());
- }
- else
- {
- }
- if ( (data->length() == 0) && (pos != ( (int) string::npos) ) )
- {
- //the match string is the first item.
- pos = -2;
- pos = FIRST;
- }
- }
- if ( (pos != NOT_FOUND) && (pos != FIRST) )
- {
- pos = FOUND;
- }
- return pos;
- #endif
- }
- /*
-
- int Data::match( const Data& match, Data* retModifiedData, bool doReplace, const Data& replaceWith)
- {
- int retVal;
- string::size_type pos = buf.find(match.getData());
-
- if (pos == string::npos)
- {
- cpLog(LOG_DEBUG_STACK, "Match not found");
- return NOT_FOUND;
- }
-
- string::size_type replacePos = pos + match.length();
- retVal = FIRST;
-
- if (retModifiedData)
- {
- (*retModifiedData) = buf.substr(0, pos);
- if(retModifiedData->length()) retVal = FOUND;
- }
-
- if (doReplace)
- {
- newbuf = new char[strlen(buf) + 1];
- strcpy(newbuf, replaceWith.buf);
- strcat(newbuf, buf);
- strcpy(
- buf.replace(0, replacePos, replaceWith.getData());
- }
-
- return retVal;
- }
-
- */
- bool isEqualNoCase( const NewData& leftData, const NewData& rightData )
- {
- string leftbuf(leftData.buf);
- string rightbuf(rightData.buf);
- string::const_iterator leftIter = leftbuf.begin();
- string::const_iterator rightIter = rightbuf.begin();
- while ( (leftIter != leftbuf.end()) && (rightIter != rightbuf.end()) )
- {
- if (toupper(*leftIter) != toupper(*rightIter))
- {
- return false;
- }
- ++leftIter;
- ++rightIter;
- }
- if ( (leftIter != leftbuf.end()) || (rightIter != rightbuf.end()) )
- {
- // since both aren't the same length, they're not equal
- return false;
- }
- return true;
- }
- void NewData::removeSpaces()
- {
- //removes spaces before and after the characters.
- //Leaves the embedded spaces as is.
- if (strlen(buf) == 0)
- {
- return ;
- }
- char space = SPACE[0];
- char ch = buf[0];
- do
- {
- if (strlen(buf) == 0)
- break;
- if ( ch == space)
- {
- replace(0, strlen(SPACE), "");
- ch = buf[0];
- }
- else
- {
- break;
- }
- }
- while (ch == space);
- ch = buf[strlen(buf) - 1];
- do
- {
- if (strlen(buf) == 0)
- break;
- //if there are chars after val, discard .
- if (ch == space)
- {
- replace(strlen(buf) - 1, (strlen(buf) - 1) + strlen(SPACE) , "");
- ch = buf[strlen(buf) - 1];
- }
- else
- {
- break;
- }
- }
- while (ch == space);
- }
- void
- NewData::expand(NewData startFrom, NewData findstr, NewData replstr, NewData delimiter)
- {
- string::size_type startPos = (string(buf)).find(startFrom.getData());
- if (startPos < string::npos)
- {
- string::size_type delimPos = (string(buf)).find(delimiter.getData(), startPos);
- string::size_type findPos = (string(buf)).find( findstr.getData(), startPos);
- while (findPos < delimPos)
- {
- //found replstr, replace
- replace( findPos, strlen(findstr.getData()), replstr.getData());
- //find next.
- //delimPos = buf.find( delimiter.getData(), findPos);
- delimPos = (string(buf)).find( delimiter.getData(), findPos + static_cast < string > (replstr.getData()).size() );
- findPos = (string(buf)).find( findstr.getData(), findPos);
- }
- }
- }