Fix-LE.cs #2

  • //
  • main/
  • guest/
  • philip_kania/
  • Tools/
  • src/
  • Fix-LE.cs
  • View
  • Commits
  • Open Download .zip Download (3 KB)
// Copyright (c) 2009 Exemplics LLC

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE 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 NONINFRINGEMENT. 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 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Fix-LE.exe - Remove extraneous 0x0d (carriage return) bytes from a
// Microsoft "Unicode" (UTF16-LE) file.
// 
// When Microsoft Windows "Unicode" files are submitted into Perforce
// with a file type of "text" and the files are subsequently sync-ed to
// your workspace (and your client workspace "LineEnd" setting is "local"
// or "win"), Perforce line ending translation will replace 0x0a (line
// feed) with 0x0d0a. This corrupts the files because the correct line
// ending byte sequence 0x0d00 0x0a00 becomes 0x0d00 0x0d0a 0x00. This
// program removes those extra 0x0d bytes.
// 
// To use:
//    p4 sync <problem-file>
//    p4 edit -tutf16 <problem-file>
//    fix-le <problem-file> <fixed-file>
//    copy /y <fixed-file> <problem-file>
//    p4 submit

// To build Fix-LE.exe:
//    Using Microsoft .NET Framework SDK v2.0 Commmand Prompt:
//       csc Fix-LE.cs [/keyfile:<key-file>]

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Fix erroneous line ending translation")]
[assembly: AssemblyDescription("This program removes extraneous 0x0d bytes from a utf16 (Windows Unicode) file. It is released under the MIT license.")]
[assembly: AssemblyCompany("Exemplics LLC")]
[assembly: AssemblyProduct("Exemplics Tools")]
[assembly: AssemblyCopyright("©  2009 Exemplics LLC")]
[assembly: AssemblyVersion("2009.1.*")]
[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]
namespace Exemplics {
  class FixLineEnds {
    static int Main(string[] args)
    {
      if (args.Length != 2)
      {
        WriteUsage();
        return 1;
      }
      byte[] contents = File.ReadAllBytes(args[0]);
      if (contents.Length < 1)
        return 2;

      FileStream fs  = new FileStream(args[1], FileMode.Create);
      BinaryWriter w = new BinaryWriter(fs);
      byte previousByte = contents[0];
      for (int i = 1; i < contents.Length; i++) 
      {
        if ((previousByte != 0xd) || (contents[i] != 0xa))
          w.Write(previousByte);
        previousByte = contents[i];
      }
      w.Write(previousByte);
      w.Close();
      fs.Close();
      return 0;
    }
    private static void WriteUsage()
    {
      Console.Error.WriteLine("Usage: fix-le <problem-file> <fixed-file>");
    }
  } 
}
# Change User Description Committed
#2 7187 Philip Kania Changed order of conditional subexpressions to match order of bytes in the file.
Chage from-file and to-file in Usage to problem-file and fixed-file.
#1 7186 Philip Kania Added utility to fix erroreous line ending translation applied to Windows Unicode (UTF16-LE) files whose Perforce filetype is "text".