Skip to content

Instantly share code, notes, and snippets.


Martyr2 Martyr2

View GitHub Profile
Martyr2 /
Last active February 15, 2020 16:44
DateValidator class in Java which will validate dates
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateValidator {
public boolean isThisDateValid(String dateToValidate, String dateFromat) {
if (dateToValidate == null) {
return false;
Martyr2 / read_line_number.cs
Created October 11, 2017 02:51
Reads the Nth line from a text file. It will throw exceptions if the file has fewer lines or Nth line is negative.
View read_line_number.cs
/// <summary>
/// Reads the Nth line from the file specified by filepath.
/// </summary>
/// <param name="filepath">Path to the file to read</param>
/// <param name="linenumber">Line number of the line to return</param>
/// <returns>Returns Nth line</returns>
private String readLineNumber(String filepath, int linenumber) {
if (linenumber > 0) {
using (StreamReader reader = new System.IO.StreamReader(filepath)) {
var count = 1;
Martyr2 / grayscale.cs
Created October 11, 2017 02:42
Grayscale an image (in other words make it black and white)
View grayscale.cs
/// <summary>
/// Grayscales an image
/// </summary>
/// <param name="source">Source image to grayscale</param>
/// <returns>New grayscaled image</returns>
private Image grayScale(ref Image source) {
Bitmap bitMap = new Bitmap(source);
using (Graphics g = Graphics.FromImage(bitMap)) {
Martyr2 / semantic_versioning.cs
Created October 7, 2017 23:00
Compare versions semantically and will tell you if the newer version is in fact newer than the current version. For more information refer to
View semantic_versioning.cs
/// <summary>
/// Determines if the new version is semantically newer than the current version.
/// </summary>
/// <param name="currentVersion">Current version number or build</param>
/// <param name="newVersion">New version to compare to the current version</param>
/// <returns>True if the newer version is semantically newer than the current version. False if not.</returns>
/// <remarks>Semantic versioning details at</remarks>
private bool IsSemanticallyNewerVersion(string currentVersion, string newVersion) {
if (!IsVersionSemanticallyValid(currentVersion)) {
throw new ArgumentException("Current version is not semantically valid. Cannot compare.");
Martyr2 / user.php
Created October 1, 2017 16:46
Simple User class with supporting Address class. This serves as a basic starting point for developing a quick user class for projects and can be expanded on further.
View user.php
* Basic user class to hold some standard data and designed to be expanded and altered as necessary.
class User {
private $userID = 0;
private $firstName = '';
private $lastName = '';
private $address = null;
Martyr2 / logger.php
Created September 25, 2017 05:03
Simple static logger class for logging errors. Provides convenience logging methods, date stamping the log file, getting call source and specifying log location.
View logger.php
class Logger {
private static $timestamp = 'Y-m-d H:i:s';
private static $logFileNameFormat = 'Y-m-d';
private static $logDirLocation = __DIR__;
private static $logLevels = ['DEBUG', 'INFO', 'WARN', 'ERROR'];
private static $minLogLevel = 'INFO';
Martyr2 / cryptor.php
Created September 23, 2017 17:23
Simple encryption / decryption utility class which uses openssl. Be sure to supply a strong cryptographic key. Change method as you see fit.
View cryptor.php
* Static class for simple encryption and decryption utilities.
class Cryptor {
// This key is used if one is not supplied during encryption / decryption.
private const UNIQUE_KEY = '<some default key here>';
private const METHOD = 'AES-256-CTR';
Martyr2 / flatten_array.php
Last active June 21, 2017 17:26
Iterative approach to flatten a multi-dimensional array of values.
View flatten_array.php
namespace FlattenArray;
* Iterative approach to flatten a multi-dimensional array of values.
* Notes: Non-array values are untouched so this could be an array of integers, strings, objects etc. An iterative approach is
* taken here over a recursive one as to not overload the call stack on deeply nested arrays.
function flatten($ar) {
Martyr2 / db_pdo_class.php
Last active November 29, 2018 06:12
Database PDO wrapper class for PHP
View db_pdo_class.php
* Database PDO Wrapper
if (!class_exists('Database')) {
// Define configuration
define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASS", "pass");
Martyr2 / LeftRight.cs
Created June 1, 2017 22:15
Left and Right string extension methods in C# that mimic the old VB6 functions of the same name.
View LeftRight.cs
/// <summary>
/// Takes the left "length" characters of a string. Pretty much "Left" from the old VB library.
/// </summary>
/// <param name="str">String to take characters from</param>
/// <param name="length">Length of the string to take</param>
/// <returns>Substring of original string of the left most characters as specified by length</returns>
public static string Left(this string str, int length)
return str.Substring(0, Math.Min(length, str.Length));