Panda3D
|
00001 // Filename: interrogate_datafile.cxx 00002 // Created by: drose (09Aug00) 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 "interrogate_datafile.h" 00016 00017 00018 //////////////////////////////////////////////////////////////////// 00019 // Function: idf_output_string 00020 // Description: Writes the indicated string to the output file. Uses 00021 // the given whitespace character to separate the 00022 // string's length and its contents. 00023 //////////////////////////////////////////////////////////////////// 00024 void 00025 idf_output_string(ostream &out, const string &str, char whitespace) { 00026 out << str.length() << whitespace; 00027 if (!str.empty()) { 00028 out << str << whitespace; 00029 } 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: idf_input_string 00034 // Description: Reads the given string from the input file, as 00035 // previously written by output_string(). 00036 //////////////////////////////////////////////////////////////////// 00037 void 00038 idf_input_string(istream &in, string &str) { 00039 int length; 00040 in >> length; 00041 if (in.fail()) { 00042 return; 00043 } 00044 00045 // Skip one character of whitespace, and then read the string. 00046 in.get(); 00047 str = ""; 00048 while (length > 0) { 00049 str += in.get(); 00050 length--; 00051 } 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: idf_output_string 00056 // Description: Writes the indicated string to the output file. Uses 00057 // the given whitespace character to separate the 00058 // string's length and its contents. 00059 //////////////////////////////////////////////////////////////////// 00060 void 00061 idf_output_string(ostream &out, const char *str, char whitespace) { 00062 if (str == (const char *)NULL) { 00063 out << "0 "; 00064 } else { 00065 out << strlen(str) << whitespace; 00066 if (str[0] != '\0') { 00067 out << str << whitespace; 00068 } 00069 } 00070 } 00071 00072 //////////////////////////////////////////////////////////////////// 00073 // Function: idf_input_string 00074 // Description: Reads the given string from the input file, as 00075 // previously written by output_string(). 00076 //////////////////////////////////////////////////////////////////// 00077 void 00078 idf_input_string(istream &in, const char *&str) { 00079 int length; 00080 in >> length; 00081 if (in.fail()) { 00082 return; 00083 } 00084 00085 if (length == 0) { 00086 // Don't change the string if the input length is zero. 00087 return; 00088 } 00089 00090 // Skip one character of whitespace, and then read the string. 00091 in.get(); 00092 char *readstr = new char[length + 1]; 00093 int p = 0; 00094 while (p < length) { 00095 readstr[p] = in.get(); 00096 p++; 00097 } 00098 readstr[p] = '\0'; 00099 00100 str = readstr; 00101 }