Skip to content

Issue 74 - POSIX Path Handling #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add FilePath and FilePathMarshaler
  • Loading branch information
dahlbyk committed Mar 6, 2012
commit c1a8e5f423783e866a01ef1facaa32da70daec1b
34 changes: 34 additions & 0 deletions LibGit2Sharp/Core/FilePath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace LibGit2Sharp.Core
{
internal class FilePath
{
private readonly string native;
private readonly string posix;

private FilePath(string path)
{
native = PosixPathHelper.ToNative(path);
posix = PosixPathHelper.ToPosix(path);
}

public string Native
{
get { return native; }
}

public string Posix
{
get { return posix; }
}

public override string ToString()
{
return Native;
}

public static implicit operator FilePath(string path)
{
return path == null ? null : new FilePath(path);
}
}
}
40 changes: 40 additions & 0 deletions LibGit2Sharp/Core/FilePathMarshaler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Runtime.InteropServices;

namespace LibGit2Sharp.Core
{
internal class FilePathMarshaler : Utf8Marshaler
{
private static FilePathMarshaler staticInstance;

public override IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj == null)
{
return IntPtr.Zero;
}

if (!(managedObj is FilePath))
{
throw new MarshalDirectiveException("FilePathMarshaler must be used on a FilePath.");
}

return StringToNative(((FilePath)managedObj).Posix);
}

public override object MarshalNativeToManaged(IntPtr pNativeData)
{
return (FilePath)NativeToString(pNativeData);
}

public static new ICustomMarshaler GetInstance(string cookie)
{
if (staticInstance == null)
{
return staticInstance = new FilePathMarshaler();
}

return staticInstance;
}
}
}
16 changes: 13 additions & 3 deletions LibGit2Sharp/Core/Utf8Marshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class Utf8Marshaler : ICustomMarshaler
{
private static Utf8Marshaler staticInstance;

public unsafe IntPtr MarshalManagedToNative(object managedObj)
public virtual IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj == null)
{
Expand All @@ -20,8 +20,13 @@ public unsafe IntPtr MarshalManagedToNative(object managedObj)
throw new MarshalDirectiveException("UTF8Marshaler must be used on a string.");
}

return StringToNative((string)managedObj);
}

protected unsafe IntPtr StringToNative(string value)
{
// not null terminated
byte[] strbuf = Encoding.UTF8.GetBytes((string)managedObj);
byte[] strbuf = Encoding.UTF8.GetBytes(value);
IntPtr buffer = Marshal.AllocHGlobal(strbuf.Length + 1);
Marshal.Copy(strbuf, 0, buffer, strbuf.Length);

Expand All @@ -32,7 +37,12 @@ public unsafe IntPtr MarshalManagedToNative(object managedObj)
return buffer;
}

public unsafe object MarshalNativeToManaged(IntPtr pNativeData)
public virtual object MarshalNativeToManaged(IntPtr pNativeData)
{
return NativeToString(pNativeData);
}

protected unsafe string NativeToString(IntPtr pNativeData)
{
var walk = (byte*)pNativeData;

Expand Down
2 changes: 2 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<Compile Include="Commit.cs" />
<Compile Include="CommitCollection.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="Core\FilePath.cs" />
<Compile Include="Core\FilePathMarshaler.cs" />
<Compile Include="ConfigurationLevel.cs" />
<Compile Include="Core\Compat\Tuple.cs" />
<Compile Include="Core\DisposableEnumerable.cs" />
Expand Down