delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Converting from Windows ASCII to UNIX ASCII Text FilesGo to Lloyd Kinsella's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
11/09/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet workshell windows unix linux text files format converting
Times Scored:
3
Visits:
6343
Uploader: Lloyd Kinsella
Company: Workshell Inc
Reference: www.workshell.co.uk
 
Question/Problem/Abstract:
How do you convert between Windows and UNIX ASCII files? Here's how to stop Notepad from displaying a mess...
Answer:



Ever loaded what you beleived to be a nice text file (HTML as well) into Notepad and found it to be a complete mess?

If yes then the reason is because Microsoft Windows uses CRLF (0x0D 0x0A) to end lines and UNIX, Linux, Apple Mac and most other OS's use just LF (0x0A) - they just have to be awkward don't they...

Below are two routines you can use to switch between formats, it also shows an example of callbacks and streams:

type
TConvertNotifyKind = (nkMax, nkProgress);
TConvertNotify = procedure(Kind: TConvertNotifyKind; Value: LongInt);

// UnixToWin -- Convert a UNIX single LF text stream to a Windows CRLF text stream
function UnixToWin(InStream: TStream; OutStream: TStream; Notify: TConvertNotify): Boolean;
var
B, NewB: Byte;
Value: LongInt;
begin
if not Assigned(InStream) then
  begin
   Result := False;
   Exit;
  end;
if not Assigned(OutStream) then
  begin
   Result := False;
   Exit;
  end;
if Assigned(Notify) then Notify(nkMax,InStream.Size);
Value := 0;
while InStream.Position < InStream.Size do
  begin
   InStream.Read(B,SizeOf(Byte));
    case B of
     $0A: begin
           NewB := $0D;
           OutStream.Write(NewB,SizeOf(Byte));
           NewB := $0A;
           OutStream.Write(NewB,SizeOf(Byte));
           end;
    else
     OutStream.Write(B,SizeOf(Byte));
    end;
   Inc(Value);
   if Assigned(Notify) then Notify(nkProgress,Value);
  end;
if Value = InStream.Size then
  begin
   if Assigned(Notify) then Notify(nkProgress,0);
  end;
Result := True;
end;

// WinToUnix -- Convert a CRLF Windows text stream to single LF UNIX text stream
function WinToUnix(InStream: TStream; OutStream: TStream; Notify: TConvertNotify): Boolean;
var
B: Byte;
Value: LongInt;
begin
if not Assigned(InStream) then
  begin
   Result := False;
   Exit;
  end;
if not Assigned(OutStream) then
  begin
   Result := False;
   Exit;
  end;
if Assigned(Notify) then Notify(nkMax,InStream.Size);
Value := 0;
while InStream.Position < InStream.Size do
  begin
   InStream.Read(B,SizeOf(Byte));
    if B <> $0A then
     begin
      OutStream.Write(B,SizeOf(Byte));
     end;
    Inc(Value);
    if Assigned(Notify) then Notify(nkProgress,Value);
   end;
OutStream.Seek(SizeOf(Byte),soFromEnd);
OutStream.Read(B,SizeOf(B));
  if B <> $0D then
   begin
    B := $0D;
    OutStream.Write(B,SizeOf(Byte));
   end;
if Value = InStream.Size then
  begin
   if Assigned(Notify) then Notify(nkProgress,0);
  end;
Result := True;
end;

------------------------------------------------------------------------------

P.S: If this is wrong, let me know!





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Another (quick and dirty) solution
    Stefan Pettersson (Nov 10 2001 7:12PM)

If the file you should convert (from UNIX to Windows) then there is a really quick-and-dirty solution by simply loading the file into a TStringList and then saving it back.
  
with TStringlist.Create do
begin
  LoadFromFile(filename);
  SaveToFile(filename);
end;

It's dirty, so use with care...

Respond

Unnecessary
    César Nicolás Peña Núñez (Nov 10 2001 6:46PM)

If for instance you have a memo, and write memo1.lines.text := UnixText; delphi will automatically convert all LF to CRLF you need
Respond

nice
    Eber Irigoyen (Nov 9 2001 5:48PM)

nice example of callbacks, but for the less advanced, I think you should put at least an example of calls to your converting functions

If (UnixToWin(InStream, OutStream, NotifyProcedure) ) Then
...
in this case the callback will be to the NotifyProcedure, which you can implement to show a progressbar on the form as I'll show you
Or
If (UnixToWin(InStream, OutStream, nil) )  Then
...
in this case there's no callback, but it still does the job =o)

and give an idea of how your procedure looks like, which I personally would've done like this:
procedure NotifyProcedure(Kind: TConvertNotifyKind; Value: LongInt; ProgressBar:TProgressBar);
Begin
  If (Kind=nkProgress) Then
    ProgressBar.Position:=Value
  Else
    ProgressBar.Max:=Value    
End

best regards
EberSys
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)