reSIProcate/stack  9694
HeaderFieldValueList.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 
00006 #include <cassert>
00007 
00008 #include "resip/stack/HeaderFieldValue.hxx"
00009 #include "resip/stack/HeaderFieldValueList.hxx"
00010 #include "resip/stack/ParserContainerBase.hxx"
00011 #include "resip/stack/Embedded.hxx"
00012 #include "rutil/WinLeakCheck.hxx"
00013 
00014 using namespace resip;
00015 
00016 const HeaderFieldValueList HeaderFieldValueList::Empty;
00017 
00018 HeaderFieldValueList::~HeaderFieldValueList()
00019 {
00020    freeParserContainer();
00021 }
00022 
00023 HeaderFieldValueList::HeaderFieldValueList(const HeaderFieldValueList& rhs)
00024    : mHeaders(),
00025      mPool(0),
00026      mParserContainer(0)
00027 {
00028    if (rhs.mParserContainer)
00029    {
00030       mParserContainer = rhs.mParserContainer->clone();
00031    }
00032    else if(rhs.mHeaders.size())
00033    {
00034       mHeaders=rhs.mHeaders;
00035    }
00036 }
00037 
00038 HeaderFieldValueList::HeaderFieldValueList(const HeaderFieldValueList& rhs, PoolBase& pool)
00039    : mHeaders(StlPoolAllocator<HeaderFieldValue, PoolBase>(&pool)),
00040      mPool(&pool),
00041      mParserContainer(0)
00042 {
00043    if (rhs.mParserContainer)
00044    {
00045       mParserContainer = rhs.mParserContainer->clone();
00046    }
00047    else if(rhs.mHeaders.size())
00048    {
00049       mHeaders=rhs.mHeaders;
00050    }
00051 }
00052 
00053 HeaderFieldValueList&
00054 HeaderFieldValueList::operator=(const HeaderFieldValueList& rhs)
00055 {
00056    if(this!=&rhs)
00057    {
00058       mHeaders.clear();
00059 
00060       freeParserContainer();
00061 
00062       if (rhs.mParserContainer != 0)
00063       {
00064          mParserContainer = rhs.mParserContainer->clone();
00065       }
00066       else
00067       {
00068          mHeaders=rhs.mHeaders;
00069       }
00070    }
00071    
00072    return *this;
00073 }
00074 
00075 EncodeStream&
00076 HeaderFieldValueList::encode(int headerEnum, EncodeStream& str) const
00077 {
00078    const Data& headerName = Headers::getHeaderName(static_cast<Headers::Type>(headerEnum));
00079 
00080    if (getParserContainer() != 0)
00081    {
00082       getParserContainer()->encode(headerName, str);
00083    }
00084    else
00085    {
00086       if (!headerName.empty())
00087       {
00088          str << headerName << Symbols::COLON[0] << Symbols::SPACE[0];
00089       }
00090 
00091       for (HeaderFieldValueList::const_iterator j = begin();
00092            j != end(); j++)
00093       {
00094          if (j != begin())
00095          {
00096             if (Headers::isCommaEncoding(static_cast<Headers::Type>(headerEnum)))
00097             {
00098                str << Symbols::COMMA[0] << Symbols::SPACE[0];
00099             }
00100             else
00101             {
00102                str << Symbols::CRLF << headerName << Symbols::COLON << Symbols::SPACE;
00103             }
00104          }
00105          j->encode(str);
00106       }
00107       str << Symbols::CRLF;
00108    }
00109    return str;
00110 }
00111 
00112 EncodeStream&
00113 HeaderFieldValueList::encode(const Data& headerName, EncodeStream& str) const
00114 {
00115    if (getParserContainer() != 0)
00116    {
00117       getParserContainer()->encode(headerName, str);
00118    }
00119    else
00120    {
00121       if (!headerName.empty())
00122       {
00123          str << headerName << Symbols::COLON << Symbols::SPACE;
00124       }
00125       for (HeaderFieldValueList::const_iterator j = begin();
00126            j != end(); j++)
00127       {
00128          if (j != begin())
00129          {
00130             str << Symbols::COMMA[0] << Symbols::SPACE[0];
00131          }
00132          j->encode(str);
00133       }
00134       str << Symbols::CRLF;
00135    }
00136    return str;
00137 }
00138 
00139 EncodeStream&
00140 HeaderFieldValueList::encodeEmbedded(const Data& headerName, EncodeStream& str) const
00141 {
00142   assert(!headerName.empty());
00143 
00144    if (getParserContainer() != 0)
00145    {
00146       getParserContainer()->encodeEmbedded(headerName, str);
00147    }
00148    else
00149    {
00150       bool first = true;
00151       for (HeaderFieldValueList::const_iterator j = begin();
00152            j != end(); j++)
00153       {
00154          if (first)
00155          {
00156             first = false;
00157          }
00158          else
00159          {
00160             str << Symbols::AMPERSAND;
00161          }
00162 
00163          str << headerName << Symbols::EQUALS;
00164          Data buf;
00165          {
00166             DataStream s(buf);
00167             j->encode(s);
00168          }
00169          str << Embedded::encode(buf);
00170       }
00171    }
00172    return str;
00173 }
00174 
00175 void 
00176 HeaderFieldValueList::clear()
00177 {
00178    freeParserContainer();
00179    mHeaders.clear();
00180 }
00181 
00182 bool
00183 HeaderFieldValueList::parsedEmpty() const
00184 {
00185    if (mParserContainer)
00186    {
00187       return mParserContainer->empty();
00188    }
00189    else
00190    {
00191       return mHeaders.empty();
00192    }
00193 }
00194 
00195 void 
00196 HeaderFieldValueList::freeParserContainer()
00197 {
00198    if(mParserContainer)
00199    {
00200       mParserContainer->~ParserContainerBase();
00201       // The allocator will check whether this belongs to it, and if not, fall 
00202       // back to global operator delete.
00203       if(mPool)
00204       {
00205          mPool->deallocate(mParserContainer);
00206       }
00207       else
00208       {
00209          ::operator delete(mParserContainer);
00210       }
00211       mParserContainer=0;
00212    }
00213 }
00214 
00215 
00216 /* ====================================================================
00217  * The Vovida Software License, Version 1.0 
00218  * 
00219  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00220  * 
00221  * Redistribution and use in source and binary forms, with or without
00222  * modification, are permitted provided that the following conditions
00223  * are met:
00224  * 
00225  * 1. Redistributions of source code must retain the above copyright
00226  *    notice, this list of conditions and the following disclaimer.
00227  * 
00228  * 2. Redistributions in binary form must reproduce the above copyright
00229  *    notice, this list of conditions and the following disclaimer in
00230  *    the documentation and/or other materials provided with the
00231  *    distribution.
00232  * 
00233  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00234  *    and "Vovida Open Communication Application Library (VOCAL)" must
00235  *    not be used to endorse or promote products derived from this
00236  *    software without prior written permission. For written
00237  *    permission, please contact vocal@vovida.org.
00238  *
00239  * 4. Products derived from this software may not be called "VOCAL", nor
00240  *    may "VOCAL" appear in their name, without prior written
00241  *    permission of Vovida Networks, Inc.
00242  * 
00243  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00244  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00245  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00246  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00247  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00248  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00249  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00250  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00251  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00252  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00253  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00254  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00255  * DAMAGE.
00256  * 
00257  * ====================================================================
00258  * 
00259  * This software consists of voluntary contributions made by Vovida
00260  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00261  * Inc.  For more information on Vovida Networks, Inc., please see
00262  * <http://www.vovida.org/>.
00263  *
00264  */