summaryrefslogtreecommitdiff
path: root/2005
diff options
context:
space:
mode:
authordufourj2005-04-26 19:02:47 +0000
committerdufourj2005-04-26 19:02:47 +0000
commit62c6ae5cd97f7fa85709eba90a01a7cddd6685a5 (patch)
treeed8c254f69e29ef638f6cb0515e2cd799e2c80a6 /2005
parent5356e305d3c8c8f0691fc1c1ad1f1d7b02941e1e (diff)
Ajout de la classe DataBuffer : cette classe permet de gérer un buffer de
données accessible en lecture comme en écriture.
Diffstat (limited to '2005')
-rw-r--r--2005/i/robert/src/data/Makefile.defs8
-rw-r--r--2005/i/robert/src/data/data_buffer.cc126
-rw-r--r--2005/i/robert/src/data/data_buffer.hh67
-rw-r--r--2005/i/robert/src/data/test_data_buffer.cc77
4 files changed, 276 insertions, 2 deletions
diff --git a/2005/i/robert/src/data/Makefile.defs b/2005/i/robert/src/data/Makefile.defs
index 270db4d..31698f1 100644
--- a/2005/i/robert/src/data/Makefile.defs
+++ b/2005/i/robert/src/data/Makefile.defs
@@ -1,8 +1,12 @@
-PROGRAMS += test_data
+PROGRAMS += test_data test_data_buffer
data_OBJECTS = data_input.o data_input_file.o data_input_zlib.o \
- data_output.o -lz
+ data_buffer.o data_output.o -lz
test_data_OBJECTS = test_data.o $(data_OBJECTS)
+test_data_buffer_OBJECTS = test_data_buffer.o data_buffer.o
+
test_data: $(test_data_OBJECTS)
+
+test_data_buffer: $(test_data_buffer_OBJECTS)
diff --git a/2005/i/robert/src/data/data_buffer.cc b/2005/i/robert/src/data/data_buffer.cc
new file mode 100644
index 0000000..4fc7c56
--- /dev/null
+++ b/2005/i/robert/src/data/data_buffer.cc
@@ -0,0 +1,126 @@
+// data_buffer.cc
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2005 Dufour Jrmy
+//
+// 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 <cstring> // std::memcpy
+#include <stdexcept> // std::runtime_error
+#include <algorithm> // std::swap
+
+static const unsigned min_size = 10;
+
+/// Constructeur par defaut
+DataBuffer::DataBuffer (void)
+ : size_ (min_size), headPos_ (0), tailPos_ (0)
+{
+ buffer_ = new uint8_t [size_];
+}
+
+/// Constructeur par recopie.
+DataBuffer::DataBuffer (const DataBuffer &d)
+ : headPos_ (0)
+{
+ unsigned sizeUse = d.tailPos_ - d.headPos_;
+ size_ = tailPos_ = sizeUse;
+ buffer_ = new uint8_t [size_];
+ std::memcpy (buffer_, &d.buffer_[headPos_], sizeUse);
+}
+
+/// Constructeur avec donnes.
+DataBuffer::DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated)
+ : size_ (sizeAllocated), headPos_ (0), tailPos_ (size)
+{
+ if (!data || size > sizeAllocated)
+ throw std::invalid_argument ("Paramtres invalides");
+ buffer_ = new uint8_t [size_];
+ std::memcpy (buffer_, data, tailPos_);
+}
+
+/// Surcharge de l'oprateur d'affectation
+DataBuffer &
+DataBuffer::operator= (const DataBuffer &d)
+{
+ DataBuffer b (d);
+ b.swap (*this);
+ return *this;
+}
+
+/// Lit des donnes. Mme 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 ("Paramtres invalides");
+ unsigned sizeTmp = tailPos_ - headPos_;
+ // On regarde combien de donnes peuvent tre lues
+ if (size < sizeTmp)
+ sizeTmp = size;
+ std::memcpy (buf, &buffer_[headPos_], sizeTmp);
+ headPos_ += sizeTmp;
+ // Si il n'y a plus de donnes dans le buffer, on recommence de puis 0
+ if (headPos_ == tailPos_)
+ headPos_ = tailPos_ = 0;
+ return sizeTmp;
+}
+
+/// crit des donnes. Mme convention que le write de la libc, mais lance
+/// une exception en cas d'erreur.
+void
+DataBuffer::write (const uint8_t *buf, unsigned size)
+{
+ // Vrification des paramtres
+ if (!buf)
+ throw std::invalid_argument ("Parametres invalides");
+ // Agrandissement du buffer
+ grow (size);
+ // Ecriture des donnes
+ 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_);
+ 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_);
+}
diff --git a/2005/i/robert/src/data/data_buffer.hh b/2005/i/robert/src/data/data_buffer.hh
new file mode 100644
index 0000000..11724d8
--- /dev/null
+++ b/2005/i/robert/src/data/data_buffer.hh
@@ -0,0 +1,67 @@
+#ifndef data_buffer_hh
+#define data_buffer_hh
+// data_buffer.hh
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2005 Dufour Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+
+#include "data_input.hh"
+#include "data_output.hh"
+
+#include <stdint.h> // uint8_t
+
+/// Classe contenant un buffer de donnes accessible en lecture et en
+/// criture.
+class DataBuffer : public DataInput, public DataOutput
+{
+ /// Buffer de donnes.
+ uint8_t *buffer_;
+ /// Taille allou en mmoire pour le buffer.
+ unsigned size_;
+ /// Position du dbut et de la fin des donnes dans le buffer.
+ unsigned headPos_, tailPos_;
+ public:
+ /// Constructeur par dfaut.
+ DataBuffer (void);
+ /// Constructeur par recopie.
+ DataBuffer (const DataBuffer &d);
+ /// Constructeur avec donnes.
+ DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated);
+ /// Destructeur par dfaut.
+ ~DataBuffer (void) { delete [] buffer_; }
+ /// Surcharge de l'oprateur d'affectation
+ DataBuffer &operator= (const DataBuffer &d);
+ /// Lit des donnes. Mme convention que le read de la libc, mais lance
+ /// une exception en cas d'erreur.
+ unsigned read (uint8_t *buf, unsigned size);
+ /// crit des donnes. Mme 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_; }
+ private:
+ /// Augmente la taille du buffer pour correspondre size.
+ void grow (const unsigned size);
+ /// Echange les buffers.
+ void swap (DataBuffer &d);
+};
+#endif // data_buffer_hh
diff --git a/2005/i/robert/src/data/test_data_buffer.cc b/2005/i/robert/src/data/test_data_buffer.cc
new file mode 100644
index 0000000..36095ab
--- /dev/null
+++ b/2005/i/robert/src/data/test_data_buffer.cc
@@ -0,0 +1,77 @@
+// test_data_buffer.cc
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2005 Dufour Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+
+#include "data_buffer.hh"
+
+#include <iostream>
+#include <stdexcept>
+#include <unistd.h>
+
+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 donnes");
+ // 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 vrifie que tout a t bien lue
+ }
+ catch (const std::runtime_error &r)
+ {
+ std::cerr << r.what () << std::endl;
+ return 1;
+ }
+ return 0;
+}