Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for urlescape.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	internal class URLEscape
  9. 	{
  10. 		static readonly string progver = "1.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			int rc = 0;
  16.  
  17. 			int argumentscount = args.Length;
  18. 			if ( Console.IsInputRedirected )
  19. 			{
  20. 				argumentscount++;
  21. 			}
  22. 			if ( argumentscount != 1 || args.Contains( "/?" ) )
  23. 			{
  24. 				return ShowHelp( );
  25. 			}
  26.  
  27. 			string unescapedurl = string.Empty;
  28. 			if ( Console.IsInputRedirected )
  29. 			{
  30. 				unescapedurl = Console.In.ReadToEnd( );
  31. 			}
  32. 			else if ( File.Exists( args[0] ) )
  33. 			{
  34. 				unescapedurl = File.ReadAllText( args[0] );
  35. 			}
  36. 			else
  37. 			{
  38. 				unescapedurl = args[0];
  39. 			}
  40. 			// NOTE: WebUtility.UrlEncode is not used here because it also escapes colons and forward slashes
  41. 			string escapedurl = new Uri( unescapedurl ).AbsoluteUri.ToString( ); // does not escape parentheses
  42. 			escapedurl = escapedurl.Replace( "(", "%28" ).Replace( ")", "%29" ); // escape parentheses as well
  43. 			Console.Write( escapedurl );
  44.  
  45.  
  46. 			return rc;
  47. 		}
  48.  
  49. 		public static int ShowHelp( string errorMessage = "" )
  50. 		{
  51. 			#region Help Text
  52.  
  53. 			/*
  54. 			URLEscape,  Version 1.00
  55. 			Display URL-escaped input string in console
  56.  
  57. 			Usage:  URLESCAPE  textfile
  58.  
  59. 			   or:  URLESCAPE  unescapedurl
  60.  
  61. 			   or:  some_program | URLESCAPE
  62.  
  63. 			Where:  textfile       file containing text to be escaped
  64. 			        unescapedurl   string to be escaped
  65. 			        some_program   program whose output is to be escaped
  66.  
  67. 			Note:   Exit code ("errorlevel") -1 in case of errors, otherwise 0.
  68.  
  69. 			Written by Rob van der Woude
  70. 			https://www.robvanderwoude.com
  71. 			*/
  72.  
  73. 			#endregion Help Text
  74.  
  75.  
  76. 			#region Error Message
  77.  
  78. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  79. 			{
  80. 				Console.Error.WriteLine( );
  81. 				Console.ForegroundColor = ConsoleColor.Red;
  82. 				Console.Error.Write( "ERROR: " );
  83. 				Console.ForegroundColor = ConsoleColor.White;
  84. 				Console.Error.WriteLine( errorMessage );
  85. 				Console.ResetColor( );
  86. 			}
  87.  
  88. 			#endregion Error Message
  89.  
  90.  
  91. 			#region Display Help
  92.  
  93. 			Console.Error.WriteLine( );
  94.  
  95. 			Console.Error.WriteLine( "URLEscape,  Version {0}", progver );
  96.  
  97. 			Console.Error.WriteLine( "Display URL-escaped input string in console" );
  98.  
  99. 			Console.Error.WriteLine( );
  100.  
  101. 			Console.Error.Write( "Usage:  " );
  102. 			Console.ForegroundColor = ConsoleColor.White;
  103. 			Console.Error.WriteLine( "URLESCAPE  textfile" );
  104. 			Console.ResetColor( );
  105.  
  106. 			Console.Error.WriteLine( );
  107.  
  108. 			Console.Error.Write( "   or:  " );
  109. 			Console.ForegroundColor = ConsoleColor.White;
  110. 			Console.Error.WriteLine( "URLESCAPE  unescapedurl" );
  111. 			Console.ResetColor( );
  112.  
  113. 			Console.Error.WriteLine( );
  114.  
  115. 			Console.Error.Write( "   or:  " );
  116. 			Console.ForegroundColor = ConsoleColor.White;
  117. 			Console.Error.WriteLine( "some_program | URLESCAPE" );
  118. 			Console.ResetColor( );
  119.  
  120. 			Console.Error.WriteLine( );
  121.  
  122. 			Console.Error.Write( "Where:  " );
  123. 			Console.ForegroundColor = ConsoleColor.White;
  124. 			Console.Error.Write( "textfile" );
  125. 			Console.ResetColor( );
  126. 			Console.Error.WriteLine( "       file containing text to be escaped" );
  127.  
  128. 			Console.ForegroundColor = ConsoleColor.White;
  129. 			Console.Error.Write( "        unescapedurl" );
  130. 			Console.ResetColor( );
  131. 			Console.Error.WriteLine( "   string to be escaped" );
  132.  
  133. 			Console.ForegroundColor = ConsoleColor.White;
  134. 			Console.Error.Write( "        some_program" );
  135. 			Console.ResetColor( );
  136. 			Console.Error.WriteLine( "   program whose output is to be escaped" );
  137.  
  138. 			Console.Error.WriteLine( );
  139.  
  140. 			Console.Error.WriteLine( "Note:   Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." );
  141.  
  142. 			Console.Error.WriteLine( );
  143.  
  144. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  145.  
  146. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  147.  
  148. 			#endregion Display Help
  149.  
  150.  
  151. 			return -1;
  152. 		}
  153. 	}
  154. }

page last modified: 2024-04-16; loaded in 0.0078 seconds