Skip to content

Instantly share code, notes, and snippets.

View Youka's full-sized avatar

Youka Youka

  • Germany
  • 07:45 (UTC +02:00)
View GitHub Profile
@Youka
Youka / data_stack.h
Created October 12, 2014 03:23
Data stack
#ifndef DATA_STACK_H // Include guard
#define DATA_STACK_H
#include <stdlib.h> // size_t, NULL, malloc, free
// Stack node
typedef struct data_stack_node{
void* data; // Userdata
size_t data_len; // Userdata byte length
struct data_stack_node* prev; // Reference to previous node
@Youka
Youka / goertzel_freq.lua
Created October 12, 2014 03:28
Goertzel frequency analyzer
-- Frequency analyzer
local function goertzel(samples, sample_rate, freq)
-- Get coefficient from target and maximal frequency
local coeff = 2 * math.cos(2 * math.pi * freq / sample_rate)
-- Process samples
local s_prev, s_prev2, s = 0, 0
for i=1, #samples do
s = samples[i] + coeff * s_prev - s_prev2
s_prev2, s_prev = s_prev, s
end
@Youka
Youka / number_bytes.lua
Last active April 30, 2017 00:42
Converter between byte string and number
-- Lua 5.1 or 5.2 table unpacker
local unpack = table.unpack or unpack
-- Bytes to unsigned integer
local function bton(s)
local bytes, n = {s:byte(1,-1)}, 0
for i=0, #s-1 do
n = n + bytes[1+i] * 256^i
end
return n
@Youka
Youka / string_replace.c
Last active January 13, 2016 19:57
C string find&replace
#include <string.h>
#include <stdlib.h>
// Replace string in string by creating a new one
char* str_replace(char* original, const char* find, const char* replacement, const char free_original){
// Initializations
int found_count = 0;
const size_t find_len = strlen(find), replacement_len = strlen(replacement);
char* result, *presult;
const char* poriginal = original, *found;
@Youka
Youka / dirty_data.c
Last active August 29, 2015 14:07
2d data are dirty
#include <stdint.h> // uint8_t, uint16_t, uintptr_t
#include <string.h> // memset
// Data are dirty?
int is_dirty_naive(const uint16_t width, const uint16_t height, const uint16_t stride, const uint8_t* data){
for(uint16_t row = 0; row < height; ++row){
for(uint16_t col = 0; col < width; ++col)
if(data[col] > 0)
return 1;
data += stride;
@Youka
Youka / timeout_exec.php
Created October 12, 2014 05:50
Execution with timeout
<?PHP
function timeout_exec($cmd, $stdin = "", &$stdout, &$stderr, $timeout = 0){
// Initialize output
$stdout = "";
$stderr = "";
// Start process
$pipes = array();
$process = proc_open($cmd, array(array('pipe','r'),array('pipe','w'),array('pipe','w')), $pipes);
if(!is_resource($process))
@Youka
Youka / cache.hpp
Last active August 29, 2015 14:07
Cache for pairs
#pragma once
#include <deque>
#include <algorithm>
template<typename Key, typename Value>
class Cache{
private:
std::deque<std::pair<Key,Value>> data;
const unsigned int max_size;
@Youka
Youka / scrollbar_size.js
Last active December 12, 2017 10:37
Get browser scrollbar width/height
function getScrollbarSize() {
var hiddenScroll = jQuery('<div />', {
css: {
visibility: 'hidden',
width: 100,
'overflow-y': 'scroll'
}
}).appendTo('body'),
hiddenScrollContentWidth = jQuery("<div />").css('width', '100%').appendTo(hiddenScroll).outerWidth();
hiddenScroll.remove();
@Youka
Youka / image_display.cpp
Last active April 27, 2018 18:29
Decodes, filters and displays images on windows
#include "image_display.h"
#include <windows.h>
#include <gdiplus.h>
#include <iostream>
#include <memory>
static LRESULT CALLBACK wndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:
SetWindowLongPtrA(wnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>(reinterpret_cast<CREATESTRUCTA*>(lParam)->lpCreateParams));
@Youka
Youka / lua_myobject.cpp
Last active December 4, 2023 18:41
Example of Lua in C++ and userdata objects
// Lua C API
#include <lua.hpp>
// C++ input/output streams
#include <iostream>
// MyObject as C++ class
class MyObject{
private:
double x;
public: