Panda3D
interrogate_datafile.cxx
1 // Filename: interrogate_datafile.cxx
2 // Created by: drose (09Aug00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "interrogate_datafile.h"
16 
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: idf_output_string
20 // Description: Writes the indicated string to the output file. Uses
21 // the given whitespace character to separate the
22 // string's length and its contents.
23 ////////////////////////////////////////////////////////////////////
24 void
25 idf_output_string(ostream &out, const string &str, char whitespace) {
26  out << str.length() << whitespace;
27  if (!str.empty()) {
28  out << str << whitespace;
29  }
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: idf_input_string
34 // Description: Reads the given string from the input file, as
35 // previously written by output_string().
36 ////////////////////////////////////////////////////////////////////
37 void
38 idf_input_string(istream &in, string &str) {
39  int length;
40  in >> length;
41  if (in.fail()) {
42  return;
43  }
44 
45  // Skip one character of whitespace, and then read the string.
46  in.get();
47  str = "";
48  while (length > 0) {
49  str += in.get();
50  length--;
51  }
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: idf_output_string
56 // Description: Writes the indicated string to the output file. Uses
57 // the given whitespace character to separate the
58 // string's length and its contents.
59 ////////////////////////////////////////////////////////////////////
60 void
61 idf_output_string(ostream &out, const char *str, char whitespace) {
62  if (str == (const char *)NULL) {
63  out << "0 ";
64  } else {
65  out << strlen(str) << whitespace;
66  if (str[0] != '\0') {
67  out << str << whitespace;
68  }
69  }
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: idf_input_string
74 // Description: Reads the given string from the input file, as
75 // previously written by output_string().
76 ////////////////////////////////////////////////////////////////////
77 void
78 idf_input_string(istream &in, const char *&str) {
79  int length;
80  in >> length;
81  if (in.fail()) {
82  return;
83  }
84 
85  if (length == 0) {
86  // Don't change the string if the input length is zero.
87  return;
88  }
89 
90  // Skip one character of whitespace, and then read the string.
91  in.get();
92  char *readstr = new char[length + 1];
93  int p = 0;
94  while (p < length) {
95  readstr[p] = in.get();
96  p++;
97  }
98  readstr[p] = '\0';
99 
100  str = readstr;
101 }