From 5d457f3d4b1dce77ca01cedac5b7f6151a4d293a Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 1 Jun 2024 00:33:15 +0200 Subject: [PATCH] (update to the new pikotools api): use a char32_t character as a main character when converting strings Use a char32_t instead of a wchar_t type. This is needed on systems where sizeof(wchar_t) is equal to 2. This affects classes based on the pt::Stream such as Log and HtmlTextStream. --- winixd/core/log.cpp | 7 +++ winixd/core/log.h | 1 + winixd/templates/htmltextstream.cpp | 72 +++++++++++++++++------------ winixd/templates/htmltextstream.h | 6 ++- 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/winixd/core/log.cpp b/winixd/core/log.cpp index ace2c28..42a1238 100644 --- a/winixd/core/log.cpp +++ b/winixd/core/log.cpp @@ -197,6 +197,13 @@ Log & Log::operator<<(wchar_t s) } +Log & Log::operator<<(char32_t s) +{ + pt::Log::operator<<(s); + return *this; +} + + Log & Log::operator<<(unsigned short s) { pt::Log::operator<<(s); diff --git a/winixd/core/log.h b/winixd/core/log.h index f63496d..8240924 100644 --- a/winixd/core/log.h +++ b/winixd/core/log.h @@ -74,6 +74,7 @@ public: virtual Log & operator<<(char s); virtual Log & operator<<(unsigned char); virtual Log & operator<<(wchar_t s); + virtual Log & operator<<(char32_t s); virtual Log & operator<<(bool); virtual Log & operator<<(short); virtual Log & operator<<(int s); diff --git a/winixd/templates/htmltextstream.cpp b/winixd/templates/htmltextstream.cpp index f2a7217..58fac73 100644 --- a/winixd/templates/htmltextstream.cpp +++ b/winixd/templates/htmltextstream.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2021, Tomasz Sowa + * Copyright (c) 2010-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,6 +33,7 @@ */ #include "htmltextstream.h" +#include "convert/misc.h" namespace Winix { @@ -245,6 +246,14 @@ return *this; } +HtmlTextStream & HtmlTextStream::PutChar(char32_t c) +{ + buffer.operator<<(c); + +return *this; +} + + HtmlTextStream & HtmlTextStream::PutChar(bool val) { buffer.operator<<(val); @@ -306,6 +315,13 @@ HtmlTextStream & HtmlTextStream::operator<<(RawText raw) } +HtmlTextStream & HtmlTextStream::operator<<(RawText raw) +{ + buffer.operator<<(raw.par); + return *this; +} + + HtmlTextStream & HtmlTextStream::operator<<(RawText raw) { buffer.operator<<(raw.par); @@ -453,13 +469,12 @@ HtmlTextStream & HtmlTextStream::EPutText(const char * str) bool correct; size_t utf8_len; - // CHECKME while( (utf8_len = pt::utf8_to_int(str, res, correct)) > 0 ) { if( !correct ) res = 0xFFFD; // U+FFFD "replacement character" - ETextPutChar(static_cast(res)); + ETextPutChar(static_cast(res)); str += utf8_len; } @@ -475,6 +490,11 @@ HtmlTextStream & HtmlTextStream::EPutText(const std::string & str) HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str) { + /* + * we assume the sizeof(wchar) is 4 here + * as at the moment winix works only on unixes + * so just copy directly those characters + */ for( ; *str ; ++str ) ETextPutChar(*str); @@ -528,39 +548,22 @@ HtmlTextStream & HtmlTextStream::ETextPutChar(char c) HtmlTextStream & HtmlTextStream::ETextPutChar(unsigned char c) { - return ETextPutChar(static_cast(c)); + return ETextPutChar(static_cast(c)); } HtmlTextStream & HtmlTextStream::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"'"; // (it is "'" but IE8 has a problem with ') (' is valid in HTML5, but not HTML4) - else - if( c == 10 ) - buffer << L" "; - else - if( c == 13 ) - buffer << L" "; - else - if( c == 0 ) - buffer << L"�"; - else + return ETextPutChar(static_cast(c)); +} + + +HtmlTextStream & HtmlTextStream::ETextPutChar(char32_t c) +{ + if( !pt::try_esc_to_html(c, buffer) ) buffer << c; -return *this; + return *this; } @@ -650,6 +653,17 @@ return *this; } +HtmlTextStream & HtmlTextStream::operator<<(char32_t v) +{ + if( escape ) + ETextPutChar(v); + else + PutChar(v); + +return *this; +} + + HtmlTextStream & HtmlTextStream::operator<<(bool v) { /* diff --git a/winixd/templates/htmltextstream.h b/winixd/templates/htmltextstream.h index 9bdc3fd..fa35f10 100644 --- a/winixd/templates/htmltextstream.h +++ b/winixd/templates/htmltextstream.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2022, Tomasz Sowa + * Copyright (c) 2010-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -148,6 +148,7 @@ public: HtmlTextStream & PutChar(char c); HtmlTextStream & PutChar(unsigned char c); HtmlTextStream & PutChar(wchar_t c); + HtmlTextStream & PutChar(char32_t c); HtmlTextStream & PutChar(bool val); @@ -171,6 +172,7 @@ public: HtmlTextStream & operator<<(RawText raw); HtmlTextStream & operator<<(RawText raw); HtmlTextStream & operator<<(RawText raw); + HtmlTextStream & operator<<(RawText raw); HtmlTextStream & operator<<(RawText raw); HtmlTextStream & operator<<(RawText raw); @@ -219,6 +221,7 @@ public: HtmlTextStream & ETextPutChar(char c); HtmlTextStream & ETextPutChar(unsigned char c); HtmlTextStream & ETextPutChar(wchar_t c); + HtmlTextStream & ETextPutChar(char32_t c); /* @@ -234,6 +237,7 @@ public: HtmlTextStream & operator<<(char); HtmlTextStream & operator<<(unsigned char); HtmlTextStream & operator<<(wchar_t); + HtmlTextStream & operator<<(char32_t); HtmlTextStream & operator<<(bool); HtmlTextStream & operator<<(short); HtmlTextStream & operator<<(int);