From 9523acd2d7be7b8bbbf6d8c786c68c91f9fcc700 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Wed, 19 Nov 2003 02:26:49 +0000 Subject: [PATCH 1/1] - Se agrega una funcion templateada to() para convertir de un tipo de dato arbitrario a otro a traves de un stringstream (similar a String::to()). - Se usa la nueva funcion en varios lugares. --- Server/include/plaqui/server/string.h | 15 +++++++++++++++ Server/src/httpmessage.cpp | 14 ++++---------- Server/src/string.cpp | 15 --------------- Server/tests/client_test.cpp | 6 ++---- Server/tests/receiver_test.cpp | 5 ++--- Server/tests/server_test.cpp | 5 ++--- 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/Server/include/plaqui/server/string.h b/Server/include/plaqui/server/string.h index 41e83ac..f29e1b4 100644 --- a/Server/include/plaqui/server/string.h +++ b/Server/include/plaqui/server/string.h @@ -36,6 +36,21 @@ namespace PlaQui { namespace Server { + /** + * Convierte de un tipo a otro (de p1 a p2) a través de un stringstream. + * + * \param p1 Parámetro origen. + * \param p1 Parámetro destino, al que se quiere convertir. + * + * \return Referencia a p2. + */ + template < class T1, class T2 > static T2& to(const T1& p1, T2& p2) { + std::stringstream ss(p1); + ss << p1; + ss >> p2; + return p2; + } + /// Conexión. class String: public std::string { diff --git a/Server/src/httpmessage.cpp b/Server/src/httpmessage.cpp index 578f563..c97da08 100644 --- a/Server/src/httpmessage.cpp +++ b/Server/src/httpmessage.cpp @@ -51,17 +51,13 @@ HTTPMessage::HTTPMessage(const string& _body, const string& _version): cerr << __FILE__ << ": version = " << version << " | body (" << _body.length() << ") = " << _body << endl; #endif // DEBUG + headers["Accept-Ranges"] = "bytes"; set_body(_body); } void HTTPMessage::set_body(const string& _body) { body = _body; - if (body.length()) { - stringstream ss; // TODO ver forma mas linda de convertir - ss << (body.length()); // FIXME No se por que tengo que sumarle 1. - headers["Accept-Ranges"] = "bytes"; - headers["Content-Length"] = ss.str(); - } + headers["Content-Length"] = String().from(body.length()); } const string& HTTPMessage::get_body(void) const { @@ -77,16 +73,14 @@ istream& operator>>(istream& is, HTTPMessage& m) { String sbuf(buf); sbuf.trim(); if (sbuf.length()) { - stringstream ss; - ss << sbuf; + stringstream ss(sbuf); ss >> m.headers; // Fin de las cabeceras. } else { // Hay Content-Length, entonces hay body (no respeta RFC AFAIK). if (m.headers.find("Content-Length") != m.headers.end()) { - stringstream ss(m.headers["Content-Length"]); streamsize size; - ss >> size; + to(m.headers["Content-Length"], size); char* const buf2 = new char[size+1]; if (is.readsome(buf2, size)) { m.body = buf2; diff --git a/Server/src/string.cpp b/Server/src/string.cpp index 8f9b2db..97dd19f 100644 --- a/Server/src/string.cpp +++ b/Server/src/string.cpp @@ -93,21 +93,6 @@ String String::join(const vector& v, const string& sep) { return ss.str(); } -/* -template < class T > T& String::to(T& p) const { - stringstream ss(*this); - ss >> p; - return p; -} - -template < class T > String& String::from(const T& p) { - stringstream ss; - ss << p; - ss >> (*this); - return *this; -} -*/ - } // namespace Server } // namespace PlaQui diff --git a/Server/tests/client_test.cpp b/Server/tests/client_test.cpp index c5fc75f..88027e1 100644 --- a/Server/tests/client_test.cpp +++ b/Server/tests/client_test.cpp @@ -28,7 +28,6 @@ #include "plaqui/server/controlclient.h" #include "plaqui/server/string.h" #include -#include #include #include @@ -76,11 +75,10 @@ int main(int argc, char* argv[]) { // Obtengo host. host = argv[1]; } + // Obtengo puerto. Connection::Port port = 7522; if (argc > 2) { - // Obtengo puerto. - stringstream str(argv[2]); - str >> port; + to(argv[2], port); } // Inicializa threads. diff --git a/Server/tests/receiver_test.cpp b/Server/tests/receiver_test.cpp index 6bb5f57..f1e86b9 100644 --- a/Server/tests/receiver_test.cpp +++ b/Server/tests/receiver_test.cpp @@ -26,8 +26,8 @@ // #include "plaqui/server/receiver.h" +#include "plaqui/server/string.h" #include -#include #include using namespace std; @@ -51,8 +51,7 @@ int main(int argc, char* argv[]) { Connection::Port port = 7528; if (argc > 1) { // Obtengo puerto. - stringstream str(argv[1]); - str >> port; + to(argv[1], port); } string host = "localhost"; if (argc > 2) { diff --git a/Server/tests/server_test.cpp b/Server/tests/server_test.cpp index 2cd23dd..bdb6190 100644 --- a/Server/tests/server_test.cpp +++ b/Server/tests/server_test.cpp @@ -27,9 +27,9 @@ #include "plaqui/server/connection.h" #include "plaqui/server/server.h" +#include "plaqui/server/string.h" #include #include -#include #include using namespace std; @@ -53,8 +53,7 @@ int main(int argc, char* argv[]) { // Si tiene 2 parámetros. if (argc > 2) { // Obtengo puerto. - stringstream ss(argv[2]); - ss >> port; + to(argv[2], port); } } -- 2.43.0