"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:c8631d$bai$1@news-> Siemel Naran wrote:
texel_t val;
while (in >> val)
If the stream is "25" what will you read. Will these be 2 texel_t
objects with values 2 and 5? Or will it be one with value "25"? And what will
a stream of "257" read into? Will it be 3 texel_t objects? Or will it be
one which puts the stream into a fail state because of an overflow?
What i would like is:
"25" -> 1 texel_t with value 25
"257" -> fail state
I've forget to say that in pbm (P1) files, the color is white (0) or black
(1). But the question is good for future extensions (and c++ knowledge ;).
Easiest way is to read an unsigned integer, check if it is more than 255 and
if so set the failbit, then cast to an unsigned char.
To make it clear make texexl_t a class so that you can overload operator>>
for it, and ideally you'll suffer no performance overhead because the
compiler would optimize texcel_t as if it were an unsigned integer.
class exel_t {
public:
excel_t() { }
unsigned char value() const { return d_value; }
private:
unsigned char d_value;
};
inline
std::istream& operator<<(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e.d_value = (unsigned char)(value);
return i;
}
But the use of istream >> int is still quite expensive because it handles
sentries, does locale dependent formatting, etc. There are alternative
solutions available. One is to use fscanf. Another is to get the
underlying streambuf with i.rdbuf() and parse the chars yourself manually.
There was a post on this NG in the last few days about how to convert an
array of chars into an integer.