Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for utf8removebom.cs

(view source code of utf8removebom.cs as plain text)

  1. namespace RobvanderWoude
  2. {
  3. 	class UTF8removeBOM
  4. 	{
  5. 		static string progver = "1.00";
  6.  
  7.  
  8. 		static int Main( string[] args )
  9. 		{
  10. 			#region Parse Command Line
  11.  
  12. 			if ( args.Length < 2 || args.Length > 3 )
  13. 			{
  14. 				return ShowHelp( );
  15. 			}
  16.  
  17. 			bool overwrite = false;
  18. 			string filein = args[0];
  19. 			string fileout = args[0];
  20.  
  21. 			if ( !System.IO.File.Exists( filein ) )
  22. 			{
  23. 				return ShowHelp( "Source file not found" );
  24. 			}
  25.  
  26. 			if ( args[1].ToUpper( ) == "/O" )
  27. 			{
  28. 				overwrite = true;
  29. 			}
  30. 			else
  31. 			{
  32. 				fileout = args[1];
  33. 			}
  34.  
  35. 			if ( args.Length == 3 )
  36. 			{
  37. 				if ( args[2].ToUpper( ) == "/O" )
  38. 				{
  39. 					overwrite = true;
  40. 				}
  41. 				else
  42. 				{
  43. 					fileout = args[2];
  44. 				}
  45. 			}
  46.  
  47. 			if ( !overwrite )
  48. 			{
  49. 				if ( filein == fileout )
  50. 				{
  51. 					return ShowHelp( "Use /O to overwrite existing source file" );
  52. 				}
  53. 				else
  54. 				{
  55. 					if ( System.IO.File.Exists( fileout ) )
  56. 					{
  57. 						return ShowHelp( "Use /O to overwrite existing target file" );
  58. 					}
  59. 				}
  60. 			}
  61.  
  62. 			#endregion Parse Command Line
  63.  
  64.  
  65. 			try
  66. 			{
  67. 				// Open source file
  68. 				System.IO.StreamReader sr = new System.IO.StreamReader( filein );
  69. 				// Read text from source file
  70. 				string text = sr.ReadToEnd( );
  71. 				// Close source file
  72. 				sr.Close( );
  73.  
  74. 				// Delete target file if it exists
  75. 				if ( System.IO.File.Exists( fileout ) )
  76. 				{
  77. 					System.IO.File.Delete( fileout );
  78. 				}
  79.  
  80. 				// Open target file, set encoding to UTF-8 without BOM
  81. 				System.IO.StreamWriter sw = new System.IO.StreamWriter( fileout, false, new System.Text.UTF8Encoding( false ) );
  82. 				// Write text to target file
  83. 				sw.Write( text );
  84. 				// Close target file
  85. 				sw.Close( );
  86. 			}
  87. 			catch ( System.Exception e )
  88. 			{
  89. 				return ShowHelp( e.Message );
  90. 			}
  91.  
  92. 			// Done
  93. 			return 0;
  94. 		}
  95.  
  96.  
  97. 		#region Error handling
  98.  
  99. 		public static int ShowHelp( params string[] errmsg )
  100. 		{
  101. 			#region Error Message
  102.  
  103. 			if ( errmsg.Length > 0 )
  104. 			{
  105. 				System.Collections.Generic.List<string> errargs = new System.Collections.Generic.List<string>( errmsg );
  106. 				errargs.RemoveAt( 0 );
  107. 				System.Console.Error.WriteLine( );
  108. 				System.Console.ForegroundColor = System.ConsoleColor.Red;
  109. 				System.Console.Error.Write( "ERROR:\t" );
  110. 				System.Console.ForegroundColor = System.ConsoleColor.White;
  111. 				System.Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  112. 				System.Console.ResetColor( );
  113. 			}
  114.  
  115. 			#endregion Error Message
  116.  
  117.  
  118. 			#region Help Text
  119.  
  120. 			/*
  121. 			UTF8removeBOM,  Version 1.00
  122. 			Read a text file and save the text in UTF-8 format without Byte Order Mark
  123.  
  124. 			Usage:   UTF8REMOVEBOM  textfilein  [ texfileout ]  [ /O ]
  125.  
  126. 			Where:   textfilein   is the source text file
  127. 			         textfileout  is the target text file
  128. 			                      (default if not specified: textfilein)
  129. 			         /O           overwrite existing target file,
  130. 			                      required if target file is not specified
  131. 			                      (default: abort if target file exists)
  132.  
  133. 			Notes:   At least 2 command line arguments are required.
  134. 			         Return code is -1 in case of errors, otherwise 0.
  135. 			         This simple tool was written with the following workflow in mind:
  136. 			         Use VBScript's OpenTextFile with TriStateTrue option to write text file
  137. 			         Use this tool to remove the Byte Order Mark
  138. 			         Use PHP command print( htmlentities( file_get_contents( textfile ) ) )
  139. 			         to show the text correctly in a web page
  140.  
  141. 			Written by Rob van der Woude
  142. 			http://www.robvanderwoude.com
  143. 			*/
  144.  
  145. 			#endregion Help Text
  146.  
  147.  
  148. 			#region Display help
  149.  
  150. 			System.Console.Error.WriteLine( );
  151.  
  152. 			System.Console.Error.WriteLine( "UTF8removeBOM.exe,  Version {0}", progver );
  153.  
  154. 			System.Console.Error.WriteLine( "Read a text file and save the text in UTF-8 format without Byte Order Mark" );
  155.  
  156. 			System.Console.Error.WriteLine( );
  157.  
  158. 			System.Console.Error.Write( "Usage:   " );
  159. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  160. 			System.Console.Error.WriteLine( "UTF8REMOVEBOM  textfilein  [ texfileout ]  [ /O ]" );
  161. 			System.Console.ResetColor( );
  162.  
  163. 			System.Console.Error.WriteLine( );
  164.  
  165. 			System.Console.Error.Write( "Where:   " );
  166. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  167. 			System.Console.Error.Write( "textfilein" );
  168. 			System.Console.ResetColor( );
  169. 			System.Console.Error.WriteLine( "   is the source text file" );
  170.  
  171. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  172. 			System.Console.Error.Write( "         textfileout" );
  173. 			System.Console.ResetColor( );
  174. 			System.Console.Error.WriteLine( "  is the target text file" );
  175.  
  176. 			System.Console.Error.WriteLine( "                      (default if not specified: textfilein)" );
  177.  
  178. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  179. 			System.Console.Error.Write( "         /O" );
  180. 			System.Console.ResetColor( );
  181. 			System.Console.Error.WriteLine( "           overwrite existing target file," );
  182.  
  183. 			System.Console.Error.WriteLine( "                      required if target file is not specified" );
  184.  
  185. 			System.Console.Error.WriteLine( "                      (default: abort if target file exists)" );
  186.  
  187. 			System.Console.Error.WriteLine( );
  188.  
  189. 			System.Console.Error.WriteLine( "Notes:   At least 2 command line arguments are required." );
  190.  
  191. 			System.Console.Error.WriteLine( "         Return code is -1 in case of errors, otherwise 0." );
  192.  
  193. 			System.Console.Error.WriteLine( "         This simple tool was written with the following workflow in mind:" );
  194.  
  195. 			System.Console.WriteLine( );
  196.  
  197. 			System.Console.Error.Write( "         *  VBScript's " );
  198. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  199. 			System.Console.Error.Write( "OpenTextFile" );
  200. 			System.Console.ResetColor( );
  201. 			System.Console.Error.Write( " writes text file with " );
  202. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  203. 			System.Console.Error.Write( "TriStateTrue" );
  204. 			System.Console.ResetColor( );
  205. 			System.Console.Error.WriteLine( " option" );
  206.  
  207. 			System.Console.Error.Write( "         *  " );
  208. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  209. 			System.Console.Error.Write( "UTF8removeBOM.exe" );
  210. 			System.Console.ResetColor( );
  211. 			System.Console.Error.WriteLine( " removes the Byte Order Mark" );
  212.  
  213. 			System.Console.Error.Write( "         *  PHP command " );
  214. 			System.Console.ForegroundColor = System.ConsoleColor.White;
  215. 			System.Console.Error.WriteLine( "print( htmlentities( file_get_contents( textfile ) ) )" );
  216. 			System.Console.ResetColor( );
  217.  
  218. 			System.Console.Error.WriteLine( "            shows the text correctly in a web page" );
  219.  
  220. 			System.Console.Error.WriteLine( );
  221.  
  222. 			System.Console.Error.WriteLine( "Written by Rob van der Woude" );
  223.  
  224. 			System.Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  225.  
  226. 			#endregion Display Help
  227.  
  228.  
  229. 			return -1;
  230. 		}
  231.  
  232. 		#endregion Error handling
  233. 	}
  234. }
  235.  

page last uploaded: 2019-01-21, 22:48