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