forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitStrArrayNative.cs
More file actions
43 lines (36 loc) · 1.19 KB
/
GitStrArrayNative.cs
File metadata and controls
43 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core
{
/// <summary>
/// A git_strarray where the string array and strings themselves were allocated
/// with libgit2's allocator. Only libgit2 can free this git_strarray.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct GitStrArrayNative : IDisposable
{
public GitStrArray Array;
/// <summary>
/// Enumerates each string from the array using the UTF-8 marshaler.
/// </summary>
public String[] ReadStrings()
{
var count = checked((int)Array.Count.ToUInt32());
String[] toReturn = new String[count];
for (int i = 0; i < count; i++)
{
toReturn[i] = LaxUtf8Marshaler.FromNative(Marshal.ReadIntPtr(Array.Strings, i * IntPtr.Size));
}
return toReturn;
}
public void Dispose()
{
if (Array.Strings != IntPtr.Zero)
{
NativeMethods.git_strarray_free(ref Array);
}
// Now that we've freed the memory, zero out the structure.
Array.Reset();
}
}
}