//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
//
//
// Class stubs to allow compiling on CoreClr.
//
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop.Implementation
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
///
/// A fake class to allow compilation on platforms that lack this functionality.
///
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass",
Justification = "Reviewed. Suppression is OK here because it's a collection of trivial classes.")]
public class Trace
{
///
/// A fake function to allow compilation on platforms that lack this functionality.
///
///
/// The condition.
///
///
/// The message.
///
[ConditionalAttribute("TRACE")]
public static void WriteLineIf(bool condition, string message)
{
}
///
/// A fake function to allow compilation on platforms that lack this functionality.
///
///
/// The condition.
///
///
/// The message.
///
[ConditionalAttribute("TRACE")]
public static void WriteLineIf(bool condition, object message)
{
}
///
/// Prints out the object's contents.
///
/// A string represenetation or the object.
public override string ToString()
{
return base.ToString();
}
}
///
/// A fake class to allow compilation on platforms that lack this class.
///
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass",
Justification = "Reviewed. Suppression is OK here because it's a collection of trivial classes.")]
internal static class RuntimeHelpers
{
///
/// A fake function to allow compilation on platforms that lack this functionality.
///
public static void PrepareConstrainedRegions()
{
}
///
/// A fake function to allow compilation on platforms that lack this functionality.
///
///
/// The method.
///
public static void PrepareMethod(RuntimeMethodHandle method)
{
}
}
///
/// Ascii encoding is not available on Core Clr. But UTF-8 is.
/// This class will reject any character that results in an
/// extended value.
///
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass",
Justification = "Reviewed. Suppression is OK here because it's a collection of trivial classes.")]
internal class SlowAsciiEncoding : UTF8Encoding
{
///
/// The standard encoding object.
///
private static SlowAsciiEncoding slowAsciiEncoding = new SlowAsciiEncoding();
///
/// Gets an Encoding object.
///
public static Encoding Encoding
{
get
{
return slowAsciiEncoding;
}
}
#if !MANAGEDESENT_ON_WSA
///
/// Converts a string to the byte representation.
///
///
/// The chars.
///
///
/// The char count.
///
///
/// The bytes.
///
///
/// The byte count.
///
///
/// A count of bytes stored.
///
public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
{
IntPtr toFree;
char* charsToTranslate = this.SanitizeString(chars, charCount, out toFree);
int toReturn = base.GetBytes(charsToTranslate, charCount, bytes, byteCount);
LibraryHelpers.MarshalFreeHGlobal(toFree);
return toReturn;
}
#endif
///
/// Converts a string to the byte representation.
///
///
/// The input string.
///
///
/// The byte representation of the string.
///
public override byte[] GetBytes(string inputString)
{
string stringToTranslate = this.SanitizeString(inputString);
return base.GetBytes(stringToTranslate);
}
///
/// Converts a string to the byte representation.
///
///
/// The input string.
///
///
/// The char index.
///
///
/// The char count.
///
///
/// The bytes.
///
///
/// The byte index.
///
///
/// The byte representation of the string.
///
public override int GetBytes(string inputString, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
string stringToTranslate = this.SanitizeString(inputString);
return base.GetBytes(stringToTranslate, charIndex, charCount, bytes, byteIndex);
}
///
/// Scans the string looking for unmappable characters in the ASCII set, and replaces
/// them with '?'.
///
/// A unicode string with unknown characters.
/// A string that has all legal ASCII characters.
private string SanitizeString(string inputString)
{
bool needToDuplicate = false;
string returnString = inputString;
foreach (char ch in inputString)
{
if (ch > 127)
{
needToDuplicate = true;
break;
}
}
if (needToDuplicate)
{
StringBuilder sb = new StringBuilder(inputString.Length);
foreach (char ch in inputString)
{
sb.Append(ch > 127 ? '?' : ch);
}
returnString = sb.ToString();
}
return returnString;
}
#if !MANAGEDESENT_ON_WSA
///
/// Scans the string looking for unmappable characters in the ASCII set, and replaces
/// them with '?'.
///
/// A unicode string with unknown characters.
/// The length of the string to sanitize.
/// On output, a value that needs to be freed. Only used
/// if there are any untranslaable characters.
/// A string that has all legal ASCII characters.
private unsafe char* SanitizeString(char* inputString, int charCount, out IntPtr allocedMemory)
{
allocedMemory = IntPtr.Zero;
bool needToDuplicate = false;
char* returnString = inputString;
for (int i = 0; i < charCount; ++i)
{
if (inputString[i] > 127)
{
needToDuplicate = true;
break;
}
}
if (needToDuplicate)
{
allocedMemory = LibraryHelpers.MarshalAllocHGlobal(charCount);
returnString = (char*)allocedMemory;
char* dest = returnString;
for (int i = 0; i < charCount; ++i)
{
dest[i] = inputString[i] > 127 ? '?' : inputString[i];
}
}
return returnString;
}
#endif
}
}