| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Text;
- using Renci.SshNet.Common;
- namespace Renci.SshNet
- {
- /// <summary>
- /// Encloses a path in double quotes, and escapes any embedded double quote with a backslash.
- /// </summary>
- internal sealed class RemotePathDoubleQuoteTransformation : IRemotePathTransformation
- {
- /// <summary>
- /// Encloses a path in double quotes, and escapes any embedded double quote with a backslash.
- /// </summary>
- /// <param name="path">The path to transform.</param>
- /// <returns>
- /// The transformed path.
- /// </returns>
- /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
- /// <example>
- /// <list type="table">
- /// <listheader>
- /// <term>Original</term>
- /// <term>Transformed</term>
- /// </listheader>
- /// <item>
- /// <term>/var/log/auth.log</term>
- /// <term>"/var/log/auth.log"</term>
- /// </item>
- /// <item>
- /// <term>/var/mp3/Guns N' Roses</term>
- /// <term>"/var/mp3/Guns N' Roses"</term>
- /// </item>
- /// <item>
- /// <term>/var/garbage!/temp</term>
- /// <term>"/var/garbage!/temp"</term>
- /// </item>
- /// <item>
- /// <term>/var/would be 'kewl'!/not?</term>
- /// <term>"/var/would be 'kewl'!, not?"</term>
- /// </item>
- /// <item>
- /// <term></term>
- /// <term>""</term>
- /// </item>
- /// <item>
- /// <term>Hello "World"</term>
- /// <term>"Hello \"World"</term>
- /// </item>
- /// </list>
- /// </example>
- public string Transform(string path)
- {
- ThrowHelper.ThrowIfNull(path);
- var transformed = new StringBuilder(path.Length);
- _ = transformed.Append('"');
- foreach (var c in path)
- {
- if (c == '"')
- {
- _ = transformed.Append('\\');
- }
- _ = transformed.Append(c);
- }
- _ = transformed.Append('"');
- return transformed.ToString();
- }
- }
- }
|