Add Dirk Luetjen's ssphys libraries and command-line tool
[vss2svn.git] / ssphys / SSRep / Options.cpp
blob3f515ec58f4479b89437e6699205a9fa4a9e5768
1 // Options.cpp: implementation of the COptions class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "Options.h"
7 #include "strstream"
9 //---------------------------------------------------------------------------
10 #include "LeakWatcher.h"
12 #ifdef _DEBUG
13 #define new DEBUG_NEW
14 #undef THIS_FILE
15 static char THIS_FILE[] = __FILE__;
16 #endif
18 //////////////////////////////////////////////////////////////////////
19 // Construction/Destruction
20 //////////////////////////////////////////////////////////////////////
22 // TODO: replace the complete COptions class with a better design
24 COptions::COptions ()
25 : m_bIntegrityCheck (false),
26 m_bIncludeLabels (true),
27 m_bOnlyLabels (false),
28 m_bIncludeDeleted (false),
29 m_bOnlyDeleted (false),
30 m_bIncludeFiles (true),
31 m_bOnlyFiles (false),
32 m_bHelp (false),
33 m_bAllRecords (false),
34 m_pVersionFilter (NULL),
35 m_Version (0),
36 m_bForceOverwrite (false),
37 m_bRecursive (false),
38 m_bExtended (false),
39 m_bPhys (false)
44 COptions::~COptions ()
46 DELETE (m_pVersionFilter);
49 void COptions::PrintUsage ()
51 std::cout << "ssrep v0.8:" << std::endl;
52 std::cout << std::endl;
53 std::cout << "usage:" << std::endl;
54 std::cout << "general options: " << std::endl;
55 std::cout << " -Y user,password" << std::endl;
56 std::cout << " -d include deleted files" << std::endl;
57 std::cout << " -d+ only deleted files" << std::endl;
58 std::cout << " -f include files (default)" << std::endl;
59 std::cout << " -f+ only files" << std::endl;
60 std::cout << " -f- only projects" << std::endl;
61 std::cout << " -v# specific version number" << std::endl;
62 std::cout << std::endl;
63 std::cout << std::endl;
64 std::cout << "ssrep dir [file] displays directory information about the project" << std::endl;
65 // std::cout << "ssrep info [file] displays basic information about the item" << std::endl;
66 // std::cout << "options: " << std::endl;
67 // std::cout << " -a process all records within the file" << std::endl;
68 // std::cout << "ssrep history [item] displays the history of the specific item" << std::endl;
69 // std::cout << "options: " << std::endl;
70 // std::cout << " -L<-|+> Filter versions by labels (-) all versions without labels" << std::endl;
71 // std::cout << " (+) only versions that have labels" << std::endl;
72 // std::cout << " --del include dead records" << std::endl;
73 // std::cout << std::endl;
74 // std::cout << "ssrep get [item] [target] retrieve a specific version of the item" << std::endl;
75 // std::cout << "options: " << std::endl;
76 // std::cout << " -f force overwrite of target file" << std::endl;
77 // std::cout << " -v# specifies the version to get" << std::endl;
78 std::cout << std::endl;
79 std::cout << " -h --help print help text" << std::endl;
82 void COptions::ParseCommandLine (int argc, char* argv[])
84 for (int i = 1; i< argc; ++i)
86 if (argv[i][0] == '-')
88 if (IsArgChar (argv[i][1], '?'))
90 m_bHelp = true;
92 else if (IsArgChar (argv[i][1], 'a') || IsLongArgument (&argv[i][1], "all"))
94 m_bAllRecords = true;
96 else if (IsArgChar (argv[i][1], 'd'))
98 m_bIncludeDeleted = true;
99 m_bOnlyDeleted = ToBool (argv[i][2], m_bOnlyDeleted);
101 else if (IsArgChar (argv[i][1], 'e'))
103 m_bExtended = true;
105 else if (IsArgChar (argv[i][1], 'f'))
107 m_bIncludeFiles = true;
108 m_bOnlyFiles = ToBool (argv[i][2], m_bIncludeFiles );
110 else if (IsLongArgument (&argv[i][1], "force"))
112 m_bForceOverwrite = true;
114 else if (IsArgChar (argv[i][1], 'h') || IsLongArgument (&argv[i][1], "help"))
116 m_bHelp = true;
118 else if (IsArgChar (argv[i][1], 'i'))
120 // Ignore: Do not ask for input under any circumstances.
121 // m_bIgnore = true;
122 // m_bIgnore = ToBool (argv[i][2], m_bIgnore);
124 else if (IsArgChar (argv[i][1], 'l'))
126 m_bOnlyLabels = true;
127 m_bIncludeLabels = ToBool (argv[i][2], m_bOnlyLabels);
129 else if (IsArgChar (argv[i][1], 'n') || IsLongArgument (&argv[i][1], "names"))
131 if (++i < argc)
132 m_NamesDat = argv[i];
134 else if (IsArgChar (argv[i][1], 'o'))
136 m_LogFile = &argv[i][2];
138 else if (IsArgChar (argv[i][1], 'p'))
140 m_bPhys = true;
141 m_bPhys = ToBool (argv[i][2], m_bPhys);
144 else if (IsArgChar (argv[i][1], 'r') || IsLongArgument (&argv[i][1], "recursive"))
146 m_bRecursive = true;
147 m_bRecursive = ToBool (argv[i][2], m_bRecursive);
149 else if (IsArgChar (argv[i][1], 's'))
151 m_SrcSafeIniPath = &argv[i][2];
153 else if (IsArgChar (argv[i][1], 'v') || IsLongArgument (&argv[i][1], "version"))
155 std::string filter;
156 if (IsArgChar (argv[i][1], 'v'))
157 filter = &argv[i][2];
158 else if (++i < argc)
159 filter = argv[i];
161 int start;
162 char tilde;
163 std::istrstream ist (filter.c_str (), filter.length());
164 ist >> start;
165 int end = start;
166 ist >> tilde;
167 ist >> end;
169 DELETE (m_pVersionFilter);
170 m_pVersionFilter = new CVersionRangeFilter (end, start);
172 m_Version = start;
173 m_VersionDate = filter;
175 else if (IsArgChar (argv[i][1], 'y'))
177 std::string userpw = &argv[i][2];
178 m_User = userpw.substr(0, userpw.find (','));
179 m_Password = userpw.substr(userpw.find (',')+1);
182 else
184 if (m_Command.empty ())
186 m_Command = argv[i];
187 // TODO: tolower bekommt ein (int) als parameter, std::string ist aber ein uchar --> Probleme mit negativen chars
188 std::transform (m_Command.begin(), m_Command.end(), m_Command.begin(), tolower);
190 else
191 m_Items.push_back (argv[i]);
195 if (m_Command == "get" && m_Items.size () > 1)
197 m_Dest = m_Items.back();
198 m_Items.pop_back();
203 bool COptions::IsLongArgument (const char* ch, const char* arg)
205 if (!ch)
206 return false;
208 std::string str = (ch[0] == '-') ? &ch[1] : &ch[0];
209 if (str == arg)
210 return true;
212 return false;
215 bool COptions::IsArgChar (const char ch, const char arg)
217 if (!ch)
218 return false;
219 else if (ch == arg)
220 return true;
221 else if (tolower(ch) == arg)
222 return true;
223 else if (toupper(ch) == arg)
224 return true;
225 return false;
228 bool COptions::ToBool (const char ch, bool& val)
230 if (!ch)
231 return val;
232 if (ch == '-')
233 val = false;
234 else if (ch == '+')
235 val = true;
237 return val;