Skip to content

Instantly share code, notes, and snippets.

Martyr2 Martyr2

View GitHub Profile
@Martyr2
Martyr2 / date_validator.java
Created Oct 11, 2017
DateValidator class in Java which will validate dates
View date_validator.java
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
Martyr2 / read_line_number.cs
Created Oct 11, 2017
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
Martyr2 / grayscale.cs
Created Oct 11, 2017
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
Martyr2 / semantic_versioning.cs
Created Oct 7, 2017
Compare versions semantically and will tell you if the newer version is in fact newer than the current version. For more information refer to http://semver.org
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 http://semver.org</remarks>
private bool IsSemanticallyNewerVersion(string currentVersion, string newVersion) {
if (!IsVersionSemanticallyValid(currentVersion)) {
throw new ArgumentException("Current version is not semantically valid. Cannot compare.");
@Martyr2
Martyr2 / user.php
Created Oct 1, 2017
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
<?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
Martyr2 / logger.php
Created Sep 25, 2017
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
<?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
Martyr2 / cryptor.php
Created Sep 23, 2017
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
<?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
Martyr2 / flatten_array.php
Last active Jun 21, 2017
Iterative approach to flatten a multi-dimensional array of values.
View flatten_array.php
<?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
Martyr2 / db_pdo_class.php
Last active Nov 29, 2018
Database PDO wrapper class for PHP
View db_pdo_class.php
<?php
/**
* Database PDO Wrapper
*/
if (!class_exists('Database')) {
// Define configuration
define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASS", "pass");
@Martyr2
Martyr2 / LeftRight.cs
Created Jun 1, 2017
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));
}
You can’t perform that action at this time.