Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

cmdlnopt.cpp

Go to the documentation of this file.
00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 Julien Couot 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file cmdlnopt.cpp 00022 * Options passed by the command line. 00023 */ 00024 00025 00026 //--------------------------------------------------------------------------- 00027 // For compilers that support precompilation, includes "wx.h". 00028 #include <wx/wxprec.h> 00029 00030 #ifdef __BORLANDC__ 00031 #pragma hdrstop 00032 #endif 00033 00034 #ifndef WX_PRECOMP 00035 // Include your minimal set of headers here, or wx.h 00036 #include <wx/wx.h> 00037 #endif 00038 00039 #include <wx/cmdline.h> 00040 #include <wx/config.h> 00041 #include <wx/fileconf.h> 00042 #include <wx/filename.h> 00043 #include <wx/msgout.h> 00044 #include <wx/tokenzr.h> 00045 #include <wx/txtstrm.h> 00046 #include <wx/wfstream.h> 00047 00048 #include "cmdlnopt.hpp" 00049 #include "utils.hpp" 00050 00051 #include "compat.hpp" 00052 //--------------------------------------------------------------------------- 00053 00054 00055 /// The C++ standard namespace. 00056 using namespace std; 00057 00058 00059 // Static attributes of the CmdLineOptions class 00060 CmdLineOptions::Actions CmdLineOptions::action; 00061 CmdLineOptions::CkSumsFileTypes CmdLineOptions::createType; 00062 wxArrayString CmdLineOptions::files; 00063 wxString CmdLineOptions::checksumsFileName; 00064 bool CmdLineOptions::deleteTempLists; 00065 //--------------------------------------------------------------------------- 00066 00067 00068 /// Messages output stream. 00069 static wxMessageOutput* msgOut = NULL; 00070 //--------------------------------------------------------------------------- 00071 00072 00073 /** 00074 * Default constructor. 00075 * 00076 * Don't allow to create an instance out this of this class. 00077 */ 00078 CmdLineOptions::CmdLineOptions() 00079 { 00080 } 00081 //--------------------------------------------------------------------------- 00082 00083 00084 /** 00085 * Initializes the options. 00086 * 00087 * Should be called at the application startup. 00088 * 00089 * param parser Command line parser. 00090 * @return <CODE>true</CODE> if the passed commands are corrects, 00091 * <CODE>false</CODE> otherwise. 00092 */ 00093 bool CmdLineOptions::init(const wxCmdLineParser& parser) 00094 { 00095 msgOut = wxMessageOutput::Get(); 00096 wxString str; 00097 00098 action = aNone; 00099 createType = cftNone; 00100 deleteTempLists = false; 00101 files.Clear(); 00102 checksumsFileName.Clear(); 00103 00104 if (parser.Found(wxT("V"))) 00105 { 00106 msgOut->Printf(wxT("%s"), ::getAppName().c_str()); 00107 return false; 00108 } 00109 00110 // Get the list of files. 00111 size_t paramCount = parser.GetParamCount(); 00112 for (size_t i = 0; i < paramCount; i++) 00113 files.Add(parser.GetParam(i)); 00114 00115 // Expand the list of names of files. 00116 deleteTempLists = parser.Found(wxT("delete-temp-list")); 00117 expandFilesList(parser); 00118 00119 // Checks for incompatibles switchs and/or options and/or number of given 00120 // files. Checks the validity of the given options. 00121 if (!checkVerifySwitch(parser) || !checkCreateOption(parser) || 00122 !checkAppendOption(parser) || !checkCreateTypeOption(parser)) 00123 return false; 00124 00125 // If no action has been set, checks if a checksums' file must be opened. 00126 if (!checkOpenChecksumsFile(parser)) 00127 return false; 00128 00129 return true; 00130 } 00131 //--------------------------------------------------------------------------- 00132 00133 00134 /** 00135 * Cleans up the structures that take memory. 00136 * 00137 * Call this function when you do not need the command line options. 00138 */ 00139 void CmdLineOptions::cleanup() 00140 { 00141 files.Clear(); 00142 checksumsFileName.Clear(); 00143 } 00144 //--------------------------------------------------------------------------- 00145 00146 00147 /** 00148 * Get the type of the checksums' file to create. 00149 * 00150 * If the ct parameter has been specified, return the given type, otherwise 00151 * try to get the type with the extension of the checksums' file. 00152 * 00153 * @return The type of the checksums' file to create or <CODE>cftNone</CODE> 00154 * if the type of the checksums' file cannot be determined. 00155 */ 00156 CmdLineOptions::CkSumsFileTypes CmdLineOptions::getchecksumsFileType() 00157 { 00158 CkSumsFileTypes res = createType; 00159 00160 if (res == cftNone) 00161 { 00162 // Try to get the type of the checksums' file with the extension. 00163 wxString ext; 00164 wxFileName::SplitPath(checksumsFileName, NULL, NULL, &ext); 00165 if (ext.CmpNoCase(wxT("sfv")) == 0) 00166 res = cftSFV; 00167 else if (ext.CmpNoCase(wxT("md5")) == 0) 00168 res = cftMD5; 00169 } 00170 00171 return res; 00172 } 00173 //--------------------------------------------------------------------------- 00174 00175 00176 /** 00177 * Loads the list(s) of files, delete the file that contains the list if the 00178 * <CODE>deleteTempLists</CODE> switch is activated. 00179 * 00180 * @param parser Command line parser. 00181 */ 00182 void CmdLineOptions::expandFilesList(const wxCmdLineParser& parser) 00183 { 00184 wxString lists; 00185 if (!parser.Found(wxT("fl"), &lists)) 00186 // No file. 00187 return; 00188 00189 wxStringTokenizer tkzLists(lists, wxT("|"), wxTOKEN_STRTOK); 00190 while (tkzLists.HasMoreTokens()) 00191 { 00192 wxString fileName = tkzLists.GetNextToken(); 00193 if (!fileName.empty()) 00194 { 00195 wxLogNull logNo; // No log 00196 bool read = false; // the file has been opened and read ? 00197 00198 { // open a block to destroy the wxFileInputStream before erasing the file 00199 wxFileInputStream input(fileName); 00200 if (input.Ok()) 00201 { 00202 // Reads the lines 00203 wxTextInputStream text(input); 00204 wxString line; 00205 00206 line = text.ReadLine(); 00207 while (!input.Eof()) 00208 { 00209 line.Strip(wxString::both); 00210 if (!line.empty()) 00211 files.Add(line); 00212 line = text.ReadLine(); 00213 } 00214 read = true; 00215 } 00216 } 00217 00218 if (read && deleteTempLists) 00219 { 00220 // Checks if the read file has the extension of the temp file. 00221 wxString ext; 00222 wxFileName::SplitPath(fileName, NULL, NULL, NULL, &ext); 00223 wxString tmpExts = wxConfig::Get()->Read(_T("Files/TempExts"), _T("tmp temp")); 00224 wxStringTokenizer tkz(tmpExts, wxT(" \t\r\n"), wxTOKEN_STRTOK); 00225 bool found = false; 00226 while (!found && tkz.HasMoreTokens()) 00227 if (::compareFileName(tkz.GetNextToken(), ext) == 0) 00228 found = true; 00229 00230 if (found) 00231 ::wxRemoveFile(fileName); 00232 } 00233 } 00234 } 00235 } 00236 //--------------------------------------------------------------------------- 00237 00238 00239 /** 00240 * Make all the names of the files in the array of files absolute. 00241 * 00242 * You should call <CODE>expandFilesList</CODE> before to be sure that the 00243 * list of names of files doesn't contain pointer to list of files. 00244 * 00245 * @param cwd Reference path to make the paths absolute. Must be an absolute 00246 * path (if not, nothing is done) and don't contains a name of file. 00247 */ 00248 void CmdLineOptions::makeFilesListAbsolute(const wxString& cwd) 00249 { 00250 wxFileName refPath(cwd, wxString()); 00251 if (!refPath.IsAbsolute()) 00252 return; 00253 00254 wxString ref = refPath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR); 00255 00256 size_t l = files.GetCount(); 00257 for (size_t i = 0; i < l; i++) 00258 { 00259 wxFileName fn(files[i]); 00260 if (!fn.IsAbsolute()) 00261 { 00262 fn.MakeAbsolute(ref); 00263 files[i] = fn.GetFullPath(); 00264 } 00265 } 00266 } 00267 //--------------------------------------------------------------------------- 00268 00269 00270 /** 00271 * Checks the compatibility of the verify switch with other options or switchs. 00272 * 00273 * param parser Command line parser. 00274 * @return <CODE>true</CODE> if the verify switch is compatible with other 00275 * options or switchs, <CODE>false</CODE> otherwise. 00276 */ 00277 bool CmdLineOptions::checkVerifySwitch(const wxCmdLineParser& parser) 00278 { 00279 if (parser.Found(wxT("v"))) 00280 { 00281 // Checks for incompatible switchs or options. 00282 if (parser.Found(wxT("c"))) 00283 { 00284 msgOut->Printf(_("The verify switch and the create option are incompatibles.")); 00285 return false; 00286 } 00287 if (parser.Found(wxT("a"))) 00288 { 00289 msgOut->Printf(_("The verify switch and the append option are incompatibles.")); 00290 return false; 00291 } 00292 00293 // Checks the number of given file 00294 if (files.IsEmpty()) 00295 { 00296 msgOut->Printf(_("The verify switch needs that you specify one file to check.")); 00297 return false; 00298 } 00299 if (files.GetCount() > 1) 00300 { 00301 msgOut->Printf(_("The verify switch needs that you specify only one file to check.")); 00302 return false; 00303 } 00304 00305 // Checks the name of the file to open 00306 files[0] = files[0].Strip(wxString::both); 00307 if (files[0].empty()) 00308 { 00309 msgOut->Printf(_("The name of the file to check is empty.")); 00310 return false; 00311 } 00312 00313 // Make the file name to open absolute if needed. 00314 wxFileName fn(files[0]); 00315 if (!fn.IsAbsolute()) 00316 { 00317 fn.MakeAbsolute(wxFileName::GetCwd()); 00318 files[0] = fn.GetFullPath(); 00319 } 00320 00321 // Set the action to do. 00322 action = aVerify; 00323 } 00324 00325 return true; 00326 } 00327 //--------------------------------------------------------------------------- 00328 00329 00330 /** 00331 * Checks the compatibility of the append option with other options or switchs. 00332 * 00333 * @param parser Command line parser. 00334 * @return <CODE>true</CODE> if the append option is compatible with other 00335 * options or switchs, <CODE>false</CODE> otherwise. 00336 */ 00337 bool CmdLineOptions::checkAppendOption(const wxCmdLineParser& parser) 00338 { 00339 wxString param; 00340 00341 if (parser.Found(wxT("a"), &param)) 00342 { 00343 param = param.Strip(wxString::both); 00344 00345 // Remove the path separators at the end of the file name 00346 wxString seps = wxFileName::GetPathSeparators(); 00347 while (!param.empty() && seps.Find(param.Last()) != -1) 00348 param.RemoveLast(); 00349 00350 wxFileName fn(param); 00351 00352 // Checks for incompatible switchs or options. 00353 if (parser.Found(wxT("v"))) 00354 { 00355 msgOut->Printf(_("The append option and the verify switch are incompatibles.")); 00356 return false; 00357 } 00358 if (parser.Found(wxT("c"))) 00359 { 00360 msgOut->Printf(_("The append and create options are incompatibles.")); 00361 return false; 00362 } 00363 00364 // Checks if the file name where the checksums will be added is empty. 00365 if (param.empty()) 00366 { 00367 msgOut->Printf(_("The name of the checksums' file where the checksums will be added is empty.")); 00368 return false; 00369 } 00370 00371 // Make the file name where the checksums will be added absolute if needed. 00372 if (!fn.IsAbsolute()) 00373 fn.MakeAbsolute(wxFileName::GetCwd()); 00374 00375 // checks if the directory of the where the checksums will be added exists 00376 if (!wxFileName::DirExists(fn.GetPath(wxPATH_GET_VOLUME))) 00377 { 00378 msgOut->Printf(_("The directory %s doesn't exist."), fn.GetPath(wxPATH_GET_VOLUME).c_str()); 00379 return false; 00380 } 00381 00382 // Make the names of the files to add absolute. 00383 makeFilesListAbsolute(::wxGetCwd()); 00384 00385 checksumsFileName = fn.GetFullPath(); 00386 00387 // Set the action to do. 00388 action = aAppend; 00389 } 00390 00391 return true; 00392 } 00393 //--------------------------------------------------------------------------- 00394 00395 00396 /** 00397 * Checks the compatibility of the create option with other options or switchs. 00398 * 00399 * @param parser Command line parser. 00400 * @return <CODE>true</CODE> if the create option is compatible with other 00401 * options or switchs, <CODE>false</CODE> otherwise. 00402 */ 00403 bool CmdLineOptions::checkCreateOption(const wxCmdLineParser& parser) 00404 { 00405 wxString param; 00406 00407 if (parser.Found(wxT("c"), &param)) 00408 { 00409 param = param.Strip(wxString::both); 00410 00411 // Remove the path separators at the end of the file name 00412 wxString seps = wxFileName::GetPathSeparators(); 00413 while (!param.empty() && seps.Find(param.Last()) != -1) 00414 param.RemoveLast(); 00415 00416 wxFileName fn(param); 00417 00418 // Checks for incompatible switchs or options. 00419 if (parser.Found(wxT("v"))) 00420 { 00421 msgOut->Printf(_("The create option and the verify switch are incompatibles.")); 00422 return false; 00423 } 00424 if (parser.Found(wxT("a"))) 00425 { 00426 msgOut->Printf(_("The create and append options are incompatibles.")); 00427 return false; 00428 } 00429 00430 // Checks if the file name to create is empty. 00431 if (param.empty()) 00432 { 00433 msgOut->Printf(_("The name of the checksums' file to create is empty.")); 00434 return false; 00435 } 00436 00437 // Make the file name to create absolute if needed. 00438 if (!fn.IsAbsolute()) 00439 fn.MakeAbsolute(wxFileName::GetCwd()); 00440 00441 // checks if the directory of the file to create exists 00442 if (!wxFileName::DirExists(fn.GetPath(wxPATH_GET_VOLUME))) 00443 { 00444 msgOut->Printf(_("The directory %s doesn't exist."), fn.GetPath(wxPATH_GET_VOLUME).c_str()); 00445 return false; 00446 } 00447 00448 // Make the names of the files to add absolute. 00449 makeFilesListAbsolute(::wxGetCwd()); 00450 00451 checksumsFileName = fn.GetFullPath(); 00452 00453 // Set the action to do. 00454 action = aCreate; 00455 } 00456 00457 return true; 00458 } 00459 //--------------------------------------------------------------------------- 00460 00461 00462 /** 00463 * Checks the compatibility of the create type option with other options or 00464 * switchs. 00465 * 00466 * @param parser Command line parser. 00467 * @return <CODE>true</CODE> if the create type option is compatible with other 00468 * options or switchs, <CODE>false</CODE> otherwise. 00469 */ 00470 bool CmdLineOptions::checkCreateTypeOption(const wxCmdLineParser& parser) 00471 { 00472 wxString param; 00473 00474 if (parser.Found(wxT("ct"), &param)) 00475 { 00476 // Checks the given type 00477 param = param.Strip(wxString::both); 00478 00479 if (param.CmpNoCase(wxT("sfv")) == 0) 00480 createType = cftSFV; 00481 else if (param.CmpNoCase(wxT("md5")) == 0) 00482 createType = cftMD5; 00483 else 00484 { 00485 msgOut->Printf(_("The type of the checksums' file to create is invalid. Valids types are 'sfv' and 'md5'.")); 00486 return false; 00487 } 00488 } 00489 00490 return true; 00491 } 00492 //--------------------------------------------------------------------------- 00493 00494 00495 /** 00496 * If no action has been set, checks if a checksums' file must be opened. 00497 * 00498 * param parser Command line parser. 00499 * @return <CODE>true</CODE> if no error or incompatibility has been detected, 00500 * <CODE>false</CODE> otherwise. 00501 */ 00502 bool CmdLineOptions::checkOpenChecksumsFile(const wxCmdLineParser& parser) 00503 { 00504 if (action == aNone) 00505 { 00506 if (files.GetCount() > 1) 00507 { 00508 msgOut->Printf(_("You can specify only one file to check at the same time.")); 00509 return false; 00510 } 00511 00512 if (files.GetCount() == 1) 00513 { 00514 // Checks the name of the file to open 00515 files[0] = files[0].Strip(wxString::both); 00516 if (files[0].empty()) 00517 { 00518 msgOut->Printf(_("The name of the file to check is empty.")); 00519 return false; 00520 } 00521 00522 // Make the file name to open absolute if needed. 00523 wxFileName fn(files[0]); 00524 if (!fn.IsAbsolute()) 00525 { 00526 fn.MakeAbsolute(wxFileName::GetCwd()); 00527 files[0] = fn.GetFullPath(); 00528 } 00529 00530 // Set the action to do. 00531 action = aOpen; 00532 } 00533 } 00534 00535 return true; 00536 } 00537 //---------------------------------------------------------------------------

Generated on Sun May 30 13:37:44 2004 for wxChecksums by doxygen 1.3.7