1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: implementation of the circular buffer
8
////////////////////////////////////////////////////////////////////////////
10
#include "CircularBuffer.h"
17
#define vsnprintf _vsnprintf
24
//////////////////////////////////////////////////////////////////////
25
// Construction/Destruction
26
//////////////////////////////////////////////////////////////////////
28
CircularBuffer::CircularBuffer(int nSize)
30
m_pszData = (char *)malloc(nSize);
37
CircularBuffer::~CircularBuffer()
43
void CircularBuffer::Dump(FILE *pOutFile)
49
if(m_nEnd > m_nStart){
50
nWritten = fwrite(m_pszData + m_nStart, 1, m_nEnd-m_nStart, pOutFile);
53
nWritten = fwrite(m_pszData + m_nStart, 1, m_nSize-m_nStart, pOutFile);
54
nWritten = fwrite(m_pszData, 1, m_nEnd, pOutFile);
58
void CircularBuffer::Add(const char *szMsg)
60
int nMsgSize = strlen(szMsg);
61
if(nMsgSize > m_nSize)
64
//write after the end mark
65
int nAvail1 = m_nSize - m_nEnd;
66
if(nMsgSize <= nAvail1){
67
memcpy(m_pszData + m_nEnd, szMsg, nMsgSize);
69
//recalc new boundaries
70
if(m_nStart > m_nEnd && m_nStart < (m_nEnd + nMsgSize))
71
m_nStart = m_nEnd + nMsgSize;
76
memcpy(m_pszData + m_nEnd, szMsg, nAvail1);
77
memcpy(m_pszData, szMsg + nAvail1, nMsgSize - nAvail1);
79
//recalc new boundaries
81
m_nStart = nMsgSize - nAvail1;
82
else if (m_nStart < m_nEnd && m_nStart < (nMsgSize - nAvail1))
83
m_nStart = nMsgSize - nAvail1;
84
m_nEnd = nMsgSize - nAvail1;
89
void CircularBuffer::Printf(const char *fmt, ...)
91
char buffer[1024] = "";
93
// create string using format and list of parameters
96
vsnprintf(buffer, sizeof(buffer)-1, fmt, args);
102
void CircularBuffer::AddMsg(const char *szMsg)
104
char buffer[2024] = "";
106
//TOFIX time in miliseconds (Win - _ftime, Linux - ?)
107
// create string using format and list of parameters
111
sprintf(buffer, "%02d:%02d:%02d.%03d ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
113
struct timeval the_time;
114
int result = gettimeofday(&the_time, NULL);
116
struct tm *pTime = localtime(&the_time.tv_sec);
117
sprintf(buffer, "%02d:%02d:%02d", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
121
sprintf(szMilisec, ".%03d ", (int)(the_time.tv_usec/1000));
122
strcat(buffer, szMilisec);
125
time_t nTime = time(NULL);
126
struct tm *pTime = localtime(&nTime);
127
sprintf(buffer, "%02d:%02d:%02d ", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
131
strcat(buffer, szMsg);