dvbmisc.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. libdvbmisc - DVB miscellaneous library
  3. Copyright (C) 2005 Manu Abraham <abraham.manu@gmail.com>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  15. */
  16. #ifndef DVB_MISC_H
  17. #define DVB_MISC_H
  18. #include <stdarg.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <sys/time.h>
  22. #define ERROR 0
  23. #define NOTICE 1
  24. #define INFO 2
  25. #define DEBUG 3
  26. #define print(x, y, z, fmt, arg...) do { \
  27. if (z) { \
  28. if ((x > ERROR) && (x > y)) \
  29. vprint("%s: " fmt "\n", __func__ , ##arg); \
  30. else if ((x > NOTICE) && (x > y)) \
  31. vprint("%s: " fmt "\n",__func__ , ##arg); \
  32. else if ((x > INFO) && (x > y)) \
  33. vprint("%s: " fmt "\n", __func__ , ##arg); \
  34. else if ((x > DEBUG) && (x > y)) \
  35. vprint("%s: " fmt "\n", __func__ , ##arg); \
  36. } else { \
  37. if (x > y) \
  38. vprint(fmt, ##arg); \
  39. } \
  40. } while(0)
  41. static inline void vprint(char *fmt, ...)
  42. {
  43. va_list args;
  44. va_start(args, fmt);
  45. vfprintf(stderr, fmt, args);
  46. va_end(args);
  47. }
  48. static inline int time_after(struct timeval oldtime, uint32_t delta_ms)
  49. {
  50. // calculate the oldtime + add on the delta
  51. uint64_t oldtime_ms = (oldtime.tv_sec * 1000) + (oldtime.tv_usec / 1000);
  52. oldtime_ms += delta_ms;
  53. // calculate the nowtime
  54. struct timeval nowtime;
  55. gettimeofday(&nowtime, 0);
  56. uint64_t nowtime_ms = (nowtime.tv_sec * 1000) + (nowtime.tv_usec / 1000);
  57. // check
  58. return nowtime_ms > oldtime_ms;
  59. }
  60. #endif