Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
ForeverZer0 / IsBetween.cs
Created June 5, 2018 16:41
[C#] Extension method to test if a numeric type is within a given range
public static bool IsBetween<T>(this T value, T min, T max) where T : struct, IComparable, IComparable<T>,
IConvertible, IEquatable<T>, IFormattable
{
return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0;
}
@ForeverZer0
ForeverZer0 / Color.cs
Last active June 5, 2018 16:48
[C#] RGBA Color structure using a vector
#region MIT License
// Color.cs is distributed under the MIT License (MIT)
//
// Copyright (c) 2018, Eric Freed
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
module Kernel
def suppress_warning
return unless block_given?
verbosity = $VERBOSE
$VERBOSE = nil
yield
$VERBOSE = verbosity
end
end
@ForeverZer0
ForeverZer0 / extract.rb
Created July 14, 2018 10:37
Unpacks a tar.gz file using Ruby, with symbolic-link and long name support
require 'rubygems/package'
require 'zlib'
TAR_LONGLINK = '././@LongLink'
tar_gz_archive = '/path/to/archive.tar.gz'
destination = '/where/extract/to'
Gem::Package::TarReader.new( Zlib::GzipReader.open tar_gz_archive ) do |tar|
dest = nil
tar.each do |entry|
@ForeverZer0
ForeverZer0 / libpng.c
Last active August 5, 2018 07:34
Basic PNG read/write example using libpng.
/*
* A simple libpng example program
* http://zarb.org/~gc/html/libpng.html
*
* Modified by Yoshimasa Niwa to make it much simpler
* and support all defined color_type.
*
* To build, use the next instruction on OS X.
* $ brew install libpng
* $ clang -lz -lpng15 libpng_test.c
@ForeverZer0
ForeverZer0 / String Replace Character.c
Created August 8, 2018 04:15
Function to do basic character replacement in a string.
char* replace_char(char* str, char find, char replace){
char *pos = strchr(str,find);
while (pos)
{
*pos = replace;
pos = strchr(pos,find);
}
return str;
}
@ForeverZer0
ForeverZer0 / bitmap.c
Created August 13, 2018 23:51
Cross-platform C sources to read device-independent bitmap files
/*
* Windows BMP file functions for OpenGL.
*
* Written by Michael Sweet.
*/
#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
@ForeverZer0
ForeverZer0 / DataContractXml.cs
Last active August 31, 2018 05:40
Simple utility methods for reading/writing objects decorated with DataContractAttribute in XML format.
public static bool Serialize<T>(T obj, Stream stream)
{
try
{
var xml = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 };
var serializer = new DataContractSerializer(typeof(T));
using (var writer = XmlWriter.Create(stream, xml))
{
serializer.WriteObject(writer, obj);
}
@ForeverZer0
ForeverZer0 / ColorExtensions.cs
Created September 1, 2018 00:48
Provides conversion to/from System.Drawing.Color and System.ConsoleColor.
using System;
using System.Drawing;
namespace MyNamespace
{
public static class Extensions
{
public static Color ToColor(this ConsoleColor color)
{
switch (color)
@ForeverZer0
ForeverZer0 / ConsoleHelper.cs
Created September 3, 2018 02:25
Simple helper methods for spawning/closing an attached console to a Windows application.
using System;
using System.Runtime.InteropServices;
namespace MyNamespace
{
public static class ConsoleHelper
{
private const int SW_SHOW = 5;
public static void Show()