output.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include <limits.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <unistd.h>
  10. #include <stddef.h>
  11. #include <time.h>
  12. #include <errno.h>
  13. #include "main.h"
  14. #include "cmdopt.h"
  15. #include "error.h"
  16. #include "output.h"
  17. /////////////////////////////////////////////////////////////////////////////
  18. extern int g_nVerbosity;
  19. extern bool g_bPluginMode;
  20. /////////////////////////////////////////////////////////////////////////////
  21. int GfaTfuGetPercentString(uint32_t nCur, uint32_t nMax, char *pszString, size_t nCChString)
  22. {
  23. double fCur = (double)nCur;
  24. double fMax = (double)nMax;
  25. double fPerc = fCur / fMax * 100;
  26. int nPerc = (int)fPerc;
  27. snprintf(pszString, nCChString, "%3d %%", nPerc);
  28. return nPerc;
  29. }
  30. /////////////////////////////////////////////////////////////////////////////
  31. int GfaTfuPrintF(int verb, const char *pszFormat, ...)
  32. {
  33. if(verb <= g_nVerbosity)
  34. {
  35. int nRet;
  36. FILE *pf = (verb <= 1) ? stderr : stdout;
  37. va_list args;
  38. va_start(args, pszFormat);
  39. nRet = vfprintf(pf, pszFormat, args);
  40. fflush(pf);
  41. va_end(args);
  42. return nRet;
  43. }
  44. return 0;
  45. }
  46. int GfaTfuTaggedPrintF(int nTagNum, const char *pszFormat, ...)
  47. {
  48. if(g_bPluginMode)
  49. {
  50. int nRet;
  51. FILE *pf = stdout;
  52. char szFormat[256];
  53. va_list args;
  54. va_start(args, pszFormat);
  55. sprintf(szFormat, "<%d>%s</%d>\n", nTagNum, pszFormat, nTagNum);
  56. nRet = vfprintf(pf, szFormat, args);
  57. fflush(pf);
  58. va_end(args);
  59. return nRet;
  60. }
  61. return 0;
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. void GfaTfuDumpImageInfo(const char *pszContext, LPCGFA_IMG_INFO pii)
  65. {
  66. if((g_nVerbosity >= 2) && pszContext && pii)
  67. {
  68. TRACE2("%s Image Information:\n", pszContext);
  69. if( (pii->nImgLength != 0xFFFFFFFF) &&
  70. (pii->nImgCRC32 != 0xFFFFFFFF))
  71. {
  72. TRACE2(" Length: %u\n", pii->nImgLength);
  73. TRACE2(" CRC32: 0x%08X\n", pii->nImgCRC32);
  74. TRACE2(" Mat.Nr.: %s\n", pii->szImgMaterialNum);
  75. TRACE2(" Build: %s\n", pii->szImgNameBuild);
  76. }
  77. else
  78. {
  79. TRACE2(" No valid image found.\n");
  80. }
  81. }
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. uint64_t GfaTfuTimeval2Us(const struct timeval *ptv)
  85. {
  86. if(ptv)
  87. return (uint64_t)ptv->tv_sec * 1000000 + (uint64_t)ptv->tv_usec;
  88. return (uint64_t)-1;
  89. }
  90. /////////////////////////////////////////////////////////////////////////////
  91. const struct timeval* GfaTfuUs2Timeval(uint64_t usTime, struct timeval *ptv)
  92. {
  93. if(ptv)
  94. {
  95. ptv->tv_sec = usTime / 1000000;
  96. ptv->tv_usec = usTime % 1000000;
  97. }
  98. return ptv;
  99. }
  100. /////////////////////////////////////////////////////////////////////////////
  101. uint64_t GfaTfuTimespec2Ns(const struct timespec *pts)
  102. {
  103. if(pts)
  104. return (uint64_t)pts->tv_sec * 1000000000 + (uint64_t)pts->tv_nsec;
  105. return (uint64_t)-1;
  106. }
  107. /////////////////////////////////////////////////////////////////////////////
  108. const struct timespec* GfaTfuNs2Timespec(uint64_t nsTime, struct timespec *pts)
  109. {
  110. if(pts)
  111. {
  112. pts->tv_sec = nsTime / 1000000000;
  113. pts->tv_nsec = nsTime % 1000000000;
  114. }
  115. return pts;
  116. }
  117. /////////////////////////////////////////////////////////////////////////////
  118. int64_t GfaTfuClockDiff(const struct timespec *pts1, const struct timespec *pts2)
  119. {
  120. int64_t nRet = 0;
  121. if(pts1 && pts2)
  122. {
  123. uint64_t t1 = GfaTfuTimespec2Ns(pts1);
  124. uint64_t t2 = GfaTfuTimespec2Ns(pts2);
  125. nRet = (int64_t)(t1 - t2);
  126. }
  127. return nRet;
  128. }
  129. /////////////////////////////////////////////////////////////////////////////
  130. void GfaTfuGetClock(struct timespec *pts)
  131. {
  132. clock_gettime(CLOCK_MONOTONIC, pts);
  133. }