00001 // Filename: streamReader.cxx 00002 // Created by: drose (04Aug02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "streamReader.h" 00016 00017 00018 //////////////////////////////////////////////////////////////////// 00019 // Function: StreamReader::get_string 00020 // Access: Published 00021 // Description: Extracts a variable-length string. 00022 //////////////////////////////////////////////////////////////////// 00023 string StreamReader:: 00024 get_string() { 00025 nassertr(!_in->eof() && !_in->fail(), string()); 00026 00027 // First, get the length of the string 00028 size_t size = get_uint16(); 00029 00030 char *buffer = (char *)alloca(size); 00031 _in->read(buffer, size); 00032 size_t read_bytes = _in->gcount(); 00033 return string(buffer, read_bytes); 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: StreamReader::get_string32 00038 // Access: Published 00039 // Description: Extracts a variable-length string with a 32-bit 00040 // length field. 00041 //////////////////////////////////////////////////////////////////// 00042 string StreamReader:: 00043 get_string32() { 00044 nassertr(!_in->eof() && !_in->fail(), string()); 00045 00046 // First, get the length of the string 00047 size_t size = get_uint32(); 00048 00049 char *buffer = (char *)PANDA_MALLOC_ARRAY(size); 00050 _in->read(buffer, size); 00051 size_t read_bytes = _in->gcount(); 00052 string result(buffer, read_bytes); 00053 PANDA_FREE_ARRAY(buffer); 00054 return result; 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: StreamReader::get_z_string 00059 // Access: Published 00060 // Description: Extracts a variable-length string, as a 00061 // NULL-terminated string. 00062 //////////////////////////////////////////////////////////////////// 00063 string StreamReader:: 00064 get_z_string() { 00065 nassertr(!_in->eof() && !_in->fail(), string()); 00066 00067 string result; 00068 int ch = _in->get(); 00069 while (!_in->eof() && !_in->fail() && ch != '\0') { 00070 result += ch; 00071 ch = _in->get(); 00072 } 00073 00074 return result; 00075 } 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function: StreamReader::get_fixed_string 00079 // Access: Published 00080 // Description: Extracts a fixed-length string. However, if a zero 00081 // byte occurs within the string, it marks the end of 00082 // the string. 00083 //////////////////////////////////////////////////////////////////// 00084 string StreamReader:: 00085 get_fixed_string(size_t size) { 00086 nassertr(!_in->eof() && !_in->fail(), string()); 00087 00088 char *buffer = (char *)alloca(size); 00089 _in->read(buffer, size); 00090 size_t read_bytes = _in->gcount(); 00091 string result(buffer, read_bytes); 00092 00093 size_t zero_byte = result.find('\0'); 00094 return result.substr(0, zero_byte); 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: StreamReader::skip_bytes 00099 // Access: Published 00100 // Description: Skips over the indicated number of bytes in the 00101 // stream. 00102 //////////////////////////////////////////////////////////////////// 00103 void StreamReader:: 00104 skip_bytes(size_t size) { 00105 nassertv(!_in->eof() && !_in->fail()); 00106 nassertv((int)size >= 0); 00107 00108 while (size > 0) { 00109 _in->get(); 00110 size--; 00111 } 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: StreamReader::extract_bytes 00116 // Access: Published 00117 // Description: Extracts the indicated number of bytes in the 00118 // stream and returns them as a string. Returns empty 00119 // string at end-of-file. 00120 //////////////////////////////////////////////////////////////////// 00121 string StreamReader:: 00122 extract_bytes(size_t size) { 00123 if (_in->eof() || _in->fail()) { 00124 return string(); 00125 } 00126 00127 char *buffer = (char *)alloca(size); 00128 _in->read(buffer, size); 00129 size_t read_bytes = _in->gcount(); 00130 return string(buffer, read_bytes); 00131 } 00132 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function: StreamReader::readline 00136 // Access: Published 00137 // Description: Assumes the stream represents a text file, and 00138 // extracts one line up to and including the trailing 00139 // newline character. Returns empty string when the end 00140 // of file is reached. 00141 // 00142 // The interface here is intentionally designed to be 00143 // similar to that for Python's File.readline() 00144 // function. 00145 //////////////////////////////////////////////////////////////////// 00146 string StreamReader:: 00147 readline() { 00148 string line; 00149 int ch = _in->get(); 00150 while (!_in->eof() && !_in->fail()) { 00151 line += ch; 00152 if (ch == '\n') { 00153 // Here's the newline character. 00154 return line; 00155 } 00156 ch = _in->get(); 00157 } 00158 00159 return line; 00160 } 00161