123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <memory.h>
- #include <time.h>
- #include <stdint.h>
- #include "message_file.h"
- #define _XOPEN_SOURCE
- namespace
- {
- char decode[256];
- char encode[256][3];
- bool code_init_flag=false;
- void init_code()
- {
- memset(decode,0,sizeof(decode));
- memset(encode,0,sizeof(encode));
- for(int c='A';c<='F';c++)
- decode[c]=c-'A'+10;
- for(int c='0';c<='9';c++)
- decode[c]=c-'0';
-
- for(int i=0;i<256;i++)
- {
- sprintf(encode[i],"%02X",i);
- }
-
- decode['\r']=-1;
- decode['\n']=-1;
- decode[' ']=-1;
- }
- inline int ch2int(const char c)
- {
- return (int) (uint8_t) c;
- }
- inline int convert(const char*p,char*o)
- {
- char c;
- char*r=o;
- for(;*p;p++)
- {
- if(decode[ch2int(*p)]<0)
- continue;
- c=decode[ch2int(*p++)]<<4;
- *o++=c+decode[ch2int(*p)];
- }
- return o-r;
- }
- }
- message_file::message_file(FILE*fp)
- {
- m_last_line[0]=0;
- m_file=fp;
- m_owner=false;
-
- if(!code_init_flag)
- {
- init_code();
- code_init_flag=true;
- }
- }
- message_file::message_file(const char*fname)
- {
- m_last_line[0]=0;
- m_file=fopen(fname,"r");
- m_owner=true;
-
- if(!code_init_flag)
- {
- init_code();
- code_init_flag=true;
- }
- }
- message_file::~message_file()
- {
- if(m_owner)
- fclose(m_file);
- }
- inline const char* now(char*date_str,uint64_t time)
- {
- time_t ntime=time/1000;
-
- #ifndef _WINDOWS
- struct tm buff;
- const struct tm*t=localtime_r(&ntime, &buff);
- #else
- const struct tm*t = localtime(&ntime);
- #endif
-
- sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d.%03d" ,
- t->tm_year+1900,t->tm_mon+1,t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,(int)(time%1000));
- return date_str;
- }
- int message_file::put_line(uint64_t time,char*buf,int buflen)
- {
- char b[8192];
- printf("[%s]\n",now(b,time));
- int n=0,t;
- for(int i=0;i<buflen;i++)
- {
- t=(int)(unsigned char)buf[i];
- n+=sprintf(&b[n],"%s ",encode[t]);
- }
- printf("%s\n",b);
- return 0;
- }
- int message_file::get_line(uint64_t*time,char*buf,int buflen)
- {
- const char*p;
- while(m_last_line[0]!='[')
- {
- if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
- return 0;
- }
- {
- struct tm tm;
- memset(&tm, 0, sizeof(struct tm));
- #ifndef _WINDOWS
- const char*ms=strptime(&m_last_line[1], "%Y-%m-%d %H:%M:%S", &tm);
- *time=1000*mktime(&tm)+atoi(ms+1);
- #else
- std::string str(m_last_line,strlen(m_last_line));
- *time = parseLine(str);
- #endif
-
-
- }
- char*o=buf;
- for(;;)
- {
- if(nullptr==(p=fgets(m_last_line,sizeof(m_last_line),m_file)))
- return 0;
- if(*p=='[')
- break;
- o+=convert(p,o);
- }
- return o-buf;
- }
- time_t message_file::parseLine(string tmp)
- {
- std::string ms;
- time_t tmpsecond =0;
- int pos = tmp.find("[");
- if(pos!=tmp.npos)
- {
- tmp.erase(pos,1);
- }
- else
- return tmpsecond;
- pos = tmp.find("]");
- if(pos!=tmp.npos)
- {
- tmp.erase(pos,1);
- }
- else
- {
- return tmpsecond;
- }
- pos = tmp.find(".");
- if(pos != tmp.npos)
- {
- ms = tmp.substr(pos+1,3);
- tmp.erase(pos,4);
- }
- else
- {
- return tmpsecond;
- }
- tmpsecond =GetTimeStampByStr(tmp.c_str())*1000 + atoi(ms.c_str());
- return tmpsecond;
- }
- time_t message_file::GetTimeStampByStr( const char* pDate)
- {
- const char* pStart = pDate;
- char szYear[5], szMonth[3], szDay[3], szHour[3], szMin[3], szSec[3];
- szYear[0] = *pDate++;
- szYear[1] = *pDate++;
- szYear[2] = *pDate++;
- szYear[3] = *pDate++;
- szYear[4] = 0x0;
- ++pDate;
- szMonth[0] = *pDate++;
- szMonth[1] = *pDate++;
- szMonth[2] = 0x0;
- ++pDate;
- szDay[0] = *pDate++;
- szDay[1] = *pDate++;
- szDay[2] = 0x0;
- ++pDate;
- szHour[0] = *pDate++;
- szHour[1] = *pDate++;
- szHour[2] = 0x0;
- ++pDate;
- szMin[0] = *pDate++;
- szMin[1] = *pDate++;
- szMin[2] = 0x0;
- ++pDate;
- szSec[0] = *pDate++;
- szSec[1] = *pDate++;
- szSec[2] = 0x0;
- tm tmObj;
- tmObj.tm_year = atoi(szYear)-1900;
- tmObj.tm_mon = atoi(szMonth)-1;
- tmObj.tm_mday = atoi(szDay);
- tmObj.tm_hour = atoi(szHour);
- tmObj.tm_min = atoi(szMin);
- tmObj.tm_sec = atoi(szSec);
- tmObj.tm_isdst= -1;
- return mktime(&tmObj);
- }
|