Skip to content

Instantly share code, notes, and snippets.

@opsJson
opsJson / a1-notation.h
Last active January 16, 2022 08:49
Convert integer to Google Sheets A1 Notation, and vice-versa.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char itoc(int i) {
if (i < 0) return 0;
return ((i-1) % 26) + 65;
}
@opsJson
opsJson / easy-win32-controls.h
Last active February 24, 2022 22:27
Easy interface for drawing Windows controls.
#ifndef EASY_WIN32_CONTROLS_H
#define EASY_WIN32_CONTROLS_H
// EXAMPLE:
// https://ibb.co/GRHmv5s
// case WM_CREATE: {
// startWindow(5, 5, hwnd);
//
// newWindow(STATIC, "Edit Example:", 0, 0, 0);
@opsJson
opsJson / segfault-check.c
Last active April 13, 2024 18:54
SegFault check
#include <stdlib.h>
#include <stdio.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <fcntl.h>
#define PIPE(X) _pipe(X, 2, O_RAW)
#else
#include <unistd.h>
#define PIPE(X) pipe(X)
#endif
@opsJson
opsJson / old-json-parser.c
Last active May 29, 2022 05:25
Simple JSON parser. (OLD VERSION)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum json_value_type {
jv_string,
jv_number,
jv_array,
jv_object
};
@opsJson
opsJson / sizeof-heap-var.c
Last active February 23, 2022 16:43
Stores the size of allocated memory before the void * given by memory allocation functions. You can get it easily with msize();
#include <stdio.h>
#include <stdlib.h>
void *__malloc__(size_t size) {
size_t *memory;
memory = (size_t*)malloc(sizeof(size_t) + size);
if (memory == NULL) return NULL;
*memory = size;
@opsJson
opsJson / unique-ptr.c
Last active March 19, 2022 02:23
Call a destructor function when variable is out of scope.
#include <stdio.h>
#include <stdlib.h>
#define unique_ptr(destructor) __attribute__ ((cleanup(destructor)))
void destructor(void *ptr) {
void *aux = *(void**)ptr;
fclose(aux);
printf("file closed.\n");
@opsJson
opsJson / GetFunctionFromDLL.c
Last active October 12, 2023 17:54
needs to be compiled on the same DLL architecture. (32bit or 64bit)
#include <stdio.h>
#include <windows.h>
void *GetFunctionFromDll(LPCSTR DLL, LPCSTR procName) {
HMODULE hModule;
void *function;
if ((hModule = LoadLibrary(DLL)) == NULL)
{
fwprintf(stderr, L"ERROR: could not load DLL.\n");
@opsJson
opsJson / wav.c
Last active March 19, 2022 02:52
Easily read and write WAV sound files.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct riff {
unsigned char chunkID[4];
unsigned int chunkSize;
unsigned char format[4];
} Riff;
@opsJson
opsJson / bmp.c
Last active March 19, 2022 05:29
Easily read and write bitmaps image files.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct header {
unsigned char signature[2];
unsigned int fileSize;
unsigned int reserved;
unsigned int dataOffset;
} Header;
@opsJson
opsJson / easy-curl.c
Last active June 23, 2022 01:59
Easy cURL interface
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct EasyCurlString {
int size;
char **str;
} EasyCurlString;