NCBI C++ ToolKit
writer_htmlenc.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: writer_htmlenc.cpp 33815 2007-05-04 17:18:18Z kazimird $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Aaron Ucko
27 *
28 * File Description:
29 * CWriter_HTMLEncoder -- HTML-encode supplied data on the fly before
30 * passing it to a standard ostream.
31 *
32 * ===========================================================================
33 */
34 
35 #include <ncbi_pch.hpp>
36 #include <html/writer_htmlenc.hpp>
37 #include <html/htmlhelper.hpp>
38 
40 
41 
43 {
45  m_Stream << "&amp;";
46  }
47 }
48 
49 
50 ERW_Result CWriter_HTMLEncoder::Write(const void* buf, size_t count,
51  size_t* bytes_written)
52 {
53  // Using HTMLHelper::HTMLEncode might have been somewhat more
54  // efficient, but this approach handles corner cases more readily.
55 
56  const char* p = static_cast<const char*>(buf);
57 
58  if ((m_Flags & fTrailingAmpersand) && count) {
59  if (p[0] == '#') {
60  m_Stream << '&';
61  } else {
62  m_Stream << "&amp;";
63  }
65  }
66 
67  size_t n;
68  for (n = 0; n < count && m_Stream; ++n) {
69  switch (p[n]) {
70  case '&':
71  if ((m_Flags & fPassNumericEntities) && n == count - 1) {
73  } else if ((m_Flags & fPassNumericEntities) && p[n + 1] == '#') {
74  m_Stream << '&';
75  } else {
76  m_Stream << "&amp;";
77  }
78  break;
79 
80  case '"': m_Stream << "&quot;"; break;
81  case '<': m_Stream << "&lt;"; break;
82  case '>': m_Stream << "&gt;"; break;
83  default: m_Stream << p[n]; break;
84  }
85  }
86  if (bytes_written) {
87  *bytes_written = n;
88  }
89 
90  return m_Stream.eof() ? eRW_Eof : m_Stream.bad() ? eRW_Error : eRW_Success;
91 }
92 
93 
95 {
96  // Ignores m_TrailingAmp, because flushing may not come at a semantic
97  // boundary.
98  m_Stream.flush();
99  return m_Stream.eof() ? eRW_Eof : m_Stream.bad() ? eRW_Error : eRW_Success;
100 }
101 
102 
CNcbiOstream & m_Stream
ERW_Result Write(const void *buf, size_t count, size_t *bytes_written=0)
Write up to "count" bytes from the buffer pointed to by the "buf" argument onto the output device.
ERW_Result Flush(void)
Flush pending data (if any) down to the output device.
@ fPassNumericEntities
Like CHTMLHelper::HTMLEncode, pass numeric entity specifications of the form &#......
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ERW_Result
Result codes for I/O operations.
@ eRW_Eof
End of data, should be considered permanent.
@ eRW_Error
Unrecoverable error, no retry possible.
@ eRW_Success
Everything is okay, I/O completed.
HTML library helper classes and functions.
char * buf
yy_size_t n
CWriter_HTMLEncoder – HTML-encode supplied data on the fly before passing it to a standard ostream.
Modified on Tue Jul 16 13:24:34 2024 by modify_doxy.py rev. 669887