using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Unhwp;
internal static class NativeMethods
{
private const string LibraryName = "unhwp";
static NativeMethods()
{
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDllImport);
}
private static IntPtr ResolveDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName != LibraryName)
return IntPtr.Zero;
string[] namesToTry;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
namesToTry = new[] { "unhwp_native", "unhwp" };
}
else
{
namesToTry = new[] { "unhwp" };
}
foreach (var name in namesToTry)
{
if (NativeLibrary.TryLoad(name, assembly, searchPath, out var handle))
return handle;
}
return IntPtr.Zero;
}
public const int UNHWP_FLAG_FRONTMATTER = 1;
public const int UNHWP_FLAG_ESCAPE_SPECIAL = 2;
public const int UNHWP_FLAG_PARAGRAPH_SPACING = 4;
public const int UNHWP_JSON_PRETTY = 0;
public const int UNHWP_JSON_COMPACT = 1;
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_version();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_last_error();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr unhwp_parse_file([MarshalAs(UnmanagedType.LPUTF8Str)] string path);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_parse_bytes(IntPtr data, UIntPtr len);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void unhwp_free_document(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_to_markdown(IntPtr doc, int flags);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_to_text(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_to_json(IntPtr doc, int format);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_plain_text(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int unhwp_section_count(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int unhwp_resource_count(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_get_title(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_get_author(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void unhwp_free_string(IntPtr str);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr unhwp_get_resource_ids(IntPtr doc);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr unhwp_get_resource_info(
IntPtr doc,
[MarshalAs(UnmanagedType.LPUTF8Str)] string resourceId);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr unhwp_get_resource_data(
IntPtr doc,
[MarshalAs(UnmanagedType.LPUTF8Str)] string resourceId,
out UIntPtr outLen);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void unhwp_free_bytes(IntPtr data, UIntPtr len);
}