From 8f486613be58ced269db1d437e560c16558604e8 Mon Sep 17 00:00:00 2001 From: becquet Date: Thu, 10 May 2007 18:49:20 +0000 Subject: Création de chuck, le programme du robot 2007. --- i/chuck/src/data/Makefile.defs | 16 ++++ i/chuck/src/data/data_buffer.cc | 130 ++++++++++++++++++++++++++ i/chuck/src/data/data_buffer.hh | 83 ++++++++++++++++ i/chuck/src/data/data_circular_buffer.cc | 123 ++++++++++++++++++++++++ i/chuck/src/data/data_circular_buffer.hh | 64 +++++++++++++ i/chuck/src/data/data_input.cc | 37 ++++++++ i/chuck/src/data/data_input.hh | 44 +++++++++ i/chuck/src/data/data_input_file.cc | 46 +++++++++ i/chuck/src/data/data_input_file.hh | 45 +++++++++ i/chuck/src/data/data_input_zlib.cc | 84 +++++++++++++++++ i/chuck/src/data/data_input_zlib.hh | 48 ++++++++++ i/chuck/src/data/data_output.cc | 34 +++++++ i/chuck/src/data/data_output.hh | 44 +++++++++ i/chuck/src/data/test_data.cc | 56 +++++++++++ i/chuck/src/data/test_data_buffer.cc | 77 +++++++++++++++ i/chuck/src/data/test_data_circular_buffer.cc | 92 ++++++++++++++++++ 16 files changed, 1023 insertions(+) create mode 100644 i/chuck/src/data/Makefile.defs create mode 100644 i/chuck/src/data/data_buffer.cc create mode 100644 i/chuck/src/data/data_buffer.hh create mode 100644 i/chuck/src/data/data_circular_buffer.cc create mode 100644 i/chuck/src/data/data_circular_buffer.hh create mode 100644 i/chuck/src/data/data_input.cc create mode 100644 i/chuck/src/data/data_input.hh create mode 100644 i/chuck/src/data/data_input_file.cc create mode 100644 i/chuck/src/data/data_input_file.hh create mode 100644 i/chuck/src/data/data_input_zlib.cc create mode 100644 i/chuck/src/data/data_input_zlib.hh create mode 100644 i/chuck/src/data/data_output.cc create mode 100644 i/chuck/src/data/data_output.hh create mode 100644 i/chuck/src/data/test_data.cc create mode 100644 i/chuck/src/data/test_data_buffer.cc create mode 100644 i/chuck/src/data/test_data_circular_buffer.cc (limited to 'i/chuck/src/data') diff --git a/i/chuck/src/data/Makefile.defs b/i/chuck/src/data/Makefile.defs new file mode 100644 index 0000000..c0fe78d --- /dev/null +++ b/i/chuck/src/data/Makefile.defs @@ -0,0 +1,16 @@ +PROGRAMS += test_data test_data_buffer test_data_circular_buffer + +data_OBJECTS = data_input.o data_input_file.o data_input_zlib.o \ + data_buffer.o data_output.o data_circular_buffer.o -lz + +test_data_OBJECTS = test_data.o $(data_OBJECTS) + +test_data_buffer_OBJECTS = test_data_buffer.o data_buffer.o + +test_data_circular_buffer_OBJECTS = test_data_circular_buffer.o data_circular_buffer.o + +#UTESTS += data + +#data.utest: test_data + +#data_UTEST = ./test_data /tmp/toto.tar.gz > /dev/null diff --git a/i/chuck/src/data/data_buffer.cc b/i/chuck/src/data/data_buffer.cc new file mode 100644 index 0000000..c0d8cef --- /dev/null +++ b/i/chuck/src/data/data_buffer.cc @@ -0,0 +1,130 @@ +// data_buffer.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_buffer.hh" + +#include // std::memcpy +#include // std::runtime_error +#include // std::swap + +static const unsigned min_size = 10; + +/// Constructeur par defaut +DataBuffer::DataBuffer (void) + : size_ (min_size), headPos_ (0), tailPos_ (0), type_ (NoType) +{ + buffer_ = new uint8_t [size_]; +} + +/// Constructeur par recopie. +DataBuffer::DataBuffer (const DataBuffer &d) + : headPos_ (0), type_ (NoType) +{ + unsigned sizeUse = d.tailPos_ - d.headPos_; + size_ = tailPos_ = sizeUse; + type_ = d.type_; + buffer_ = new uint8_t [size_]; + std::memcpy (buffer_, &d.buffer_[headPos_], sizeUse); +} + +/// Constructeur avec données. +DataBuffer::DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated, + dataType_e type) + : size_ (sizeAllocated), headPos_ (0), tailPos_ (size), type_ (type) +{ + if (!data || size > sizeAllocated) + throw std::invalid_argument ("Paramêtres invalides"); + buffer_ = new uint8_t [size_]; + std::memcpy (buffer_, data, tailPos_); +} + +/// Surcharge de l'opérateur d'affectation +DataBuffer & +DataBuffer::operator= (const DataBuffer &d) +{ + DataBuffer b (d); + b.swap (*this); + return *this; +} + +/// Lit des données. Même convention que le read de la libc, mais lance +/// une exception en cas d'erreur. +unsigned +DataBuffer::read (uint8_t *buf, unsigned size) +{ + if (!buf) + throw std::invalid_argument ("Paramêtres invalides"); + unsigned sizeTmp = tailPos_ - headPos_; + // On regarde combien de données peuvent être lues + if (size < sizeTmp) + sizeTmp = size; + std::memcpy (buf, &buffer_[headPos_], sizeTmp); + headPos_ += sizeTmp; + // Si il n'y a plus de données dans le buffer, on recommence de puis 0 + if (headPos_ == tailPos_) + headPos_ = tailPos_ = 0; + return sizeTmp; +} + +/// Écrit des données. Même convention que le write de la libc, mais lance +/// une exception en cas d'erreur. +void +DataBuffer::write (const uint8_t *buf, unsigned size) +{ + // Vérification des paramêtres + if (!buf) + throw std::invalid_argument ("Parametres invalides"); + // Agrandissement du buffer + grow (size); + // Ecriture des données + std::memcpy (&buffer_[tailPos_], buf, size); + tailPos_ += size; +} + +/// Augmente la taille du buffer pour correspondre à size. +void +DataBuffer::grow (const unsigned size) +{ + // Taille restant non utilisé dans le buffer + unsigned tailleLibre = size_ - tailPos_; + // Si la taille du buffer n'est pas suffisante + if (tailleLibre < size) + { + // On augmente de deux fois la taille + DataBuffer d (&buffer_[headPos_], tailPos_ - headPos_, + 2 * size + size_, type_); + d.swap (*this); + } +} + +/// Echange les buffers. +void +DataBuffer::swap (DataBuffer &d) +{ + std::swap (buffer_, d.buffer_); + std::swap (size_, d.size_); + std::swap (headPos_, d.headPos_); + std::swap (tailPos_, d.tailPos_); + std::swap (type_, d.type_); +} diff --git a/i/chuck/src/data/data_buffer.hh b/i/chuck/src/data/data_buffer.hh new file mode 100644 index 0000000..7b71a4d --- /dev/null +++ b/i/chuck/src/data/data_buffer.hh @@ -0,0 +1,83 @@ +#ifndef data_buffer_hh +#define data_buffer_hh +// data_buffer.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "data_input.hh" +#include "data_output.hh" + +#include // uint8_t + +/// Classe contenant un buffer de données accessible en lecture et en +/// écriture. +class DataBuffer : public DataInput, public DataOutput +{ + public: + /// Type de données possible du buffer. + enum dataType_e { + NoType, + Image, + AskImage + }; + private: + /// Buffer de données. + uint8_t *buffer_; + /// Taille alloué en mémoire pour le buffer. + unsigned size_; + /// Position du début et de la fin des données dans le buffer. + unsigned headPos_, tailPos_; + /// Type de données du buffer. + dataType_e type_; + /// Augmente la taille du buffer pour correspondre à size. + void grow (const unsigned size); + public: + /// Constructeur par défaut. + DataBuffer (void); + /// Constructeur par recopie. + DataBuffer (const DataBuffer &d); + /// Constructeur avec données. + DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated, + dataType_e type = NoType); + /// Destructeur par défaut. + ~DataBuffer (void) { delete [] buffer_; } + /// Surcharge de l'opérateur d'affectation + DataBuffer &operator= (const DataBuffer &d); + /// Lit des données. Même convention que le read de la libc, mais lance + /// une exception en cas d'erreur. + unsigned read (uint8_t *buf, unsigned size); + /// Écrit des données. Même convention que le write de la libc, mais lance + /// une exception en cas d'erreur. + void write (const uint8_t *buf, unsigned size); + /// Taille utile du buffer. + unsigned size (void) const { return tailPos_ - headPos_; } + /// Echange les buffers. + void swap (DataBuffer &d); + /// Type de données du buffer. + dataType_e type (void) const { return type_; } + /// Change le type de données du buffer. + void setType (const dataType_e type) { type_ = type; } + /// Empty/clear the content of the buffer. + void clear (void) { tailPos_ = headPos_ = 0; } +}; +#endif // data_buffer_hh diff --git a/i/chuck/src/data/data_circular_buffer.cc b/i/chuck/src/data/data_circular_buffer.cc new file mode 100644 index 0000000..be17526 --- /dev/null +++ b/i/chuck/src/data/data_circular_buffer.cc @@ -0,0 +1,123 @@ +// data_circular_buffer.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_circular_buffer.hh" + +#include // std::memcpy +#include // std::runtime_error +#include // std::swap, std::min + +#include // XXX + +/// Default constructor. +/// Size is the allocated size in memory. +DataCircularBuffer::DataCircularBuffer (unsigned size) + : size_ (size + 1), startPos_ (0), endPos_ (0) +{ + // The + 1 for size is because, in some cases, we will have an empty case. + buffer_ = new uint8_t [size_]; +} + +/// Destructor. +DataCircularBuffer::~DataCircularBuffer (void) +{ + delete [] buffer_; +} + +/// Read data of maximum size in the data pointer (data must be allocated of +/// size). +unsigned +DataCircularBuffer::read (uint8_t *data, unsigned size) +{ + if (!data || !size) + throw std::invalid_argument ("Invalid parameter"); + return internalRead (data, size); +} + +/// Internal read, do the real job. +unsigned +DataCircularBuffer::internalRead (uint8_t *data, unsigned size) +{ + // Size of data to read + unsigned sizeToRead = std::min (maxReadableData (), size); + // Size of data to read for the end of the buffer + unsigned sizeToReadEnd = std::min (size_ - startPos_, sizeToRead); + // Copy end part of buffer + if (data) + std::memcpy (&data[0], &buffer_[startPos_], sizeToReadEnd); + // Need to do the begin part of buffer ? + int sizeToReadStart = sizeToRead - sizeToReadEnd; + if (sizeToReadStart > 0) + { + if (data) + std::memcpy (&data[sizeToReadEnd], &buffer_[0], sizeToReadStart); + // Move start of data + startPos_ = 0 + sizeToReadStart; + } + else + // Move start of data + startPos_ = (startPos_ + sizeToRead) % size_; + return sizeToRead; +} + +/// Write data of size length. +void +DataCircularBuffer::write (const uint8_t *data, unsigned size) +{ + if (!data || !size) + throw std::invalid_argument ("Invalid parameter"); + + // Max size we can write + unsigned sizeToWrite = std::min (size, size_ - 1); + + // Read data before overwrite it if needed + int sizeToRead = sizeToWrite - (size_ - maxReadableData ()) + 1; + if (sizeToRead > 0) + if (internalRead (0, sizeToRead) != (unsigned) sizeToRead) + throw std::runtime_error ("Unexpected error"); + + // Max size we can write until the end of the buffer + unsigned sizeToWriteEnd = std::min (size_ - endPos_, sizeToWrite); + // Write first part. + std::memcpy (&buffer_[endPos_], &data[size - sizeToWrite + 0], sizeToWriteEnd); + // More to write ? + unsigned sizeToWriteStart = sizeToWrite - sizeToWriteEnd; + if (sizeToWriteStart > 0) + { + std::memcpy (&buffer_[0], &data[size - sizeToWrite + sizeToWriteEnd], sizeToWriteStart); + endPos_ = 0 + sizeToWriteStart; + } + else + endPos_ = (endPos_ + sizeToWrite) % size_; +} + +/// Swap data between tow DataCircularBuffer. +void +DataCircularBuffer::swap (DataCircularBuffer &d) +{ + std::swap (buffer_, d.buffer_); + std::swap (size_, d.size_); + std::swap (startPos_, d.startPos_); + std::swap (endPos_, d.endPos_); +} diff --git a/i/chuck/src/data/data_circular_buffer.hh b/i/chuck/src/data/data_circular_buffer.hh new file mode 100644 index 0000000..f08426d --- /dev/null +++ b/i/chuck/src/data/data_circular_buffer.hh @@ -0,0 +1,64 @@ +#ifndef data_circular_buffer_hh +#define data_circular_buffer_hh +// data_circular_buffer.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include "data_input.hh" +#include "data_output.hh" + +#include // uint8_t + +class DataCircularBuffer : public DataInput, public DataOutput +{ + private: + /// Calculate the quantity of maximum data we can read. + unsigned maxReadableData (void) + { return (endPos_ + size_ - startPos_) % size_; } + /// Internal read, do the real job. + unsigned internalRead (uint8_t *data, unsigned size); + /// Buffer. + uint8_t *buffer_; + /// Size of the buffer. + unsigned size_; + /// Start and end positions of data in the buffer. + unsigned startPos_, endPos_; + public: + /// Default constructor. + /// Size is the allocated size in memory. + DataCircularBuffer (unsigned size); + /// Destructor. + ~DataCircularBuffer (void); + /// Read data of maximum size in the data pointer (data must be allocated + /// of size). + unsigned read (uint8_t *data, unsigned size); + /// Write data of size length. + void write (const uint8_t *data, unsigned size); + /// Size of the buffer. + unsigned size (void) const { return size_; } + /// Swap data between tow DataCircularBuffer. + void swap (DataCircularBuffer &d); +}; + +#endif // data_circular_buffer_hh diff --git a/i/chuck/src/data/data_input.cc b/i/chuck/src/data/data_input.cc new file mode 100644 index 0000000..cfc9b2a --- /dev/null +++ b/i/chuck/src/data/data_input.cc @@ -0,0 +1,37 @@ +// data_input.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input.hh" + +/// Lit un caractère. Renvois -1 en cas de fin de fichier. +int +DataInput::getc (void) +{ + uint8_t c; + if (read (&c, 1) == 1) + return c & 0xff; + else + return -1; +} + diff --git a/i/chuck/src/data/data_input.hh b/i/chuck/src/data/data_input.hh new file mode 100644 index 0000000..ae59366 --- /dev/null +++ b/i/chuck/src/data/data_input.hh @@ -0,0 +1,44 @@ +#ifndef data_input_hh +#define data_input_hh +// data_input.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include + +/// Classe d'entrée de données. C'est une sorte de istream simplifié +/// permettant d'accéder à des données en lecture. +class DataInput +{ + public: + /// Destructeur. + virtual ~DataInput (void) { } + /// Lit des données. Même convention que le read de la libc, mais lance + /// une exception en cas d'erreur. + virtual unsigned read (uint8_t *buf, unsigned size) = 0; + /// Lit un caractère. Renvois -1 en cas de fin de fichier. + int getc (void); +}; + +#endif // data_input_hh diff --git a/i/chuck/src/data/data_input_file.cc b/i/chuck/src/data/data_input_file.cc new file mode 100644 index 0000000..75b7500 --- /dev/null +++ b/i/chuck/src/data/data_input_file.cc @@ -0,0 +1,46 @@ +// data_input_file.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input_file.hh" +#include "utils/errno_exception.hh" + +/// Constructeur. +DataInputFile::DataInputFile (const std::string &name) + : name_ (name), file_ (name.c_str ()) +{ + if (!file_) + throw errno_exception (name_, errno); +} + +/// Lit des données. Même convention que le read de la libc, mais lance +/// une exception en cas d'erreur. +unsigned +DataInputFile::read (uint8_t *buf, unsigned size) +{ + file_.read (reinterpret_cast (buf), size); + if (file_.bad ()) + throw errno_exception (name_, errno); + return file_.gcount (); +} + diff --git a/i/chuck/src/data/data_input_file.hh b/i/chuck/src/data/data_input_file.hh new file mode 100644 index 0000000..29d22ca --- /dev/null +++ b/i/chuck/src/data/data_input_file.hh @@ -0,0 +1,45 @@ +#ifndef data_input_file_hh +#define data_input_file_hh +// data_input_file.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input.hh" + +#include +#include + +/// Data depuis un fichier standard. +class DataInputFile : public DataInput +{ + std::string name_; + std::ifstream file_; + public: + /// Constructeur. + DataInputFile (const std::string &name); + /// Lit des données. Même convention que le read de la libc, mais lance + /// une exception en cas d'erreur. + unsigned read (uint8_t *buf, unsigned size); +}; + +#endif // data_input_file_hh diff --git a/i/chuck/src/data/data_input_zlib.cc b/i/chuck/src/data/data_input_zlib.cc new file mode 100644 index 0000000..f8f5322 --- /dev/null +++ b/i/chuck/src/data/data_input_zlib.cc @@ -0,0 +1,84 @@ +// data_input_zlib.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input_zlib.hh" + +#include + +/// Constructeur. +DataInputZlib::DataInputZlib (DataInput &data) + : data_ (data), inBuf_ (0) +{ + // Allocate input buffer. + inBuf_ = new uint8_t[inBufSize_]; + // Initialise the zlib structure. + zStream_.next_in = 0; + zStream_.avail_in = 0; + zStream_.next_out = 0; + zStream_.avail_out = 0; + zStream_.zalloc = 0; + zStream_.zfree = 0; + zStream_.opaque = 0; + if (inflateInit2 (&zStream_, 32 + 15) != Z_OK) + { + delete[] inBuf_; + throw std::runtime_error (zStream_.msg); + } +} + +/// Destructeur. +DataInputZlib::~DataInputZlib (void) +{ + inflateEnd (&zStream_); + delete[] inBuf_; +} + +/// Lit des données. Même convention que le read de la libc, mais lance +/// une exception en cas d'erreur. +unsigned +DataInputZlib::read (uint8_t *buf, unsigned size) +{ + // Set output buffer. + zStream_.next_out = buf; + zStream_.avail_out = size; + // While not satisfied. + while (zStream_.avail_out) + { + // Feed more data. + if (zStream_.avail_in == 0) + { + zStream_.next_in = inBuf_; + zStream_.avail_in = data_.read (inBuf_, inBufSize_); + } + // Uncompress. + int r = inflate (&zStream_, Z_SYNC_FLUSH); + // Check result. + if (r == Z_STREAM_END) + return size - zStream_.avail_out; + else if (r != Z_OK) + throw std::runtime_error (zStream_.msg); + } + return size; +} + diff --git a/i/chuck/src/data/data_input_zlib.hh b/i/chuck/src/data/data_input_zlib.hh new file mode 100644 index 0000000..0b7cf5f --- /dev/null +++ b/i/chuck/src/data/data_input_zlib.hh @@ -0,0 +1,48 @@ +#ifndef data_input_zlib_hh +#define data_input_zlib_hh +// data_input_zlib.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input.hh" + +#include + +/// Data depuis un data compressé en zlib. +class DataInputZlib : public DataInput +{ + DataInput &data_; + uint8_t *inBuf_; + static const unsigned inBufSize_ = 1024; + z_stream zStream_; + public: + /// Constructeur. + DataInputZlib (DataInput &data); + /// Destructeur. + ~DataInputZlib (void); + /// Lit des données. Même convention que le read de la libc, mais lance + /// une exception en cas d'erreur. + unsigned read (uint8_t *buf, unsigned size); +}; + +#endif // data_input_zlib_hh diff --git a/i/chuck/src/data/data_output.cc b/i/chuck/src/data/data_output.cc new file mode 100644 index 0000000..ed04e97 --- /dev/null +++ b/i/chuck/src/data/data_output.cc @@ -0,0 +1,34 @@ +// data_output.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_output.hh" + +/// Écrit un caractère. +void +DataOutput::putc (int c) +{ + uint8_t uc = c; + write (&uc, 1); +} + diff --git a/i/chuck/src/data/data_output.hh b/i/chuck/src/data/data_output.hh new file mode 100644 index 0000000..7141892 --- /dev/null +++ b/i/chuck/src/data/data_output.hh @@ -0,0 +1,44 @@ +#ifndef data_output_hh +#define data_output_hh +// data_output.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include + +/// Classe de sortie de données. C'est une sorte de ostream simplifié +/// permettant d'accéder à des données en écriture. +class DataOutput +{ + public: + /// Destructeur. + virtual ~DataOutput (void) { } + /// Écrit des données. Même convention que le write de la libc, mais lance + /// une exception en cas d'erreur. + virtual void write (const uint8_t *buf, unsigned size) = 0; + /// Écrit un caractère. + void putc (int c); +}; + +#endif // data_output_hh diff --git a/i/chuck/src/data/test_data.cc b/i/chuck/src/data/test_data.cc new file mode 100644 index 0000000..acc57ac --- /dev/null +++ b/i/chuck/src/data/test_data.cc @@ -0,0 +1,56 @@ +// test_data.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_input_file.hh" +#include "data_input_zlib.hh" + +#include +#include +#include + +int +main (int argc, char **argv) +{ + try + { + if (argc != 2) + throw std::runtime_error ("Syntax: test_data file"); + DataInputFile f (argv[1]); + DataInputZlib z (f); + uint8_t buf[1024]; + int r = z.read (buf, 1024); + while (r != 0) + { + write (1, buf, r); + r = z.read (buf, r); + } + } + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} + diff --git a/i/chuck/src/data/test_data_buffer.cc b/i/chuck/src/data/test_data_buffer.cc new file mode 100644 index 0000000..36095ab --- /dev/null +++ b/i/chuck/src/data/test_data_buffer.cc @@ -0,0 +1,77 @@ +// test_data_buffer.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "data_buffer.hh" + +#include +#include +#include + +void +check_data (uint8_t *b1, uint8_t *b2, unsigned size) +{ + for (unsigned i = 0; i < size; i ++) + if (b1[i] != b2[i]) + throw std::runtime_error ("Erreur, buffers invalides"); +} + +int +main (int argc, char **argv) +{ + try + { + DataBuffer d; + uint8_t bufW[100]; + uint8_t bufR[150]; + // Remplissage des buffers de test + memset (bufW, 42, 100); + memset (bufR, 1, 150); + // On en écrit 50 + d.write (bufW, 50); + // On en lit 42 + if (d.read (bufR, 42) == 42) + check_data (bufW, bufR, 42); + else + throw std::runtime_error ("Erreur de taille de données"); + // On en re écrit 50 + d.write (&bufW[50], 50); + unsigned pos = 42; + unsigned status; + // On lit jusqu'à la fin + do + { + status = d.read (&bufR[pos], 10); + if (status) + check_data (&bufW[pos], &bufR[pos], status); + pos += status; + } while (status); + // On vérifie que tout a été bien lue + } + catch (const std::runtime_error &r) + { + std::cerr << r.what () << std::endl; + return 1; + } + return 0; +} diff --git a/i/chuck/src/data/test_data_circular_buffer.cc b/i/chuck/src/data/test_data_circular_buffer.cc new file mode 100644 index 0000000..9bdc063 --- /dev/null +++ b/i/chuck/src/data/test_data_circular_buffer.cc @@ -0,0 +1,92 @@ +// test_data_circular_buffer.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_circular_buffer.hh" + +#include +#include +#include +#include + +#define MAX_SIZE_BUFF 101 +#define MAX_SIZE_CIRC 99 + +unsigned +randVal (double max) +{ + return 1 + (int) (max * std::rand () / (RAND_MAX + 1.0)); +} + +int +main (void) +{ + // Init random + std::srand (std::time (NULL)); + try + { + DataCircularBuffer d (MAX_SIZE_CIRC); + uint8_t buffOrig[MAX_SIZE_BUFF]; + uint8_t buffRes[MAX_SIZE_BUFF]; + + int i, compt, tmp; + // Fill buffer + for (i = 0; i < MAX_SIZE_BUFF; i++) + memset (&buffOrig[i], i, 1); + + // Fill circular buffer + compt = 0; + for (i = 0; i < 42; i++) + { + tmp = randVal (MAX_SIZE_BUFF - compt); + d.write (&buffOrig[compt], tmp); + compt = (tmp + compt) % MAX_SIZE_BUFF; + } + + // Read + compt = 0; + unsigned res; + do + { + tmp = randVal (7.); + res = d.read (&buffRes[compt], tmp); + compt += tmp; + } while (res != 0); + + // Compare + for (i = 0; i < MAX_SIZE_CIRC - 1; i++) + { + if (buffRes [i] != buffOrig [(buffRes[0] +i) % MAX_SIZE_BUFF]) + throw std::runtime_error ("Test failed !"); + } + + } + + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} + -- cgit v1.2.3