Advertisement
Guest User

Max file name sorting

a guest
Aug 4th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.61 KB | None | 0 0
  1. program maxLines;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils, Windows, Classes, ShlObj, IOUtils, Generics.Collections,  Generics.Defaults;
  7.  
  8. type
  9.   TLines = class
  10.   private
  11.     FLineLength: Integer;
  12.     FLines: TList<Integer>;
  13.   public
  14.     constructor Create(aLineLength, aLine: Integer);
  15.     property LineLength: Integer read FLineLength write FLineLength;
  16.     property Lines: TList<Integer> read FLines;
  17.   end;
  18.  
  19. constructor TLines.Create(aLineLength, aLine: Integer);
  20. begin
  21.   inherited Create;
  22.   FLines := TList<Integer>.Create();
  23.   FLineLength:= aLineLength;
  24.   FLines.Add(aLine);
  25. end;
  26.  
  27. function CompareByLineLength(const Left, Right: TLines): Integer;
  28. begin
  29.   Result := Left.LineLength - Right.LineLength;
  30. end;
  31.  
  32. function GetDocumentsPath: string;
  33. var
  34.   LStr: array[0 .. MAX_PATH] of Char;
  35. begin
  36.   SetLastError(ERROR_SUCCESS);
  37.   if SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, @LStr) = S_OK then
  38.     Result := LStr;
  39. end;
  40.  
  41. procedure PrintList(FList: TList<TLines>);
  42. var element: TLines; Line: Integer;
  43. begin
  44.   for element in FList do begin
  45.     Write(Format('%d: ', [element.LineLength]));
  46.     for Line in element.Lines do
  47.       Write(Format('%d ', [Line]));
  48.     Writeln;
  49.   end;
  50. end;
  51.  
  52. const
  53.   LinesMax = 10;
  54. var
  55.   fs: TStringStream;
  56.   fsReader: TStreamReader;
  57.   FLines: TList<TLines>;
  58.   CurrentLine, CurrentLineLength, LineIndex: Integer;
  59.   FLine: TLines;
  60.   FileName: String;
  61. begin
  62.   CurrentLine:= 0;
  63.   FLines := TList<TLines>.Create(TComparer<TLines>.Construct(CompareByLineLength));
  64.   fs:= TStringStream.Create('', TEncoding.UTF8);
  65.   fsReader:= TStreamReader.Create(fs, TEncoding.UTF8);
  66.   try
  67.     try
  68.       fs.LoadFromFile(TPath.Combine(GetDocumentsPath, '123.txt'));
  69.       while not fsReader.EndOfStream do begin
  70.         Inc(CurrentLine);
  71.         FileName:= fsReader.ReadLine;
  72.         CurrentLineLength:= Length(ExtractFileName(FileName));
  73.         FLine := TLines.Create(CurrentLineLength, CurrentLine);
  74.         if FLines.BinarySearch(FLine, LineIndex) then
  75.         begin
  76.           FLines[LineIndex].Lines.Add(CurrentLine);
  77.         end
  78.         else if LineIndex > 0 then begin
  79.           if FLines.Count = LinesMax then begin
  80.             FLines.Extract(FLines[0]);
  81.             FLines.Insert(LineIndex-1, FLine);
  82.           end else
  83.             FLines.Insert(LineIndex, FLine);
  84.         end else begin
  85.           if FLines.Count < LinesMax then FLines.Insert(0, FLine);
  86.         end;
  87.       end;
  88.       PrintList(FLines);
  89.       Readln;
  90.     except
  91.       on E: Exception do begin
  92.         Writeln(E.ClassName, ': ', E.Message);
  93.         Readln;
  94.       end;
  95.     end;
  96.   finally
  97.     fs.Free;
  98.   end;
  99. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement