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