각 OS별 텍스트 파일의 NewLine 처리는 다음과 같음.
LF (0x0A) : Unix, Linux, Mac OS X, FreeBSD, BeOS, Amiga, RISC OS
CR+LF (0x0D+0x0A) : MS-DOS, OS/2, Microsoft Windows, Symbian OS
CR (0x0D) : Mac OS up to version 9 and OS-9
CR+LF (0x0D+0x0A) : MS-DOS, OS/2, Microsoft Windows, Symbian OS
CR (0x0D) : Mac OS up to version 9 and OS-9
이기종 간 파일 전송 후 텍스트 파일의 보정을 한다던지 하기 위해 아래와 같은 클래스를 만들어 사용.
쓰는거래봐야 델파이에선 Unix -> Windows 정도겠지만 까먹지 않기 위해 기록해 둠.
unit uNewLine;
interface
Windows, SysUtils;
type
TNewLine=class
const
NO_NEWLINE=0;
UNIX=1;
WINDOWS=2;
MAC=3;
public
class function GetNewLineType(s: String): Integer;
class function ToUnix(s: String): String;
class function ToWindows(s: String): String;
class function ToMac(s: String): String;
class function UnixToWindows(s: String): String;
class function WindowsToUnix(s: String): String;
class function UnixToMac(s: String): String;
class function MacToUnix(s: String): String;
class function WindowsToMac(s: String): String;
class function MacToWindows(s: String): String;
end;
implementation
{ TNewLine }
class function TNewLine.GetNewLineType(s: String): Integer;
begin
if Pos(#13#10, s)>0 then Result:=Self.WINDOWS
else if Pos(#10, s)>0 then Result:=Self.UNIX
else if Pos(#13, s)>0 then Result:=Self.MAC
else Result:=Self.NO_NEWLINE;
end;
class function TNewLine.ToUnix(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if itype=Self.WINDOWS then Result:=Self.WindowsToUnix(s)
else if iType=Self.MAC then Result:=Self.MacToUnix(s);
end;
class function TNewLine.ToWindows(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if iType=Self.UNIX then Result:=Self.UnixToWindows(s)
else if iType=Self.MAC then Result:=Self.MacToWindows(s);
end;
class function TNewLine.ToMac(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if iType=Self.UNIX then Result:=Self.UnixToMac(s)
else if iType=Self.WINDOWS then Result:=Self.WindowsToMac(s);
end;
class function TNewLine.UnixToWindows(s: String): String;
begin
Result:=StringReplace(s, #10, #13#10, [rfReplaceAll]);
end;
class function TNewLine.WindowsToUnix(s: String): String;
begin
Result:=StringReplace(s, #13#10, #10, [rfReplaceAll]);
end;
class function TNewLine.UnixToMac(s: String): String;
begin
Result:=StringReplace(s, #10, #13, [rfReplaceAll]);
end;
class function TNewLine.MacToUnix(s: String): String;
begin
Result:=StringReplace(s, #13, #10, [rfReplaceAll]);
end;
class function TNewLine.WindowsToMac(s: String): String;
begin
Result:=StringReplace(s, #13#10, #13, [rfReplaceAll]);
end;
class function TNewLine.MacToWindows(s: String): String;
begin
Result:=StringReplace(s, #13, #13#10, [rfReplaceAll]);
end;
interface
Windows, SysUtils;
type
TNewLine=class
const
NO_NEWLINE=0;
UNIX=1;
WINDOWS=2;
MAC=3;
public
class function GetNewLineType(s: String): Integer;
class function ToUnix(s: String): String;
class function ToWindows(s: String): String;
class function ToMac(s: String): String;
class function UnixToWindows(s: String): String;
class function WindowsToUnix(s: String): String;
class function UnixToMac(s: String): String;
class function MacToUnix(s: String): String;
class function WindowsToMac(s: String): String;
class function MacToWindows(s: String): String;
end;
implementation
{ TNewLine }
class function TNewLine.GetNewLineType(s: String): Integer;
begin
if Pos(#13#10, s)>0 then Result:=Self.WINDOWS
else if Pos(#10, s)>0 then Result:=Self.UNIX
else if Pos(#13, s)>0 then Result:=Self.MAC
else Result:=Self.NO_NEWLINE;
end;
class function TNewLine.ToUnix(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if itype=Self.WINDOWS then Result:=Self.WindowsToUnix(s)
else if iType=Self.MAC then Result:=Self.MacToUnix(s);
end;
class function TNewLine.ToWindows(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if iType=Self.UNIX then Result:=Self.UnixToWindows(s)
else if iType=Self.MAC then Result:=Self.MacToWindows(s);
end;
class function TNewLine.ToMac(s: String): String;
var iType: Integer;
begin
Result:=s;
iType:=Self.GetNewLineType(s);
if iType=Self.UNIX then Result:=Self.UnixToMac(s)
else if iType=Self.WINDOWS then Result:=Self.WindowsToMac(s);
end;
class function TNewLine.UnixToWindows(s: String): String;
begin
Result:=StringReplace(s, #10, #13#10, [rfReplaceAll]);
end;
class function TNewLine.WindowsToUnix(s: String): String;
begin
Result:=StringReplace(s, #13#10, #10, [rfReplaceAll]);
end;
class function TNewLine.UnixToMac(s: String): String;
begin
Result:=StringReplace(s, #10, #13, [rfReplaceAll]);
end;
class function TNewLine.MacToUnix(s: String): String;
begin
Result:=StringReplace(s, #13, #10, [rfReplaceAll]);
end;
class function TNewLine.WindowsToMac(s: String): String;
begin
Result:=StringReplace(s, #13#10, #13, [rfReplaceAll]);
end;
class function TNewLine.MacToWindows(s: String): String;
begin
Result:=StringReplace(s, #13, #13#10, [rfReplaceAll]);
end;
'Delphi' 카테고리의 다른 글
[Delphi] 폰트 크기 픽셀로 가져오기. (Font, Pixel) (0) | 2010.06.08 |
---|---|
[Delphi] Hook (WH_GETMESSAGE, WH_CALLWNDPROC) (0) | 2010.06.07 |
[Delphi] 실행파일 버전정보 가져오기 (0) | 2010.06.04 |
[Delphi] 문자열이 숫자로만 되어 있는지 체크 (0) | 2010.06.03 |
[Delphi] 윈도우 다시 그리기 (0) | 2010.06.01 |