Panda3D
 All Classes Functions Variables Enumerations
datagram_ui.cxx
1 // Filename: datagram_ui.cxx
2 // Created by: drose (09Feb00)
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 "datagram_ui.h"
16 #include "datagramIterator.h"
17 #include "pstrtod.h"
18 
19 #include <string>
20 #include <stdlib.h>
21 #include <ctype.h>
22 
23 enum DatagramElement {
24  DE_int32,
25  DE_float64,
26  DE_string,
27  DE_end
28 };
29 
30 istream &
31 operator >> (istream &in, NetDatagram &datagram) {
32  datagram.clear();
33 
34  // First, read a line of text.
35  string line;
36  getline(in, line);
37 
38  // Now parse the text.
39  size_t p = 0;
40  while (p < line.length()) {
41  // Skip whitespace
42  while (p < line.length() && isspace(line[p])) {
43  p++;
44  }
45 
46  // What have we got?
47  if (p < line.length()) {
48  if (isdigit(line[p]) || line[p] == '-') {
49  // A number.
50  size_t start = p;
51  p++;
52  while (p < line.length() && isdigit(line[p])) {
53  p++;
54  }
55  if (p < line.length() && line[p] == '.') {
56  // A floating-point number.
57  p++;
58  while (p < line.length() && isdigit(line[p])) {
59  p++;
60  }
61  double num = patof(line.substr(start, p - start).c_str());
62  datagram.add_int8(DE_float64);
63  datagram.add_float64(num);
64  } else {
65  // An integer.
66  int num = atoi(line.substr(start, p - start).c_str());
67  datagram.add_int8(DE_int32);
68  datagram.add_int32(num);
69  }
70 
71  } else if (line[p] == '"') {
72  // A quoted string.
73  p++;
74  size_t start = p;
75  while (p < line.length() && line[p] != '"') {
76  p++;
77  }
78  string str = line.substr(start, p - start);
79  datagram.add_int8(DE_string);
80  datagram.add_string(str);
81  p++;
82 
83  } else {
84  // An unquoted string.
85  size_t start = p;
86  while (p < line.length() && !isspace(line[p])) {
87  p++;
88  }
89  string str = line.substr(start, p - start);
90  datagram.add_int8(DE_string);
91  datagram.add_string(str);
92  }
93  }
94  }
95  datagram.add_int8(DE_end);
96  return in;
97 }
98 
99 ostream &
100 operator << (ostream &out, const NetDatagram &datagram) {
101  DatagramIterator di(datagram);
102 
103  DatagramElement de = (DatagramElement)di.get_int8();
104  while (de != DE_end) {
105  switch (de) {
106  case DE_int32:
107  out << di.get_int32() << " ";
108  break;
109 
110  case DE_float64:
111  out << di.get_float64() << " ";
112  break;
113 
114  case DE_string:
115  out << "\"" << di.get_string() << "\" ";
116  break;
117 
118  default:
119  out << "(invalid datagram)";
120  return out;
121  }
122  de = (DatagramElement)di.get_int8();
123  }
124  return out;
125 }
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:43
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
Definition: datagram.I:228
void add_int8(PN_int8 value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:128
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition: netDatagram.cxx:93
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:159
A class to retrieve the individual data elements previously stored in a Datagram. ...