@@ -270,16 +270,20 @@ procedure TFormTrackerModify.FormCreate(Sender: TObject);
270270 Width := Constraints.MinWidth;
271271 Height := Constraints.MinHeight;
272272
273+ // there must be one command line or more for a console mode.
274+ FConcoleMode := ParamCount > 0 ;
275+
273276 // Show the default trackers
274277 LoadTrackersTextFileAddTrackers;
275278
276279 // Load the unwanted trackers list.
277280 LoadTrackersTextFileRemoveTrackers;
278281
279-
280-
281- // Check is program is started as console
282- ConsoleMode;
282+ // Start program in console mode
283+ if FConcoleMode then
284+ begin
285+ ConsoleMode;
286+ end ;
283287
284288 // Update some captions
285289 Caption := FORM_CAPTION;
@@ -683,9 +687,15 @@ procedure TFormTrackerModify.UpdateTorrent;
683687 begin
684688 // When succesfull the log file shows, 3 lines,
685689 // OK + Count torrent files + Count Trackers
686- FTrackerList.LogStringList.Add(' OK' );
687- FTrackerList.LogStringList.Add(IntToStr(FTrackerList.TorrentFileNameList.Count));
688- FTrackerList.LogStringList.Add(IntToStr(CountTrackers));
690+
691+ // if there is already a items inside there there must be something wrong.
692+ // Do not add 'OK'
693+ if FTrackerList.LogStringList.Count = 0 then
694+ begin
695+ FTrackerList.LogStringList.Add(CONSOLE_SUCCESS_STATUS);
696+ FTrackerList.LogStringList.Add(IntToStr(FTrackerList.TorrentFileNameList.Count));
697+ FTrackerList.LogStringList.Add(IntToStr(CountTrackers));
698+ end ;
689699 end
690700 else
691701 begin
@@ -782,123 +792,104 @@ procedure TFormTrackerModify.ConsoleMode;
782792var
783793 FileNameOrDirStr: UTF8String;
784794 StringList: TStringList;
795+ MustExitWithErrorCode: boolean;
785796begin
786- // The first parameter[1] is path to file or dir.
787- // if program is started with two parameter then in must be a console mode.
797+ // There are two options
798+ // -
799+ // One parameter only
800+ // This is the first tracker-editor version with only 'sort' trackers list.
801+ // The first parameter[1] is path to file or dir.
802+ // -
803+ // Two parameter version
804+ // This is later version where there is more selection about the tracker list.
805+
806+ // Will be set to True when error occure.
807+ MustExitWithErrorCode := False;
788808
789- // update the torrent via console mode if there is a parameter detected.
790- if ParamCount > 0 then
791- begin
792- // there must be 2 command line items for console mode
793- FConcoleMode := ParamCount = 2 ;
794-
795- try
796- if FConcoleMode then
797- begin
798- // Create the log file. The old one will be overwritten
799- AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + LOG_FILE_NAME);
800- ReWrite(FLogFile);
801- end ;
809+ try
810+ // Create the log file. The old one will be overwritten
811+ AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + FILE_NAME_CONSOLE_LOG);
812+ ReWrite(FLogFile);
802813
803- // Get the startup command lime parameters.
804- // ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList);
814+ // Get the startup command lime parameters.
815+ if ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList) then
816+ begin
817+ // There is no error. Proceed with reading the torrent files
805818
806- // If FTrackerList.LogStringList empty then there is no error.
807- // if FTrackerList.LogStringList.Text = '' then
808- if ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList) then
819+ if ExtractFileExt(FileNameOrDirStr) = ' ' then
820+ begin // There is no file extention. It must be a folder.
821+ if LoadTorrentViaDir(FileNameOrDirStr) then
822+ begin
823+ // Show all the tracker inside the torrent files.
824+ ShowTrackerInsideFileList;
825+ // Mark all trackers as selected
826+ CheckedOnOffAllTrackers(True);
827+ // Some tracker must be removed. Console and windows mode.
828+ UpdateViewRemoveTracker;
829+ // update torrent
830+ UpdateTorrent;
831+ end ;
832+ end
833+ else // a single torrent file is selected?
809834 begin
810- // There is no error. Proceed with reading the torrent files
811-
812- if ExtractFileExt(FileNameOrDirStr) = ' ' then
813- begin // There is no file extention. It must be a folder.
814- if LoadTorrentViaDir(FileNameOrDirStr) then
815- begin
816- // Show all the tracker inside the torrent files.
817- ShowTrackerInsideFileList;
818- // Mark all trackers as selected
819- CheckedOnOffAllTrackers(True);
820- // Some tracker must be removed. Console and windows mode.
821- UpdateViewRemoveTracker;
822-
823- if FConcoleMode then
835+ if ExtractFileExt(FileNameOrDirStr) = ' .torrent' then
836+ begin
837+ StringList := TStringList.Create;
838+ try
839+ // Convert Filenames to stringlist format.
840+ StringList.Add(FileNameOrDirStr);
841+
842+ // Extract all the trackers inside the torrent file
843+ if AddTorrentFileList(StringList) then
824844 begin
845+ // Show all the tracker inside the torrent files.
846+ ShowTrackerInsideFileList;
847+ // Mark all trackers as selected
848+ CheckedOnOffAllTrackers(True);
849+ // Some tracker must be removed. Console and windows mode.
850+ UpdateViewRemoveTracker;
825851 // update torrent
826852 UpdateTorrent;
827853 end ;
828854
855+ finally
856+ StringList.Free;
829857 end ;
830858 end
831- else // a torrent file is selected?
832- begin
833- if ExtractFileExt(FileNameOrDirStr) = ' .torrent' then
834- begin
835- StringList := TStringList.Create;
836- try
837- // Convert Filenames to stringlist format.
838- StringList.Add(FileNameOrDirStr);
839-
840- // Extract all the trackers inside the torrent file
841- if AddTorrentFileList(StringList) then
842- begin
843- // Show all the tracker inside the torrent files.
844- ShowTrackerInsideFileList;
845- // Mark all trackers as selected
846- CheckedOnOffAllTrackers(True);
847- // Some tracker must be removed. Console and windows mode.
848- UpdateViewRemoveTracker;
849-
850- if FConcoleMode then
851- begin
852- // update torrent
853- UpdateTorrent;
854- end ;
855-
856- end ;
857-
858- finally
859- StringList.Free;
860- end ;
861- end
862- else
863- begin // Error. this is not a torrent file
864- FTrackerList.LogStringList.Add(' ERROR: No torrent file selected.' );
865- end ;
859+ else
860+ begin // Error. this is not a torrent file
861+ FTrackerList.LogStringList.Add(' ERROR: No torrent file selected.' );
866862 end ;
867863 end ;
864+ end ;
868865
869- if FConcoleMode then
870- begin
871- // Write to log file. And close the file.
872- WriteLn(FLogFile, FTrackerList.LogStringList.Text);
873- CloseFile(FLogFile);
874-
875- // Shutdown the console program
876- Application.terminate;
877- end ;
866+ // Write to log file. And close the file.
867+ WriteLn(FLogFile, FTrackerList.LogStringList.Text);
868+ CloseFile(FLogFile);
878869
870+ // check if log data is success full
871+ // if (no data) or (not CONSOLE_SUCCESS_STATUS) then error
872+ MustExitWithErrorCode := FTrackerList.LogStringList.Count = 0 ;
873+ if not MustExitWithErrorCode then
874+ begin
875+ MustExitWithErrorCode := FTrackerList.LogStringList[0 ] <> CONSOLE_SUCCESS_STATUS;
876+ end ;
879877
880- except
881- if FConcoleMode then
882- begin
883- // Shutdown the console program.
884- // This is needed or else the program will keep running forever.
885- // exit with error code
886- System.ExitCode := 1 ;
887- Application.terminate;
888- end
889- else
890- begin
891- // show to the end user that something is wrong
892- raise;
893- end ;
878+ except
879+ // Shutdown the console program.
880+ // This is needed or else the program will keep running forever.
881+ // exit with error code
882+ MustExitWithErrorCode := True;
883+ end ;
894884
895- end ;
896- end
897- else
898- begin // the program
899- FConcoleMode := False;
885+ if MustExitWithErrorCode then
886+ begin
887+ // exit with error code
888+ System.ExitCode := 1 ;
900889 end ;
901890
891+ // Always shutdown the program when in console mode.
892+ Application.terminate;
902893end ;
903894
904895
0 commit comments