Panda3D

datagram_ui.cxx

00001 // Filename: datagram_ui.cxx
00002 // Created by:  drose (09Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "datagram_ui.h"
00016 #include "datagramIterator.h"
00017 #include "pstrtod.h"
00018 
00019 #include <string>
00020 #include <stdlib.h>
00021 #include <ctype.h>
00022 
00023 enum DatagramElement {
00024   DE_int32,
00025   DE_float64,
00026   DE_string,
00027   DE_end
00028 };
00029 
00030 istream &
00031 operator >> (istream &in, NetDatagram &datagram) {
00032   datagram.clear();
00033 
00034   // First, read a line of text.
00035   string line;
00036   getline(in, line);
00037 
00038   // Now parse the text.
00039   size_t p = 0;
00040   while (p < line.length()) {
00041     // Skip whitespace
00042     while (p < line.length() && isspace(line[p])) {
00043       p++;
00044     }
00045 
00046     // What have we got?
00047     if (p < line.length()) {
00048       if (isdigit(line[p]) || line[p] == '-') {
00049         // A number.
00050         size_t start = p;
00051         p++;
00052         while (p < line.length() && isdigit(line[p])) {
00053           p++;
00054         }
00055         if (p < line.length() && line[p] == '.') {
00056           // A floating-point number.
00057           p++;
00058           while (p < line.length() && isdigit(line[p])) {
00059             p++;
00060           }
00061           double num = patof(line.substr(start, p - start).c_str());
00062           datagram.add_int8(DE_float64);
00063           datagram.add_float64(num);
00064         } else {
00065           // An integer.
00066           int num = atoi(line.substr(start, p - start).c_str());
00067           datagram.add_int8(DE_int32);
00068           datagram.add_int32(num);
00069         }
00070 
00071       } else if (line[p] == '"') {
00072         // A quoted string.
00073         p++;
00074         size_t start = p;
00075         while (p < line.length() && line[p] != '"') {
00076           p++;
00077         }
00078         string str = line.substr(start, p - start);
00079         datagram.add_int8(DE_string);
00080         datagram.add_string(str);
00081         p++;
00082 
00083       } else {
00084         // An unquoted string.
00085         size_t start = p;
00086         while (p < line.length() && !isspace(line[p])) {
00087           p++;
00088         }
00089         string str = line.substr(start, p - start);
00090         datagram.add_int8(DE_string);
00091         datagram.add_string(str);
00092       }
00093     }
00094   }
00095   datagram.add_int8(DE_end);
00096   return in;
00097 }
00098 
00099 ostream &
00100 operator << (ostream &out, const NetDatagram &datagram) {
00101   DatagramIterator di(datagram);
00102 
00103   DatagramElement de = (DatagramElement)di.get_int8();
00104   while (de != DE_end) {
00105     switch (de) {
00106     case DE_int32:
00107       out << di.get_int32() << " ";
00108       break;
00109 
00110     case DE_float64:
00111       out << di.get_float64() << " ";
00112       break;
00113 
00114     case DE_string:
00115       out << "\"" << di.get_string() << "\" ";
00116       break;
00117 
00118     default:
00119       out << "(invalid datagram)";
00120       return out;
00121     }
00122     de = (DatagramElement)di.get_int8();
00123   }
00124   return out;
00125 }
 All Classes Functions Variables Enumerations