root/trunk/ssphys/SSPhysLib/SSItemInfoObject.cpp

Revision 210 (checked in by ldavis, 4 years ago)

Modified looking just for uppercase/lowercase extension to looking for
uppercase/lowercase leaf. This accounts for all my leaves, instead of
most of them.

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 // SSItemInfoObject.cpp: implementation of the SSItems class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "StdAfx.h"
6 #include "SSFiles.h"
7 #include "SSName.h"
8 #include "SSItemInfoObject.h"
9 #include "SSParentFolderObject.h"
10 #include "SSBranchFileObject.h"
11 #include <boost/filesystem/operations.hpp>
12 #include <cctype>
13
14 //---------------------------------------------------------------------------
15 #include "LeakWatcher.h"
16
17 #ifdef _DEBUG
18 #define new DEBUG_NEW
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
26
27 SSItemInfoObject::SSItemInfoObject (SSRecordPtr pRecord)
28   : SSObject (pRecord, eItemRecord)
29 {
30   if (pRecord->GetLen() < sizeof (DH))
31     throw SSRecordException ("not enough data for info object");
32  
33   m_pInfoItem = GetData ();
34 }
35
36 SSItemInfoObject::~SSItemInfoObject ()
37 {
38 }
39
40 SSItemInfoObject* SSItemInfoObject::MakeItemInfo (SSRecordPtr pRecord)
41 {
42   if (pRecord->GetLen () < sizeof (DH))
43     throw SSException ("not enough bytes for DH Header in record");
44
45   const DH* pDh = reinterpret_cast<const DH*> (pRecord->GetBuffer ());
46   if (pDh->type == 1)
47     return new SSProjectItem (pRecord);
48   else if (pDh->type == 2)
49     return new SSFileItem (pRecord);
50   else
51     throw SSException ("unsupported item type");
52
53   return (SSItemInfoObject*) NULL;
54 }
55
56
57
58 SSVersionObject SSItemInfoObject::GetVersion (int i)
59 {
60   SSFileImpPtr filePtr = GetFile ();
61
62   // TODO: Cache versions
63   SSVersionObject version (filePtr->GetRecord (GetHistoryOffsetLast ()));
64
65   while (version && version.GetVersionNumber () != i)
66   {
67     version = version.GetPreviousObject();
68   }
69
70   return version;
71 }
72
73
74 bool SSItemInfoObject::Get (int ver, const char* dest)
75 {
76 //  SSFileImpPtr filePtr = GetFile ();
77 //
78 //  std::string lastVersion = filePtr->GetFileName () + GetLatestExt ();
79 //  char tmrFile[2][255];
80 //  char* ptr[2] = {tmrFile[0], (char*)dest};
81 //  ptr[0] = tmpnam (ptr[0]);
82 // 
83 //  CopyFile (lastVersion.c_str(), ptr[0]);
84 //
85 //  SSVersionObject version (filePtr->GetRecord (GetHistoryOffsetLast ()));
86 // 
87 //  while (version && version.GetVersionNumber() > ver)
88 //  {
89 //    if (version.GetActionId() == Checked_in)
90 //    {
91 //      SSCheckedInAction* pAction = dynamic_cast <SSCheckedInAction*> (version.GetAction());
92 //      SSRecordPtr pRecord = pAction->GetFileDelta();
93 //      ReverseDelta (ptr[0], pRecord->GetBuffer(), pRecord->GetLen(), ptr[1]);
94 //      std::swap (ptr[0], ptr[1]);
95 //    }
96 //
97 //    version = version.GetPrevious ();
98 //  }
99 //
100 //  if (0 != strcmp (ptr[0], dest))
101 //  {
102 //    CopyFile (ptr[0], dest);
103 //    _unlink (ptr[0]);
104 //  }
105 //  else
106 //  {
107 //    _unlink (ptr[1]);
108 //  }
109   return false;
110 }
111
112 bool SSItemInfoObject::Validate()
113 {
114   SSFileImpPtr filePtr = GetFile ();
115
116   bool retval = true;
117   retval &= warn_if (GetLatestExt() != ".A" && GetLatestExt() != ".B");
118   retval &= warn_if (GetHistoryOffsetEnd() != filePtr->Size());
119   retval &= warn_if (filePtr->GetRecord (GetHistoryOffsetLast ())->GetType() != eHistoryRecord);
120   retval &= warn_if (filePtr->GetRecord (GetHistoryOffsetBegin ())->GetType() != eHistoryRecord);
121
122   int nCount = 0;
123   long offset = GetHistoryOffsetLast ();
124   eAction lastActionId = Labeled;
125
126   do {
127     SSRecordPtr pRecord = filePtr->GetRecord (offset);
128     retval &= warn_if (pRecord->GetType() != eHistoryRecord);
129
130     std::auto_ptr <SSObject> objectPtr (SSObject::MakeObject(pRecord));
131     SSVersionObject* pVersion = dynamic_cast<SSVersionObject*> (objectPtr.get ());
132     if (pVersion)
133     {
134       SSVersionObject previous (pVersion->GetPreviousObject ());
135       offset = previous ? previous.GetOffset() : 0L;
136       if (pVersion->GetActionID() == RollBack)
137         nCount += pVersion->GetVersionNumber() - 1;
138       lastActionId = pVersion->GetActionID();
139     }
140     else
141       offset = 0L;
142
143     ++nCount;
144   } while (offset != 0L /*GetHistoryOffsetBegin ()*/);
145  
146   retval &= warn_if (nCount != GetNumberOfActions ());
147   retval &= warn_if (lastActionId != Created_File && lastActionId != Created_Project && lastActionId != RollBack);
148   return retval;
149 }
150
151 void SSItemInfoObject::ToXml (XMLNode* pParent) const
152 {
153   XMLElement type  (pParent, "Type", GetType());
154   XMLElement data  (pParent, "DataFileName", GetDataFileName ());
155   XMLElement ext   (pParent, "LatestExt", GetLatestExt());
156   GetSSName().ToXml (pParent);
157   XMLElement noActions (pParent, "NumberOfActions", GetNumberOfActions());
158 }
159
160 std::string SSItemInfoObject::GetDataFileName () const
161 {
162   std::string fileName = GetFile ()->GetFileName () + GetLatestExt ();
163   boost::filesystem::path fpath(fileName, boost::filesystem::native);
164
165   if (!boost::filesystem::exists(fpath) && fpath.has_leaf() && fpath.has_branch_path())
166   {
167     std::string lcLeaf = fpath.leaf();
168     std::string ucLeaf = fpath.leaf();
169     for (int i = 0; i < lcLeaf.length(); ++i) {
170         lcLeaf[i] = tolower(lcLeaf[i]);
171         ucLeaf[i] = toupper(ucLeaf[i]);
172     }
173
174     boost::filesystem::path ucPath = fpath.branch_path() / ucLeaf;
175     boost::filesystem::path lcPath = fpath.branch_path() / lcLeaf;
176     if (boost::filesystem::exists(lcPath))
177     {
178         fileName = lcPath.string();
179     }
180     else if (boost::filesystem::exists(ucPath))
181     {
182         fileName = ucPath.string();
183     }
184   }
185   return fileName;
186 }
187
188 void SSItemInfoObject::Dump (std::ostream& os) const
189 {
190   SSObject::Dump (os);
191
192   SSFileImpPtr filePtr = GetFile ();
193
194   os << "Item Type:                      ";
195  
196   assert (m_pInfoItem);
197   switch (m_pInfoItem->type)
198   {
199   case 1: os << "Project" << std::endl; break;
200   case 2: os << "File" << std::endl; break;
201   default: os << "Unknown (" << m_pInfoItem->type << ")" << std::endl; break;
202   }
203
204   SSName ssName (GetSSName ());
205   os << "Last Name:                      " << ssName << std::endl;
206 //  os << "Number Of Records:              " << GetNumberOfRecords () << std::endl;
207   os << "LatestExt of last version:        " << m_pInfoItem->latestExt[0] << m_pInfoItem->latestExt[1] << std::endl;
208   os << "Offset to first History record: 0x" << std::hex << GetHistoryOffsetBegin()<< std::dec << std::endl;
209   os << "Offset to last History record:  0x" << std::hex << GetHistoryOffsetLast() << std::dec << std::endl;
210   os << "Offset to the end of the file:  0x" << std::hex << GetHistoryOffsetEnd()  << std::dec << std::endl;
211   os << "Size of the file:               0x" << std::hex << filePtr->Size() << std::dec << std::endl;
212 }
213
214
215
216
217
218 //---------------------------------------------------------------------------
219 SSProjectItem::SSProjectItem (SSRecordPtr pRecord)
220   : SSItemInfoObject (pRecord)
221 {
222   if (pRecord->GetLen() < sizeof (DH_PROJECT))
223     throw SSRecordException ("not enough data for project info object");
224
225   m_pProjectInfo = GetData ();
226 }
227
228 void SSProjectItem::ToXml (XMLNode* pParent) const
229 {
230   SSItemInfoObject::ToXml (pParent);
231   XMLElement spec        (pParent, "ParentSpec", GetParentSpec ());
232   XMLElement phys        (pParent, "ParentPhys", GetParentPhys ());
233   XMLElement noitems     (pParent, "NumberOfItems", GetNumberOfItems ());
234   XMLElement noProjects  (pParent, "NumberOfProjects", GetNumberOfProjects ());
235 }
236
237 void SSProjectItem::Dump (std::ostream& os) const
238 {
239   SSItemInfoObject::Dump (os);
240  
241 //  Hexdump (oss, dummy4, 20);
242   os << "last parent spec:      " << GetParentSpec () << std::endl;
243   os << "parentPhys:            " << GetParentPhys () << std::endl;
244   os << "Number of Items:       " << GetNumberOfItems () << std::endl;
245   os << "Number of Projects:    " << GetNumberOfProjects () << std::endl;
246 }
247
248
249
250
251 //---------------------------------------------------------------------------
252 SSFileItem::SSFileItem (SSRecordPtr pRecord)
253   : SSItemInfoObject (pRecord)
254 {
255   if (pRecord->GetLen() < sizeof (DH_FILE))
256     throw SSRecordException ("not enough data for file info object");
257
258   m_pFileInfo = GetData ();
259 }
260
261 bool SSFileItem::Validate()
262 {
263   SSItemInfoObject::Validate ();
264
265   const DH_FILE* pFileInfoItem = GetData();
266
267   bool retval = true;
268   retval &= warn_if (pFileInfoItem->numberOfItems > 0);
269   retval &= warn_if (pFileInfoItem->numberOfProjects > 0);
270
271   byte knownFlags[] =
272   {
273     0x00, // initial
274 //    0x01, // ??? locked
275     0x02, // binary
276     0x04, // store only latest revision
277     0x20, // shared
278     0x22, // shared binary
279     0x41, // checked out, locked
280 //    0x42, // ???
281     0x43,  // checked out binary, locked
282     0x61, // shared, checked out, locked
283     0x63, // shared, checked out, locked, binary
284 //    0x101, // ??
285 //    0x102 // ??
286   };
287   int i = 0;
288   for (; i < countof (knownFlags); i++)
289     if (pFileInfoItem->flag == knownFlags[i]) 
290       break;
291  
292   if (i == countof (knownFlags))
293   {
294     std::strstream strm;
295     strm << ("unknown combination of flags in the FileInfo record: 0x") << std::hex << pFileInfoItem->flag << std::ends;
296     Warning (strm.str());
297     retval = false;
298   }
299  
300   return retval;
301 }
302
303 bool SSFileItem::GetLocked () const
304 {
305   return ((GetFlag () & 0x01) == 0x01) ? true : false;
306 }
307
308 eFileType SSFileItem::GetFileType () const
309 {
310   return ((GetFlag () & 0x02) == 0x02) ? eFileTypeBinary : eFileTypeText;
311 }
312
313 bool SSFileItem::GetStoreOnlyLatestRev () const
314 {
315   return ((GetFlag () & 0x04) == 0x04) ? true : false;
316 }
317
318 bool SSFileItem::GetShared () const
319 {
320   return ((GetFlag () & 0x20) == 0x20) ? true : false;
321 }
322
323 bool SSFileItem::GetCheckedOut () const
324 {
325   return ((GetFlag () & 0x40) == 0x40) ? true : false;
326 }
327
328
329 SSParentFolderObject* SSFileItem::GetFirstParentFolder ()
330 {
331   SSRecordPtr pRecord = GetFile()->GetRecord(GetOffsetPFRecord ());
332   return new SSParentFolderObject (pRecord);
333 }
334
335 SSBranchFileObject* SSFileItem::GetFirstBranchFile ()
336 {
337   SSRecordPtr pRecord = GetFile()->GetRecord(GetOffsetBFRecord ());
338   return new SSBranchFileObject (pRecord);
339 }
340
341 void SSFileItem::ToXml (XMLNode* pParent) const
342 {
343   SSItemInfoObject::ToXml (pParent);
344   std::stringstream strm; strm << std::hex << "0x" << GetFlag ();
345   XMLElement flags       (pParent, "Flags", strm.str());
346   XMLElement locked      (pParent, "Locked", GetLocked ());
347   XMLElement binary      (pParent, "Binary", GetFileType () == eFileTypeBinary ? true : false);
348   XMLElement store       (pParent, "StoreOnlyLatestRev", GetStoreOnlyLatestRev ());
349   XMLElement checkedOut  (pParent, "CheckedOut", GetCheckedOut ());
350   XMLElement shared      (pParent, "Shared", GetShared ());
351   XMLElement sharedSrc   (pParent, "ShareSrc", GetShareSrcPhys());
352   XMLElement ref         (pParent, "NumberOfReferences", GetNumberOfReferences());
353   XMLElement branch      (pParent, "NumberOfBranches", GetNumberOfBranches ());
354 }
355
356 void SSFileItem::Dump (std::ostream& os) const
357 {
358   SSItemInfoObject::Dump (os);
359
360 //  Hexdump (oss, dummy4, 20);
361
362   const DH_FILE* pFileInfoItem = GetData ();
363   os << "Status:               0x" << std::hex << pFileInfoItem->flag << std::dec << ": ";
364   if (pFileInfoItem->flag == 0x00)
365     os << "normal";
366   os << ((GetFileType () == eFileTypeBinary) ? "binary" : "text");
367   os << (GetStoreOnlyLatestRev () ?  ", store only latest revision" : "");
368   os << (GetLocked () ? ", locked" : "");
369   os << (GetCheckedOut () ? ", checked out, " : "");
370   os << (GetShared () ? ", shared" : "");
371   os << std::endl;
372
373   if (pFileInfoItem->flag & 0xff98)
374     os << "unknown flags encountered" << std::endl;
375
376   os << "Share source physical file:         " << pFileInfoItem->shareSrcSpec << std::endl;
377
378   os << "Offset to first parent record:      0x" << std::hex << pFileInfoItem->offsetPFRecord << std::dec << std::endl;
379   os << "Reference count:                    " << pFileInfoItem->numberOfReferences << std::endl;
380   os << "Offset to 1st checkout record:      0x" << std::hex << pFileInfoItem->offsetCFRecord1 << std::dec << std::endl;
381   os << "Offset to 2nd checkout record:      0x" << std::hex << pFileInfoItem->offsetCFRecord2 << std::dec << std::endl;
382
383   os << "Number of Items:                    " << std::hex << pFileInfoItem->numberOfItems << std::dec << std::endl;
384   os << "Number of Projects:                 " << std::hex << pFileInfoItem->numberOfProjects << std::dec << std::endl;
385 }
386
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.