summaryrefslogtreecommitdiffstats
path: root/wxstreamex.cpp
blob: dfbf7c7a0035dc5a2f502bebfc6daa0bdfeb14e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
}