From d8260d83838355c1e3a45f84daedac4dfa3c5953 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 11 Sep 2012 05:19:45 +0000 Subject: [PATCH] added: a new way: HEX format to saving/reading from PostgreSQL bytea columns added: to Request: // binary page BinaryPage binary_page; // a compressed page ready to send to the client BinaryPage compressed_page; // if true then either page or ajaxpage will be sent to the client // if false then binary_page is sent // default: true bool use_text_page; BinaryPage is defined as (in requesttypes.h): typedef PT::TextStreamBase BinaryPage; added: to Compress: now it can gets BinaryPage as arguments (input, output) changed: winix version to: 0.5.0 added: in templates: TexTextStream class for taking input to the TeX typesetting system git-svn-id: svn://ttmath.org/publicrep/winix/trunk@884 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/Makefile.dep | 179 +++++++------- core/app.cpp | 84 ++++++- core/app.h | 4 + core/compress.cpp | 127 +++++++++- core/compress.h | 16 +- core/request.cpp | 4 + core/request.h | 11 + core/requesttypes.h | 3 + core/run.cpp | 1 + core/version.h | 6 +- db/dbbase.cpp | 50 +++- db/dbbase.h | 8 +- db/dbtextstream.cpp | 26 +- db/dbtextstream.h | 2 + functions/Makefile.dep | 5 +- plugins/group/Makefile.dep | 4 +- plugins/menu/Makefile.dep | 3 +- plugins/stats/Makefile.dep | 10 +- plugins/thread/Makefile.dep | 2 +- templates/Makefile.dep | 14 ++ templates/Makefile.o.dep | 2 +- templates/textextstream.cpp | 456 ++++++++++++++++++++++++++++++++++++ templates/textextstream.h | 163 +++++++++++++ 23 files changed, 1051 insertions(+), 129 deletions(-) create mode 100755 templates/textextstream.cpp create mode 100755 templates/textextstream.h diff --git a/core/Makefile.dep b/core/Makefile.dep index ee49ce6..9249468 100755 --- a/core/Makefile.dep +++ b/core/Makefile.dep @@ -51,14 +51,13 @@ app.o: plugin.h pluginmsg.h cookieparser.h postmultiparser.h app.o: acceptencodingparser.h acceptbaseparser.h basethread.o: basethread.h synchro.h bbcodeparser.o: bbcodeparser.h htmlfilter.h -compress.o: compress.h log.h textstream.h logmanipulators.h -compress.o: ../../pikotools/textstream/textstream.h +compress.o: compress.h requesttypes.h ../../pikotools/textstream/textstream.h compress.o: ../../pikotools/space/space.h ../../pikotools/date/date.h compress.o: ../../pikotools/convert/convert.h compress.o: ../../pikotools/convert/inttostr.h compress.o: ../../pikotools/membuffer/membuffer.h -compress.o: ../../pikotools/textstream/types.h slog.h cur.h request.h -compress.o: requesttypes.h item.h error.h config.h +compress.o: ../../pikotools/textstream/types.h log.h textstream.h +compress.o: logmanipulators.h slog.h cur.h request.h item.h error.h config.h compress.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h compress.o: ../../pikotools/textstream/types.h htmlfilter.h compress.o: ../templates/htmltextstream.h ../core/textstream.h misc.h @@ -527,18 +526,17 @@ plugindata.o: ../templates/templates.h ../templates/patterncacher.h plugindata.o: ../templates/indexpatterns.h ../templates/patterns.h plugindata.o: ../templates/changepatterns.h ../templates/htmltextstream.h plugindata.o: ../core/sessionmanager.h -postmultiparser.o: postmultiparser.h error.h requesttypes.h config.h -postmultiparser.o: ../../pikotools/space/spaceparser.h -postmultiparser.o: ../../pikotools/space/space.h -postmultiparser.o: ../../pikotools/textstream/types.h htmlfilter.h misc.h -postmultiparser.o: item.h ../../pikotools/space/space.h -postmultiparser.o: ../../pikotools/date/date.h +postmultiparser.o: postmultiparser.h error.h requesttypes.h postmultiparser.o: ../../pikotools/textstream/textstream.h +postmultiparser.o: ../../pikotools/space/space.h ../../pikotools/date/date.h postmultiparser.o: ../../pikotools/convert/convert.h postmultiparser.o: ../../pikotools/convert/inttostr.h postmultiparser.o: ../../pikotools/membuffer/membuffer.h -postmultiparser.o: ../../pikotools/textstream/types.h -postmultiparser.o: ../../pikotools/utf8/utf8.h log.h textstream.h +postmultiparser.o: ../../pikotools/textstream/types.h config.h +postmultiparser.o: ../../pikotools/space/spaceparser.h +postmultiparser.o: ../../pikotools/space/space.h +postmultiparser.o: ../../pikotools/textstream/types.h htmlfilter.h misc.h +postmultiparser.o: item.h ../../pikotools/utf8/utf8.h log.h textstream.h postmultiparser.o: logmanipulators.h slog.h cur.h request.h postmultiparser.o: ../templates/htmltextstream.h ../core/textstream.h postmultiparser.o: ../../pikotools/space/spacetojson.h session.h user.h @@ -555,46 +553,46 @@ rebus.o: ../../pikotools/textstream/types.h htmlfilter.h rebus.o: ../templates/htmltextstream.h ../core/textstream.h misc.h rebus.o: ../../pikotools/utf8/utf8.h ../../pikotools/space/spacetojson.h rebus.o: session.h user.h plugindata.h rebus.h mount.h ../templates/locale.h -request.o: request.h requesttypes.h item.h ../../pikotools/space/space.h -request.o: ../../pikotools/date/date.h error.h config.h -request.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h -request.o: ../../pikotools/textstream/types.h htmlfilter.h textstream.h -request.o: ../templates/htmltextstream.h ../core/textstream.h misc.h -request.o: ../../pikotools/textstream/textstream.h +request.o: request.h requesttypes.h ../../pikotools/textstream/textstream.h +request.o: ../../pikotools/space/space.h ../../pikotools/date/date.h request.o: ../../pikotools/convert/convert.h request.o: ../../pikotools/convert/inttostr.h request.o: ../../pikotools/membuffer/membuffer.h -request.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h -request.o: ../../pikotools/space/spacetojson.h log.h logmanipulators.h slog.h -request.o: cur.h session.h user.h plugindata.h rebus.h mount.h -request.o: ../templates/locale.h plugin.h pluginmsg.h system.h job.h -request.o: basethread.h synchro.h dirs.h dircontainer.h ../db/db.h -request.o: ../db/dbbase.h ../db/dbconn.h ../db/dbtextstream.h ../core/error.h -request.o: ../db/dbitemquery.h ../core/item.h ../db/dbitemcolumns.h -request.o: ../core/user.h ../core/group.h ../core/dircontainer.h -request.o: ../core/ugcontainer.h ../notify/notify.h ../notify/notifypool.h -request.o: ../templates/patterns.h ../templates/locale.h -request.o: ../templates/localefilter.h ../../ezc/src/ezc.h -request.o: ../../ezc/src/generator.h ../../ezc/src/pattern.h -request.o: ../../ezc/src/functions.h ../../ezc/src/funinfo.h -request.o: ../../ezc/src/stringconv.h ../notify/notifythread.h -request.o: ../core/basethread.h ../notify/templatesnotify.h ../core/config.h -request.o: ../core/users.h ugcontainer.h lastcontainer.h mounts.h -request.o: mountparser.h crypt.h run.h users.h groups.h group.h loadavg.h -request.o: image.h threadmanager.h timezones.h timezone.h sessionmanager.h -request.o: sessioncontainer.h ../functions/functions.h -request.o: ../functions/functionbase.h ../core/request.h ../core/system.h -request.o: ../core/synchro.h ../functions/functionparser.h ../core/cur.h -request.o: ../functions/adduser.h ../functions/cat.h ../functions/chmod.h -request.o: ../functions/privchanger.h ../functions/chown.h -request.o: ../functions/ckeditor.h ../functions/cp.h ../functions/default.h -request.o: ../functions/download.h ../functions/emacs.h ../functions/env.h -request.o: ../functions/last.h ../functions/login.h ../functions/logout.h -request.o: ../functions/ln.h ../functions/ls.h ../functions/man.h -request.o: ../functions/meta.h ../functions/mkdir.h ../functions/mv.h -request.o: ../functions/nicedit.h ../functions/node.h ../functions/passwd.h -request.o: ../functions/priv.h ../functions/pw.h ../functions/reload.h -request.o: ../functions/rm.h ../functions/rmuser.h ../functions/sort.h +request.o: ../../pikotools/textstream/types.h item.h error.h config.h +request.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h +request.o: ../../pikotools/textstream/types.h htmlfilter.h textstream.h +request.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +request.o: ../../pikotools/utf8/utf8.h ../../pikotools/space/spacetojson.h +request.o: log.h logmanipulators.h slog.h cur.h session.h user.h plugindata.h +request.o: rebus.h mount.h ../templates/locale.h plugin.h pluginmsg.h +request.o: system.h job.h basethread.h synchro.h dirs.h dircontainer.h +request.o: ../db/db.h ../db/dbbase.h ../db/dbconn.h ../db/dbtextstream.h +request.o: ../core/error.h ../db/dbitemquery.h ../core/item.h +request.o: ../db/dbitemcolumns.h ../core/user.h ../core/group.h +request.o: ../core/dircontainer.h ../core/ugcontainer.h ../notify/notify.h +request.o: ../notify/notifypool.h ../templates/patterns.h +request.o: ../templates/locale.h ../templates/localefilter.h +request.o: ../../ezc/src/ezc.h ../../ezc/src/generator.h +request.o: ../../ezc/src/pattern.h ../../ezc/src/functions.h +request.o: ../../ezc/src/funinfo.h ../../ezc/src/stringconv.h +request.o: ../notify/notifythread.h ../core/basethread.h +request.o: ../notify/templatesnotify.h ../core/config.h ../core/users.h +request.o: ugcontainer.h lastcontainer.h mounts.h mountparser.h crypt.h run.h +request.o: users.h groups.h group.h loadavg.h image.h threadmanager.h +request.o: timezones.h timezone.h sessionmanager.h sessioncontainer.h +request.o: ../functions/functions.h ../functions/functionbase.h +request.o: ../core/request.h ../core/system.h ../core/synchro.h +request.o: ../functions/functionparser.h ../core/cur.h ../functions/adduser.h +request.o: ../functions/cat.h ../functions/chmod.h ../functions/privchanger.h +request.o: ../functions/chown.h ../functions/ckeditor.h ../functions/cp.h +request.o: ../functions/default.h ../functions/download.h +request.o: ../functions/emacs.h ../functions/env.h ../functions/last.h +request.o: ../functions/login.h ../functions/logout.h ../functions/ln.h +request.o: ../functions/ls.h ../functions/man.h ../functions/meta.h +request.o: ../functions/mkdir.h ../functions/mv.h ../functions/nicedit.h +request.o: ../functions/node.h ../functions/passwd.h ../functions/priv.h +request.o: ../functions/pw.h ../functions/reload.h ../functions/rm.h +request.o: ../functions/rmuser.h ../functions/sort.h request.o: ../functions/specialdefault.h ../functions/stat.h request.o: ../functions/subject.h ../functions/template.h request.o: ../functions/tinymce.h ../functions/uname.h ../functions/upload.h @@ -626,32 +624,32 @@ session.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h sessioncontainer.o: sessioncontainer.h session.h item.h sessioncontainer.o: ../../pikotools/space/space.h ../../pikotools/date/date.h sessioncontainer.o: error.h user.h plugindata.h rebus.h textstream.h cur.h -sessioncontainer.o: request.h requesttypes.h config.h -sessioncontainer.o: ../../pikotools/space/spaceparser.h -sessioncontainer.o: ../../pikotools/space/space.h -sessioncontainer.o: ../../pikotools/textstream/types.h htmlfilter.h -sessioncontainer.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +sessioncontainer.o: request.h requesttypes.h sessioncontainer.o: ../../pikotools/textstream/textstream.h sessioncontainer.o: ../../pikotools/convert/convert.h sessioncontainer.o: ../../pikotools/convert/inttostr.h sessioncontainer.o: ../../pikotools/membuffer/membuffer.h -sessioncontainer.o: ../../pikotools/textstream/types.h +sessioncontainer.o: ../../pikotools/textstream/types.h config.h +sessioncontainer.o: ../../pikotools/space/spaceparser.h +sessioncontainer.o: ../../pikotools/space/space.h +sessioncontainer.o: ../../pikotools/textstream/types.h htmlfilter.h +sessioncontainer.o: ../templates/htmltextstream.h ../core/textstream.h misc.h sessioncontainer.o: ../../pikotools/utf8/utf8.h sessioncontainer.o: ../../pikotools/space/spacetojson.h mount.h log.h sessioncontainer.o: logmanipulators.h slog.h ../templates/locale.h sessionmanager.o: sessionmanager.h sessioncontainer.h session.h item.h sessionmanager.o: ../../pikotools/space/space.h ../../pikotools/date/date.h sessionmanager.o: error.h user.h plugindata.h rebus.h textstream.h cur.h -sessionmanager.o: request.h requesttypes.h config.h -sessionmanager.o: ../../pikotools/space/spaceparser.h -sessionmanager.o: ../../pikotools/space/space.h -sessionmanager.o: ../../pikotools/textstream/types.h htmlfilter.h -sessionmanager.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +sessionmanager.o: request.h requesttypes.h sessionmanager.o: ../../pikotools/textstream/textstream.h sessionmanager.o: ../../pikotools/convert/convert.h sessionmanager.o: ../../pikotools/convert/inttostr.h sessionmanager.o: ../../pikotools/membuffer/membuffer.h -sessionmanager.o: ../../pikotools/textstream/types.h +sessionmanager.o: ../../pikotools/textstream/types.h config.h +sessionmanager.o: ../../pikotools/space/spaceparser.h +sessionmanager.o: ../../pikotools/space/space.h +sessionmanager.o: ../../pikotools/textstream/types.h htmlfilter.h +sessionmanager.o: ../templates/htmltextstream.h ../core/textstream.h misc.h sessionmanager.o: ../../pikotools/utf8/utf8.h sessionmanager.o: ../../pikotools/space/spacetojson.h mount.h lastcontainer.h sessionmanager.o: system.h job.h basethread.h synchro.h dirs.h dircontainer.h @@ -699,30 +697,31 @@ sessionmanager.o: ../core/sessionmanager.h sessionparser.o: sessionparser.h session.h item.h sessionparser.o: ../../pikotools/space/space.h ../../pikotools/date/date.h sessionparser.o: error.h user.h plugindata.h rebus.h textstream.h -sessionparser.o: sessioncontainer.h cur.h request.h requesttypes.h config.h -sessionparser.o: ../../pikotools/space/spaceparser.h -sessionparser.o: ../../pikotools/space/space.h -sessionparser.o: ../../pikotools/textstream/types.h htmlfilter.h -sessionparser.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +sessionparser.o: sessioncontainer.h cur.h request.h requesttypes.h sessionparser.o: ../../pikotools/textstream/textstream.h sessionparser.o: ../../pikotools/convert/convert.h sessionparser.o: ../../pikotools/convert/inttostr.h sessionparser.o: ../../pikotools/membuffer/membuffer.h -sessionparser.o: ../../pikotools/textstream/types.h +sessionparser.o: ../../pikotools/textstream/types.h config.h +sessionparser.o: ../../pikotools/space/spaceparser.h +sessionparser.o: ../../pikotools/space/space.h +sessionparser.o: ../../pikotools/textstream/types.h htmlfilter.h +sessionparser.o: ../templates/htmltextstream.h ../core/textstream.h misc.h sessionparser.o: ../../pikotools/utf8/utf8.h sessionparser.o: ../../pikotools/space/spacetojson.h mount.h users.h log.h sessionparser.o: logmanipulators.h slog.h ../templates/locale.h -slog.o: slog.h cur.h request.h requesttypes.h item.h -slog.o: ../../pikotools/space/space.h ../../pikotools/date/date.h error.h -slog.o: config.h ../../pikotools/space/spaceparser.h -slog.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h -slog.o: htmlfilter.h textstream.h ../templates/htmltextstream.h -slog.o: ../core/textstream.h misc.h ../../pikotools/textstream/textstream.h -slog.o: ../../pikotools/convert/convert.h ../../pikotools/convert/inttostr.h +slog.o: slog.h cur.h request.h requesttypes.h +slog.o: ../../pikotools/textstream/textstream.h ../../pikotools/space/space.h +slog.o: ../../pikotools/date/date.h ../../pikotools/convert/convert.h +slog.o: ../../pikotools/convert/inttostr.h slog.o: ../../pikotools/membuffer/membuffer.h -slog.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h -slog.o: ../../pikotools/space/spacetojson.h session.h user.h plugindata.h -slog.o: rebus.h mount.h logmanipulators.h ../templates/locale.h +slog.o: ../../pikotools/textstream/types.h item.h error.h config.h +slog.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h +slog.o: ../../pikotools/textstream/types.h htmlfilter.h textstream.h +slog.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +slog.o: ../../pikotools/utf8/utf8.h ../../pikotools/space/spacetojson.h +slog.o: session.h user.h plugindata.h rebus.h mount.h logmanipulators.h +slog.o: ../templates/locale.h synchro.o: synchro.h system.o: system.h job.h basethread.h synchro.h ../../pikotools/space/space.h system.o: dirs.h item.h ../../pikotools/date/date.h dircontainer.h ../db/db.h @@ -813,21 +812,21 @@ user.o: user.h users.o: users.h sessionmanager.h sessioncontainer.h session.h item.h users.o: ../../pikotools/space/space.h ../../pikotools/date/date.h error.h users.o: user.h plugindata.h rebus.h textstream.h cur.h request.h -users.o: requesttypes.h config.h ../../pikotools/space/spaceparser.h -users.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h -users.o: htmlfilter.h ../templates/htmltextstream.h ../core/textstream.h -users.o: misc.h ../../pikotools/textstream/textstream.h +users.o: requesttypes.h ../../pikotools/textstream/textstream.h users.o: ../../pikotools/convert/convert.h ../../pikotools/convert/inttostr.h users.o: ../../pikotools/membuffer/membuffer.h -users.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h -users.o: ../../pikotools/space/spacetojson.h mount.h lastcontainer.h system.h -users.o: job.h basethread.h synchro.h dirs.h dircontainer.h ../db/db.h -users.o: ../db/dbbase.h ../db/dbconn.h ../db/dbtextstream.h ../core/error.h -users.o: ../db/dbitemquery.h ../core/item.h ../db/dbitemcolumns.h -users.o: ../core/user.h ../core/group.h ../core/dircontainer.h -users.o: ../core/ugcontainer.h log.h logmanipulators.h slog.h -users.o: ../templates/locale.h ../notify/notify.h ../notify/notifypool.h -users.o: ../templates/patterns.h ../templates/locale.h +users.o: ../../pikotools/textstream/types.h config.h +users.o: ../../pikotools/space/spaceparser.h ../../pikotools/space/space.h +users.o: ../../pikotools/textstream/types.h htmlfilter.h +users.o: ../templates/htmltextstream.h ../core/textstream.h misc.h +users.o: ../../pikotools/utf8/utf8.h ../../pikotools/space/spacetojson.h +users.o: mount.h lastcontainer.h system.h job.h basethread.h synchro.h dirs.h +users.o: dircontainer.h ../db/db.h ../db/dbbase.h ../db/dbconn.h +users.o: ../db/dbtextstream.h ../core/error.h ../db/dbitemquery.h +users.o: ../core/item.h ../db/dbitemcolumns.h ../core/user.h ../core/group.h +users.o: ../core/dircontainer.h ../core/ugcontainer.h log.h logmanipulators.h +users.o: slog.h ../templates/locale.h ../notify/notify.h +users.o: ../notify/notifypool.h ../templates/patterns.h ../templates/locale.h users.o: ../templates/localefilter.h ../../ezc/src/ezc.h users.o: ../../ezc/src/generator.h ../../ezc/src/pattern.h users.o: ../../ezc/src/functions.h ../../ezc/src/funinfo.h diff --git a/core/app.cpp b/core/app.cpp index a864bcc..5e59ab7 100755 --- a/core/app.cpp +++ b/core/app.cpp @@ -977,6 +977,7 @@ void App::FilterCompressSend(bool compressing, int compress_encoding, const std: + int App::SelectDeflateVersion() { if( cur.request->browser_msie ) @@ -1070,7 +1071,7 @@ void App::AddDebugInfo(std::wstring & out) -void App::SendAnswer() +void App::SendTextAnswer() { const std::wstring * source; Header header = h_200; @@ -1083,7 +1084,6 @@ int compress_encoding; else source = &cur.request->page.Str(); - SelectCompression(source->length(), compressing, compress_encoding); if( status == WINIX_ERR_NO_ITEM || status == WINIX_ERR_NO_FUNCTION || status == WINIX_ERR_UNKNOWN_PARAM ) @@ -1102,6 +1102,86 @@ int compress_encoding; } +void App::SendData(const BinaryPage & page, FCGX_Stream * out) +{ + const size_t buf_size = 4096; + + if( send_data_buf.size() != buf_size ) + send_data_buf.resize(buf_size); + + BinaryPage::const_iterator i = page.begin(); + BinaryPage::const_iterator end = page.end(); + +// log << log1 << "size: " << page.size() << logend; + +// for(size_t x=0 ; x 0 ) + FCGX_PutStr(send_data_buf.c_str(), s, fcgi_request.out); + } +} + + +void App::SendBinaryAnswer() +{ +BinaryPage & source = cur.request->binary_page; +BinaryPage & compressed_page = cur.request->compressed_page; +Header header = h_200; +Error status = cur.request->status; +bool compressing; +int compress_encoding; + + + SelectCompression(source.size(), compressing, compress_encoding); + + if( status == WINIX_ERR_NO_ITEM || status == WINIX_ERR_NO_FUNCTION || status == WINIX_ERR_UNKNOWN_PARAM ) + header = h_404; + + if( status == WINIX_ERR_PERMISSION_DENIED || status == WINIX_ERR_CANT_CHANGE_USER || status == WINIX_ERR_CANT_CHANGE_GROUP ) + header = h_403; + + // !! IMPROVE ME add header: content-size + + SendHeaders(compressing, compress_encoding, header); + + if( CanSendContent(header) ) + { + if( compressing ) + { + compress.Compressing(source, compressed_page, compress_encoding); + SendData(compressed_page, fcgi_request.out); + } + else + { + SendData(source, fcgi_request.out); + } + } +} + + +void App::SendAnswer() +{ + if( cur.request->use_text_page ) + SendTextAnswer(); + else + SendBinaryAnswer(); +} + + void App::LogUser(const char * msg, uid_t id) { log << log3 << msg << " "; diff --git a/core/app.h b/core/app.h index 0611ede..0e6647b 100755 --- a/core/app.h +++ b/core/app.h @@ -121,6 +121,7 @@ private: std::string source_a; std::string sendh_t, sendh_t2, sendh_t3; std::string sendfilea, sendfile2a; + std::string send_data_buf; void ProcessRequestThrow(); void ProcessRequest(); @@ -135,8 +136,11 @@ private: void Make(); void SaveSessionsIfNeeded(); // !! wywalic do menagera sesji?? void LogAccess(); + void SendData(const BinaryPage & page, FCGX_Stream * out); void ReadRequest(); + void SendTextAnswer(); + void SendBinaryAnswer(); void SendAnswer(); void PrintEnv(); diff --git a/core/compress.cpp b/core/compress.cpp index 7a3d7d5..efd9681 100755 --- a/core/compress.cpp +++ b/core/compress.cpp @@ -2,7 +2,7 @@ * This file is a part of Winix * and is not publicly distributed * - * Copyright (c) 2008-2011, Tomasz Sowa + * Copyright (c) 2008-2012, Tomasz Sowa * All rights reserved. * */ @@ -15,7 +15,8 @@ Compress::Compress() { - buffer = 0; + buffer_in = 0; + buffer_out = 0; buffer_max_len = 65536; // 64KB ready_for_compress = false; compress_level = 6; @@ -27,8 +28,8 @@ Compress::Compress() Compress::~Compress() { - if( buffer ) - delete [] buffer; + delete [] buffer_in; + delete [] buffer_out; if( raw_deflate_inited ) deflateEnd(&strm_raw_deflate); @@ -43,17 +44,23 @@ Compress::~Compress() bool Compress::AllocateMemory() { - if( buffer ) - delete [] buffer; + if( buffer_in ) + delete [] buffer_in; + + if( buffer_out ) + delete [] buffer_out; + + buffer_in = 0; + buffer_out = 0; try { - buffer = new char[buffer_max_len]; + buffer_in = new char[buffer_max_len]; + buffer_out = new char[buffer_max_len]; } catch(const std::bad_alloc &) { log << log1 << "Compress: can't allocate memory" << logend; - buffer = 0; return false; } @@ -133,9 +140,8 @@ int Compress::Init(int compress_level_) { compress_level = compress_level_; - if( buffer == 0 ) - if( !AllocateMemory() ) - return 1; + if( !AllocateMemory() ) + return 1; if( InitRawDeflate() && InitDeflate() && InitGzip() ) ready_for_compress = true; @@ -163,7 +169,69 @@ size_t have; do { strm.avail_out = buffer_max_len; - strm.next_out = (Bytef*)buffer; + strm.next_out = (Bytef*)buffer_out; + ret = deflate(&strm, flush); + + if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR ) + { + log << log1 << "Compress: problem with deflate()" << logend; + return 2; + } + + have = buffer_max_len - strm.avail_out; + last_out_size += have; + FCGX_PutStr(buffer_out, have, out_stream); + } + while( strm.avail_out == 0 ); + + if( strm.avail_in != 0 ) + { + log << log1 << "Compress: problem with deflate() - not all input is used" << logend; + return 2; + } + } + while( flush != Z_FINISH ); + + + if( ret != Z_STREAM_END ) + { + log << log1 << "Compress: problem with deflate() - stream not complete" << logend; + return 2; + } + +return 0; +} + + +void Compress::CopyToInputBuffer(BinaryPage::const_iterator & i, size_t len) +{ + for(size_t a=0 ; a buffer_max_len) ? buffer_max_len : source_len; + source_len -= strm.avail_in; + flush = (source_len == 0) ? Z_FINISH : Z_NO_FLUSH; + strm.next_in = (Bytef*)buffer_in; + CopyToInputBuffer(i, strm.avail_in); + + do + { + strm.avail_out = buffer_max_len; + strm.next_out = (Bytef*)buffer_out; ret = deflate(&strm, flush); if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR ) @@ -174,7 +242,7 @@ size_t have; have = buffer_max_len - strm.avail_out; last_out_size += have; - FCGX_PutStr(buffer, have, out_stream); + out.write(buffer_out, have); } while( strm.avail_out == 0 ); @@ -289,3 +357,36 @@ return ret; +/* + return: + 0 - ok; + 1 - can't allocate memory + 2 - error during compressing + 3 - not inited (use Init() first) + 100 - unknown +*/ +int Compress::Compressing(const BinaryPage & in, BinaryPage & out, int encoding) +{ +int ret; +z_stream * pstrm; + + last_out_size = 0; + out.clear(); + + if( !ready_for_compress ) + { + log << log1 << "Compress: not ready yet" << logend; + return 3; + } + + if( in.empty() ) + return 0; + + pstrm = SelectStream(encoding); + ret = MakeCompress(*pstrm, in, out, encoding); + ResetStream(pstrm, encoding); + PutLog(in.size(), encoding); + +return ret; +} + diff --git a/core/compress.h b/core/compress.h index 318cb62..9b43c6a 100755 --- a/core/compress.h +++ b/core/compress.h @@ -2,7 +2,7 @@ * This file is a part of Winix * and is not publicly distributed * - * Copyright (c) 2008-2011, Tomasz Sowa + * Copyright (c) 2008-2012, Tomasz Sowa * All rights reserved. * */ @@ -13,6 +13,8 @@ #include #include #include +#include "requesttypes.h" + class Compress @@ -33,7 +35,8 @@ public: 2 - gzip */ int CompressAndPut(const char * source, size_t source_len, FCGX_Stream * out_stream, int encoding = 2); - size_t last_out_size; + int Compressing(const BinaryPage & in, BinaryPage & out, int encoding = 2); + private: @@ -43,13 +46,20 @@ private: bool InitGzip(); int MakeCompress(z_stream & strm, const char * source, size_t source_len, FCGX_Stream * out_stream, int encoding); + int MakeCompress(z_stream & strm, const BinaryPage & page, BinaryPage & out, int encoding); z_stream * SelectStream(int encoding); void ResetStream(z_stream * pstrm, int encoding); void PutLog(size_t source_len, int encoding); + void CopyToInputBuffer(BinaryPage::const_iterator & i, size_t len); int compress_level; size_t buffer_max_len; - char * buffer; + + // size of the last compressed page + size_t last_out_size; + + char * buffer_in; + char * buffer_out; z_stream strm_raw_deflate, strm_deflate, strm_gzip; bool raw_deflate_inited, deflate_inited, gzip_inited; bool ready_for_compress; diff --git a/core/request.cpp b/core/request.cpp index be5794a..ba4b244 100755 --- a/core/request.cpp +++ b/core/request.cpp @@ -120,6 +120,10 @@ void Request::Clear() ClearAjax(); ajax_serializer = 0; + binary_page.clear(); + compressed_page.clear(); + use_text_page = true; + gen_trim_white = false; gen_skip_new_line = false; gen_use_special_chars = false; diff --git a/core/request.h b/core/request.h index 55af97c..b5e3d3d 100755 --- a/core/request.h +++ b/core/request.h @@ -48,6 +48,17 @@ struct Request HtmlTextStream page, debug; TextStream ajaxpage; + // binary page + BinaryPage binary_page; + + // a compressed page ready to send to the client + BinaryPage compressed_page; + + // if true then either page or ajaxpage will be sent to the client + // if false then binary_page is sent + // default: true + bool use_text_page; + // if set to true then the standard template system will not be generated // default: false bool page_generated; diff --git a/core/requesttypes.h b/core/requesttypes.h index bba1908..0ce633e 100755 --- a/core/requesttypes.h +++ b/core/requesttypes.h @@ -13,6 +13,7 @@ #include #include #include +#include "textstream/textstream.h" // !! narazie uzywane tylko w post multi parserze @@ -45,5 +46,7 @@ typedef std::vector ParamTab; typedef std::map CookieTab; +typedef PT::TextStreamBase BinaryPage; + #endif diff --git a/core/run.cpp b/core/run.cpp index dea7f9f..d78fd61 100755 --- a/core/run.cpp +++ b/core/run.cpp @@ -181,6 +181,7 @@ void Run::ChildThrow() execve(command, par, env); // if we are here then there is something wrong (execve failed) + // !! IMPROVE ME may add exit() ? } diff --git a/core/version.h b/core/version.h index 6df507b..8659dd3 100755 --- a/core/version.h +++ b/core/version.h @@ -2,7 +2,7 @@ * This file is a part of Winix * and is not publicly distributed * - * Copyright (c) 2008-2011, Tomasz Sowa + * Copyright (c) 2008-2012, Tomasz Sowa * All rights reserved. * */ @@ -12,8 +12,8 @@ #define WINIX_VER_MAJOR 0 -#define WINIX_VER_MINOR 4 -#define WINIX_VER_REVISION 8 +#define WINIX_VER_MINOR 5 +#define WINIX_VER_REVISION 0 #endif diff --git a/db/dbbase.cpp b/db/dbbase.cpp index 0eb0ece..ba682fa 100755 --- a/db/dbbase.cpp +++ b/db/dbbase.cpp @@ -470,8 +470,9 @@ return err; /* converting from a bytea + the old way (escape format) */ - +/* int DbBase::CharToInt(char c) { return (int)(unsigned char)(c-'0'); @@ -537,6 +538,53 @@ size_t i = 0; out += c; } } +*/ + + + + + +/* + converting from a bytea + the new way (hex format) +*/ + + +char DbBase::UnescapeBinHexToDigit(char hex) +{ + if( hex>='0' && hex<='9' ) + return hex - '0'; + + if( hex>='a' && hex<='z' ) + return hex - 'a' + 10; + + if( hex>='A' && hex<='Z' ) + return hex - 'A' + 10; + +return 0; +} + + +void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out) +{ + if( clear_out ) + out.clear(); + + if( len < 2 || str[0]!='\\' || str[1]!='x' ) + { + log << log1 << "Db: unsupported binary format (skipping)" << logend; + return; + } + + for(size_t i=2 ; i + 1 < len ; i+=2 ) + { + int c1 = UnescapeBinHexToDigit(str[i]); + int c2 = UnescapeBinHexToDigit(str[i+1]); + + out += ((c1 << 4) | c2); + } +} + /* end of converting from bytea diff --git a/db/dbbase.h b/db/dbbase.h index 04b5a59..4d9156f 100755 --- a/db/dbbase.h +++ b/db/dbbase.h @@ -93,9 +93,11 @@ protected: private: - static int CharToInt(char c); - static bool IsCorrectOctalDigit(char c); - static int UnescapeBin(const char * str, size_t & i, size_t len); +// static int CharToInt(char c); +// static bool IsCorrectOctalDigit(char c); +// static int UnescapeBin(const char * str, size_t & i, size_t len); + + static char UnescapeBinHexToDigit(char hex); DbTextStream bquery; PT::SpaceParser conf_parser; diff --git a/db/dbtextstream.cpp b/db/dbtextstream.cpp index a1bc8ef..bae8e49 100755 --- a/db/dbtextstream.cpp +++ b/db/dbtextstream.cpp @@ -217,6 +217,10 @@ return *this; */ +/* + old PostgreSQL Escape Format +*/ +/* DbTextStream & DbTextStream::EBinPutChar(char c_) { char buf[20]; @@ -235,6 +239,26 @@ char buf[20]; return *this; } +*/ + +// get hex digit for c_ between <0, 15> +char DbTextStream::EBinGetHex(char c) +{ + if( c < 10 ) + return c + '0'; + +return c - 10 + 'A'; +} + + +DbTextStream & DbTextStream::EBinPutChar(char c) +{ + buffer += EBinGetHex(((unsigned char)c) >> 4); + buffer += EBinGetHex(((unsigned char)c) & 0x0f); + +return *this; +} + DbTextStream & DbTextStream::ETextPutChar(char c) @@ -341,7 +365,7 @@ DbTextStream & DbTextStream::EPutBin(const char * str, size_t len) if( ext_escape ) buffer += 'E'; - buffer += '\''; + buffer += "\'\\\\x"; for(size_t i = 0 ; i < len ; ++i) EBinPutChar(str[i]); diff --git a/db/dbtextstream.h b/db/dbtextstream.h index 297ce5a..ebd8e42 100755 --- a/db/dbtextstream.h +++ b/db/dbtextstream.h @@ -161,6 +161,8 @@ private: bool ext_escape; TextStream tmp_stream; + char EBinGetHex(char c); + }; diff --git a/functions/Makefile.dep b/functions/Makefile.dep index f31d4ad..36d1aeb 100755 --- a/functions/Makefile.dep +++ b/functions/Makefile.dep @@ -420,10 +420,10 @@ functionbase.o: ../core/timezones.h ../core/synchro.h functionparser.o: functionparser.h ../db/db.h ../db/dbbase.h ../db/dbconn.h functionparser.o: ../db/dbtextstream.h ../core/textstream.h ../core/misc.h functionparser.o: ../core/item.h ../core/requesttypes.h -functionparser.o: ../../pikotools/date/date.h functionparser.o: ../../pikotools/textstream/textstream.h functionparser.o: ../../pikotools/space/space.h functionparser.o: ../../pikotools/textstream/types.h +functionparser.o: ../../pikotools/date/date.h functionparser.o: ../../pikotools/convert/convert.h functionparser.o: ../../pikotools/convert/inttostr.h functionparser.o: ../../pikotools/membuffer/membuffer.h @@ -1057,9 +1057,8 @@ privchanger.o: ../../pikotools/textstream/types.h ../core/dirs.h privchanger.o: ../core/item.h ../core/dircontainer.h ../db/db.h privchanger.o: ../db/dbbase.h ../db/dbconn.h ../db/dbtextstream.h privchanger.o: ../core/textstream.h ../core/misc.h ../core/requesttypes.h -privchanger.o: ../../pikotools/date/date.h privchanger.o: ../../pikotools/textstream/textstream.h -privchanger.o: ../../pikotools/convert/convert.h +privchanger.o: ../../pikotools/date/date.h ../../pikotools/convert/convert.h privchanger.o: ../../pikotools/convert/inttostr.h privchanger.o: ../../pikotools/membuffer/membuffer.h privchanger.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h diff --git a/plugins/group/Makefile.dep b/plugins/group/Makefile.dep index 10ea806..a5ff61e 100755 --- a/plugins/group/Makefile.dep +++ b/plugins/group/Makefile.dep @@ -109,9 +109,9 @@ templates.o: ../../../ezc/src/generator.h ../../../ezc/src/pattern.h templates.o: ../../core/item.h ../../../ezc/src/functions.h templates.o: ../../../ezc/src/funinfo.h ../../../ezc/src/stringconv.h templates.o: ../../../pikotools/utf8/utf8.h ../../core/misc.h -templates.o: ../../core/requesttypes.h ../../../pikotools/date/date.h +templates.o: ../../core/requesttypes.h templates.o: ../../../pikotools/textstream/textstream.h -templates.o: ../../../pikotools/space/space.h +templates.o: ../../../pikotools/space/space.h ../../../pikotools/date/date.h templates.o: ../../../pikotools/convert/convert.h templates.o: ../../../pikotools/convert/inttostr.h templates.o: ../../../pikotools/membuffer/membuffer.h diff --git a/plugins/menu/Makefile.dep b/plugins/menu/Makefile.dep index 1ce7283..e792c18 100755 --- a/plugins/menu/Makefile.dep +++ b/plugins/menu/Makefile.dep @@ -97,10 +97,11 @@ templates.o: ../../../ezc/src/generator.h ../../../ezc/src/pattern.h templates.o: ../../core/item.h ../../../ezc/src/functions.h templates.o: ../../../ezc/src/funinfo.h ../../../ezc/src/stringconv.h templates.o: ../../../pikotools/utf8/utf8.h ../../core/misc.h -templates.o: ../../core/requesttypes.h ../../../pikotools/date/date.h +templates.o: ../../core/requesttypes.h templates.o: ../../../pikotools/textstream/textstream.h templates.o: ../../../pikotools/space/space.h templates.o: ../../../pikotools/textstream/types.h +templates.o: ../../../pikotools/date/date.h templates.o: ../../../pikotools/convert/convert.h templates.o: ../../../pikotools/convert/inttostr.h templates.o: ../../../pikotools/membuffer/membuffer.h diff --git a/plugins/stats/Makefile.dep b/plugins/stats/Makefile.dep index 264ebf5..a70a069 100755 --- a/plugins/stats/Makefile.dep +++ b/plugins/stats/Makefile.dep @@ -82,16 +82,16 @@ templates.o: templates.h ../../core/plugin.h ../../core/pluginmsg.h templates.o: ../../core/log.h ../../core/plugindata.h ../../core/config.h templates.o: ../../../pikotools/space/spaceparser.h templates.o: ../../../pikotools/space/space.h ../../core/htmlfilter.h -templates.o: ../../core/request.h ../../core/requesttypes.h ../../core/item.h +templates.o: ../../core/request.h ../../core/requesttypes.h +templates.o: ../../../pikotools/textstream/textstream.h templates.o: ../../../pikotools/space/space.h templates.o: ../../../pikotools/textstream/types.h -templates.o: ../../../pikotools/date/date.h ../../core/error.h -templates.o: ../../core/textstream.h ../../core/misc.h -templates.o: ../../../pikotools/textstream/textstream.h +templates.o: ../../../pikotools/date/date.h templates.o: ../../../pikotools/convert/convert.h templates.o: ../../../pikotools/convert/inttostr.h templates.o: ../../../pikotools/membuffer/membuffer.h -templates.o: ../../../pikotools/textstream/types.h +templates.o: ../../../pikotools/textstream/types.h ../../core/item.h +templates.o: ../../core/error.h ../../core/textstream.h ../../core/misc.h templates.o: ../../../pikotools/utf8/utf8.h ../../templates/htmltextstream.h templates.o: ../../core/textstream.h ../../../pikotools/space/spacetojson.h templates.o: ../../core/system.h ../../core/job.h ../../core/basethread.h diff --git a/plugins/thread/Makefile.dep b/plugins/thread/Makefile.dep index ccbe121..7587ed0 100755 --- a/plugins/thread/Makefile.dep +++ b/plugins/thread/Makefile.dep @@ -66,10 +66,10 @@ createthread.o: ../../functions/uname.h ../../functions/upload.h createthread.o: ../../functions/uptime.h ../../functions/who.h createthread.o: ../../functions/vim.h ../../core/htmlfilter.h funthread.o: ../../core/misc.h ../../core/item.h ../../core/requesttypes.h -funthread.o: ../../../pikotools/date/date.h funthread.o: ../../../pikotools/textstream/textstream.h funthread.o: ../../../pikotools/space/space.h funthread.o: ../../../pikotools/textstream/types.h +funthread.o: ../../../pikotools/date/date.h funthread.o: ../../../pikotools/convert/convert.h funthread.o: ../../../pikotools/convert/inttostr.h funthread.o: ../../../pikotools/membuffer/membuffer.h diff --git a/templates/Makefile.dep b/templates/Makefile.dep index 914e39b..354441f 100755 --- a/templates/Makefile.dep +++ b/templates/Makefile.dep @@ -967,6 +967,20 @@ templates.o: ../functions/subject.h ../functions/template.h templates.o: ../functions/tinymce.h ../functions/uname.h templates.o: ../functions/upload.h ../functions/uptime.h ../functions/who.h templates.o: ../functions/vim.h ../templates/templates.h +textextstream.o: textextstream.h ../core/textstream.h misc.h localefilter.h +textextstream.o: locale.h ../../pikotools/space/spaceparser.h +textextstream.o: ../../pikotools/space/space.h +textextstream.o: ../../pikotools/textstream/types.h +textextstream.o: ../../pikotools/textstream/textstream.h +textextstream.o: ../../pikotools/space/space.h ../../pikotools/date/date.h +textextstream.o: ../../pikotools/convert/convert.h +textextstream.o: ../../pikotools/convert/inttostr.h +textextstream.o: ../../pikotools/membuffer/membuffer.h +textextstream.o: ../../pikotools/textstream/types.h ../../ezc/src/ezc.h +textextstream.o: ../../ezc/src/generator.h ../../ezc/src/pattern.h +textextstream.o: ../../ezc/src/item.h ../../ezc/src/funinfo.h +textextstream.o: ../../ezc/src/functions.h ../../ezc/src/stringconv.h +textextstream.o: ../../pikotools/utf8/utf8.h htmltextstream.h upload.o: ../core/request.h templates.h ../../ezc/src/ezc.h upload.o: ../../ezc/src/generator.h ../../ezc/src/pattern.h upload.o: ../../ezc/src/item.h ../../ezc/src/funinfo.h diff --git a/templates/Makefile.o.dep b/templates/Makefile.o.dep index be149e8..762bdd4 100755 --- a/templates/Makefile.o.dep +++ b/templates/Makefile.o.dep @@ -1 +1 @@ -o = adduser.o changepatterns.o config.o dir.o doc.o env.o filters.o htmltextstream.o indexpatterns.o insert.o item.o last.o locale.o localefilter.o login.o ls.o man.o misc.o miscspace.o mount.o patterncacher.o patterns.o priv.o pw.o rebus.o slog.o stat.o sys.o template.o templates.o upload.o uptime.o user.o who.o winix.o +o = adduser.o changepatterns.o config.o dir.o doc.o env.o filters.o htmltextstream.o indexpatterns.o insert.o item.o last.o locale.o localefilter.o login.o ls.o man.o misc.o miscspace.o mount.o patterncacher.o patterns.o priv.o pw.o rebus.o slog.o stat.o sys.o template.o templates.o textextstream.o upload.o uptime.o user.o who.o winix.o diff --git a/templates/textextstream.cpp b/templates/textextstream.cpp new file mode 100755 index 0000000..b901ab2 --- /dev/null +++ b/templates/textextstream.cpp @@ -0,0 +1,456 @@ +/* + * This file is a part of Winix + * and is not publicly distributed + * + * Copyright (c) 2012, Tomasz Sowa + * All rights reserved. + * + */ + +#include "textextstream.h" + + + + +TexTextStream::TexTextStream() +{ +} + + + + +/* + without escaping +*/ + + +TexTextStream & TexTextStream::PutText(const char * str) +{ + TextStream::operator<<(str); + +return *this; +} + + +TexTextStream & TexTextStream::PutText(const char * str, size_t len) +{ + TextStream::operator<<(str); + +return *this; +} + + +TexTextStream & TexTextStream::PutText(const std::string * str) +{ + return PutText(str->c_str()); +} + + +TexTextStream & TexTextStream::PutText(const std::string & str) +{ + return PutText(str.c_str()); +} + + + +TexTextStream & TexTextStream::PutText(const wchar_t * str) +{ + TextStream::operator<<(str); + +return *this; +} + + +TexTextStream & TexTextStream::PutText(const std::wstring * str) +{ + return PutText(str->c_str()); +} + + +TexTextStream & TexTextStream::PutText(const std::wstring & str) +{ + return PutText(str.c_str()); +} + + + + + + + +TexTextStream & TexTextStream::operator<<(const RawText & raw) +{ + return PutText(raw.par); +} + + +TexTextStream & TexTextStream::operator<<(const RawText & raw) +{ + return PutText(raw.par); +} + + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + return PutText(raw.par); +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + return PutText(raw.par); +} + + + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + return PutText(raw.par); +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + return PutText(raw.par); +} + + + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + +TexTextStream & TexTextStream::operator<<(RawText raw) +{ + TextStream::operator<<(raw.par); + return *this; +} + + + + +TexTextStream & TexTextStream::Write(const char * buf, size_t len) +{ + TextStream::Write(buf, len); + return *this; +} + +TexTextStream & TexTextStream::Write(const wchar_t * buf, size_t len) +{ + TextStream::Write(buf, len); + return *this; +} + + +TexTextStream & TexTextStream::write(const char * buf, size_t len) +{ + TextStream::write(buf, len); + return *this; +} + +TexTextStream & TexTextStream::write(const wchar_t * buf, size_t len) +{ + TextStream::write(buf, len); + return *this; +} + + + + + +/* + with escaping +*/ + + + + +TexTextStream & TexTextStream::ETextPutChar(char c) +{ + return ETextPutChar(static_cast(c)); +} + + +TexTextStream & TexTextStream::ETextPutChar(wchar_t c) +{ + if( c == '$' ) + buffer += L"\\$"; + else + if( c == '#' ) + buffer += L"\\#"; + else + if( c == '%' ) + buffer += L"\\%"; + else + if( c == '&' ) + buffer += L"\\&"; + else + if( c == '\\' ) + buffer += L"$\\backslash$"; + else + if( c == '{' ) + buffer += L"$\\{$"; + else + if( c == '}' ) + buffer += L"$\\}$"; + else + if( c == '^' ) + buffer += L""; // !! IMPROVE ME add \char with specific code + else + if( c == '_' ) + buffer += L"\\_"; + else + if( c == '~' ) + buffer += L""; // !! IMPROVE ME add \char with specific code + else + if( c == '-' ) + buffer += L"{-}"; + else + if( c != 0 ) + buffer += c; + +return *this; +} + + +TexTextStream & TexTextStream::EPutText(const char * str) +{ + for( ; *str ; ++str ) + ETextPutChar(*str); + +return *this; +} + + + +TexTextStream & TexTextStream::EPutText(const char * str, size_t len) +{ + for(size_t i=0 ; ic_str(), str->size()); +} + + +TexTextStream & TexTextStream::EPutText(const std::string & str) +{ + return EPutText(str.c_str(), str.size()); +} + + +TexTextStream & TexTextStream::EPutText(const wchar_t * str) +{ + for( ; *str ; ++str ) + ETextPutChar(*str); + +return *this; +} + + +TexTextStream & TexTextStream::EPutText(const wchar_t * str, size_t len) +{ + for(size_t i=0 ; ic_str(), str->size()); +} + + +TexTextStream & TexTextStream::EPutText(const std::wstring & str) +{ + return EPutText(str.c_str(), str.size()); +} + + + + +TexTextStream & TexTextStream::operator<<(const char * str) +{ + return EPutText(str); +} + + +TexTextStream & TexTextStream::operator<<(const std::string * str) +{ + return EPutText(str); +} + + +TexTextStream & TexTextStream::operator<<(const std::string & str) +{ + return EPutText(str); +} + + + + +TexTextStream & TexTextStream::operator<<(const wchar_t * str) +{ + return EPutText(str); +} + + +TexTextStream & TexTextStream::operator<<(const std::wstring * str) +{ + return EPutText(str); +} + + +TexTextStream & TexTextStream::operator<<(const std::wstring & str) +{ + return EPutText(str); +} + + + + +TexTextStream & TexTextStream::operator<<(char v) +{ + ETextPutChar(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(wchar_t v) +{ + ETextPutChar(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(int v) +{ + TextStream::operator<<(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(long v) +{ + TextStream::operator<<(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(unsigned int v) +{ + TextStream::operator<<(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(unsigned long v) +{ + TextStream::operator<<(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(double v) +{ + TextStream::operator<<(v); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(const void * v) +{ + TextStream::operator<<(v); + +return *this; +} + + + +TexTextStream & TexTextStream::operator<<(const PT::Space & space) +{ + tmp_stream.Clear(); + // !! IMPROVE ME + // we can calculate how many memory is needed beforehand + space.Serialize(tmp_stream, true, false); + operator<<(tmp_stream.Str()); + tmp_stream.Clear(); + +return *this; +} + + +TexTextStream & TexTextStream::operator<<(const PT::Date & date) +{ + tmp_stream.Clear(); + // !! IMPROVE ME + // we can calculate how many memory is needed beforehand + date.Serialize(tmp_stream); + operator<<(tmp_stream.Str()); + tmp_stream.Clear(); + +return *this; +} + diff --git a/templates/textextstream.h b/templates/textextstream.h new file mode 100755 index 0000000..d073a4c --- /dev/null +++ b/templates/textextstream.h @@ -0,0 +1,163 @@ +/* + * This file is a part of Winix + * and is not publicly distributed + * + * Copyright (c) 2012, Tomasz Sowa + * All rights reserved. + * + */ + +#ifndef headerfile_winix_templates_textextstream +#define headerfile_winix_templates_textextstream + +#include +#include "core/textstream.h" + + + +/* + TexTextStream is used as a buffer for creating a html page + By default all operators<< escape its string arguments. If you don't want + to escape an argument you should use a helper function R() (raw argument) + note: you have to define the function yourself, we do not provide it + because such a short name would make a mess in namespaces + + sample: + create a helper function R as follows: + + template + TexTextStream::RawText R(const RawType & par) + { + return TexTextStream::RawText(par); + } + + now you can use TexTextStream in an easy way: + TexTextStream page; + std::string key = "some string"; + page << key << R("

