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