Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / formatjson.html
Last active December 14, 2015 10:39
A Web Page for Reformatting JSON Text
<!DOCTYPE html>
<html lang="en">
<!--
Copyright (c) 2013 Kristopher Johnson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
@kristopherjohnson
kristopherjohnson / formatjson.coffee.md
Last active December 14, 2015 21:49
Reads JSON from standard input and writes equivalent nicely-formatted JSON to standard output.

formatjson

This Literate CoffeeScript program reads JSON from standard input and writes equivalent nicely-formatted JSON to standard output.

Define a couple of short variable names for the Node.js stdin and stdout members.

stdin = process.stdin
stdout = process.stdout
@kristopherjohnson
kristopherjohnson / KDJKeychainItemWrapper.h
Created March 21, 2013 12:18
My version of the KeychainItemWrapper class from Apple's GenericKeychain sample, converted to ARC and modernized
// Based upon code from Apple's "GenericKeychain" sample application
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
@kristopherjohnson
kristopherjohnson / KDJConfig.h
Last active August 24, 2016 03:54
Objective-C class which retrieves values from a resource file Config.plist. Similar to NSUserDefaults, but read-only.
// Interface to the values stored in the Config.plist resource file
@interface KDJConfig : NSObject
// Return singleton instance
+ (KDJConfig *)sharedConfig;
// Return string value with specified key from Config.plist, or default value if not specified in the file
- (NSString *)stringForKey:(NSString *)key defaultValue:(NSString *)defaultValue;
@kristopherjohnson
kristopherjohnson / NSMutableURLRequest+KDJBasicAuthentication.h
Created March 26, 2013 15:52
Category on NSMutableURLRequest that sets HTTP "Authorization" header for basic access authentication, using code appropriated from AFNetworking
#import <Foundation/Foundation.h>
@interface NSMutableURLRequest (KDJBasicAuthentication)
// Sets the "Authorization" HTTP header to a basic authentication value
// with Base64-encoded username and password. This overwrites any existing
// value for this header.
- (void)setAuthorizationHeaderWithUsername:(NSString *)username
password:(NSString *)password;
- (IBAction)downloadFile:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://www.hulkshare.com/dl/qw30o7x373a8/stan_courtois_&_felly_vs_cutting_crew_-_die_in_your_arms_(x-centrik_mix)_%5B_www.themusix.net_%5D.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[url lastPathComponent]];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];
@kristopherjohnson
kristopherjohnson / temporaryFilePath.m
Last active December 15, 2015 13:19
Generate a unique temporary file path using Cocoa and C library
#include <stdlib.h>
// Generate a unique temporary file path
- (NSString *)temporaryFilePath {
// Construct full-path template by prepending tmp directory path to filename template
NSString *tempFilePathTemplate = [NSString pathWithComponents:@[NSTemporaryDirectory(), @"MyAppTemp.XXXXXX"]];
// Convert template to ASCII for use by C library functions
const char* tempFilePathTemplateASCII = [tempFilePathTemplate cStringUsingEncoding:NSASCIIStringEncoding];
- (BOOL)waitForCompletion:(NSTimeInterval)timeoutSecs {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutSecs];
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
if([timeoutDate timeIntervalSinceNow] < 0.0)
break;
} while (!done);
return done;
@kristopherjohnson
kristopherjohnson / KDJRegularExpressionPatternMatcher.m
Last active December 16, 2015 02:19
Kiwi matcher for regular-expression patterns
#import "Kiwi.h"
// Kiwi matcher used like this:
//
// [[subject should] matchPattern:(NSString*)regexPattern];
// [[subject should] matchPattern:(NSString*)regexPattern options:(NSRegularExpressionOptions)options];
//
// Examples:
//
@kristopherjohnson
kristopherjohnson / NSRunLoop+KDJRepeatBlock.h
Created April 17, 2013 11:22
NSRunLoop category that allows specification of a block to be repeatedly executed after each iteration of the run loop
#import <Foundation/Foundation.h>
@interface NSRunLoop (KDJRepeatBlock)
// Repeatedly call -runMode:beforeDate: and invoke the block until
//
// - the limitDate is reached, or
// - the block sets *stop to YES.
- (void)runMode:(NSString *)mode untilDate:(NSDate *)limitDate repeat:(void (^)(BOOL *stop))block;