00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
#include <wx/wxprec.h>
00029
00030
#ifdef __BORLANDC__
00031
#pragma hdrstop
00032
#endif
00033
00034
#ifndef WX_PRECOMP
00035
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
00056
using namespace std;
00057
00058
00059
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
00069 static wxMessageOutput*
msgOut = NULL;
00070
00071
00072
00073
00074
00075
00076
00077
00078 CmdLineOptions::CmdLineOptions()
00079 {
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
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
00111 size_t paramCount = parser.GetParamCount();
00112
for (size_t i = 0; i < paramCount; i++)
00113
files.Add(parser.GetParam(i));
00114
00115
00116
deleteTempLists = parser.Found(wxT(
"delete-temp-list"));
00117
expandFilesList(parser);
00118
00119
00120
00121
if (!
checkVerifySwitch(parser) || !
checkCreateOption(parser) ||
00122 !
checkAppendOption(parser) || !
checkCreateTypeOption(parser))
00123
return false;
00124
00125
00126
if (!
checkOpenChecksumsFile(parser))
00127
return false;
00128
00129
return true;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 void CmdLineOptions::cleanup()
00140 {
00141
files.Clear();
00142
checksumsFileName.Clear();
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 CmdLineOptions::CkSumsFileTypes CmdLineOptions::getchecksumsFileType()
00157 {
00158
CkSumsFileTypes res =
createType;
00159
00160
if (res == cftNone)
00161 {
00162
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
00178
00179
00180
00181
00182 void CmdLineOptions::expandFilesList(
const wxCmdLineParser& parser)
00183 {
00184 wxString lists;
00185
if (!parser.Found(wxT(
"fl"), &lists))
00186
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;
00196
bool read =
false;
00197
00198 {
00199 wxFileInputStream input(fileName);
00200
if (input.Ok())
00201 {
00202
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
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
00241
00242
00243
00244
00245
00246
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
00272
00273
00274
00275
00276
00277 bool CmdLineOptions::checkVerifySwitch(
const wxCmdLineParser& parser)
00278 {
00279
if (parser.Found(wxT(
"v")))
00280 {
00281
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
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
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
00314 wxFileName fn(files[0]);
00315
if (!fn.IsAbsolute())
00316 {
00317 fn.MakeAbsolute(wxFileName::GetCwd());
00318 files[0] = fn.GetFullPath();
00319 }
00320
00321
00322
action = aVerify;
00323 }
00324
00325
return true;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 bool CmdLineOptions::checkAppendOption(
const wxCmdLineParser& parser)
00338 {
00339 wxString param;
00340
00341
if (parser.Found(wxT(
"a"), ¶m))
00342 {
00343 param = param.Strip(wxString::both);
00344
00345
00346 wxString seps = wxFileName::GetPathSeparators();
00347
while (!param.empty() && seps.Find(param.Last()) != -1)
00348 param.RemoveLast();
00349
00350 wxFileName fn(param);
00351
00352
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
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
00372
if (!fn.IsAbsolute())
00373 fn.MakeAbsolute(wxFileName::GetCwd());
00374
00375
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
00383
makeFilesListAbsolute(::wxGetCwd());
00384
00385
checksumsFileName = fn.GetFullPath();
00386
00387
00388
action = aAppend;
00389 }
00390
00391
return true;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 bool CmdLineOptions::checkCreateOption(
const wxCmdLineParser& parser)
00404 {
00405 wxString param;
00406
00407
if (parser.Found(wxT(
"c"), ¶m))
00408 {
00409 param = param.Strip(wxString::both);
00410
00411
00412 wxString seps = wxFileName::GetPathSeparators();
00413
while (!param.empty() && seps.Find(param.Last()) != -1)
00414 param.RemoveLast();
00415
00416 wxFileName fn(param);
00417
00418
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
00431
if (param.empty())
00432 {
00433
msgOut->Printf(_(
"The name of the checksums' file to create is empty."));
00434
return false;
00435 }
00436
00437
00438
if (!fn.IsAbsolute())
00439 fn.MakeAbsolute(wxFileName::GetCwd());
00440
00441
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
00449
makeFilesListAbsolute(::wxGetCwd());
00450
00451
checksumsFileName = fn.GetFullPath();
00452
00453
00454
action = aCreate;
00455 }
00456
00457
return true;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 bool CmdLineOptions::checkCreateTypeOption(
const wxCmdLineParser& parser)
00471 {
00472 wxString param;
00473
00474
if (parser.Found(wxT(
"ct"), ¶m))
00475 {
00476
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
00497
00498
00499
00500
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
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
00523 wxFileName fn(files[0]);
00524
if (!fn.IsAbsolute())
00525 {
00526 fn.MakeAbsolute(wxFileName::GetCwd());
00527 files[0] = fn.GetFullPath();
00528 }
00529
00530
00531
action = aOpen;
00532 }
00533 }
00534
00535
return true;
00536 }
00537