Skip to content

Instantly share code, notes, and snippets.

View JPGygax68's full-sized avatar

Hans-Peter Gygax JPGygax68

View GitHub Profile
@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);
}
@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.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 / 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 / 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 / promisechain.js
Last active September 18, 2017 19:35
How to create a dynamic #promise chain.
// var index = ... hash of id => filename
return _.reduce(index, function(seq, filename, id) {
return seq.then( function() { return fs.read(filename); } )
}, Q.resolve());
@JPGygax68
JPGygax68 / backbone-single-reflow.js
Last active September 18, 2017 19:35
Oz Katz' (thanks!) single-reflow #Backbone #collection render()
render: function() {
this.$el.empty();
var container = document.createDocumentFragment();
// render each subview, appending to our root element
_.each(this._views, function(subview) {
container.appendChild(subview.render().el)
});
this.$el.append(container);
}
@JPGygax68
JPGygax68 / binding.gyp
Last active September 18, 2017 19:09
#Node: How to specify build-specific library directories for #node-gyp (binding.gyp)
{
'targets': [
{
'target_name': 'lsexcel_node',
'include_dirs': [ 'support/' ],
'sources': [ 'bindings.cc', 'support/utils.cc' ],
'configurations': {
'Debug': {
'msvs_settings': {
'VCLinkerTool': {
@JPGygax68
JPGygax68 / require_config.html
Last active September 18, 2017 19:33
Copy-paste example of a #require_js configuration, including loading require.js from the "scripts" subdirectory.Indented two levels (2 spaces per level) for correct insertion in the document header.
<script type="text/javascript" src="scripts/require.js" data-main="app/main"></script>
<script>
requirejs.config({
paths: {
'underscore': 'lib/underscore',
'jquery' : 'lib/jquery-2.0.3',
'backbone' : 'lib/backbone',
'relational': 'lib/backbone-relational',
'css' : 'lib/css.min'
},
@JPGygax68
JPGygax68 / local_webserver_Gruntfile.js
Last active September 18, 2017 20:58
Using #grunt-express as a local web server during development #tags: web server, grunt-server
"use strict";
var path = require('path');
module.exports = function(grunt) {
grunt.initConfig({
express: {
defaults: {
options: {
port: 3000,