Skip to content

Instantly share code, notes, and snippets.

View JPGygax68's full-sized avatar

Hans-Peter Gygax JPGygax68

View GitHub Profile
@JPGygax68
JPGygax68 / FilePtr.cpp
Last active September 18, 2017 19:35
#RIIA wrapper for FILE*. No copy semantics yet.
class FilePtr {
public:
FilePtr() { fp = nullptr; }
FilePtr(FILE *fp_) { fp = fp_; }
FilePtr & operator = (FILE *fp_) { assert(fp == nullptr); fp = fp_; return *this; }
~FilePtr() { if (fp != nullptr) fclose(fp); }
operator FILE * () { return fp; }
private:
FILE *fp;
};
@JPGygax68
JPGygax68 / nestedAsync.js
Last active September 18, 2017 19:36
Simple pattern to detect when nested #asynchronous operations are fully done. Caveats: no error handling - use for simple scripts only!
"use strict";
var pending_asops = 0;
var global_data;
startingAsop();
asyncFunction('param1', 'param2', function(err, data) { global_data = data; } );
function allDone() {
// Use the complete data!
@JPGygax68
JPGygax68 / main.js
Last active September 18, 2017 19:36
Node with #Express, #Jade, #Stylus, and #Mongoose
var express = require('express')
, path = require('path')
, stylus = require('stylus')
, nib = require('nib')
, mongoose = require('mongoose');
var app = express();
app.locals.title = 'My Web App';
@JPGygax68
JPGygax68 / getWindowsErrorString.cpp
Last active September 18, 2017 19:07
Retrieve the text associated with a #Windows #error code, and return it as a std::string
static const std::string
getWindowsErrorString(int err = 0)
{
LPSTR s;
if (err == 0) err = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
@JPGygax68
JPGygax68 / main.cpp
Created April 26, 2013 09:40
Minimal C++ main.cpp
#include <iostream>
using namespace std;
int
main (int argc, char *argv[])
{
cout << "Press ENTER to continue....." << endl << endl;
cin.ignore(1);
}