ChangeLE converts line endings on files both to, and from, Windows and Unix/OSX style line endings.



Use (at your own risk) and only on single byte character set files!

By Joe C. Hecht A.K.A. TJoe

TJoe's splot at CODE4SALE, LLC

TJoe's Coding 101 Page

Contact TJoe



Pascal source is provided, and compiles using most all versions of Delphi or Free Pascal.

Binaries are provided for the following platforms:

Windows Intel x32
Windows Intel x64
Linux Intel x32
Linux Intel x64
Linix ARM (such as the Raspberry Pi*, BeagleBone*, Banana Pi*).

*Trademarks and copyrights are trademarks and copyrights of their respective owners.

Download the zip



program changele;



//------------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// LEGALESE - BEGIN - ENGLISH - WESTERN ISO-IEC 8859-1.
// -----------------------------------------------------------------------------
// COPYRIGHT (c) 2010-2016 by CODE4SALE, LLC - ALL RIGHTS RESERVED.
// THE (c) USED ABOVE IS INTENDED TO DENOTE THE TRUE COPYRIGHT SYMBOL AS USED
// IN THE NOTICE BELOW THAT MAY NOT PROPERLY DISPLAY ON ALL COMPUTER SYSTEMS:
// COPYRIGHT © 2010-2016 by CODE4SALE, LLC - ALL RIGHTS RESERVED.
// CONTENTS ARE CONSIDERED CONFIDENTIAL, UNLESS OTHERWISE INDICATED.
// PLEASE SEE YOUR LICENSE AGREEMENT FOR ANY USAGE, ARCHIVAL, DISTRIBUTION, OR
// OTHER RIGHTS THAT MAY HAVE BEEN GRANTED. CONTACT: WWW.CODE4SALE.COM.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WIT.
// -----------------------------------------------------------------------------
// LEGALESE - END.
// -----------------------------------------------------------------------------
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// Compiled and tested using Delphi 6.0, RADStudio 7.0 and Free Pascal 2.6.0
// Windows 8, Linix Ubuntu 14.10, and OSX 10.10
// 32 and 64 bit compilers tested where applicable.
// To compile:
// dcc32 changele.dpr (Delphi)
// dcc64 changele.dpr (Delphi)
// fpc changele.dpr   (Free Pascal)
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// Set up for the compiler. If we are compiling under Free Pascal then act more
// "Delphi like", if not, then we are compiling under Delphi, so we will let Delphi
// know we want a command line (console) application. For later versions of
// Delphi (XE2 and above), namespacing will be required for the system units we
// use, so we will check the compiler version and add a "define" indicating we
// need to add namespacing for those affected versions.
//------------------------------------------------------------------------------
{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
//------------------------------------------------------------------------------
{$IFNDEF FPC}
  {$APPTYPE CONSOLE}
  {$IFDEF VER230} //DELPHI XE2
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
  {$IFDEF VER240} //DELPHI XE3
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
  {$IFDEF VER250} //DELPHI XE4
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
  {$IFDEF VER260} //DELPHI XE5
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
  {$IFDEF VER270} //DELPHI XE6
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
  {$IFDEF VER280} //DELPHI XE7
    {$DEFINE DELPHI_VERSION_NAMESPACING_REQUIRED}
  {$ENDIF}
{$ENDIF}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// List the other units we will use.
//------------------------------------------------------------------------------
uses
//------------------------------------------------------------------------------
{$IFDEF DELPHI_VERSION_NAMESPACING_REQUIRED}
  System.SysUtils,
  System.Classes;
{$ELSE}
  SysUtils,
  Classes;
{$ENDIF}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// Define some line ending constants:
//------------------------------------------------------------------------------
const UNIX_LINE_ENDING    = #10;       // a line feed character
const WINDOWS_LINE_ENDING = #13 + #10; // a carrage return + line feed character
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
procedure ShowLegal();
//------------------------------------------------------------------------------
begin
  writeln('');
  writeln('LEGALESE - BEGIN - ENGLISH - WESTERN ISO-IEC 8859-1.');
  writeln('');
  writeln('COPYRIGHT (c) 2010-2016 by CODE4SALE, LLC - ALL RIGHTS RESERVED.');
  writeln('THE (c) USED ABOVE IS INTENDED TO DENOTE THE TRUE COPYRIGHT SYMBOL AS USED');
  writeln('IN THE NOTICE BELOW THAT MAY NOT PROPERLY DISPLAY ON ALL COMPUTER SYSTEMS:');
  writeln('COPYRIGHT © 2010-2016 by CODE4SALE, LLC - ALL RIGHTS RESERVED.');
  writeln('CONTENTS ARE CONSIDERED CONFIDENTIAL, UNLESS OTHERWISE INDICATED.');
  writeln('PLEASE SEE YOUR LICENSE AGREEMENT FOR ANY USAGE, ARCHIVAL, DISTRIBUTION, OR');
  writeln('OTHER RIGHTS THAT MAY HAVE BEEN GRANTED. CONTACT: CODE4SALE.COM.');
  writeln('THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR');
  writeln('IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,');
  writeln('FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE');
  writeln('AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER');
  writeln('LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING');
  writeln('FROM, OUT OF OR IN CONNECTION WIT.');
  writeln('');
  writeln('LEGALESE - END.');
end;
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
procedure ShowUseage();
//------------------------------------------------------------------------------
begin
  writeln('Useage: changele [option] filename');
  writeln('Converts line endings to Windows or Linux/OSX.');
  writeln('Multibyte character files not supported.');
  writeln('Returns non-zero exit code on error.');
  writeln('option /w or -w : Convert to Windows line endings.');
  writeln('option /u or -u : Convert to Unix line endings.');
  writeln('option /h or -h : Display this help.');
  writeln('option /?       : Display this help.');
  writeln('option /help    : Display this help.');
  writeln('option -help    : Display this help.');
  writeln('option --h      : Display this help.');
  writeln('option --help   : Display this help.');
  writeln('option /legal   : Display legal info.');
  writeln('option -legal   : Display legal info.');
  writeln('Copyright (c) 2016 by CODE4SALE, LLC - All Rights Reserved.');
  writeln('Permission of use granted to all.');
  writeln('Use at your own risk - No liability assumed.');
end;
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// The Main program block
//------------------------------------------------------------------------------
var
  sl       : TStringList;   // A list of strings from the input file;
  ms       : TMemoryStream; // A buffer stream to write data to;
  idx      : integer;       // A looping variable;
  sle      : AnsiString;    // An ansi string to hold the line ending string;
  s        : string;        // A system string compatiable with our StringList;
  sAnsi    : AnsiString;    // An ansi string to hold a line to be output;
  sAnsiIdx : integer;       // A looping variable;
//------------------------------------------------------------------------------
begin
  // Check what was passed to use on the command line. If less than two strings,
  // were passed (an error), or if there is at least one string and the first
  //  string is a query for information, then display the usage info and quit.
  if ((ParamCount < 2) or
      ((ParamCount > 0) and
       ((LowerCase(ParamStr(1)) = '/?') or
        (LowerCase(ParamStr(1)) = '/h') or
        (LowerCase(ParamStr(1)) = '/help') or
        (LowerCase(ParamStr(1)) = '-help') or
        (LowerCase(ParamStr(1)) = '--help') or
        (LowerCase(ParamStr(1)) = '-h') or
        (LowerCase(ParamStr(1)) = '--h') or
        (LowerCase(ParamStr(1)) = '--h')))) then begin
    // if this is a query to show legal info, then we will show that and quit.
    if ((LowerCase(ParamStr(1)) = '-legal') or
        (LowerCase(ParamStr(1)) = '/legal')) then begin
      ShowLegal();
      halt(1);
    end;
    // Just show the normal useage help, then quit.
    ShowUseage();
    halt(2);
  end;
  // We will accept two possible possible options for the first passed parameter
  // to indicate if we are converting to Unix or Windows line ending formats.
  // If neither, then give an error and quit.
  if (not((LowerCase(ParamStr(1)) = '/u') or
          (LowerCase(ParamStr(1)) = '-u') or
          (LowerCase(ParamStr(1)) = '/w') or
          (LowerCase(ParamStr(1)) = '-w'))) then begin
    writeln('Option not understood');
    writeln('');
    ShowUseage();
    halt(3);
  end;
  // The file to convert is passed to us as parameter number 2. If the file
  // does not exist, then give an error and quit.
  if (not(FileExists(ParamStr(2)))) then begin
    writeln('File not found: ' + ParamStr(2));
    halt(4);
  end;
  // Assign the string "sle" the line ending to use, based on the option given.
  if ((LowerCase(ParamStr(1)) = '/u') or
      (LowerCase(ParamStr(1)) = '-u')) then begin
    sle := UNIX_LINE_ENDING;
  end else begin
    sle := WINDOWS_LINE_ENDING;
  end;
  // Create a StringList to hold the input file as an indexable list of strings
  // where each string contains one line.
  sl := TStringList.Create;
  // Try to load the file into the StringList. If an error occures, tell about it
  // then quit.
  try
    sl.LoadFromFile(ParamStr(2));
  except
    writeln('Exception loading file: ' + ParamStr(2));
    sl.Free();
    halt(5);
  end;
  // Create a MemoryStream to write our output to.
  ms := TMemoryStream.Create;
  // Try to process the file. If something explodes, we will
  // catch it below in the except handler.
  try
    // For each line in the StringList
    for idx := 0 to (sl.Count - 1) do begin
      // Copy the line into a system string type.
      s := sl[idx];
      // Set the length of our output string to the same length.
      SetLength(sAnsi,
                Length(s));
      // We will copy the characters from our system string to our
      // output string, one by one. The system string might be a
      // "widestring" type where there is more than one byte per
      // character, but we only support single character byte strings.
      // We will cast the incomming character into a single byte and
      // assign it to our output string. This is a simple way to handle
      // the problem, but it only works for files containing single
      // character string types.
      for sAnsiIdx := 1 to Length(s) do begin
        byte(sAnsi[sAnsiIdx]) := byte(char(s[sAnsiIdx]));
      end;
      // Write our output string to the output buffer.
      ms.WriteBuffer(sAnsi[1],
                     Length(s));
      // Write our line ending the output buffer (again, casting
      // the characters to bytes).
      for sAnsiIdx := 1 to Length(sle) do begin
        ms.WriteBuffer(byte(sle[sAnsiIdx]),
                       1);
      end;
    end;
  except
    // Something went dreadfully wrong, and we exploded. Show and error quit.
    writeln('Exception writing stream: ' + ParamStr(2));
    ms.Free();
    sl.Free();
    halt(6);
  end;
  // Try to save the file. If there is an error, then show it and quit.
  try
    ms.SaveToFile(ParamStr(2));
  except
    writeln('Exception saving file: ' + ParamStr(2));
    ms.Free();
    sl.Free();
    halt(7);
  end;
  // We are done - clean up.
  ms.Free();
  sl.Free();
//------------------------------------------------------------------------------
end.




TJoe's splot at CODE4SALE, LLC

TJoe's Coding 101 Page

Contact TJoe

Code4Sale Main Page
DMCA (Digital Millennium Copyright Act) Contact Information
Contact Our Webmaster

Copyright © 2016 by CODE4SALE, LLC - All rights reserved.
Product names, trademarks, and servicemarks mentioned are owned by their respective owners.

Valid HTML 4.0!