CopyOnWriteData.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:24k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
C/C++
- /* ====================================================================
- * 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 CopyOnWriteData_cxx_Version =
- "$Id: CopyOnWriteData.cxx,v 1.23 2001/05/28 21:28:34 bko Exp $";
- //Authors: Sunitha Kumar, Cullen Jennings
- #include "CopyOnWriteData.hxx"
- #include <cstdio>
- #include <ctype.h>
- //PURIFYSTART
- #include <strings.h>
- //PURIFYSTOP
- #include "cpLog.h"
- #if USE_HASH_MAP
- #include <hash_map>
- #endif
- #define INLINE_ inline
- static const int baseMultiple = 32;
- INLINE_
- CopyOnWriteData::CopyOnWriteData()
- :
- data_(),
- myCdata(),
- changed(true)
- {
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( const char* str, int length )
- :
- data_(str, length),
- myCdata(),
- changed(true)
- {
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( const char* str )
- :
- data_(str, mystrlen(str)),
- myCdata(),
- changed(true)
- {
- changed = true;
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( const string& str )
- :
- data_(str.c_str(), str.length()),
- myCdata(),
- changed(true)
- {
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( const mstring& mstr )
- :
- data_(mstr.c_str(), mstr.length()),
- myCdata(),
- changed(true)
- {
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( int value )
- :
- data_(8),
- myCdata(),
- changed(true)
- {
- char str[256];
- sprintf(str, "%d", value);
- int tmp = mystrlen(str);
- data_.makeWritable(tmp);
- mmemcpy(data_.bitsPtr, str, tmp);
- data_.size = tmp;
- }
- INLINE_
- CopyOnWriteData::CopyOnWriteData( const CopyOnWriteData& data )
- :
- data_(data.data_),
- myCdata(),
- changed(true)
- {
- }
- INLINE_
- CopyOnWriteData&
- CopyOnWriteData::operator=( const char* str )
- {
- ASSERT_WRITE(t1, myAssertable);
- // Vocal::Threads::WriteLock
- int tmp = mystrlen(str);
- data_.clear();
- data_.makeWritable(tmp);
- mmemcpy(data_.bitsPtr, str, tmp);
- data_.size = tmp;
- changed = true;
- return *this;
- }
- INLINE_
- CopyOnWriteData&
- CopyOnWriteData::operator=( const CopyOnWriteData& data )
- {
- ASSERT_WRITE(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- if (this != &data)
- {
- data_.clear();
- data_ = data.data_;
- }
- changed = true;
- return *this;
- }
- INLINE_
- const char*
- CopyOnWriteData::getData() const
- {
- ASSERT_READ(t1, myAssertable);
- if(changed)
- {
- changed = false;
- return myCdata.c_str(data_.bitsPtr, data_.size);
- }
- else
- {
- return myCdata.get_c_str();
- }
- }
- INLINE_
- const char*
- CopyOnWriteData::getDataBuf() const
- {
- ASSERT_READ(t1, myAssertable);
- return data_.bitsPtr;
- }
- INLINE_
- char
- CopyOnWriteData::getChar(int i) const
- {
- ASSERT_READ(t1, myAssertable);
- assert(i < data_.size);
- return data_.bitsPtr[i];
- }
- // this version does not check for assert_read, so it is internal only
- INLINE_
- char
- CopyOnWriteData::getCharInternal(int i) const
- {
- assert(i < data_.size);
- return data_.bitsPtr[i];
- }
- INLINE_
- void
- CopyOnWriteData::setchar(int i, char c)
- {
- ASSERT_WRITE(t1, myAssertable);
- data_.makeWritable(i + 1);
- if (i >= data_.size)
- {
- // do something now
- data_.size = i + 1;
- }
- data_.bitsPtr[i] = c;
- changed = true;
- }
- INLINE_
- char
- CopyOnWriteData::operator[](int i) const
- {
- ASSERT_READ(t1, myAssertable);
- return getCharInternal(i);
- }
- INLINE_
- int
- CopyOnWriteData::length() const
- {
- ASSERT_READ(t1, myAssertable);
- return data_.size;
- }
- INLINE_
- void
- CopyOnWriteData::setBufferSize(int size)
- {
- ASSERT_WRITE(t1, myAssertable);
- data_.makeWritable(size);
- changed = true;
- }
- INLINE_
- int
- CopyOnWriteData::compare(const char* str, int length) const
- {
- ASSERT_READ(t1, myAssertable);
- return data_.compare(str, length);
- }
- INLINE_
- int
- CopyOnWriteData::compare(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return data_.compare(data.data_.bitsPtr, data.data_.size);
- }
- INLINE_
- int
- CopyOnWriteData::compareNoCase(const char* str, int length) const
- {
- ASSERT_READ(t1, myAssertable);
- return data_.compareNoCase(str, length);
- }
- INLINE_
- int
- CopyOnWriteData::compareNoCase(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return data_.compareNoCase(data.data_.bitsPtr, data.data_.size);
- }
- INLINE_
- bool
- CopyOnWriteData::operator==(const char* str) const
- {
- // return ( data_.compare(str, mystrlen(str)) == 0 );
- ASSERT_READ(t1, myAssertable);
- return ( data_.compareCstr(str) == 0 );
- }
- INLINE_
- bool
- CopyOnWriteData::operator==( const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return ( data_.compare(data.data_.bitsPtr, data.data_.size) == 0 );
- }
- INLINE_
- bool
- CopyOnWriteData::operator!=(const char* str) const
- {
- ASSERT_READ(t1, myAssertable);
- return !operator==(str);
- }
- INLINE_
- bool
- CopyOnWriteData::operator!=(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return !operator==(data);
- }
- INLINE_
- bool
- CopyOnWriteData::operator>(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return ( data_.compare(data.data_.bitsPtr, data.data_.size) > 0 );
- }
- INLINE_
- bool
- CopyOnWriteData::operator<(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- return ( data_.compare(data.data_.bitsPtr, data.data_.size) < 0 );
- }
- INLINE_
- bool
- CopyOnWriteData::operator>(const char* data) const
- {
- ASSERT_READ(t1, myAssertable);
- return ( data_.compareCstr(data) > 0 );
- }
- INLINE_
- bool
- CopyOnWriteData::operator<(const char* data) const
- {
- ASSERT_READ(t1, myAssertable);
- return ( data_.compareCstr(data) < 0 );
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::operator+(const CopyOnWriteData& data) const
- {
- ASSERT_READ(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- CopyOnWriteData tmp(*this);
- tmp += data;
- return tmp;
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::operator+(const char* str) const
- {
- ASSERT_READ(t1, myAssertable);
- CopyOnWriteData tmp(*this);
- tmp += str;
- return tmp;
- }
- INLINE_
- void
- CopyOnWriteData::operator+=(const CopyOnWriteData& data)
- {
- ASSERT_WRITE(t1, myAssertable);
- ASSERT_READ(t2, data.myAssertable);
- if (data.data_.size)
- {
- data_.makeWritable(data_.size + data.data_.size);
- mmemcpy(data_.bitsPtr + data_.size,
- data.data_.bitsPtr, data.data_.size);
- data_.size += data.data_.size;
- }
- changed = true;
- }
- INLINE_
- void
- CopyOnWriteData::operator+=(const char* str)
- {
- ASSERT_WRITE(t1, myAssertable);
- int len = mystrlen(str);
- if (len)
- {
- data_.makeWritable(data_.size + len);
- mmemcpy(data_.bitsPtr + data_.size, str, len);
- data_.size += len;
- }
- changed = true;
- }
- INLINE_
- CopyOnWriteData::~CopyOnWriteData()
- {
- }
- INLINE_
- void
- CopyOnWriteData::erase()
- {
- ASSERT_WRITE(t1, myAssertable);
- data_.clear();
- changed = true;
- }
- INLINE_
- CopyOnWriteData::operator string() const
- {
- ASSERT_READ(t1, myAssertable);
- return string(data_.bitsPtr, static_cast < unsigned int > (data_.size));
- }
- INLINE_
- CopyOnWriteData::operator const char*() const
- {
- return getData();
- }
- INLINE_
- CopyOnWriteData::operator mstring() const
- {
- return mstring(getData());
- }
- INLINE_
- CopyOnWriteData::operator int() const
- {
- ASSERT_READ(t1, myAssertable);
- int l = data_.size;
- int val = 0;
- char* p = data_.bitsPtr;
- while (l--)
- {
- char c = *p++;
- if ((c >= '0') && (c <= '9'))
- {
- val *= 10;
- val += c - '0';
- }
- else
- {
- return val;
- }
- }
- return val;
- }
- INLINE_
- void CopyOnWriteData::lowercase()
- {
- ASSERT_WRITE(t1, myAssertable);
- int len = data_.size;
- data_.makeWritable(len);
- int i = 0;
- while(i < len)
- {
- data_.bitsPtr[i] = std::tolower(data_.bitsPtr[i]);
- i++;
- }
- changed = true;
- }
- INLINE_
- void CopyOnWriteData::uppercase()
- {
- ASSERT_WRITE(t1, myAssertable);
- int len = data_.size;
- data_.makeWritable(len);
- int i = 0;
- while(i < len)
- {
- data_.bitsPtr[i] = std::toupper(data_.bitsPtr[i]);
- i++;
- }
- changed = true;
- }
- INLINE_
- string CopyOnWriteData::getstring() const
- {
- ASSERT_READ(t1, myAssertable);
- return string(data_.bitsPtr, static_cast < unsigned int > (data_.size));
- }
- INLINE_
- int
- CopyOnWriteData::find( const CopyOnWriteData& match, int start )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int pos = 0;
- if ( start != npos )
- {
- pos = start;
- }
- const char* ptr = data_.bitsPtr + pos;
- while ((pos + match.data_.size) <= data_.size)
- {
- if ( (*(ptr) == *match.data_.bitsPtr ) &&
- (strncmp(ptr,
- match.data_.bitsPtr,
- match.data_.size)
- == 0))
- {
- // a match has been found
- return pos;
- }
- ++ptr;
- ++pos; // or not
- }
- return npos;
- }
- INLINE_
- int
- CopyOnWriteData::find( const char* match, int start )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int matchLength = mystrlen(match);
- int pos = 0;
- if ( start != npos )
- {
- pos = start;
- }
- int compares = data_.size - matchLength - pos;
- const char* ptr = data_.bitsPtr + pos;
- while ((pos + matchLength) <= data_.size)
- // while((pos + matchLength) <= data_.size)
- while (compares-- >= 0)
- {
- if (
- (*match == *(ptr)) &&
- ( strncmp(ptr,
- match,
- matchLength)
- == 0))
- {
- // a match has been found
- return pos;
- }
- ++ptr;
- ++pos; // or not
- }
- return npos;
- }
- INLINE_
- int
- CopyOnWriteData::match( const char* match,
- CopyOnWriteData* retModifiedCopyOnWriteData,
- bool doReplace,
- CopyOnWriteData replaceWith)
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- if (replaceWith != "")
- {
- int retVal;
- string::size_type pos = getstring().find(match);
- if (pos == string::npos)
- {
- return NOT_FOUND;
- }
- int matchLength = mystrlen(match);
- string::size_type replacePos = pos + matchLength;
- retVal = FIRST;
- if (retModifiedCopyOnWriteData)
- {
- // (*retModifiedCopyOnWriteData) = getstring().substr(0, pos);
- ASSERT_UNLOCK(myAssertable);
- (*retModifiedCopyOnWriteData) = *this;
- ASSERT_WRITELOCK(myAssertable);
- (*retModifiedCopyOnWriteData).data_.substr(0, pos);
- if (retModifiedCopyOnWriteData->data_.size)
- {
- retVal = FOUND;
- }
- }
- if (doReplace)
- {
- if (replacePos <= static_cast < unsigned int > (data_.size) )
- {
- replace(0, replacePos, replaceWith);
- }
- else
- {
- printf("pos=%d match.len=%d replacePos=%d buf.size()= %dn",
- pos,
- matchLength,
- replacePos,
- data_.size );
- }
- }
- return retVal;
- }
- else
- {
- int retVal;
- ASSERT_UNLOCK(myAssertable);
- int pos = find(match);
- ASSERT_WRITELOCK(myAssertable);
- if (pos == npos)
- {
- return NOT_FOUND;
- }
- int matchLength = mystrlen(match);
- int replacePos = pos + matchLength;
- retVal = FIRST;
- if (retModifiedCopyOnWriteData)
- {
- // (*retModifiedCopyOnWriteData) = getstring().substr(0, pos);
- ASSERT_UNLOCK(myAssertable);
- (*retModifiedCopyOnWriteData) = *this;
- ASSERT_WRITELOCK(myAssertable);
- (*retModifiedCopyOnWriteData).data_.substr(0, pos);
- if (retModifiedCopyOnWriteData->data_.size)
- {
- retVal = FOUND;
- }
- }
- if (doReplace)
- {
- if (replacePos <= data_.size )
- {
- data_.truncate(replacePos, 0);
- }
- else
- {
- printf("pos=%d match.len=%d replacePos=%d buf.size()= %dn",
- pos,
- matchLength,
- replacePos,
- data_.size );
- }
- }
- return retVal;
- }
- }
- static INLINE_
- bool isIn(char c, const char* match)
- {
- char p;
- while((p = *match++))
- {
- if(p == c)
- {
- return true;
- }
- }
- return false;
- }
- class CharSet
- {
- public:
- CharSet(const char* matchString);
- bool isIn(char c);
- private:
- bool myArray[256];
- };
- INLINE_
- CharSet::CharSet(const char* matchString)
- {
- bzero(myArray, 256 * sizeof(bool));
- while(*matchString != ' ')
- {
- myArray[*matchString] = true;
- }
- }
- INLINE_ bool
- CharSet::isIn(char c)
- {
- return myArray[c];
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::parseOutsideQuotes( const char* match,
- bool useQuote,
- bool useAngle,
- bool* matchFail )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int pos = 0;
- bool inDoubleQuotes = false;
- bool inAngleBrackets = false;
- bool foundAny = false;
- while(!foundAny && (pos < data_.size))
- {
- char p = *(data_.bitsPtr + pos);
- switch (p)
- {
- case '"':
- if(!inAngleBrackets && useQuote)
- {
- inDoubleQuotes = !inDoubleQuotes;
- }
- break;
- case '<':
- if(!inDoubleQuotes && useAngle)
- {
- inAngleBrackets = true;
- }
- break;
- case '>':
- if(!inDoubleQuotes && useAngle)
- {
- inAngleBrackets = false;
- }
- break;
- default:
- break;
- }
- if(!inDoubleQuotes && !inAngleBrackets && isIn(p, match))
- {
- foundAny = true;
- }
- pos++;
- }
- int pos2 = pos;
- while(
- foundAny &&
- (pos2 < data_.size) &&
- isIn(*(data_.bitsPtr + pos2), match)
- )
- {
- pos2++;
- }
- CopyOnWriteData result;
- if(foundAny)
- {
- ASSERT_UNLOCK(myAssertable);
- result = *this;
- ASSERT_WRITELOCK(myAssertable);
- result.data_.substr(0, pos - 1);
- data_.substr(pos2, data_.size);
- if(matchFail)
- {
- *matchFail = false;
- }
- }
- else
- {
- if(matchFail)
- {
- *matchFail = true;
- }
- }
- return result;
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::parse( const char* match, bool* matchFail )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int pos = 0;
- bool foundAny = false;
- while(!foundAny && (pos < data_.size))
- {
- char p = *(data_.bitsPtr + pos);
- if(isIn(p, match))
- {
- foundAny = true;
- }
- pos++;
- }
- int pos2 = pos;
- while(
- foundAny &&
- (pos2 < data_.size) &&
- isIn(*(data_.bitsPtr + pos2), match)
- )
- {
- pos2++;
- }
- CopyOnWriteData result;
- if(foundAny)
- {
- ASSERT_UNLOCK(myAssertable);
- result = *this;
- ASSERT_WRITELOCK(myAssertable);
- result.data_.substr(0, pos - 1);
- data_.substr(pos2, data_.size);
- if(matchFail)
- {
- *matchFail = false;
- }
- }
- else
- {
- if(matchFail)
- {
- *matchFail = true;
- }
- }
- return result;
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::matchChar( const char* match, char* matchedChar )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int pos = 0;
- bool foundAny = false;
- while(!foundAny && (pos < data_.size))
- {
- char p = *(data_.bitsPtr + pos);
- if(isIn(p, match))
- {
- foundAny = true;
- if(matchedChar)
- {
- *matchedChar = p;
- }
- }
- pos++;
- }
- CopyOnWriteData result;
- if(foundAny)
- {
- ASSERT_UNLOCK(myAssertable);
- result = *this;
- ASSERT_WRITELOCK(myAssertable);
- result.data_.substr(0, pos - 1);
- data_.substr(pos, data_.size);
- }
- else if(matchedChar)
- {
- *matchedChar = ' ';
- }
- return result;
- }
- INLINE_
- CopyOnWriteData
- CopyOnWriteData::getLine( bool* matchFail )
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- const int STARTING = 0;
- const int HAS_CR = 1;
- const int HAS_LF = 2;
- const int HAS_CRLF = 3;
- int state = STARTING;
- int pos = 0;
- bool foundAny = false;
- while(!foundAny && (pos < data_.size))
- {
- char p = *(data_.bitsPtr + pos);
- switch(p)
- {
- case 'r':
- state = HAS_CR;
- break;
- case 'n':
- if(state == HAS_CR)
- {
- state = HAS_CRLF;
- }
- else
- {
- state = HAS_LF;
- }
- foundAny = true;
- break;
- default:
- state = STARTING;
- }
- pos++;
- }
- int pos2 = pos;
- if(state == HAS_CRLF)
- {
- pos--;
- }
- CopyOnWriteData result;
- if(foundAny)
- {
- ASSERT_UNLOCK(myAssertable);
- result = *this;
- ASSERT_WRITELOCK(myAssertable);
- result.data_.substr(0, pos - 1);
- data_.substr(pos2, data_.size);
- if(matchFail)
- {
- *matchFail = false;
- }
- }
- else
- {
- if(matchFail)
- {
- *matchFail = true;
- }
- }
- return result;
- }
- INLINE_
- bool isEqualNoCase(const CopyOnWriteData& left, const CopyOnWriteData& right)
- {
- ASSERT_READ(t1, left.myAssertable);
- ASSERT_READ(t2, right.myAssertable);
- return (left.compareNoCase(right) == 0);
- }
- INLINE_
- bool isEqualNoCase(const char* left, const CopyOnWriteData& right)
- {
- ASSERT_READ(t1, right.myAssertable);
- return (right.compareNoCase(left, mystrlen(left)) == 0);
- }
- INLINE_
- void
- CopyOnWriteData::replace(int startpos, int endpos,
- const CopyOnWriteData& replaceStr)
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- if (data_.size)
- {
- string str = getstring();
- str.replace(startpos, endpos, replaceStr.getstring());
- *this = CopyOnWriteData(str);
- }
- }
- INLINE_
- void CopyOnWriteData::removeSpaces()
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- if (data_.size == 0)
- {
- return ;
- }
- int firstChar = 0;
- while ((firstChar < data_.size) && (getCharInternal(firstChar) == ' '))
- {
- firstChar++;
- }
- int lastChar = data_.size;
- while ((lastChar > 0) && (getCharInternal(lastChar - 1) == ' '))
- {
- lastChar--;
- }
- if (firstChar > lastChar)
- {
- firstChar = lastChar;
- }
- data_.substr(firstChar, lastChar);
- }
- INLINE_
- void CopyOnWriteData::removeLWS()
- {
- ASSERT_WRITE(t1, myAssertable);
- changed = true;
- int replaceTo;
- int pos = -1;
- ASSERT_UNLOCK(myAssertable);
- pos = find ("rn");
- ASSERT_WRITELOCK(myAssertable);
- if (pos > data_.size)
- {
- ASSERT_UNLOCK(myAssertable);
- pos = find("n");
- ASSERT_WRITELOCK(myAssertable);
- }
- else
- {
- replaceTo = pos+1; //should end after rn
- }
- if (pos > data_.size)
- {
- return;
- }
- else
- {
- replaceTo = pos; //should end after n
- }
- bool replaceFlag = false;
- do
- {
- while ( (replaceTo+1 < data_.size) &&
- ( (getCharInternal(replaceTo+1) == 't') ||
- (getCharInternal(replaceTo+1) == ' ')
- )
- )
- {
- char temp = getCharInternal(replaceTo);
- replaceTo++;
- temp = getCharInternal(replaceTo);
- replaceFlag = true;
- }
- if (replaceFlag)
- {
- int replaceFrom = pos;
- while ( (replaceFrom-1 > 0) &&
- ( (getCharInternal(replaceFrom-1) == 't') ||
- (getCharInternal(replaceFrom-1) == ' ')
- )
- )
- {
- char temp = getCharInternal(replaceFrom);
- replaceFrom--;
- temp = getCharInternal(replaceFrom);
- }
- int replaceNum = replaceTo-replaceFrom;
- replace(replaceFrom, replaceNum, ""); //replace with nothing
- }
- //remember pos.
- int initpos = pos;
- ASSERT_UNLOCK(myAssertable);
- pos = find("rn", initpos+2);
- ASSERT_WRITELOCK(myAssertable);
- if (pos > data_.size)
- {
- ASSERT_UNLOCK(myAssertable);
- pos = find("n", initpos+2);
- ASSERT_WRITELOCK(myAssertable);
- replaceTo = pos;
- }
- else
- {
- replaceTo = pos+1;
- }
- }
- while (pos < data_.size);
- }
- INLINE_
- void
- CopyOnWriteData::expand(CopyOnWriteData startFrom,
- CopyOnWriteData findstr,
- CopyOnWriteData replstr,
- CopyOnWriteData delimiter)
- {
- ASSERT_WRITE(t1,myAssertable);
- ASSERT_READ(t2,startFrom.myAssertable);
- ASSERT_READ(t3,findstr.myAssertable);
- ASSERT_READ(t4,replstr.myAssertable);
- ASSERT_READ(t5, delimiter.myAssertable);
- changed = true;
- ASSERT_UNLOCK(myAssertable);
- int startPos = find(startFrom);
- ASSERT_WRITELOCK(myAssertable);
- if (startPos < npos)
- {
- ASSERT_UNLOCK(myAssertable);
- int delimPos
- = find(delimiter, startPos);
- int findPos
- = find(findstr, startPos);
- ASSERT_WRITELOCK(myAssertable);
- while (findPos < delimPos)
- {
- //found replstr, replace
- replace( findPos, findstr.data_.size, replstr.getstring());
- //find next.
- ASSERT_UNLOCK(myAssertable);
- delimPos
- = find( delimiter,
- findPos + replstr.data_.size);
- findPos
- = find( findstr, findPos);
- ASSERT_WRITELOCK(myAssertable);
- }
- }
- }
- INLINE_
- ostream&
- operator<<(ostream& s, const CopyOnWriteData& data)
- {
- ASSERT_READ(t1, data.myAssertable);
- s.write(data.data_.bitsPtr, data.data_.size);
- return s;
- }
- INLINE_
- bool
- operator==(const char* str, const CopyOnWriteData& d)
- {
- ASSERT_READ(t1, d.myAssertable);
- return ( d.data_.compare(str, mystrlen(str)) == 0 );
- }
- #if 0
- INLINE_
- bool
- operator!=(const char* str, CopyOnWriteData& d)
- {
- return (d.data_.compare(str, mystrlen(str)) != 0 );
- }
- #endif
- /* Local Variables: */
- /* c-file-style: "stroustrup" */
- /* indent-tabs-mode: nil */
- /* c-file-offsets: ((access-label . -) (inclass . ++)) */
- /* c-basic-offset: 4 */
- /* End: */