changeset 18252:284e5c87f27b stable

Fix saving int8 and uint8 in plain text format (bug #40980) * oct-inttypes.h (operator<<): Specialise this operator's octave_int<T> overloads for T = int8_t and T = uint8_t so that it calls non-char versions of std::operator<<
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 29 Dec 2013 10:11:30 -0500
parents 0b5f669f5b03
children 545a77c3206e cae24b7cfaf4
files liboctave/util/oct-inttypes.h
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/util/oct-inttypes.h	Thu Jan 09 21:48:34 2014 +0100
+++ b/liboctave/util/oct-inttypes.h	Sun Dec 29 10:11:30 2013 -0500
@@ -1023,6 +1023,50 @@
   return is;
 }
 
+// We need to specialise for char and unsigned char because
+// std::operator<< and std::operator>> are overloaded to input and
+// output the ASCII character values instead of a representation of
+// their numerical value (e.g. os << char(10) outputs a space instead
+// of outputting the characters '1' and '0')
+
+template <>
+inline std::ostream&
+operator << (std::ostream& os, const octave_int<int8_t>& ival)
+{
+  os << static_cast<int> (ival.value ());
+  return os;
+}
+
+template <>
+inline std::ostream&
+operator << (std::ostream& os, const octave_int<uint8_t>& ival)
+{
+  os << static_cast<unsigned int> (ival.value ());
+  return os;
+}
+
+
+template <>
+inline std::istream&
+operator >> (std::istream& is, octave_int<int8_t>& ival)
+{
+  int tmp = 0;
+  is >> tmp;
+  ival = static_cast<int8_t> (tmp);
+  return is;
+}
+
+template <>
+inline std::istream&
+operator >> (std::istream& is, octave_int<uint8_t>& ival)
+{
+  unsigned int tmp = 0;
+  is >> tmp;
+  ival = static_cast<uint8_t> (tmp);
+  return is;
+}
+
+
 // Bitwise operations
 
 #define OCTAVE_INT_BITCMP_OP(OP) \