Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// (c) 2018 const.me
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace FixEncoding
{
/// <summary>This app fixes UTF16-encoded source files created by Visual Studio 2017 IDE, making them compatible with third party source control tools, and third-party C++ compilers.</summary>
/// <remarks>Tested with Visual Studio 2017, .NET 4.6.1</remarks>
/// <seealso href="https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/33917956-do-not-create-utf16-source-code-files-it-is-not-c?category_id=35066" />
static class Program
{
/// <summary>A set of file extensions to fix.</summary>
static readonly HashSet<string> extensions = new HashSet<string>( StringComparer.OrdinalIgnoreCase )
{
".cpp", ".c", ".h", ".hpp", ".xml", ".cc", ".inl"
};
static void processFile( string path, string dir )
{
if( path.ToLowerInvariant().Contains( @"\.git\" ) ) // Exclude the content of ".git", it has a bunch of XMLs
return;
string ext = Path.GetExtension( path );
if( null == ext ) return;
if( !extensions.Contains( ext ) )
return;
using( var fs = File.Open( path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read ) )
{
if( fs.Length < 2 )
return;
byte[] bom = new byte[ 2 ];
if( 2 != fs.Read( bom, 0, 2 ) )
return;
if( bom[ 0 ] == 0xFF && bom[ 1 ] == 0xFE )
{
// Detected UTF16 BOM
encodeFile( fs );
Console.WriteLine( "{0}", GetRelativePath( path, dir ) );
}
}
}
static string GetRelativePath( string filespec, string folder )
{
// https://stackoverflow.com/a/703292/126995
Uri pathUri = new Uri( filespec );
// Folders must end in a slash
if( !folder.EndsWith( Path.DirectorySeparatorChar.ToString() ) )
folder += Path.DirectorySeparatorChar;
Uri folderUri = new Uri( folder );
return Uri.UnescapeDataString( folderUri.MakeRelativeUri( pathUri ).ToString().Replace( '/', Path.DirectorySeparatorChar ) );
}
/// <summary>New encoding to use, UTF8 with BOM</summary>
static readonly Encoding fixedEncoding = new UTF8Encoding( true, true );
static void encodeFile( FileStream fs )
{
fs.Seek( 0, SeekOrigin.Begin );
string content;
using( var reader = new StreamReader( fs, Encoding.Unicode, true, 32768, true ) )
content = reader.ReadToEnd();
content = content.TrimEnd( '\r', '\n' );
fs.Seek( 0, SeekOrigin.Begin );
using( var writer = new StreamWriter( fs, fixedEncoding ) )
{
writer.Write( content );
writer.Flush();
fs.SetLength( fs.Position );
}
}
static void walkDirectory( string dir )
{
if( !Directory.Exists( dir ) )
Console.WriteLine( "{0} is not a directory", dir );
else foreach( string file in Directory.EnumerateFiles( dir, "*.*", SearchOption.AllDirectories ) )
processFile( file, dir );
}
static int Main( string[] args )
{
if( args.Length <= 0 )
{
Console.WriteLine( "Usage: FixEncoding.exe <directories..>" );
return 1;
}
foreach( var dir in args )
walkDirectory( dir );
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.