Add Dirk Luetjen's ssphys libraries and command-line tool
[vss2svn.git] / ssphys / SSRep / ssrep.cpp
blob683fd9331a5dff44cb255a072618a949075231de
1 // ssphys.cpp : Defines the entry point for the console application.
2 //
4 #include "stdafx.h"
6 #include "Options.h"
7 #include <SSLib\SSDatabase.h>
8 #include <SSPhysLib\SSItemInfoObject.h>
9 #include <direct.h> // _getcwd
10 #include "Formatter.h"
11 #include "fstream"
13 //---------------------------------------------------------------------------
14 #include "LeakWatcher.h"
16 #ifdef _DEBUG
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
23 //---------------------------------------------------------------------------
24 void Debug (const std::string& message)
26 // std::cerr << "INFO: " << message << std::endl;
29 void Info (const char* message)
31 std::cerr << "INFO: " << message << std::endl;
34 void Notice (const char* message)
36 std::cerr << "NOTICE: " << message << std::endl;
39 void Warning (const char* message)
41 std::cerr << "WARNING: " << message << std::endl;
44 void Error (const char* message)
46 std::cerr << "ERROR: " << message << std::endl;
54 bool fexists (const char* file)
56 FILE* pFile = fopen (file, "r");
57 if (pFile)
59 fclose (pFile);
60 return true;
62 return false;
65 void VssGet (const std::string& physFile, const COptions& options)
67 try
69 if (options.GetDestination ().empty ())
70 throw SSException ("please specify a destination for get operation");
72 if (fexists (options.GetDestination ().c_str ()) && !options.GetForceOverwrite ())
73 throw SSException ("destination file exists. Please use overwrite flag");
76 SSHistoryFile file (physFile);
77 #if 0
79 std::auto_ptr<SSItemInfoObject> pItem (file.GetItemInfo ());
80 if (pItem->GetType() == SSITEM_PROJECT)
81 throw SSException ("get does not work on project files");
83 std::auto_ptr<SSVersionObject> pVersion (pItem->GetVersion (options.GetVersion ()));
84 if (pVersion.get())
86 pItem->Get (options.GetVersion (), options.GetDestination ().c_str());
89 #endif
91 catch (SSException& ex)
93 std::cerr << "error: " << ex.what() << std::endl;
97 void VssInfo (const std::string& physFile, const COptions& options)
99 SSRecordFile* pFile = NULL;
102 pFile = SSRecordFile::MakeFile (physFile);
103 if (pFile)
105 pFile->Dump (std::cout);
106 if (options.GetAllRecords ())
107 pFile->DumpRecords (std::cout);
110 catch (SSException& ex)
112 std::cerr << "error: " << ex.what() << std::endl;
115 DELETE (pFile);
118 void VssValidate (const std::string& physFile, const COptions& options)
122 std::auto_ptr<SSRecordFile> pFile (SSRecordFile::MakeFile (physFile));
123 if (pFile.get())
125 pFile->Validate ();
128 catch (SSException& ex)
130 std::cerr << "error: " << ex.what() << std::endl;
134 int VssCurrentProject (SSDatabase& dataBase, IFormatter* pFormatter, const COptions& options)
136 int errorVal = 0;
138 StringVector items = options.GetItems ();
139 if (!items.empty())
140 errorVal = dataBase.SetCurrentProject (items.front());
142 if (errorVal == 0)
143 return pFormatter->DoProject (dataBase, options);
145 return errorVal;
148 int main(int argc, char* argv[])
150 COptions options;
151 options.ParseCommandLine (argc, argv);
153 // redirect stdout
154 std::ofstream logfile; // log file
155 std::streambuf *sb = NULL;
156 if (!options.GetLogFile().empty())
158 logfile.open(options.GetLogFile().c_str(), std::ios_base::app | std::ios_base::out); // log file
159 sb = std::cout.rdbuf(); // save old sb
160 std::cout.rdbuf(logfile.rdbuf()); // redirect
163 if (options.GetHelp () || options.GetCommand () == "help")
165 options.PrintUsage ();
166 if (sb) std::cout.rdbuf(sb);
167 return (0);
170 // TODO: set this to the correct locale
171 tzset ();
172 setlocale (LC_TIME, "German");
174 // Load the names cache
175 // SSNamesCacheFile namesCache;
176 // if (!options.GetNamesCache ().empty ())
177 // {
178 // try {
179 // if (namesCache.Open (options.GetNamesCache ()))
180 // g_NamesCache.SetFile (&namesCache);
181 // }
182 // catch (SSException& ex)
183 // {
184 // std::cerr << "names cache error: " << ex.what() << std::endl;
185 // }
186 // }
188 int exitCode = 0;
189 try
192 std::string iniPath = options.GetSrcSafeIniPath();
193 if (iniPath.empty ())
195 // grab the path form the environment
196 const char* p = getenv ("SSDIR");
197 if (p)
198 iniPath = p;
200 if (!iniPath.empty ())
201 Debug (std::string("SSDIR=") + iniPath);
204 if (iniPath.empty ())
206 // take the current directory
207 char buffer[_MAX_PATH];
208 if (_getcwd (buffer, _MAX_PATH) != NULL)
209 iniPath = buffer;
211 if (!iniPath.empty ())
212 Debug (std::string("CWD=") + iniPath);
215 SSDatabase dataBase;
216 if (!dataBase.Open (iniPath, options.GetUser(), options.GetPassword()))
218 throw SSException (std::string ("could not open database: ") + iniPath);
221 // dataBase.SetVersionDate (options.GetVersionDate ());
223 std::auto_ptr<IFormatter> pFormatter (CVssFormatterFactory::MakeFormatter());
225 if (options.GetCommand () == "whoami")
226 exitCode = pFormatter->DoWhoAmI (options);
227 else if (options.GetCommand () == "project")
228 exitCode = pFormatter->DoProject (dataBase, options);
229 else if (options.GetCommand () == "cp")
230 exitCode = VssCurrentProject (dataBase, pFormatter.get(), options);
231 else
233 // get the list of work items
234 StringVector items = options.GetItems ();
236 // if now items are specified at the command line, we get the current project
237 if (items.empty ())
238 items.push_back (std::string ("."));
240 StringVector::const_iterator iter;
241 for (iter = items.begin (); iter != items.end (); ++iter)
243 SSItemPtr pItem = dataBase.GetSSItem (*iter, false);
244 if (!pItem)
245 continue;
247 // SSItemPtr pItem = pItem->GetVersion (1);
248 // assert (pItem->GetVersionNumber() == 1);
249 // pItem = pItem->GetVersion(options.GetVersionDate ());
250 // if (!pItem)
251 // continue;
253 if (options.GetCommand () == "history")
254 exitCode = pFormatter->DoHistory (pItem, options);
255 else if (options.GetCommand () == "properties")
256 exitCode = pFormatter->DoProperties (pItem, options);
257 else if (options.GetCommand () == "filetype")
258 exitCode = pFormatter->DoFiletype (pItem, options);
259 else if (options.GetCommand () == "dir")
260 exitCode = pFormatter->DoDirectory (pItem, options);
261 else if (options.GetCommand () == "get")
262 VssGet (*iter, options);
263 else if (options.GetCommand () == "info")
264 VssInfo (*iter, options);
265 else if (options.GetCommand () == "validate")
266 VssValidate (*iter, options);
267 else
268 throw std::runtime_error (std::string ("unsupported command: ") + options.GetCommand());
272 catch (std::exception& ex)
274 std::cout << "error: " << ex.what() << std::endl;
275 options.PrintUsage ();
276 exitCode = 1;
279 if (sb)
281 std::cout.rdbuf(sb); // restore sb
282 logfile.close ();
285 return exitCode;