Wexus2 0.20
|
00001 00002 /* 00003 * Copyright (c) 2011 Aleksander B. Demko 00004 * This source code is distributed under the MIT license. 00005 * See the accompanying file LICENSE.MIT.txt for details. 00006 */ 00007 00008 #ifndef __INCLUDED_WEXUS_HTMLSTRING_H__ 00009 #define __INCLUDED_WEXUS_HTMLSTRING_H__ 00010 00011 #include <QString> 00012 #include <QIODevice> 00013 #include <QTextStream> 00014 00015 namespace wexus 00016 { 00017 class HTMLString; 00018 class HTMLEncoderDevice; 00019 } 00020 00021 /** 00022 * This is a QString that is safe for emiting directly to the 00023 * user (that is, it has been properly escaped) 00024 * 00025 * @author Aleksander Demko 00026 */ 00027 class wexus::HTMLString : public QString 00028 { 00029 public: 00030 // empty constructor 00031 HTMLString(void); 00032 /** 00033 * Creates an HTMLString from the given s. 00034 * This basically marks s as already encoded. 00035 * 00036 * @author Aleksander Demko 00037 */ 00038 explicit HTMLString(const QString &s); 00039 00040 /** 00041 * HTML encode the given string and return 00042 * the encoded string. 00043 * 00044 * @author Aleksander Demko 00045 */ 00046 static HTMLString encode(const QString &s); 00047 00048 /// assignment operator 00049 HTMLString & operator = (const HTMLString &rhs) { *this = rhs; return *this; } 00050 /// assignment operator 00051 HTMLString & operator = (const QString &rhs) { QString::operator = (rhs); return *this; } 00052 }; 00053 00054 /** 00055 * Encoded any bytes to html before sending them to the 00056 * linked device. 00057 * 00058 * This is an output (write) device only. 00059 * 00060 * @author Aleksander Demko 00061 */ 00062 class wexus::HTMLEncoderDevice : public QIODevice 00063 { 00064 public: 00065 /// constructor 00066 HTMLEncoderDevice(QIODevice *outputdev); 00067 00068 void setEnabled(bool e) { dm_enabled = e; } // this is used in the operator<< HTMLString 00069 00070 protected: 00071 // just fails 00072 virtual qint64 readData(char * data, qint64 maxSize); 00073 /// implemented here 00074 virtual qint64 writeData(const char * data, qint64 maxSize); 00075 00076 private: 00077 bool dm_enabled; 00078 00079 QIODevice *dm_dev; 00080 // a temporary buffer used to prep data before sending it 00081 // to the linked QIODevice 00082 QByteArray dm_buf; 00083 }; 00084 00085 QTextStream & operator << (QTextStream &o, const wexus::HTMLString &htmlString); 00086 00087 #endif 00088