Changeset 606 for trunk


Ignore:
Timestamp:
Apr 20, 2008, 5:46:56 AM (13 years ago)
Author:
Peter Johansson
Message:

Fixes ticket:318. Changed HtmlStream? to be inherited from ostream and created a HtmlBuf? (inherited from streambuf) that does the real work.

Location:
trunk
Files:
5 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r605 r606  
    1313# Copyright (C) 2006 Jari Häkkinen
    1414# Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    15 # Copyright (C) 2008 Peter Johansson
    1615#
    1716# This file is part of svndigest, http://trac.thep.lu.se/svndigest
     
    154153   wc_found="yes";
    155154fi
    156 echo $wc_found
    157155AC_CHECK_FILE("$srcdir/lib/subversion_info.cc",info_found="yes",info_found="no")
    158156
  • trunk/lib/HtmlBuf.cc

    r604 r606  
    33/*
    44  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2008 Peter Johansson
    56
    67  This file is part of svndigest, http://trac.thep.lu.se/svndigest
     
    2223*/
    2324
    24 #include "HtmlStream.h"
     25#include "HtmlBuf.h"
    2526
     27#include <cstdio>
     28#include <iostream>
    2629#include <string>
    2730
     
    2932namespace svndigest{
    3033
    31   HtmlStream::HtmlStream(std::ostream& os)
    32     : os_(os)
     34  HtmlBuf::HtmlBuf(std::streambuf& buf)
     35    : std::streambuf(), buf_(buf)
    3336  {
    3437    map_['"']=std::string("\"");
     
    5356  }
    5457
    55 
    56   HtmlStream::~HtmlStream(void)
     58  HtmlBuf::int_type HtmlBuf::overflow (HtmlBuf::int_type c)
    5759  {
    58   }
    59 
    60 
    61   void HtmlStream::print(std::stringstream& ss)
    62   {
    63     char c;
    64     while (true){
    65       ss.get(c);
    66       if (!ss.good())
    67         return;
    68       std::map<char, std::string>::const_iterator i = map_.find(c);
    69       if (i==map_.end())
    70         os_ << c;
    71       else
    72         os_ << i->second;
    73     }
    74   }
    75 
    76  
    77   std::ostream& HtmlStream::stream(void)
    78   {
    79     return os_;
     60    std::map<char, std::string>::const_iterator i = map_.find(c);
     61    if (i==map_.end())
     62      return buf_.sputc(c);
     63    // writing
     64    if (buf_.sputn(i->second.c_str(), i->second.size()) ==
     65        static_cast<std::streamsize>(i->second.size()))
     66      return c;
     67    return EOF;
    8068  }
    8169
  • trunk/lib/HtmlBuf.h

    r604 r606  
    1 #ifndef _theplu_svndigest_html_stream_
    2 #define _theplu_svndigest_html_stream_
     1#ifndef _theplu_svndigest_html_buf_
     2#define _theplu_svndigest_html_buf_
    33
    44// $Id$
    55
    66/*
    7   Copyright (C) 2007 Peter Johansson
     7  Copyright (C) 2007, 2008 Peter Johansson
    88
    99  This file is part of svndigest, http://trac.thep.lu.se/svndigest
     
    2525*/
    2626
     27#include <cstdio>
     28#include <locale>
    2729#include <map>
    28 #include <fstream>
    29 #include <sstream>
     30#include <streambuf>
     31#include <string>
    3032
    3133namespace theplu{
    3234namespace svndigest{
    33 
    3435  ///
    35   /// Filtering text to html code.
     36  /// Buffer that translates certain chars to the corresponding string
     37  /// representing the character in HTML. The class is typically not
     38  /// used directly but through the convenience class HtmlStream.
    3639  ///
    37   class HtmlStream
     40  class HtmlBuf : public std::streambuf
    3841  {
    3942  public:
     43    /// \param buf Buffer output is sent to.
     44    HtmlBuf(std::streambuf& buf);
    4045
    41     ///
    42     /// @brief Constructor
    43     ///
    44     HtmlStream(std::ostream&);
    45 
    46     ///
    47     /// @brief Destructor
    48     ///
    49     ~HtmlStream(void);
    50 
    51     ///
    52     /// \brief filter and print stringstream
    53     ///
    54     void print(std::stringstream&);
    55 
    56     ///
    57     /// \return reference to underlying ostream
    58     ///
    59     std::ostream& stream(void);
     46    typedef std::char_traits<char>::int_type int_type;
    6047
    6148  private:
    62     ///
    63     /// @brief Copy Constructor, not implemented
    64     ///
    65     HtmlStream(const HtmlStream&);
     49    // dictionary how to transform chars
     50    std::map<char, std::string> map_;
     51    std::streambuf& buf_;
    6652
    67     std::map<char, std::string> map_;
    68     std::ostream& os_;
     53    // called from base class
     54    int_type overflow (int_type c);
    6955  };
    70 
    71 
    72   template <typename T>
    73   HtmlStream& operator<<(HtmlStream& html, const T& rhs)
    74   {
    75     std::stringstream ss;
    76     ss << rhs;
    77     html.print(ss);
    78     return html;
    79   }
    8056
    8157}} // end of namespace svndigest and namespace theplu
  • trunk/lib/HtmlStream.cc

    r519 r606  
    2323
    2424#include "HtmlStream.h"
     25#include "HtmlBuf.h"
    2526
    2627#include <string>
     
    3031
    3132  HtmlStream::HtmlStream(std::ostream& os)
    32     : os_(os)
     33    : std::ostream(), hbuf_(*os.rdbuf()), os_(os)
    3334  {
    34     map_['"']=std::string("\"");
    35     map_['\'']=std::string("\'");
    36     map_['\n']=std::string("<br />");
    37     map_['<']=std::string("&lt;");
    38     map_['>']=std::string("&gt;");
    39     map_['&']=std::string("&amp;");
    40     map_[' ']=std::string("&nbsp;");
    41     // This should be configurable, but for now indentation is two spaces.
    42     map_['\t']=std::string("&nbsp;&nbsp;");
    43     map_['å']=std::string("&aring;");
    44     map_['ä']=std::string("&auml;");
    45     map_['ö']=std::string("&ouml;");
    46     map_['Å']=std::string("&Aring;");
    47     map_['Ä']=std::string("&Auml;");
    48     map_['Ö']=std::string("&Ouml;");
    49     map_['é']=std::string("&eacute;");
    50     map_['É']=std::string("&Eacute;");
    51     map_['á']=std::string("&aacute;");
    52     map_['Á']=std::string("&Aacute;");
     35    // setting member variable in base class
     36    rdbuf(&hbuf_);
    5337  }
    5438
     
    5943
    6044
    61   void HtmlStream::print(std::stringstream& ss)
    62   {
    63     char c;
    64     while (true){
    65       ss.get(c);
    66       if (!ss.good())
    67         return;
    68       std::map<char, std::string>::const_iterator i = map_.find(c);
    69       if (i==map_.end())
    70         os_ << c;
    71       else
    72         os_ << i->second;
    73     }
    74   }
    75 
    76  
    7745  std::ostream& HtmlStream::stream(void)
    7846  {
  • trunk/lib/HtmlStream.h

    r519 r606  
    2525*/
    2626
     27#include "HtmlBuf.h"
     28
    2729#include <map>
    2830#include <fstream>
     
    3133namespace theplu{
    3234namespace svndigest{
    33 
    3435  ///
    35   /// Filtering text to html code.
     36  /// \brief Convenient class for using HtmlBuf.
    3637  ///
    37   class HtmlStream
     38  /// Class is created from another ostream, whose buffer is used to
     39  /// create an internal HtmlBuf. In other words, when this class is
     40  /// used, typical with operator<<, data is sent to the HtmlBuffer,
     41  /// which transforms it as appropriate and sends it further to its
     42  /// target buffer. The target buffer is the buffer of the ostream
     43  /// this class is created of.
     44  ///
     45  class HtmlStream : public std::ostream
    3846  {
    3947  public:
    40 
    4148    ///
    42     /// @brief Constructor
     49    /// Creates a HtmlBuf from target.rdbuf().
    4350    ///
    44     HtmlStream(std::ostream&);
     51    HtmlStream(std::ostream& target);
    4552
    4653    ///
     
    4855    ///
    4956    ~HtmlStream(void);
    50 
    51     ///
    52     /// \brief filter and print stringstream
    53     ///
    54     void print(std::stringstream&);
    5557
    5658    ///
     
    6567    HtmlStream(const HtmlStream&);
    6668
    67     std::map<char, std::string> map_;
     69    HtmlBuf hbuf_;
    6870    std::ostream& os_;
    6971  };
    70 
    71 
    72   template <typename T>
    73   HtmlStream& operator<<(HtmlStream& html, const T& rhs)
    74   {
    75     std::stringstream ss;
    76     ss << rhs;
    77     html.print(ss);
    78     return html;
    79   }
    8072
    8173}} // end of namespace svndigest and namespace theplu
  • trunk/lib/Makefile.am

    r570 r606  
    2929  Commitment.h Configuration.h css.h\
    3030  Date.h Directory.h File.h first_page.h Functor.h Gnuplot.h GnuplotFE.h \
    31   HtmlStream.h html_utility.h LineTypeParser.h \
     31  HtmlBuf.h HtmlStream.h html_utility.h LineTypeParser.h \
    3232  LogIterator.h Node.h rmdirhier.h \
    3333  Stats.h StatsCollection.h subversion_info.h SVN.h SVNblame.h  \
     
    3838  Commitment.cc Configuration.cc \
    3939  css.cc Date.cc Directory.cc File.cc first_page.cc\
    40   Functor.cc Gnuplot.cc GnuplotFE.cc  HtmlStream.cc \
     40  Functor.cc Gnuplot.cc GnuplotFE.cc  HtmlBuf.cc HtmlStream.cc \
    4141  html_utility.cc LineTypeParser.cc LogIterator.cc Node.cc \
    4242  rmdirhier.cc Stats.cc StatsCollection.cc subversion_info.cc SVN.cc        \
  • trunk/test/Makefile.am

    r598 r606  
    2323# 02111-1307, USA.
    2424
    25 check_PROGRAMS = config_test date gnuplot_pipe parser trac utility_test
     25check_PROGRAMS = config_test date gnuplot_pipe html_buf_test \
     26  parser trac utility_test
    2627
    2728# these tests are only for developers since we do not distribute test repository
     
    5556date_SOURCES = date.cc
    5657gnuplot_pipe_SOURCES = gnuplot_pipe.cc
     58html_buf_test_SOURCES = html_buf_test.cc
    5759parser_SOURCES = parser.cc
    5860stats_test_SOURCES = stats_test.cc
Note: See TracChangeset for help on using the changeset viewer.