Add Dirk Luetjen's ssphys libraries and command-line tool
[vss2svn.git] / ssphys / SSRep / Formatter.cpp
blob7dac9f3eec690614f824ddc67f66fbe9991a4008
1 // Formatter.cpp: implementation of the CFormatter class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "Formatter.h"
8 //////////////////////////////////////////////////////////////////////
9 // Construction/Destruction
10 //////////////////////////////////////////////////////////////////////
12 class CVssFormatter : public IFormatter
14 public:
15 virtual int DoWhoAmI (const COptions& options);
16 virtual int DoProject (const SSDatabase& database, const COptions& options);
18 virtual int DoDirectory (const SSItemPtr pItem, const COptions& options);
19 virtual int DoProperties (const SSItemPtr pItem, const COptions& options);
20 virtual int DoFiletype (const SSItemPtr pItem, const COptions& options);
21 virtual int DoHistory (const SSItemPtr pItem, const COptions& options);
26 int CVssFormatter::DoWhoAmI (const COptions& options)
28 if (options.GetUser().empty ())
30 // TODO: get the current windows user name
31 return 100; // unknown user
33 std::cout << options.GetUser () << std::endl;
35 return 0;
39 int CVssFormatter::DoProject (const SSDatabase& database, const COptions& options)
41 std::string cp = database.GetCurrentProject();
42 if (cp.empty ())
44 throw SSException ("could not read current project");
46 std::cout << "Current project is " << cp << std::endl;
48 return 0;
53 int CVssFormatter::DoDirectory (SSItemPtr pItem, const COptions& options)
55 assert (pItem);
56 try
58 if (pItem->GetType () != SSITEM_PROJECT)
59 throw SSException ("please specify a project item");
61 // Header
62 std::cout << pItem->GetSpec() << ":" << std::endl;
64 std::auto_ptr<SSItems> pItems (pItem->GetItems (options.GetDeleted()));
65 if (!pItems.get ())
67 std::cout << "No items found under " << pItem->GetSpec() << std::endl;
68 return 1;
71 // filter items
72 std::vector <SSItemPtr> filtered;
73 for (long i = 0; i < pItems->GetCount (); ++i)
75 SSItemPtr pItem = pItems->GetItem(i);
76 bool bInclude = true;
78 // filter delted items
79 if ( pItem->GetDeleted () && options.GetDeleted()
80 || !pItem->GetDeleted() && !(options.GetOnlyDeleted() && options.GetDeleted()))
81 bInclude &= true;
82 else
83 bInclude &= false;
85 // filter file/project items
86 if ( pItem->GetType() == SSITEM_FILE && options.GetFiles()
87 || pItem->GetType() == SSITEM_PROJECT && !(options.GetOnlyFiles() && options.GetFiles()))
88 bInclude &= true;
89 else
90 bInclude &= false;
92 if (bInclude)
94 filtered.push_back(pItem);
98 // first print all project items
99 std::vector <SSItemPtr>::iterator itor;
100 std::vector <SSItemPtr>::iterator end = filtered.end ();
101 for (itor = filtered.begin(); itor != end; ++itor)
103 SSItemPtr pItem = *itor;
104 if (pItem->GetType () == SSITEM_PROJECT)
106 std::cout << "$" << pItem->GetName();
107 if (options.GetPhysOutput())
108 std::cout << "\t" << pItem->GetPhysical();
109 std::cout << std::endl;
113 // second print all file items
114 for (itor = filtered.begin(); itor != end; ++itor)
116 SSItemPtr pItem = *itor;
117 if (pItem->GetType () == SSITEM_FILE)
119 std::cout << pItem->GetName();
120 if (options.GetExtendedOutput () && pItem->GetIsCheckedOut())
122 // User\tDate\Time\CheckouPath, z.B.:
123 // ValidatingEdit.cpp Mirco 4.06.04 13:07 C:\Mirco
124 // ValidatingEdit.h Mirco 4.06.04 13:07 C:\Mirco
125 std::cout << "\tTODO: Display check out information";
126 } else if (options.GetPhysOutput())
127 std::cout << "\t" << pItem->GetPhysical();
129 std::cout << std::endl;
133 std::cout << std::endl;
135 // recurse into all subdirectories
136 if (options.GetRecursive())
138 for (itor = filtered.begin(); itor != end; ++itor)
140 SSItemPtr pItem = *itor;
141 if (pItem->GetType () == SSITEM_PROJECT)
142 DoDirectory (pItem, options);
146 catch (SSException& ex)
148 std::cerr << "error: " << ex.what() << std::endl;
149 return 1;
152 return 0;
156 int CVssFormatter::DoProperties (SSItemPtr pItem, const COptions& options)
158 assert (pItem);
161 if (pItem->GetType () == SSITEM_PROJECT)
163 std::auto_ptr<SSItems> pItems (pItem->GetItems (true));
164 int files = 0;
165 int projects = 0;
166 int deleted = 0;
167 for (long i = 0; i < pItems->GetCount (); ++i)
169 SSItemPtr pItem = pItems->GetItem(i);
170 // filter delted items
171 if ( pItem->GetDeleted ())
172 ++deleted;
173 else if ( pItem->GetType () == SSITEM_PROJECT)
174 ++projects;
175 else if ( pItem->GetType () == SSITEM_FILE)
176 ++files;
179 //Project: $/
180 //Contains:
181 // 2 Files ( +30 deleted )
182 // 12 Subproject(s)
183 std::cout << "Project: " << pItem->GetSpec() << std::endl;
184 std::cout << "Contains: " << std::endl;
185 std::cout << files << " Files ( +" << deleted << " deleted )" << std::endl;
186 std::cout << projects << " Subproject(s)" << std::endl;
188 else if (pItem->GetType () == SSITEM_FILE)
190 //File: $/ValidatingEdit.h
191 //Type: Text
192 //Size: 624 bytes 31 lines
193 //Store only latest version: No
196 //Latest: Last Label: 1.9.3
197 // Version: 178 Version: 178
198 // Date: 15.11.04 17:13 Date: 15.11.04 17:13
201 catch (SSException& ex)
203 std::cerr << "error: " << ex.what() << std::endl;
204 return 1;
207 return 0;
211 int CVssFormatter::DoFiletype (const SSItemPtr pItem, const COptions& options)
213 if (!pItem)
214 return 1;
216 try
218 if (pItem->GetType () == SSITEM_PROJECT)
220 std::auto_ptr<SSItems> pItems (pItem->GetItems (options.GetDeleted()));
221 for (long i = 0; i < pItems->GetCount (); ++i)
223 SSItemPtr pItem = pItems->GetItem(i);
225 if ( pItem->GetType () == SSITEM_PROJECT)
226 std::cout << "\n\n" << pItem->GetSpec () << std::endl;
227 else if ( pItem->GetType () == SSITEM_FILE)
229 std::string binary = pItem->GetBinary() ? "Binary" : "Text";
230 std::cout << pItem->GetName () << "\t" << binary << std::endl;
235 else if (pItem->GetType () == SSITEM_FILE)
237 std::string binary = pItem->GetBinary() ? "Binary" : "Text";
238 std::cout << pItem->GetName () << "\t" << binary << std::endl;
241 catch (SSException& ex)
243 std::cerr << "error: " << ex.what() << std::endl;
244 return 1;
246 return 0;
250 int CVssFormatter::DoHistory (const SSItemPtr pItem, const COptions& options)
252 assert (pItem);
255 // std::auto_ptr<SSVersions> pVersions = pItem->GetVersions(0);
256 // for (long i = 0; i < pVersions->GetCount (); ++i)
257 // {
258 // SSVersionPtr pVersions (pVersions->GetVersion(i));
259 // pVersions->ReportVssStyle
260 // }
262 catch (SSException& ex)
264 std::cerr << "error: " << ex.what() << std::endl;
265 return 1;
268 return 0;
272 IFormatter* CVssFormatterFactory::MakeFormatter ()
274 return new CVssFormatter ();