From 310a2c101b32a5e71a616027b6a1b788a341bc02 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 5 Mar 2013 17:39:48 +0100 Subject: initial GPLv2 release --- wxstreamex.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 wxstreamex.cpp (limited to 'wxstreamex.cpp') diff --git a/wxstreamex.cpp b/wxstreamex.cpp new file mode 100644 index 0000000..dfbf7c7 --- /dev/null +++ b/wxstreamex.cpp @@ -0,0 +1,108 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Manuel Mausz (manuel@mausz.at) + * Christian Raschko (c.raschko@netcore.at) + */ + +// Header +#include "wxstreamex.h" + +//------------------------------------------------------------------------------ +// Writes a fixed string on the stream +bool wxDataOutputStreamEx::WriteFixedString(wxString const &str, wxUint8 size) +{ + if((str.Length()+1) > size) + return false; + + wxCSConv conv_iso(_T("iso-8859-1")); + wxCharBuffer buffer = str.mb_str(conv_iso); + + strm->PutC(wxUint8(strlen(buffer))); + + for(size_t i = 0; i < strlen(buffer); i++) + strm->PutC(str[i]); + + for(size_t i = 0; i < (size-strlen(buffer)-1); i++) + strm->PutC('\0'); + + return true; +} + +//------------------------------------------------------------------------------ +// Writes zero terminated string on the stream with zero +bool wxDataOutputStreamEx::WriteZeroString(wxString const &str) +{ + if(str.Length() == 0) + return false; + + wxCSConv conv_iso(_T("iso-8859-1")); + wxCharBuffer buffer = str.mb_str(conv_iso); + strm->Write(buffer, strlen(buffer)); + strm->PutC('\0'); + + return true; +} + +//------------------------------------------------------------------------------ + +// Reades a fixed string from the stream +bool wxDataInputStreamEx::ReadFixedString(wxString &str, wxUint8 size) +{ + size_t strlen = 0; + char buffer[TS_MAX_LENGTH]; + int i = 0; + + strlen = strm->GetC(); + for(i = 0; i < size-1; i++) + { + if(strm->Eof()) + { + buffer[i] = '\0'; + str = wxString::FromAscii(buffer); + return false; + } + + buffer[i] = strm->GetC(); + } + + buffer[i] = '\0'; + str = wxString::FromAscii(buffer); + wxUnusedVar(strlen); + return true; +} + +//------------------------------------------------------------------------------ +// Reades a zero terminated string from the stream +bool wxDataInputStreamEx::ReadZeroString(wxString &str) +{ + wxChar c; + char buffer[TS_MAX_LENGTH]; + unsigned i = 0; + + c = strm->GetC(); + while((c != '\0') && (!strm->Eof())) + { + buffer[i] = c; + i++; + c = strm->GetC(); + } + buffer[i] = '\0'; + str = wxString::FromAscii(buffer); + if(strm->Eof()) + return false; + + return true; +} + -- cgit v1.2.3