Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

judo.cpp

00001 //============================================================================
00002 // Project:       Jabber Universal Document Objects (Judo)
00003 // Filename:      judo.cpp
00004 // Description:   Utility routines
00005 // Created at:    Fri Jun 29 11:37:01 2001
00006 // Modified at:   Mon Jul 30 18:09:58 2001
00007 // 
00008 //   License:
00009 // 
00010 // The contents of this file are subject to the Jabber Open Source License
00011 // Version 1.0 (the "License").  You may not copy or use this file, in either
00012 // source code or executable form, except in compliance with the License.  You
00013 // may obtain a copy of the License at http://www.jabber.com/license/ or at
00014 // http://www.opensource.org/.  
00015 //
00016 // Software distributed under the License is distributed on an "AS IS" basis,
00017 // WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
00018 // for the specific language governing rights and limitations under the
00019 // License.
00020 //
00021 //   Copyrights
00022 //
00023 // Portions created by or assigned to Jabber.com, Inc. are 
00024 // Copyright (c) 1999-2001 Jabber.com, Inc.  All Rights Reserved.  
00025 // 
00026 // $Id: judo.cpp,v 1.2 2002/07/13 19:30:19 temas Exp $
00027 //============================================================================
00028 
00029 #include "judo.hpp"
00030 using namespace std;
00031 
00032 void judo::unescape(const char* src, unsigned int srcLen, string& dest, bool append)
00033 {
00034     unsigned int i, j;
00035     int len;
00036 
00037     // Setup string size
00038     if (append)
00039     {
00040         len = j = dest.length();
00041         dest.resize(len + srcLen);
00042     }
00043     else
00044     {
00045         j = 0;
00046         len = 0;
00047         dest.resize(srcLen);
00048     }
00049 
00050     // Walk the input text, unescaping as we go..
00051     for (i = 0; i < srcLen; i++)
00052     {
00053         // See if this is an escape character
00054         if (src[i] == '&')
00055         {
00056             if (strncmp(&src[i+1],"amp;",4)==0)
00057             {
00058                 dest[j] = '&';
00059                 i += 4;
00060             } else if (strncmp(&src[i+1],"quot;",5)==0) {
00061                 dest[j] = '\"';
00062                 i += 5;
00063             } else if (strncmp(&src[i+1],"apos;",5)==0) {
00064                 dest[j] = '\'';
00065                 i += 5;
00066             } else if (strncmp(&src[i+1],"lt;",3)==0) {
00067                 dest[j] = '<';
00068                 i += 3;
00069             } else if (strncmp(&src[i+1],"gt;",3)==0) {
00070                 dest[j] = '>';
00071                 i += 3;
00072             } else {
00073                 dest[j] = src[i];
00074             }
00075         }
00076         // Not an escape character, so just copy the 
00077         // exact character
00078         else
00079             dest[j] = src[i];
00080         j++;
00081         len++;
00082     }
00083     // Cleanup
00084     dest.resize(len);
00085 }
00086 
00087 string judo::escape(const string& src)
00088 {
00089     int i,j,oldlen,newlen;
00090 
00091     if (src.empty())
00092         return string(src);
00093 
00094     oldlen = newlen = src.length();
00095     for(i = 0; i < oldlen; i++)
00096     {
00097         switch(src[i])
00098         {
00099         case '<':
00100         case '>':
00101             newlen+=4; break;
00102         case '&' : 
00103             newlen+=5; break;
00104         case '\'': 
00105         case '\"': 
00106             newlen+=6; break;
00107         }
00108     }
00109 
00110     if(oldlen == newlen) 
00111         return string(src);
00112         
00113     string result;
00114     result.reserve(newlen);
00115         
00116     for(i = j = 0; i < oldlen; i++)
00117     {
00118         switch(src[i])
00119         {
00120         case '&':
00121             result += "&amp;"; break;
00122         case '\'':
00123             result += "&apos;"; break;
00124         case '\"':
00125             result += "&quot;"; break;
00126         case '<':
00127             result += "&lt;"; break;
00128         case '>':
00129             result += "&gt;"; break;
00130         default:
00131             result += src[i];
00132         }
00133     }
00134     return result;
00135 }

Generated on Thu Jul 24 13:31:51 2003 for jabberoo by doxygen1.3-rc3