Skip to content

Commit 7229f16

Browse files
committed
Changes to be an FRC Utilities library, and adds UTF8 string manipulation
1 parent cb673ee commit 7229f16

21 files changed

+223
-43
lines changed
File renamed without changes.

NativeLibraryUtilities.sln renamed to FRC-Utilities.sln

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26114.2
3+
VisualStudioVersion = 15.0.26403.7
54
MinimumVisualStudioVersion = 10.0.40219.1
65
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DB3E1B73-3837-468A-B67F-0640EE5FFA9B}"
76
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeLibraryUtilities", "src\NativeLibraryUtilities\NativeLibraryUtilities.csproj", "{132E6AEA-F5FC-4991-9DA1-6F2DD41AF500}"
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FRC-Utilities", "src\FRC-Utilities\FRC-Utilities.csproj", "{132E6AEA-F5FC-4991-9DA1-6F2DD41AF500}"
98
EndProject
109
Global
1110
GlobalSection(SolutionConfigurationPlatforms) = preSolution

build.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Task("Pack")
111111
.IsDependentOn("Test")
112112
.Does(() =>
113113
{
114-
foreach (var project in GetFiles("./src/NativeLibraryUtilities/*.csproj"))
114+
foreach (var project in GetFiles("./src/FRC-Utilities/*.csproj"))
115115
{
116116
DotNetCorePack(
117117
project.GetDirectory().FullPath,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
namespace FRC
6+
{
7+
/// <summary>
8+
/// Holds a Cached UTF8 string to pass to native code. There is no way to initialize or dispose of this
9+
/// string from user code.
10+
/// </summary>
11+
[StructLayout(LayoutKind.Sequential)]
12+
public struct CachedNativeString
13+
{
14+
/// <summary>
15+
/// Pointer to this string, null terminated. Do not modify this pointer.
16+
/// </summary>
17+
public IntPtr Buffer;
18+
/// <summary>
19+
/// The Length of this string without the null terminator;
20+
/// </summary>
21+
public UIntPtr Length;
22+
23+
internal CachedNativeString(string vStr)
24+
{
25+
unsafe
26+
{
27+
fixed (char* str = vStr)
28+
{
29+
var encoding = Encoding.UTF8;
30+
int bytes = encoding.GetByteCount(str, vStr.Length);
31+
Buffer = Marshal.AllocHGlobal((bytes + 1) * sizeof(byte));
32+
Length = (UIntPtr)bytes;
33+
byte* data = (byte*)Buffer.ToPointer();
34+
encoding.GetBytes(str, vStr.Length, data, bytes);
35+
data[bytes] = 0;
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Runtime.InteropServices;
6+
7+
namespace FRC
8+
{
9+
/// <summary>
10+
/// Holds a UTF8 string to pass to native code. Make sure to properly dispose of this to avoid a memory leak
11+
/// </summary>
12+
[StructLayout(LayoutKind.Sequential)]
13+
public struct DisposableNativeString : IDisposable
14+
{
15+
/// <summary>
16+
/// Pointer to this string, null terminated. Do not modify this pointer.
17+
/// </summary>
18+
public IntPtr Buffer;
19+
/// <summary>
20+
/// The Length of this string without the null terminator;
21+
/// </summary>
22+
public UIntPtr Length;
23+
24+
/// <summary>
25+
/// Creates a new UTF8 string from a managed string
26+
/// </summary>
27+
/// <param name="vStr">The managed string</param>
28+
public DisposableNativeString(string vStr)
29+
{
30+
unsafe
31+
{
32+
fixed (char* str = vStr)
33+
{
34+
var encoding = Encoding.UTF8;
35+
int bytes = encoding.GetByteCount(str, vStr.Length);
36+
Buffer = Marshal.AllocHGlobal((bytes + 1) * sizeof(byte));
37+
Length = (UIntPtr)bytes;
38+
byte* data = (byte*)Buffer.ToPointer();
39+
encoding.GetBytes(str, vStr.Length, data, bytes);
40+
data[bytes] = 0;
41+
}
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Disposes of the native string
47+
/// </summary>
48+
public void Dispose()
49+
{
50+
Marshal.FreeHGlobal(Buffer);
51+
}
52+
}
53+
}

src/NativeLibraryUtilities/NativeLibraryUtilities.csproj renamed to src/FRC-Utilities/FRC-Utilities.csproj

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Description>Utility library for interfacing with native libraries.</Description>
5-
<Copyright>Copyright 2016 RobotDotNet</Copyright>
6-
<AssemblyTitle>Native Library Utilities</AssemblyTitle>
4+
<Description>FRC Utility Library</Description>
5+
<Copyright>Copyright 2016-2017 RobotDotNet</Copyright>
6+
<AssemblyTitle>FRC Utilities</AssemblyTitle>
77
<Authors>RobotDotNet</Authors>
8-
<TargetFrameworks>net35;net45;netstandard1.5</TargetFrameworks>
8+
<TargetFrameworks>net40;netstandard1.5</TargetFrameworks>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10-
<AssemblyName>NativeLibraryUtilities</AssemblyName>
11-
<PackageId>NativeLibraryUtilities</PackageId>
12-
<PackageTags>Native;Library;Delegates</PackageTags>
13-
<PackageProjectUrl>https://github.com/robotdotnet/NativeLibraryUtilities</PackageProjectUrl>
14-
<PackageLicenseUrl>https://github.com/robotdotnet/NativeLibraryUtilities/blob/master/LICENSE.txt</PackageLicenseUrl>
10+
<AssemblyName>FRC.Utilities</AssemblyName>
11+
<PackageId>FRC.Utilities</PackageId>
12+
<PackageTags>Native;Library;Delegates;FRC</PackageTags>
13+
<PackageProjectUrl>https://github.com/robotdotnet/FRC-Utilities</PackageProjectUrl>
14+
<PackageLicenseUrl>https://github.com/robotdotnet/FRC-Utilities/blob/master/LICENSE.txt</PackageLicenseUrl>
1515
<RepositoryType>git</RepositoryType>
16-
<RepositoryUrl>git://github.com/robotdotnet/NativeLibraryUtilities</RepositoryUrl>
16+
<RepositoryUrl>git://github.com/robotdotnet/FRC-Utilities</RepositoryUrl>
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<RootNamespace>FRC</RootNamespace>
21+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
2022
</PropertyGroup>
2123

22-
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
24+
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
2325
<Reference Include="System" />
2426
<Reference Include="Microsoft.CSharp" />
2527
</ItemGroup>
2628

27-
<ItemGroup Condition=" '$(TargetFramework)' == 'net35' ">
28-
<Reference Include="System" />
29-
</ItemGroup>
30-
31-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' ">
32-
<DefineConstants>$(DefineConstants);NET35</DefineConstants>
33-
<!-- Workaround to allow .NET 3.5 to properly build. -->
34-
<FrameworkPathOverride>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
35-
</PropertyGroup>
36-
3729
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
3830
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
3931
</PropertyGroup>

src/NativeLibraryUtilities/EmbeddedLibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/EmbeddedLibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4-
namespace NativeLibraryUtilities
4+
namespace FRC.NativeLibraryUtilties
55
{
66
///<summary>
77
/// Library loader for embedded devices

src/NativeLibraryUtilities/ILibraryInformation.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/ILibraryInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NativeLibraryUtilities
1+
namespace FRC.NativeLibraryUtilties
22
{
33
/// <summary>
44
/// Interface for getting information about a loaded native library.

src/NativeLibraryUtilities/ILibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/ILibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace NativeLibraryUtilities
3+
namespace FRC.NativeLibraryUtilties
44
{
55
/// <summary>
66
/// Interface for platform specific native interface to the library

src/NativeLibraryUtilities/LinuxLibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/LinuxLibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.IO;
33
using System.Runtime.InteropServices;
44

5-
namespace NativeLibraryUtilities
5+
namespace FRC.NativeLibraryUtilties
66
{
77
/// <summary>
88
/// This class handles native libraries on Linux

src/NativeLibraryUtilities/MacOsLibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/MacOsLibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.IO;
33
using System.Runtime.InteropServices;
44

5-
namespace NativeLibraryUtilities
5+
namespace FRC.NativeLibraryUtilties
66
{
77
/// <summary>
88
/// This class handles native libraries on Mac OS

src/NativeLibraryUtilities/NativeDelegateAttribute.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/NativeDelegateAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace NativeLibraryUtilities
3+
namespace FRC.NativeLibraryUtilties
44
{
55
/// <summary>
66
/// Specifies that the attributed field should be considered a target for native initialization

src/NativeLibraryUtilities/NativeDelegateInitializer.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/NativeDelegateInitializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Runtime.InteropServices;
55
using System.Linq;
66

7-
namespace NativeLibraryUtilities
7+
namespace FRC.NativeLibraryUtilties
88
{
99
/// <summary>
1010
/// This class contains methods to initialize delegates
@@ -43,7 +43,7 @@ public static Delegate SetupNativeDelegate(ILibraryInformation library, string n
4343
/// <param name="library">The object containing the native library to load from</param>
4444
public static void SetupNativeDelegates<T>(ILibraryInformation library)
4545
{
46-
#if NET35
46+
#if !NETSTANDARD
4747
var info = typeof(T);
4848
#else
4949
TypeInfo info = typeof(T).GetTypeInfo();
@@ -77,7 +77,7 @@ public static void SetupNativeDelegates<T>(ILibraryInformation library)
7777
public static List<string> GetNativeDelegateList<T>()
7878
{
7979
List<string> nativeList = new List<string>();
80-
#if NET35
80+
#if !NETSTANDARD
8181
var info = typeof(T);
8282
#else
8383
TypeInfo info = typeof(T).GetTypeInfo();
@@ -100,7 +100,7 @@ public static List<string> GetNativeDelegateList<T>()
100100
public static List<string> GetNativeDelegateList(Type type)
101101
{
102102
List<string> nativeList = new List<string>();
103-
#if NET35
103+
#if !NETSTANDARD
104104
var info = type;
105105
#else
106106
TypeInfo info = type.GetTypeInfo();

src/NativeLibraryUtilities/NativeLibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/NativeLibraryLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Reflection;
55
using System.Runtime.InteropServices;
66

7-
namespace NativeLibraryUtilities
7+
namespace FRC.NativeLibraryUtilties
88
{
99
/// <summary>
1010
/// This class handles loading of a native library
@@ -251,7 +251,7 @@ private void ExtractNativeLibrary(string resourceLocation, string extractLocatio
251251

252252
private void ExtractNativeLibrary(string resourceLocation, string extractLocation, Type type)
253253
{
254-
#if NET35
254+
#if !NETSTANDARD
255255
ExtractNativeLibrary(resourceLocation, extractLocation, type.Assembly);
256256
#else
257257
ExtractNativeLibrary(resourceLocation, extractLocation, type.GetTypeInfo().Assembly);

src/NativeLibraryUtilities/Net35Extensions.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/Net40Extensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.Reflection;
33
using System.Runtime.InteropServices;
44

5-
namespace NativeLibraryUtilities
5+
namespace FRC.NativeLibraryUtilties
66
{
7-
#if NET35
7+
#if !NETSTANDARD
88
internal static class Net35Extensions
99
{
1010

src/NativeLibraryUtilities/OsType.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/OsType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NativeLibraryUtilities
1+
namespace FRC.NativeLibraryUtilties
22
{
33
/// <summary>
44
/// Enumeration of the OS type for this system.

src/NativeLibraryUtilities/StringUtil.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/StringUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NativeLibraryUtilities
1+
namespace FRC.NativeLibraryUtilties
22
{
33
internal static class StringUtil
44
{

src/NativeLibraryUtilities/Uname.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/Uname.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Runtime.InteropServices;
33
// ReSharper disable InconsistentNaming
44

5-
namespace NativeLibraryUtilities
5+
namespace FRC.NativeLibraryUtilties
66
{
77
internal sealed class Utsname
88
: IEquatable<Utsname>

src/NativeLibraryUtilities/WindowsLibraryLoader.cs renamed to src/FRC-Utilities/NativeLibraryUtilities/WindowsLibraryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.IO;
33
using System.Runtime.InteropServices;
44

5-
namespace NativeLibraryUtilities
5+
namespace FRC.NativeLibraryUtilties
66
{
77
/// <summary>
88
/// This class handles native libraries on Windows

0 commit comments

Comments
 (0)