root/trunk/ssphys/SSPhysLib/SSRecord.cpp

Revision 339 (checked in by toby, 2 years ago)

add missing include; thanks to Ed Avis for the patch

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 // SSRecord.cpp: implementation of the SSRecord class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include <cstring>
6 #include "StdAfx.h"
7 #include "SSRecord.h"
8 #include "SSFiles.h"
9 #include "crc.h"
10
11 //---------------------------------------------------------------------------
12 #include "LeakWatcher.h"
13
14 #ifdef _DEBUG
15 #define new DEBUG_NEW
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
19
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23
24
25 struct TypeMap {
26   char  _string[3];
27   eType _eType;
28 };
29
30 TypeMap g_TypeMap[] = {
31   {"DH", eItemRecord},
32   {"MC", eCommentRecord},
33   {"EL", eHistoryRecord},
34   {"CF", eCheckOutRecord},
35   {"PF", eParentFolder},
36   {"FD", eFileDelta},
37   {"HN", eNamesCache},
38   {"SN", eNameCacheEntry},
39   {"JP", eProjectEntry},
40   {"HU", eUsersHeader},
41   {"UU", eUser},
42   {"BF", eBranchFile},
43 };
44
45 eType SSRecord::StringToType (const char type[2])
46 {
47   for (int i = 0; i< countof(g_TypeMap); ++i)
48   {
49     if (0 == memcmp (type, g_TypeMap[i]._string, 2))
50       return g_TypeMap[i]._eType;
51   }
52
53   return eUnknown;
54 }
55
56 std::string SSRecord::TypeToString (eType type)
57 {
58   for (int i = 0; i< countof(g_TypeMap); ++i)
59   {
60     if (type == g_TypeMap[i]._eType)
61       return std::string (g_TypeMap[i]._string, 2);
62   }
63
64   if (type == eNone)
65     return "none";
66
67   return "unknown";
68 }
69
70 bool SSRecord::IsValid () const
71 {
72   // TODO: check checksum
73   return m_pBuffer != NULL;
74 }
75
76 eType SSRecord::GetType () const
77 {
78   if (!m_pBuffer)
79     return eNone;
80   return SSRecord::StringToType (m_Header.type);
81 }
82
83 SSRecord::SSRecord (SSFileImpPtr filePtr, long offset)
84   : m_FileImpPtr (filePtr), m_Offset(offset), m_pBuffer(NULL), m_Len (0)
85 {
86   int fileLength = m_FileImpPtr->Size ();
87
88   if (!m_FileImpPtr->Read (offset, &m_Header, sizeof(m_Header)))
89     throw SSException ("could not read record header");
90
91   // OPTIMIZE: We do not nead to read all the record payload in advance (esp. for FD records)
92   if (m_Header.size > 0)
93   {
94     if (offset + sizeof(m_Header) + m_Header.size > fileLength)
95       throw SSRecordException ("bad header: length variable exceeds file size");
96
97     m_pBuffer = new byte[m_Header.size];
98     if (!m_FileImpPtr->Read (/*offset + sizeof(m_Header), */ m_pBuffer, m_Header.size))
99       throw SSException ("could not read record data");
100
101     short crc = calc_crc16 (m_pBuffer, m_Header.size);
102     if (m_Header.checksum != (short)crc && m_Header.checksum != 0)
103     {
104       SSRecordException ex("wrong checksum");
105       Warning (ex.what());
106       _RAISE (ex);
107     }
108   }
109  
110   m_Len = m_Header.size;
111 }
112
113 SSRecord::SSRecord (eType type, const void* buffer, int len)
114   : m_Offset(0), m_pBuffer(NULL), m_Len (0)
115 {
116   m_Header.checksum = calc_crc16 (buffer, len);
117   strncpy (m_Header.type, TypeToString (type).c_str(), 2);
118   m_Header.size = len;
119
120   // OPTIMIZE: We do not nead to read all the record payload in advance (esp. for FD records)
121   if (m_Header.size > 0)
122   {
123     m_pBuffer = new byte[m_Header.size];
124     memcpy (m_pBuffer, buffer, m_Header.size);
125   }
126  
127   m_Len = m_Header.size;
128 }
129
130 void SSRecord::Dump (std::ostream& os) const
131 {
132   os << "Offset: " << GetOffset ();
133   os << " Type: " << GetRecordType ();
134   os << " Len: " << GetLen();
135   std::string validity = IsValid() ? "valid" : "invalid";
136   os << " crc: " << m_Header.checksum << " -> " << validity << std::endl;
137 }
138
139 SSRecord::~SSRecord ()
140 {
141   delete [] m_pBuffer;
142 }
Note: See TracBrowser for help on using the browser.

PumaCode.org recommends CVSDude for fast, professional Subversion and Trac hosting:

CVSDude.com

These ads are automatically generated by Google. Revenue from these ads helps to pay for hosting this site; however, these ads do not constitute an endorsement by PumaCode.org.