html goes here

"); + + // !! UPDATE INFO this is TEX text + only html tags "" and "" will be correctly escaped + + currently following characters are escaped: + < -> < + > -> > + & ->   +*/ +class TexTextStream : public TextStream +{ +public: + + + TexTextStream(); + + + /* + a helper struct to select a proper operator<< + (for non-escaping versions of these operators) + */ + template + struct RawText + { + const RawType & par; + + RawText(const RawText & p) : par(p.par) {} + RawText(const RawType & p) : par(p) {} + }; + + + + /* + without escaping + */ + + TexTextStream & PutText(const char *); + TexTextStream & PutText(const char *, size_t len); + TexTextStream & PutText(const std::string *); + TexTextStream & PutText(const std::string &); + TexTextStream & PutText(const wchar_t * str); + TexTextStream & PutText(const wchar_t * str, size_t len); + TexTextStream & PutText(const std::wstring * str); + TexTextStream & PutText(const std::wstring & str); + + /* + we need this template operator for such calling: + HtmlTextStream_object << R("some string"); + "some string" is actually a table (not a pointer) + */ + template + TexTextStream & operator<<(const RawText & raw) { return PutText(raw.par); } + + template + TexTextStream & operator<<(const RawText & raw) { return PutText(raw.par); } + + TexTextStream & operator<<(const RawText & raw); + TexTextStream & operator<<(const RawText & raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + TexTextStream & operator<<(RawText raw); + + // 'write' don't escapes too + // with these methods you can write a zero character too + TexTextStream & Write(const char * buf, size_t len); + TexTextStream & Write(const wchar_t * buf, size_t len); + // for compatibility with standard library (Ezc uses it) + TexTextStream & write(const char * buf, size_t len); + TexTextStream & write(const wchar_t * buf, size_t len); + + + /* + with escaping + */ + + TexTextStream & ETextPutChar(char c); + TexTextStream & ETextPutChar(wchar_t c); + + TexTextStream & EPutText(const char * str); + TexTextStream & EPutText(const char * str, size_t len); + TexTextStream & EPutText(const std::string * str); + TexTextStream & EPutText(const std::string & str); + TexTextStream & EPutText(const wchar_t * str); + TexTextStream & EPutText(const wchar_t * str, size_t len); + TexTextStream & EPutText(const std::wstring * str); + TexTextStream & EPutText(const std::wstring & str); + + + TexTextStream & operator<<(const char * str); + TexTextStream & operator<<(const std::string * str); + TexTextStream & operator<<(const std::string & str); + TexTextStream & operator<<(const wchar_t * str); + TexTextStream & operator<<(const std::wstring * str); + TexTextStream & operator<<(const std::wstring & str); + TexTextStream & operator<<(char); + TexTextStream & operator<<(wchar_t); + TexTextStream & operator<<(int); + TexTextStream & operator<<(long); + TexTextStream & operator<<(unsigned int); + TexTextStream & operator<<(unsigned long); + TexTextStream & operator<<(double); + TexTextStream & operator<<(const void *); + TexTextStream & operator<<(const PT::Space & space); + TexTextStream & operator<<(const PT::Date & Date); + +private: + + TextStream tmp_stream; + +}; + + + + +#endif +