message_file.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #define _XOPEN_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <memory.h>
  6. #include <time.h>
  7. #include <stdint.h>
  8. #include "message_file.h"
  9. namespace
  10. {
  11. char decode[256];
  12. char encode[256][3];
  13. bool code_init_flag=false;
  14. void init_code()
  15. {
  16. memset(decode,0,sizeof(decode));
  17. memset(encode,0,sizeof(encode));
  18. for(int c='A';c<='F';c++)
  19. decode[c]=c-'A'+10;
  20. for(int c='0';c<='9';c++)
  21. decode[c]=c-'0';
  22. for(int i=0;i<256;i++)
  23. {
  24. sprintf(encode[i],"%02X",i);
  25. }
  26. decode['\r']=-1;
  27. decode['\n']=-1;
  28. decode[' ']=-1;
  29. }
  30. inline int ch2int(const char c)
  31. {
  32. return (int) (uint8_t) c;
  33. }
  34. inline int convert(const char*p,char*o)
  35. {
  36. char c;
  37. char*r=o;
  38. for(;*p;p++)
  39. {
  40. if(decode[ch2int(*p)]<0)
  41. continue;
  42. c=decode[ch2int(*p++)]<<4;
  43. *o++=c+decode[ch2int(*p)];
  44. }
  45. return o-r;
  46. }
  47. }
  48. message_file::message_file(FILE*fp)
  49. {
  50. m_last_line[0]=0;
  51. m_file=fp;
  52. m_owner=false;
  53. if(!code_init_flag)
  54. {
  55. init_code();
  56. code_init_flag=true;
  57. }
  58. }
  59. message_file::message_file(const char*fname)
  60. {
  61. m_last_line[0]=0;
  62. m_file=fopen(fname,"r");
  63. m_owner=true;
  64. if(!code_init_flag)
  65. {
  66. init_code();
  67. code_init_flag=true;
  68. }
  69. }
  70. message_file::~message_file()
  71. {
  72. if(m_owner)
  73. fclose(m_file);
  74. }
  75. inline const char* now(char*date_str,uint64_t time)
  76. {
  77. time_t ntime=time/1000;
  78. struct tm buff;
  79. const struct tm*t=localtime_r(&ntime, &buff);
  80. sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d.%03d" ,
  81. t->tm_year+1900,t->tm_mon+1,t->tm_mday,
  82. t->tm_hour,t->tm_min,t->tm_sec,(int)(time%1000));
  83. return date_str;
  84. }
  85. int message_file::put_line(uint64_t time,char*buf,int buflen)
  86. {
  87. char b[8192];
  88. printf("[%s]\n",now(b,time));
  89. int n=0,t;
  90. for(int i=0;i<buflen;i++)
  91. {
  92. t=(int)(unsigned char)buf[i];
  93. n+=sprintf(&b[n],"%s ",encode[t]);
  94. }
  95. printf("%s\n",b);
  96. return 0;
  97. }
  98. int line_type(const char*s)
  99. {
  100. if(s[0]==0 || s[0]=='\n')
  101. return 0;
  102. if(strncmp("2019-",s,5)==0)
  103. return 1;
  104. return 2;
  105. }
  106. int message_file::get_line(uint64_t*time,char*buf,int buflen)
  107. {
  108. return get_line(time,buf,buflen,0);
  109. }
  110. int message_file::get_line(uint64_t*time,char*buf,int buflen,char*m)
  111. {
  112. int type=0;
  113. while(1)
  114. {
  115. if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
  116. return 0;
  117. type=line_type(m_last_line);
  118. if(type==0)
  119. continue;
  120. if(type==1)
  121. {
  122. struct tm tm;
  123. memset(&tm, 0, sizeof(struct tm));
  124. const char*ms=strptime(&m_last_line[0], "%Y-%m-%d %H:%M:%S", &tm);
  125. *time=1000LL*mktime(&tm)+atoi(ms+1);
  126. if(m) strcpy(m,m_last_line);
  127. continue;
  128. }
  129. break;
  130. }
  131. char*o=buf;
  132. while(1)
  133. {
  134. if(line_type(m_last_line)<=1)
  135. break;
  136. o+=convert(m_last_line,o);
  137. if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
  138. return 0;
  139. }
  140. return o-buf;
  141. }