~ubuntu-branches/ubuntu/vivid/notecase/vivid

« back to all changes in this revision

Viewing changes to src/lib/CircularBuffer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Vijay(Vijay)
  • Date: 2007-06-14 00:13:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070614001348-z9e2vbgtenb9nhoo
Tags: 1.5.6-0ubuntu1
* New Upstream release 
*  The libgnomevfs2-dev is also added to Build-Depends 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: implementation of the circular buffer
 
7
//
 
8
////////////////////////////////////////////////////////////////////////////
 
9
 
 
10
#include "CircularBuffer.h"
 
11
#include <stdlib.h>
 
12
#include <string.h>
 
13
#include <stdarg.h>
 
14
#include <time.h>
 
15
 
 
16
#ifdef _WIN32
 
17
 #define vsnprintf _vsnprintf 
 
18
 #include <windows.h>
 
19
#else
 
20
 #include <sys/time.h>
 
21
 #include <unistd.h>
 
22
#endif
 
23
 
 
24
//////////////////////////////////////////////////////////////////////
 
25
// Construction/Destruction
 
26
//////////////////////////////////////////////////////////////////////
 
27
 
 
28
CircularBuffer::CircularBuffer(int nSize)
 
29
{
 
30
        m_pszData = (char *)malloc(nSize);
 
31
        m_nSize   = nSize;
 
32
 
 
33
        m_nStart = 0;
 
34
        m_nEnd   = 0;
 
35
}
 
36
 
 
37
CircularBuffer::~CircularBuffer()
 
38
{
 
39
        if(m_pszData) 
 
40
                free(m_pszData);
 
41
}
 
42
 
 
43
void CircularBuffer::Dump(FILE *pOutFile)
 
44
{
 
45
        if(IsEmpty())
 
46
                return;
 
47
 
 
48
        size_t nWritten;
 
49
        if(m_nEnd > m_nStart){
 
50
                nWritten = fwrite(m_pszData + m_nStart, 1, m_nEnd-m_nStart, pOutFile);
 
51
        }
 
52
        else{
 
53
                nWritten = fwrite(m_pszData + m_nStart, 1, m_nSize-m_nStart, pOutFile);
 
54
                nWritten = fwrite(m_pszData, 1, m_nEnd, pOutFile);
 
55
        }
 
56
}
 
57
 
 
58
void CircularBuffer::Add(const char *szMsg)
 
59
{
 
60
        int nMsgSize = strlen(szMsg);
 
61
        if(nMsgSize > m_nSize)
 
62
                nMsgSize = m_nSize;
 
63
 
 
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);
 
68
 
 
69
                //recalc new boundaries
 
70
                if(m_nStart > m_nEnd && m_nStart < (m_nEnd + nMsgSize))
 
71
                        m_nStart = m_nEnd + nMsgSize;
 
72
                m_nEnd  += nMsgSize;
 
73
                
 
74
        }
 
75
        else{
 
76
                memcpy(m_pszData + m_nEnd, szMsg, nAvail1);
 
77
                memcpy(m_pszData, szMsg + nAvail1, nMsgSize - nAvail1);
 
78
 
 
79
                //recalc new boundaries
 
80
                if(m_nStart > m_nEnd)
 
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;
 
85
        }
 
86
        
 
87
}
 
88
 
 
89
void CircularBuffer::Printf(const char *fmt, ...)
 
90
{
 
91
        char buffer[1024] = "";
 
92
 
 
93
        // create string using format and list of parameters
 
94
        va_list args;
 
95
        va_start(args, fmt);
 
96
        vsnprintf(buffer, sizeof(buffer)-1, fmt, args);
 
97
        va_end(args);
 
98
 
 
99
        AddMsg(buffer);
 
100
}
 
101
 
 
102
void CircularBuffer::AddMsg(const char *szMsg)
 
103
{
 
104
        char buffer[2024] = "";
 
105
 
 
106
        //TOFIX time in miliseconds (Win - _ftime, Linux - ?)
 
107
        // create string using format and list of parameters
 
108
#ifdef _WIN32
 
109
        SYSTEMTIME st;
 
110
        GetSystemTime(&st);
 
111
        sprintf(buffer, "%02d:%02d:%02d.%03d ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
 
112
#else
 
113
        struct timeval the_time;
 
114
        int result = gettimeofday(&the_time, NULL);
 
115
        if(result >= 0){
 
116
                struct tm *pTime = localtime(&the_time.tv_sec);
 
117
                sprintf(buffer, "%02d:%02d:%02d", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
 
118
 
 
119
                //append miliseconds
 
120
                char szMilisec[10];
 
121
                sprintf(szMilisec, ".%03d ", (int)(the_time.tv_usec/1000));
 
122
                strcat(buffer, szMilisec);
 
123
        }
 
124
        else{
 
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);
 
128
        }
 
129
#endif
 
130
 
 
131
        strcat(buffer, szMsg);
 
132
 
 
133
        Add(buffer);
 
134
}