Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
  3.  * It is copyright by its individual contributors, as recorded in the
  4.  * project's Git history.  See COPYING.txt at the top level for license
  5.  * terms and a link to the Git history.
  6.  */
  7. #pragma once
  8.  
  9. #include <stdexcept>
  10. #include "physfsx.h"
  11. #include "serial.h"
  12.  
  13. class PHYSFSX_short_read : public std::runtime_error
  14. {
  15. public:
  16.         PHYSFSX_short_read(PHYSFS_File *) :
  17.                 runtime_error("short read in PHYSFS file")
  18.         {
  19.         }
  20. };
  21.  
  22. class PHYSFSX_short_write : public std::runtime_error
  23. {
  24. public:
  25.         PHYSFSX_short_write(PHYSFS_File *) :
  26.                 runtime_error("short write in PHYSFS file")
  27.         {
  28.         }
  29. };
  30.  
  31. template <typename T, typename E = PHYSFSX_short_read>
  32. void PHYSFSX_serialize_read(PHYSFS_File *fp, T &t)
  33. {
  34.         const size_t maximum_size = serial::message_type<T>::maximum_size;
  35.         uint8_t buf[maximum_size];
  36.         if (PHYSFS_read(fp, buf, sizeof(buf[0]), maximum_size) != maximum_size)
  37.                 throw E(fp);
  38.         serial::reader::bytebuffer_t b(buf);
  39.         serial::process_buffer(b, t);
  40. }
  41.  
  42. template <typename T, typename E = PHYSFSX_short_write>
  43. void PHYSFSX_serialize_write(PHYSFS_File *fp, const T &t)
  44. {
  45.         const size_t maximum_size = serial::message_type<T>::maximum_size;
  46.         uint8_t buf[maximum_size];
  47.         serial::writer::bytebuffer_t b(buf);
  48.         serial::process_buffer(b, t);
  49.         if (PHYSFS_write(fp, buf, sizeof(buf[0]), maximum_size) != maximum_size)
  50.                 throw E(fp);
  51. }
  52.