this code analyses the first 5*1024 bytes of data from a file and tells which break type it uses, or if it's a binary file...
1
2 //+ Jonas Raoni Soares Silva
3 //@ http://jsfromhell.com
4
5 type
6 TBreakType = ( btNone, btWin, btMac, btLin, btBin );
7
8 ---------
9
10 function GetBreakType( const Filename: string; const MaxDataToRead: Cardinal = 5*1024 ): TBreakType;
11 var
12 FS: TFileStream;
13 Buffer, BufferStart, BufferEnd: PChar;
14 begin
15 Result := btNone;
16 if not FileExists( Filename ) then
17 raise Exception.Create( 'GetBreakType: nome de arquivo inválido' );
18 try
19 FS := TFileStream.Create( Filename, fmOpenRead );
20 GetMem( Buffer, MaxDataToRead+1 );
21 BufferEnd := ( Buffer + FS.Read( Buffer^, MaxDataToRead ) );
22 BufferStart := Buffer;
23 BufferEnd^ :=
24 except
25 raise Exception.Create( 'GetBreakType: erro alocando memória.' );
26 end;
27 try
28 while Buffer^ <>
29 if Result = btNone then
30 if Buffer^ = ASCII_CR then begin
31 if (Buffer+1)^ = ASCII_LF then begin
32 Result := btWin;
33 Inc( Buffer );
34 end
35 else
36 Result := btMac;
37 end
38 else if Buffer^ = ASCII_LF then
39 Result := btLin;
40 Inc( Buffer );
41 end;
42 if Buffer <> BufferEnd then
43 Result := btBin;
44 finally
45 FreeMem( BufferStart, MaxDataToRead+1 );
46 FS.Free;
47 end;
48 end;