Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
static unsigned int graph[21][21] = {{0}};
@smonn
smonn / pointer.c
Created January 30, 2012 19:09
C pointer sample
#include <stdio.h>
void foo1(int * bar) {
// bar is now set to 2 outside this function
// scanf works in a similar way
// * dereferences bar, without it we would try to assign 2 to a pointer
*bar = 2;
}
void foo2(int bar) {
@smonn
smonn / domready.js
Created May 30, 2012 19:57
My DOM Ready function
/*jslint indent: 2 */
(function (global) {
'use strict';
var domready = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback is not a function');
}
function loaded() {
callback();
}
@smonn
smonn / xhr.js
Created May 30, 2012 19:58
My XML HTTP Request function
/*jslint browser: true, windows: true, indent: 2 */
// async: whether the request is asynchronuos or not
// cached: set to false to ignore browser cache
// method: the http method to use
// data: key value pairs, e.g. foo=1&bar=2
// url: the absolute or relative url to send the request to
// success: the success function, will receive the XHR object
// failure: the failure function, will receive the XHR object
// prepare: set to a function to set additional parameters to the xhr object, it should return the xhr object
(function (w) {
@smonn
smonn / readme.md
Last active March 27, 2022 10:49
UUID v4 generation

Not the smallest footprint out there, but it seems to be faster when I compare against other solutions, but not when I use jsperf.com for some reason. This solution is based upon that gist by the way. Biggest difference is that this is more readable...

This will generate a version 4 UUID. The non-packed version passes JSLint. Performance comparison at jsperf.

Reduced footprint.

@smonn
smonn / startdev.js
Last active December 13, 2015 18:08
This is my little play script written in Node.js. I use it to fetch the latest changes from the version control system and build the project(s) I'm working on. Consider it a Makefile written in JavaScript. Probably not entirely necessary, but I find it useful and more fun to maintain than a shell script.
/*jshint node: true */
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
cwd = 'C:\\working\\directory',
env = {
'PATH': 'C:\\Windows\\System32\\;'
},
commands = {
'command name': 'command-line --with-params'
@smonn
smonn / encrypt.cs
Last active December 14, 2015 00:59
Encrypt password with random salt, and example of slow equals.
/*
using System;
using System.Linq;
using System.Security.Cryptography;
*/
// The user's password in pure text.
var password = string.Empty;
@smonn
smonn / ImageHelper.cs
Created March 5, 2013 11:03
Image helper class. Basic "validation" of JPEG bytes.
/// <summary>
/// Helper methods for verifying image content types by validating the first bytes of the file.
/// You can extend this class by adding additional XxxFirstBytes constants and IsXxx methods.
/// </summary>
public static class ImageHelper
{
// These are the first three bytes of a JPEG, usually enough to determine if the file is an image/jpeg
private static readonly byte[] JpegFirstBytes = new byte[] { 0xFF, 0xD8, 0xFF };
@smonn
smonn / readme.md
Last active December 14, 2015 14:18
Issue with vagrant init: "No such file or directory".

After installing VirtualBox and Vagrant I tried to follow the getting started guide, but when I tried to run vagrant init I got this error:

C:\vagrant_guide> vagrant init
C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:247:in `mkdir': No such file or directory - H: (Errno::ENOENT)
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:247:in `fu_mkdir'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:221:in `block (2 levels) in mkdir_p'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:219:in `reverse_each'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:219:in `block in mkdir_p'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:205:in `each'

from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:205:in `mkdir_p'

@smonn
smonn / ImageBuilder.cs
Last active December 14, 2015 15:29
A class that resizes an image.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
public sealed class ImageBuilder : IDisposable
{