Skip to content

Instantly share code, notes, and snippets.

@quexy
quexy / StreamExtensions.cs
Last active November 7, 2017 11:57
Read/Write binary streams more easily
using System;
using System.Linq;
using System.Text;
namespace System.IO
{
public static class StreamExtensions
{
public static Endianness Endianness = Endianness.BigEndian;
@quexy
quexy / MultipartFormDataMediaTypeFormatter.cs
Created June 19, 2017 11:16
WebAPI 'multipart/form-data' input formatter
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace WebApi.Formatters
@quexy
quexy / VerifyJsonContent.cs
Created June 19, 2017 11:11
WepAPI SpecFlow: checks if input string as JSON of given type matches input table
///<summary> Checks whether the JSON <paramref name="content"/> as <paramref name="resultType"/> contains the entities described in the given <paramref name="table"/> </summary>
private static string[] VerifyJsonContent(string content, string resultType, Table table)
{
var matchingTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetExportedTypes())
.Where(t => (t.Namespace + "." + t.Name).EndsWith(resultType)).ToArray();
if (matchingTypes.Length != 1) return new[] { string.Format("Cannot determine matching type by the name {0}; candidates: [{1}]",
resultType, string.Join(", ", matchingTypes.Select(t => t.Namespace + "." + t.Name))) };
object message = null;
@quexy
quexy / CircularBuffer.cs
Last active June 19, 2017 11:19
Fixed size circular FIFO buffer
namespace System.Collections.Generic
{
using System.Linq;
public sealed class CircularBuffer<TItem> : IList<TItem>, ICollection<TItem>, IEnumerable<TItem>
{
private int count = 0;
private int offset = 0;
private readonly TItem[] buffer;
@quexy
quexy / CancelableActionCall.cs
Last active June 19, 2017 11:20
Cancelable wrapper for Begin--End-style async methods
using System;
using System.Linq.Expressions;
using System.Threading;
namespace Cancelable
{
public struct CancelableActionCall<TS>
{
private readonly TS _svc;
private readonly CancellationToken _token;
@quexy
quexy / SqlClientExtensions.cs
Last active November 7, 2017 12:23
Rudimentary OR/M IDbCommand & IDataReader extension methods
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace System.Data
{
public static class SqlClientExtensions
@quexy
quexy / qemu-arch-cpu.c
Last active August 29, 2015 14:06
QEMU for architecture-switching chroot (eg: initial RPi setup)
// based on: https://www.gentoo.org/proj/en/base/embedded/handbook/?part=1&chap=5
// build as: gcc -static qemu-arch-cpu.c -O3 -s -o /usr/bin/qemu-arch-cpu
// use like: ln -s /usr/bin/qemu-arch-cpu /usr/bin/qemu-arm-arm1176
#include <malloc.h>
#include <string.h>
#include <unistd.h>
char** parse_tuple(char* tuple) {
int pos = 0;
@quexy
quexy / Console.ReadPassword.cs
Last active February 7, 2017 15:18
Console masking password reader
[System.Diagnostics.DebuggerStepThrough]
public static string ReadPassword(char maskChar = '*')
{
var password = string.Empty;
var categories = new[] { UnicodeCategory.Control, UnicodeCategory.Format, UnicodeCategory.OtherNotAssigned, UnicodeCategory.PrivateUse, UnicodeCategory.Surrogate };
for (var key = Console.ReadKey(true); key.Key != ConsoleKey.Enter; key = Console.ReadKey(true))
{
if (!categories.Contains(CharUnicodeInfo.GetUnicodeCategory(key.KeyChar)))
{
password = string.Concat(password, key.KeyChar);
@quexy
quexy / SpecFlow.CollectionVerifier.cs
Last active August 29, 2015 14:01
Collection verifier for SpecFlow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace TechTalk.SpecFlow.ObjectVerification
{
public static class CollectionVerifierExtensions
{
/// <summary>
@quexy
quexy / SpecFlow.ObjectVerifier.cs
Last active August 29, 2015 14:01
Object verifier for SpecFlow
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
namespace TechTalk.SpecFlow.ObjectVerification
{
public static class ObjectVerifierExtensions
{
/// <summary>