8000 Add FilePath and FilePathMarshaler · rlazev/libgit2sharp@5e770b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e770b8

Browse files
dahlbyknulltoken
authored andcommitted
Add FilePath and FilePathMarshaler
1 parent 69229e6 commit 5e770b8

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

LibGit2Sharp/Core/FilePath.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LibGit2Sharp.Core
2+
{
3+
internal class FilePath
4+
{
5+
private readonly string native;
6+
private readonly string posix;
7+
8+
private FilePath(string path)
9+
{
10+
native = PosixPathHelper.ToNative(path);
11+
posix = PosixPathHelper.ToPosix(path);
12+
}
13+
14+
public string Native
15+
{
16+
get { return native; }
17+
}
18+
19+
public string Posix
20+
{
21+
get { return posix; }
22+
}
23+
24+
public override string ToString()
25+
{
26+
return Native;
27+
}
28+
29+
public static implicit operator FilePath(string path)
30+
{
31+
return path == null ? null : new FilePath(path);
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
internal class FilePathMarshaler : Utf8Marshaler
7+
{
8+
private static readonly FilePathMarshaler staticInstance = new FilePathMarshaler();
9+
10+
public override IntPtr MarshalManagedToNative(object managedObj)
11+
{
12+
if (managedObj == null)
13+
{
14+
return IntPtr.Zero;
15+
}
16+
17+
if (!(managedObj is FilePath))
18+
{
19+
throw new MarshalDirectiveException("FilePathMarshaler must be used on a FilePath.");
20+
}
21+
22+
return StringToNative(((FilePath)managedObj).Posix);
23+
}
24+
25+
public override object MarshalNativeToManaged(IntPtr pNativeData)
26+
{
27+
return (FilePath)NativeToString(pNativeData);
28+
}
29+
30+
public new static ICustomMarshaler GetInstance(string cookie)
31+
{
32+
return staticInstance;
33+
}
34+
}
35+
}

LibGit2Sharp/Core/Utf8Marshaler.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal Utf8Marshaler(bool ownsPointer = false)
1616

1717
#region ICustomMarshaler Members
1818

19-
public unsafe IntPtr MarshalManagedToNative(object managedObj)
19+
public virtual IntPtr MarshalManagedToNative(object managedObj)
2020
{
2121
if (managedObj == null)
2222
{
@@ -28,8 +28,13 @@ public unsafe IntPtr MarshalManagedToNative(object managedObj)
2828
throw new MarshalDirectiveException("UTF8Marshaler must be used on a string.");
2929
}
3030

31+
return StringToNative((string)managedObj);
32+
}
33+
34+
protected unsafe IntPtr StringToNative(string value)
35+
{
3136
// not null terminated
32-
byte[] strbuf = Encoding.UTF8.GetBytes((string)managedObj);
37+
byte[] strbuf = Encoding.UTF8.GetBytes(value);
3338
IntPtr buffer = Marshal.AllocHGlobal(strbuf.Length + 1);
3439
Marshal.Copy(strbuf, 0, buffer, strbuf.Length);
3540

@@ -40,7 +45,12 @@ public unsafe IntPtr MarshalManagedToNative(object managedObj)
4045
return buffer;
4146
}
4247

43-
public unsafe object MarshalNativeToManaged(IntPtr pNativeData)
48+
public virtual object MarshalNativeToManaged(IntPtr pNativeData)
49+
{
50+
return NativeToString(pNativeData);
51+
}
52+
53+
protected unsafe string NativeToString(IntPtr pNativeData)
4454
{
4555
var walk = (byte*)pNativeData;
4656

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
<Compile Include="Commit.cs" />
5252
<Compile Include="CommitCollection.cs" />
5353
<Compile Include="Configuration.cs" />
54+
<Compile Include="Core\FilePath.cs" />
55+
<Compile Include="Core\FilePathMarshaler.cs" />
5456
<Compile Include="ConfigurationLevel.cs" />
5557
<Compile Include="Core\Compat\Tuple.cs" />
5658
<Compile Include="Core\DisposableEnumerable.cs" />

0 commit comments

Comments
 (0)
0