Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/net/datagram_ui.cxx

Go to the documentation of this file.
00001 // Filename: datagram_ui.cxx
00002 // Created by:  drose (09Feb00)
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 "datagram_ui.h"
00020 #include "datagramIterator.h"
00021 
00022 #include <string>
00023 #include <stdlib.h>
00024 #include <ctype.h>
00025 
00026 enum DatagramElement {
00027   DE_int32,
00028   DE_float64,
00029   DE_string,
00030   DE_end
00031 };
00032 
00033 istream &
00034 operator >> (istream &in, NetDatagram &datagram) {
00035   datagram.clear();
00036 
00037   // First, read a line of text.
00038   string line;
00039   getline(in, line);
00040 
00041   // Now parse the text.
00042   size_t p = 0;
00043   while (p < line.length()) {
00044     // Skip whitespace
00045     while (p < line.length() && isspace(line[p])) {
00046       p++;
00047     }
00048 
00049     // What have we got?
00050     if (p < line.length()) {
00051       if (isdigit(line[p]) || line[p] == '-') {
00052         // A number.
00053         size_t start = p;
00054         p++;
00055         while (p < line.length() && isdigit(line[p])) {
00056           p++;
00057         }
00058         if (p < line.length() && line[p] == '.') {
00059           // A floating-point number.
00060           p++;
00061           while (p < line.length() && isdigit(line[p])) {
00062             p++;
00063           }
00064           double num = atof(line.substr(start, p - start).c_str());
00065           datagram.add_int8(DE_float64);
00066           datagram.add_float64(num);
00067         } else {
00068           // An integer.
00069           int num = atoi(line.substr(start, p - start).c_str());
00070           datagram.add_int8(DE_int32);
00071           datagram.add_int32(num);
00072         }
00073 
00074       } else if (line[p] == '"') {
00075         // A quoted string.
00076         p++;
00077         size_t start = p;
00078         while (p < line.length() && line[p] != '"') {
00079           p++;
00080         }
00081         string str = line.substr(start, p - start);
00082         datagram.add_int8(DE_string);
00083         datagram.add_string(str);
00084         p++;
00085 
00086       } else {
00087         // An unquoted string.
00088         size_t start = p;
00089         while (p < line.length() && !isspace(line[p])) {
00090           p++;
00091         }
00092         string str = line.substr(start, p - start);
00093         datagram.add_int8(DE_string);
00094         datagram.add_string(str);
00095       }
00096     }
00097   }
00098   datagram.add_int8(DE_end);
00099   return in;
00100 }
00101 
00102 ostream &
00103 operator << (ostream &out, const NetDatagram &datagram) {
00104   DatagramIterator di(datagram);
00105 
00106   DatagramElement de = (DatagramElement)di.get_int8();
00107   while (de != DE_end) {
00108     switch (de) {
00109     case DE_int32:
00110       out << di.get_int32() << " ";
00111       break;
00112 
00113     case DE_float64:
00114       out << di.get_float64() << " ";
00115       break;
00116 
00117     case DE_string:
00118       out << "\"" << di.get_string() << "\" ";
00119       break;
00120 
00121     default:
00122       out << "(invalid datagram)";
00123       return out;
00124     }
00125     de = (DatagramElement)di.get_int8();
00126   }
00127   return out;
00128 }

Generated on Fri May 2 00:40:34 2003 for Panda by doxygen1.3