From 084957a6b9e24f69962dd2e3e903917bd85c0763 Mon Sep 17 00:00:00 2001 From: schodet Date: Sun, 27 Mar 2005 23:42:45 +0000 Subject: Ajout de data. --- 2005/i/robert/src/data/data_input.cc | 37 ++++++++++++++ 2005/i/robert/src/data/data_input.hh | 44 ++++++++++++++++ 2005/i/robert/src/data/data_input_file.cc | 46 +++++++++++++++++ 2005/i/robert/src/data/data_input_file.hh | 45 +++++++++++++++++ 2005/i/robert/src/data/data_input_zlib.cc | 84 +++++++++++++++++++++++++++++++ 2005/i/robert/src/data/data_input_zlib.hh | 48 ++++++++++++++++++ 2005/i/robert/src/data/data_output.cc | 34 +++++++++++++ 2005/i/robert/src/data/data_output.hh | 44 ++++++++++++++++ 2005/i/robert/src/data/test_data.cc | 55 ++++++++++++++++++++ 9 files changed, 437 insertions(+) create mode 100644 2005/i/robert/src/data/data_input.cc create mode 100644 2005/i/robert/src/data/data_input.hh create mode 100644 2005/i/robert/src/data/data_input_file.cc create mode 100644 2005/i/robert/src/data/data_input_file.hh create mode 100644 2005/i/robert/src/data/data_input_zlib.cc create mode 100644 2005/i/robert/src/data/data_input_zlib.hh create mode 100644 2005/i/robert/src/data/data_output.cc create mode 100644 2005/i/robert/src/data/data_output.hh create mode 100644 2005/i/robert/src/data/test_data.cc diff --git a/2005/i/robert/src/data/data_input.cc b/2005/i/robert/src/data/data_input.cc new file mode 100644 index 0000000..cfc9b2a --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_input.hh b/2005/i/robert/src/data/data_input.hh new file mode 100644 index 0000000..ae59366 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_input_file.cc b/2005/i/robert/src/data/data_input_file.cc new file mode 100644 index 0000000..75b7500 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_input_file.hh b/2005/i/robert/src/data/data_input_file.hh new file mode 100644 index 0000000..29d22ca --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_input_zlib.cc b/2005/i/robert/src/data/data_input_zlib.cc new file mode 100644 index 0000000..f8f5322 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_input_zlib.hh b/2005/i/robert/src/data/data_input_zlib.hh new file mode 100644 index 0000000..0b7cf5f --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_output.cc b/2005/i/robert/src/data/data_output.cc new file mode 100644 index 0000000..ed04e97 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/data_output.hh b/2005/i/robert/src/data/data_output.hh new file mode 100644 index 0000000..7141892 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/data/test_data.cc b/2005/i/robert/src/data/test_data.cc new file mode 100644 index 0000000..a57b2c3 --- /dev/null +++ b/2005/i/robert/src/data/test_data.cc @@ -0,0 +1,55 @@ +// 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 + +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; +} + -- cgit v1.2.3