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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKSTREAMBUF_KERNEL_1_CPp_
#define DLIB_SOCKSTREAMBUF_KERNEL_1_CPp_
#include "sockstreambuf_kernel_1.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// output functions
// ----------------------------------------------------------------------------------------
sockstreambuf_kernel_1::int_type sockstreambuf_kernel_1::
overflow (
int_type c
)
{
if (c != EOF)
{
char temp = static_cast<char>(c);
if (con.write(&temp,1) != 1)
{
// if the write was not successful
return EOF;
}
}
return c;
}
// ----------------------------------------------------------------------------------------
std::streamsize sockstreambuf_kernel_1::
xsputn (
const char* s,
std::streamsize num
)
{
if (con.write(s,static_cast<int>(num)) != num)
{
// the write was not successful so return that 0 bytes were written
return 0;
}
return num;
}
// ----------------------------------------------------------------------------------------
// input functions
// ----------------------------------------------------------------------------------------
sockstreambuf_kernel_1::int_type sockstreambuf_kernel_1::
underflow(
)
{
if (lastread_next)
{
return lastread;
}
else if (peek != EOF)
{
return peek;
}
else
{
char temp;
if (con.read(&temp,1) != 1)
{
// some error occurred
return EOF;
}
peek = static_cast<unsigned char>(temp);
return peek;
}
}
// ----------------------------------------------------------------------------------------
sockstreambuf_kernel_1::int_type sockstreambuf_kernel_1::
uflow(
)
{
if (lastread_next)
{
lastread_next = false;
return lastread;
}
else if (peek != EOF)
{
lastread = peek;
peek = EOF;
return lastread;
}
else
{
char temp;
if (con.read(&temp,1) != 1)
{
// some error occurred
return EOF;
}
lastread = static_cast<unsigned char>(temp);
return lastread;
}
}
// ----------------------------------------------------------------------------------------
sockstreambuf_kernel_1::int_type sockstreambuf_kernel_1::
pbackfail(
int_type c
)
{
// if they are trying to push back a character that they didn't read last
// that is an error
if (c != EOF && c != lastread)
return EOF;
// if they are trying to push back a second character then thats an error
if (lastread_next)
return EOF;
lastread_next = true;
return 1;
}
// ----------------------------------------------------------------------------------------
std::streamsize sockstreambuf_kernel_1::
xsgetn (
char_type* s,
std::streamsize n
)
{
std::streamsize temp = n;
if (lastread_next && n > 0)
{
*s = lastread;
lastread_next = false;
++s;
--n;
}
if (peek != EOF && n > 0)
{
*s = peek;
peek = EOF;
++s;
--n;
}
while (n>0)
{
int status = con.read(s,static_cast<int>(n));
if (status < 1)
break;
n -= status;
s += status;
}
return temp-n;
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SOCKSTREAMBUF_KERNEL_1_CPp_
|