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