(view source code of urlescape.cs as plain text)
using System;
using System.IO;
using System.Linq;
namespace RobvanderWoude
{
internal class URLEscape
{
static readonly string progver = "1.00";
static int Main( string[] args )
{
int rc = 0;
int argumentscount = args.Length;
if ( Console.IsInputRedirected )
{
argumentscount++;
}
if ( argumentscount != 1 || args.Contains( "/?" ) )
{
return ShowHelp( );
}
string unescapedurl = string.Empty;
if ( Console.IsInputRedirected )
{
unescapedurl = Console.In.ReadToEnd( );
}
else if ( File.Exists( args[0] ) )
{
unescapedurl = File.ReadAllText( args[0] );
}
else
{
unescapedurl = args[0];
}
// NOTE: WebUtility.UrlEncode is not used here because it also escapes colons and forward slashes
string escapedurl = new Uri( unescapedurl ).AbsoluteUri.ToString( ); // does not escape parentheses
escapedurl = escapedurl.Replace( "(", "%28" ).Replace( ")", "%29" ); // escape parentheses as well
Console.Write( escapedurl );
return rc;
}
public static int ShowHelp( string errorMessage = "" )
{
#region Help Text
/*
URLEscape, Version 1.00
Display URL-escaped input string in console
Usage: URLESCAPE textfile
or: URLESCAPE unescapedurl
or: some_program | URLESCAPE
Where: textfile file containing text to be escaped
unescapedurl string to be escaped
some_program program whose output is to be escaped
Note: Exit code ("errorlevel") -1 in case of errors, otherwise 0.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Error Message
if ( !string.IsNullOrWhiteSpace( errorMessage ) )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
#endregion Error Message
#region Display Help
Console.Error.WriteLine( );
Console.Error.WriteLine( "URLEscape, Version {0}", progver );
Console.Error.WriteLine( "Display URL-escaped input string in console" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "URLESCAPE textfile" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "URLESCAPE unescapedurl" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "some_program | URLESCAPE" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "textfile" );
Console.ResetColor( );
Console.Error.WriteLine( " file containing text to be escaped" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " unescapedurl" );
Console.ResetColor( );
Console.Error.WriteLine( " string to be escaped" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " some_program" );
Console.ResetColor( );
Console.Error.WriteLine( " program whose output is to be escaped" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note: Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help
return -1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0078 seconds