serializer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /******************************************************************************
  2. Copyright (c) 2013 by Hugh Bailey <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. ******************************************************************************/
  18. #ifndef SERIALIZER_H
  19. #define SERIALIZER_H
  20. /*
  21. * General programmable serialization functions. (A shared interface to
  22. * various reading/writing to/from different inputs/outputs)
  23. *
  24. * TODO: Not currently implemented
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. enum serialize_seek_type {
  30. SERIALIZE_SEEK_START,
  31. SERIALIZE_SEEK_CURRENT,
  32. SERIALIZE_SEEK_END
  33. };
  34. struct serializer {
  35. void *param;
  36. size_t (*serialize)(struct serializer, void *, size_t);
  37. uint64_t (*seek)(struct serializer, int64_t, enum serialize_seek_type);
  38. uint64_t (*getpos)(struct serializer);
  39. };
  40. static inline size_t serialize(struct serializer *s, void *data, size_t len)
  41. {
  42. if (s->serialize)
  43. return s->serialize(s, data, len);
  44. return 0;
  45. }
  46. static inline uint64_t serializer_seek(struct serializer *s, int64_t offset,
  47. enum serialize_seek_type seek_type)
  48. {
  49. if (s->seek)
  50. return s->seek(s, offset, seek_type);
  51. return 0;
  52. }
  53. static inline uint64_t serializer_getpos(struct serializer *s)
  54. {
  55. if (s->getpos)
  56. return s->getpos(s);
  57. return 0;
  58. }
  59. static inline void serializer_write_u8(struct serializer *s, uint8_t u8)
  60. {
  61. serialize(s, &u8, sizeof(uint8_t));
  62. }
  63. static inline void serializer_write_i8(struct serializer *s, int8_t i8)
  64. {
  65. serialize(s, &i8, sizeof(int8_t));
  66. }
  67. static inline void serializer_write_u16(struct serializer *s, uint16_t u16)
  68. {
  69. serialize(s, &u16, sizeof(uint16_t));
  70. }
  71. static inline void serializer_write_i16(struct serializer *s, int16_t i16)
  72. {
  73. serialize(s, &i16, sizeof(int16_t));
  74. }
  75. static inline void serializer_write_u32(struct serializer *s, uint32_t u32)
  76. {
  77. serialize(s, &u32, sizeof(uint32_t));
  78. }
  79. static inline void serializer_write_i32(struct serializer *s, int32_t i32)
  80. {
  81. serialize(s, &i32, sizeof(int32_t));
  82. }
  83. static inline void serializer_write_u64(struct serializer *s, uint32_t u64)
  84. {
  85. serialize(s, &u64, sizeof(uint64_t));
  86. }
  87. static inline void serializer_write_i64(struct serializer *s, int32_t i64)
  88. {
  89. serialize(s, &i64, sizeof(int64_t));
  90. }
  91. static inline void serializer_write_float(struct serializer *s, float f)
  92. {
  93. serialize(s, &f, sizeof(float));
  94. }
  95. static inline void serializer_write_double(struct serializer *s, double d)
  96. {
  97. serialize(s, &d, sizeof(double));
  98. }
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif