Browse Source

More fixes to work around broken libc++ eof() in Mac OS X 10.7

rdb 7 years ago
parent
commit
84a1315104

+ 15 - 4
dtool/src/prc/streamReader.cxx

@@ -26,6 +26,9 @@ get_string() {
 
   // First, get the length of the string
   size_t size = get_uint16();
+  if (size == 0) {
+    return string();
+  }
 
   char *buffer = (char *)alloca(size);
   _in->read(buffer, size);
@@ -42,6 +45,9 @@ get_string32() {
 
   // First, get the length of the string
   size_t size = get_uint32();
+  if (size == 0) {
+    return string();
+  }
 
   char *buffer = (char *)PANDA_MALLOC_ARRAY(size);
   _in->read(buffer, size);
@@ -60,7 +66,7 @@ get_z_string() {
 
   string result;
   int ch = _in->get();
-  while (!_in->eof() && !_in->fail() && ch != '\0') {
+  while (!_in->fail() && ch != EOF && ch != '\0') {
     result += (char)ch;
     ch = _in->get();
   }
@@ -76,6 +82,10 @@ string StreamReader::
 get_fixed_string(size_t size) {
   nassertr(!_in->eof() && !_in->fail(), string());
 
+  if (size == 0) {
+    return string();
+  }
+
   char *buffer = (char *)alloca(size);
   _in->read(buffer, size);
   size_t read_bytes = _in->gcount();
@@ -90,8 +100,9 @@ get_fixed_string(size_t size) {
  */
 void StreamReader::
 skip_bytes(size_t size) {
-  nassertv(!_in->eof() && !_in->fail());
+  nassertv(!_in->fail());
   nassertv((int)size >= 0);
+  nassertv(size == 0 || !_in->eof());
 
   while (size > 0) {
     _in->get();
@@ -145,9 +156,9 @@ string StreamReader::
 readline() {
   string line;
   int ch = _in->get();
-  while (!_in->eof() && !_in->fail()) {
+  while (ch != EOF && !_in->fail()) {
     line += (char)ch;
-    if (ch == '\n') {
+    if (ch == '\n' || _in->eof()) {
       // Here's the newline character.
       return line;
     }

+ 2 - 2
dtool/src/prc/streamReader_ext.cxx

@@ -45,9 +45,9 @@ readline() {
 
   std::string line;
   int ch = in->get();
-  while (!in->eof() && !in->fail()) {
+  while (ch != EOF && !in->fail()) {
     line += ch;
-    if (ch == '\n') {
+    if (ch == '\n' || in->eof()) {
       // Here's the newline character.
       break;
     }

+ 2 - 2
panda/src/downloader/decompressor.cxx

@@ -183,7 +183,7 @@ decompress(const Filename &source_file) {
     return false;
 
   int ch = _decompress->get();
-  while (!_decompress->eof() && !_decompress->fail()) {
+  while (ch != EOF && !_decompress->fail()) {
     _dest->put(ch);
     ch = _decompress->get();
   }
@@ -207,7 +207,7 @@ decompress(Ramfile &source_and_dest_file) {
   IDecompressStream decompress(&source, false);
 
   int ch = decompress.get();
-  while (!decompress.eof() && !decompress.fail()) {
+  while (ch != EOF && !decompress.fail()) {
     dest.put(ch);
     ch = decompress.get();
   }

+ 2 - 2
panda/src/downloader/documentSpec.cxx

@@ -66,7 +66,7 @@ input(std::istream &in) {
     // Scan the tag, up to but not including the closing paren.
     std::string tag;
     in >> ch;
-    while (!in.fail() && !in.eof() && ch != ')') {
+    while (!in.fail() && ch != EOF && ch != ')') {
       tag += ch;
       // We want to include embedded whitespace, so we use get().
       ch = in.get();
@@ -81,7 +81,7 @@ input(std::istream &in) {
   // Scan the date, up to but not including the closing bracket.
   if (ch != ']') {
     std::string date;
-    while (!in.fail() && !in.eof() && ch != ']') {
+    while (!in.fail() && ch != EOF && ch != ']') {
       date += ch;
       ch = in.get();
     }

+ 2 - 2
panda/src/downloader/httpChannel.cxx

@@ -2762,7 +2762,7 @@ bool HTTPChannel::
 server_getline(string &str) {
   nassertr(!_source.is_null(), false);
   int ch = (*_source)->get();
-  while (!(*_source)->eof() && !(*_source)->fail()) {
+  while (ch != EOF && !(*_source)->fail()) {
     switch (ch) {
     case '\n':
       // end-of-line character, we're done.
@@ -2850,7 +2850,7 @@ bool HTTPChannel::
 server_get(string &str, size_t num_bytes) {
   nassertr(!_source.is_null(), false);
   int ch = (*_source)->get();
-  while (!(*_source)->eof() && !(*_source)->fail()) {
+  while (ch != EOF && !(*_source)->fail()) {
     _working_get += (char)ch;
     if (_working_get.length() >= num_bytes) {
       str = _working_get;

+ 1 - 1
panda/src/downloader/httpDate.cxx

@@ -279,7 +279,7 @@ input(std::istream &in) {
 
   string date;
   ch = in.get();
-  while (!in.fail() && !in.eof() && ch != '"') {
+  while (!in.fail() && ch != EOF && ch != '"') {
     date += ch;
     ch = in.get();
   }

+ 1 - 1
panda/src/downloader/socketStream.cxx

@@ -56,7 +56,7 @@ do_receive_datagram(Datagram &dg) {
     // Read the first two bytes: the datagram length.
     while ((int)_data_so_far.size() < _tcp_header_size) {
       int ch = _istream->get();
-      if (_istream->eof() || _istream->fail()) {
+      if (ch == EOF || _istream->fail()) {
         _istream->clear();
         return false;
       }

+ 2 - 2
panda/src/express/hashVal.cxx

@@ -50,7 +50,7 @@ input_hex(istream &in) {
   size_t i = 0;
   int ch = in.get();
 
-  while (!in.eof() && !in.fail() && isxdigit(ch)) {
+  while (ch != EOF && !in.fail() && isxdigit(ch)) {
     if (i < 32) {
       buffer[i] = (char)ch;
     }
@@ -63,7 +63,7 @@ input_hex(istream &in) {
     return;
   }
 
-  if (!in.eof()) {
+  if (ch != EOF) {
     in.putback((char)ch);
   } else {
     in.clear();

+ 1 - 1
panda/src/express/make_ca_bundle.cxx

@@ -108,7 +108,7 @@ main(int argc, char *argv[]) {
   int col = 0;
   unsigned int ch;
   ch = in.get();
-  while (!in.fail() && !in.eof()) {
+  while (!in.fail() && ch != EOF) {
     if (col == 0) {
       out << "\n  ";
     } else if (col == col_width) {

+ 4 - 5
panda/src/express/multifile.cxx

@@ -1785,8 +1785,7 @@ compare_subfile(int index, const Filename &filename) {
   in2.seekg(0);
   int byte1 = in1->get();
   int byte2 = in2.get();
-  while (!in1->fail() && !in1->eof() &&
-         !in2.fail() && !in2.eof()) {
+  while (!in1->fail() && !in2.fail()) {
     if (byte1 != byte2) {
       close_read_subfile(in1);
       return false;
@@ -2497,7 +2496,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) {
   StreamReader reader(read);
 
   streampos next_index = multifile->word_to_streampos(reader.get_uint32());
-  if (read.eof() || read.fail()) {
+  if (read.fail()) {
     _flags |= SF_index_invalid;
     return 0;
   }
@@ -2529,7 +2528,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) {
   }
 
   size_t name_length = reader.get_uint16();
-  if (read.eof() || read.fail()) {
+  if (read.fail()) {
     _flags |= SF_index_invalid;
     return 0;
   }
@@ -2543,7 +2542,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) {
   _name = string(name_buffer, name_length);
   PANDA_FREE_ARRAY(name_buffer);
 
-  if (read.eof() || read.fail()) {
+  if (read.fail()) {
     _flags |= SF_index_invalid;
     return 0;
   }

+ 2 - 2
panda/src/gobj/texture.cxx

@@ -4158,7 +4158,7 @@ do_read_dds(CData *cdata, istream &in, const string &filename, bool header_only)
     cdata->_num_mipmap_levels_read = cdata->_ram_images.size();
   }
 
-  if (in.fail() || in.eof()) {
+  if (in.fail()) {
     gobj_cat.error()
       << filename << ": truncated DDS file.\n";
     return false;
@@ -4924,7 +4924,7 @@ do_read_ktx(CData *cdata, istream &in, const string &filename, bool header_only)
     }
   }
 
-  if (in.fail() || in.eof()) {
+  if (in.fail()) {
     gobj_cat.error()
       << filename << ": truncated KTX file.\n";
     return false;

+ 4 - 4
panda/src/pnmimage/pnmimage_base.cxx

@@ -120,7 +120,7 @@ int
 pm_readbigshort(istream *in, short *sP) {
   StreamReader reader(in, false);
   *sP = reader.get_be_int16();
-  return (!in->eof() && !in->fail()) ? 0 : -1;
+  return (!in->fail()) ? 0 : -1;
 }
 
 int
@@ -134,7 +134,7 @@ int
 pm_readbiglong(istream *in, long *lP) {
   StreamReader reader(in, false);
   *lP = reader.get_be_int32();
-  return (!in->eof() && !in->fail()) ? 0 : -1;
+  return (!in->fail()) ? 0 : -1;
 }
 
 int
@@ -148,7 +148,7 @@ int
 pm_readlittleshort(istream *in, short *sP) {
   StreamReader reader(in, false);
   *sP = reader.get_int16();
-  return (!in->eof() && !in->fail()) ? 0 : -1;
+  return (!in->fail()) ? 0 : -1;
 }
 
 int
@@ -162,7 +162,7 @@ int
 pm_readlittlelong(istream *in, long *lP) {
   StreamReader reader(in, false);
   *lP = reader.get_int32();
-  return (!in->eof() && !in->fail()) ? 0 : -1;
+  return (!in->fail()) ? 0 : -1;
 }
 
 int

+ 1 - 1
pandatool/src/miscprogs/binToC.cxx

@@ -101,7 +101,7 @@ run() {
   int col = 0;
   unsigned int ch;
   ch = in.get();
-  while (!in.fail() && !in.eof()) {
+  while (!in.fail() && ch != EOF) {
     if (col == 0) {
       out << "\n  ";
     } else if (col == col_width) {