Skip to content

Instantly share code, notes, and snippets.

View JPGygax68's full-sized avatar

Hans-Peter Gygax JPGygax68

View GitHub Profile
@JPGygax68
JPGygax68 / readfile.cpp
Last active September 18, 2017 19:06
C++: read a whole #file into a string.
using namespace std;
ifstream fs(filename);
string s;
fs.seekg(0, ios::end);
s.reserve( (size_t) fs.tellg() );
fs.seekg(0, ios::beg);
s.assign( istreambuf_iterator<char>(fs), istreambuf_iterator<char>() );
@JPGygax68
JPGygax68 / gulp_textfile_wrapper.js
Last active August 29, 2015 13:59
This is a Gulp "pipe" that wraps the content of text files into CommonJS modules exporting that content as a string.
var gulp = require('gulp');
var gutil = require('gulp-util');
var through = require('through2');
/* Wrap the content of text files into CommonJS modules exporting that content as JS strings.
*/
function textFileToCommonJS(filename, options) {
var lines = [];
@JPGygax68
JPGygax68 / CMakeLists.txt
Last active September 18, 2017 19:23
#CMakeLists.txt #project #top-level file
cmake_minimum_required(VERSION 3.0)
#------------------------------------------------
# Project + basics
#
project(GPCFontRasterizer)
set(${PROJECT_NAME}_MAJOR_VERSION 0)
set(${PROJECT_NAME}_MINOR_VERSION 1)
@JPGygax68
JPGygax68 / utf8-utf32.cpp
Last active July 15, 2022 08:13
#Unicode conversion: UTF-8 to UTF-32 in C++11
#include <codecvt>
using namespace std;
#if _MSC_VER >= 1900
wstring_convert<codecvt_utf8<int32_t>, int32_t> utf32conv;
auto utf32 = utf32conv.from_bytes("The quick brown fox jumped over the lazy dog.");
// use reinterpret_cast<const char32_t *>(utf32.c_str())
#else
wstring_convert<codecvt_utf8<char32_t>, char32_t> utf32conv;
@JPGygax68
JPGygax68 / traits.cpp
Last active December 23, 2020 07:20
How to implement custom #traits (C++11)
#include <type_traits>
#include <iostream>
using std::enable_if;
using std::is_same;
using std::declval;
using std::integral_constant;
template <typename T>
using Invoke = typename T::type;
@JPGygax68
JPGygax68 / pipe.cpp
Last active September 18, 2017 19:04
How to support #piping (input/output redirection) in C++
ifstream ifs;
ofstream ofs;
istream *is = nullptr;
ostream *os = nullptr;
if (!input_file.empty()) {
ios_base::sync_with_stdio(false);
ifs.open(input_file, ios::binary);
is = &ifs;
}
@JPGygax68
JPGygax68 / indexed_struct.cpp
Last active September 18, 2017 19:22
Proof-of-concept for something similar to #tuple, but based on struct #composition instead of struct inheritance.
#include <iostream>
template <typename IndexedStruct, int Index>
struct _getter {
using Rest = decltype(std::declval<IndexedStruct>().rest);
static auto& get(IndexedStruct &inst)
{
return _getter<decltype(inst.rest), Index-1>::get(inst.rest);
@JPGygax68
JPGygax68 / opengl_rectangle.hpp
Last active September 18, 2017 19:04
Defining and drawing a rectangle with #OpenGL using a #VAO (vertex array object) and a vertex buffer.
/* Example class for rendering a rectangle using OpenGL 4, using a vertex buffer and a
* vertex array object and the triangle strip primitive.
*
* Note: GL calls are wrapped into a variadic macro of the form GL(func, arg1, arg2,...).
*/
class opengl_rectangle {
public:
void get_resources()
@JPGygax68
JPGygax68 / consume-ffmpeg-libs.cmake
Last active November 23, 2024 15:21
#CMake: how to add the #FFmpeg libraries to a target
# Detect architecture
if (CMAKE_SIZEOF_VOID_P MATCHES 8)
set( PROJECT_ARCH "x86_64" )
else(CMAKE_SIZEOF_VOID_P MATCHES 8)
set( PROJECT_ARCH "x86" )
endif(CMAKE_SIZEOF_VOID_P MATCHES 8)
# FFmpeg
@JPGygax68
JPGygax68 / find_next_nal_unit.cpp
Last active September 18, 2017 19:03
Function that finds the next #NALU inside #H264 bitstream data.
/*
* Find next NAL unit from the specified H.264 bitstream data.
*/
static auto find_next_nal_unit(const uint8_t *start, const uint8_t *end) -> const uint8_t *
{
const uint8_t *p = start;
/* Simply lookup "0x000001" pattern */
while (p <= end - 3 && (p[0] || p[1] || p[2] != 1))
++p;