1 // ssphys.cpp : Defines the entry point for the console application.
7 #include <SSLib\SSDatabase.h>
8 #include <SSPhysLib\SSItemInfoObject.h>
9 #include <direct.h> // _getcwd
10 #include "Formatter.h"
13 //---------------------------------------------------------------------------
14 #include "LeakWatcher.h"
19 static char THIS_FILE
[] = __FILE__
;
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");
65 void VssGet (const std::string
& physFile
, const COptions
& options
)
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
);
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 ()));
86 pItem
->Get (options
.GetVersion (), options
.GetDestination ().c_str());
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
);
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
;
118 void VssValidate (const std::string
& physFile
, const COptions
& options
)
122 std::auto_ptr
<SSRecordFile
> pFile (SSRecordFile::MakeFile (physFile
));
128 catch (SSException
& ex
)
130 std::cerr
<< "error: " << ex
.what() << std::endl
;
134 int VssCurrentProject (SSDatabase
& dataBase
, IFormatter
* pFormatter
, const COptions
& options
)
138 StringVector items
= options
.GetItems ();
140 errorVal
= dataBase
.SetCurrentProject (items
.front());
143 return pFormatter
->DoProject (dataBase
, options
);
148 int main(int argc
, char* argv
[])
151 options
.ParseCommandLine (argc
, argv
);
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
);
170 // TODO: set this to the correct locale
172 setlocale (LC_TIME
, "German");
174 // Load the names cache
175 // SSNamesCacheFile namesCache;
176 // if (!options.GetNamesCache ().empty ())
179 // if (namesCache.Open (options.GetNamesCache ()))
180 // g_NamesCache.SetFile (&namesCache);
182 // catch (SSException& ex)
184 // std::cerr << "names cache error: " << ex.what() << std::endl;
192 std::string iniPath
= options
.GetSrcSafeIniPath();
193 if (iniPath
.empty ())
195 // grab the path form the environment
196 const char* p
= getenv ("SSDIR");
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
)
211 if (!iniPath
.empty ())
212 Debug (std::string("CWD=") + iniPath
);
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
);
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
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);
247 // SSItemPtr pItem = pItem->GetVersion (1);
248 // assert (pItem->GetVersionNumber() == 1);
249 // pItem = pItem->GetVersion(options.GetVersionDate ());
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
);
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 ();
281 std::cout
.rdbuf(sb
); // restore sb