Skip to content

Instantly share code, notes, and snippets.

@nyg
nyg / AddJPEGComment.swift
Last active February 21, 2021 05:17
Add a JPEG comment marker to file in pure Swift.
//
// Inspired by wrjpgcom of libjpeg
// https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/wrjpgcom.c
//
// https://stackoverflow.com/a/46045524/5536516
//
// Note: To add an EXIF UserComment go here:
// https://gist.github.com/nyg/c90f36abbd30f72c8b6681ef23db886b
import Foundation
@nyg
nyg / Serialize.swift
Created August 29, 2017 19:50
Serialize NSObject (or array of) to XML in Swift.
import Foundation
class MyClass: NSObject, NSCoding {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
@nyg
nyg / MemoryAddress.swift
Last active October 12, 2023 07:42
Get the memory address of both class and structure instances in Swift.
// https://stackoverflow.com/a/45777692/5536516
import Foundation
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
@nyg
nyg / uptime.c
Last active April 1, 2024 21:33
Get boot time and uptime on macOS in C.
// Tested on macOS 10.12.
//
// Usage:
// $ clang uptime.c && ./a.out
// boot time (UNIX time): 1502299682.147510
// uptime (seconds): 410563.269028116
#include <stdlib.h>
#include <errno.h>
#include <string.h>
@nyg
nyg / yql_json.html
Created August 6, 2017 13:27
Using Yahoo Query Language (YQL) to get JSON from an external URL and bypass the Access-Control-Allow-Origin restriction.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Using YQL to get JSON from an external URL</title>
<meta charset="utf-8" />
</head>
<body>
<pre id='json'></pre>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script>
@nyg
nyg / Uptime.swift
Last active April 1, 2024 21:35
Get boot time and uptime for macOS & iOS.
// https://stackoverflow.com/a/45068046/5536516
import Foundation
func kernelBootTime() -> timeval {
var mib = [ CTL_KERN, KERN_BOOTTIME ]
var bootTime = timeval()
var bootTimeSize = MemoryLayout<timeval>.size
@nyg
nyg / UIApplicationDelegate.swift
Created July 10, 2017 14:30
Adding `shared` property to UIApplicationDelegate
// https://stackoverflow.com/a/45014628/5536516
extension UIApplicationDelegate {
static var shared: Self {
return UIApplication.shared.delegate! as! Self
}
}
/*
@nyg
nyg / sequence_iterator.swift
Last active September 20, 2017 11:41
Implementing Sequence & IteratorProtocol, Swift 3
import Foundation
class Cell {
var name: String
init(_ name: String) {
self.name = name
}
}
@nyg
nyg / enlarge_image.php
Last active February 27, 2024 22:15
Enlarge a given image.
<?php
$filename = $argv[1];
$growthFactor = $argv[2];
list($width_orig, $height_orig) = getimagesize($filename);
$width = $width_orig * $growthFactor;
$height = $height_orig * $growthFactor;
@nyg
nyg / fk_generate_delete.sql
Created January 11, 2017 10:03
Oracle: outputs the necessary delete statements to delete a row and all rows preventing it from being deleted because of "child record found" error.
DECLARE
g_level INTEGER := 0;
g_owner VARCHAR2(10) := 'owner';
PROCEDURE find_fk(pi_table IN VARCHAR2,
pi_where IN VARCHAR2) IS
l_where VARCHAR2(500);
l_pad VARCHAR2(100);
BEGIN
g_level := g_level + 1;