binary.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. voltlogger_analyzer
  3. Copyright (C) 2015 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <assert.h> /* assert() */
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include "configuration.h"
  19. #include "binary.h"
  20. #define DECLARE_GET_X_t(x) \
  21. x ## _t get_ ## x (FILE *i_f, char realtime) { \
  22. x ## _t r; \
  23. \
  24. \
  25. if (fread(&r, sizeof(r), 1, i_f) < 1) { \
  26. if (feof(i_f)) { \
  27. if (realtime) { \
  28. do { \
  29. usleep(RETRY_DELAY_USECS); \
  30. clearerr(i_f); \
  31. if (fread(&r, sizeof(r), 1, i_f) < 1 && !feof(i_f))\
  32. assert (ferror(i_f)); \
  33. return r; \
  34. } while (feof(i_f)); \
  35. } \
  36. } \
  37. assert (ferror(i_f)); \
  38. } \
  39. \
  40. return r; \
  41. }
  42. DECLARE_GET_X_t(uint64);
  43. DECLARE_GET_X_t(uint32);
  44. DECLARE_GET_X_t(uint16);
  45. DECLARE_GET_X_t(uint8);
  46. #define DECLARE_OUT_X_t(x) \
  47. int out_ ## x (FILE *o_f, x ## _t v) { \
  48. assert (fwrite(&v, sizeof(v), 1, o_f) > 0); \
  49. \
  50. return 0; \
  51. }
  52. DECLARE_OUT_X_t(uint64);
  53. DECLARE_OUT_X_t(uint32);