Skip to content

Instantly share code, notes, and snippets.

#ifndef LIST_H_
#define LIST_H_
#define LIST(TYPE, SIZE) \
\
static TYPE list_##TYPE[SIZE]; \
static int list_start_##TYPE = 0; \
static int list_end_##TYPE = 0; \
int list_##TYPE_size = SIZE; \
\
@opsJson
opsJson / hashtable.c
Created May 28, 2023 03:40
Hashtable macro in C. TYPE is the variable type, KEY_SIZE is the maximum key string size, TABLE_SIZE is the maximum table size. (must be a prime number)
#ifndef HASHTABLE_H_
#define HASHTABLE_H_
#include <string.h>
unsigned int djb2(const char *str){
unsigned long hash = 5381;
int c;
while ((c = *str++)) {
@opsJson
opsJson / autobuild.c
Last active May 22, 2023 21:00
Automatically rebuild your program when you run it
#ifndef _AUTO_BUILD_H_
#define _AUTO_BUILD_H_
#include <stdlib.h>
#ifndef AUTOBUILD_TARGET
#ifdef _WIN32
#define AUTOBUILD_TARGET "autobuild.exe"
#else
#define AUTOBUILD_TARGET "./autobuild"
@opsJson
opsJson / makestr.c
Last active February 9, 2024 10:39
Easily allocate strings with a printf-like function.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <wchar.h>
char *makestr(char *format, ...) {
va_list args;
int size;
char *result;
@opsJson
opsJson / go.c
Last active June 6, 2023 22:15
C interface for libcurl.
#ifndef _GO_H_
#define _GO_H_
#include <curl/curl.h>
typedef struct {
char *headers;
char *body;
CURL *handle;
long int statuscode;
@opsJson
opsJson / cbprint.c
Last active May 4, 2024 03:17
Easy win32 GDI Print API interface with callbacks.
#ifndef _PRINTER_PAGE_H_
#define _PRINTER_PAGE_H_
#include <windows.h>
/* compile with flags -lgdi32 and -lwinspool */
int PrintPage(wchar_t *printer_name, void (*callback)(HDC printer, void *userdata), void *userdata) {
HDC printer;
DOCINFOA docinfo;
@opsJson
opsJson / easy_sql.c
Last active December 17, 2022 17:18
Really Easy MYSQL C API interface. Features: printf like arguments, auto escape arguments, multiple statement execution and auto reconnection.
#ifndef _EASY_SQL_H_
#define _EASY_SQL_H_
#include <mysql.h>
#include <stdio.h>
#include <string.h>
typedef my_ulonglong MYSQL_COUNT;
MYSQL *sql_open(char *host, int port, char *user, char *pass, char *db);
@opsJson
opsJson / unicode.c
Last active July 29, 2022 22:14
UTF8 codec in C
void utf8_encode(int codepoint, unsigned char encoded[5]) {
if (codepoint < 0x80) {
encoded[1] = 0;
encoded[0] = codepoint;
encoded[0] &= ~(1 << 7);
return;
}
@opsJson
opsJson / elf.c
Last active September 22, 2022 03:40
ELF - Elegant Leaks Finder
#ifndef ELF_H
#define ELF_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef ELF_CAPACITY
#define ELF_CAPACITY 1024
#endif
@opsJson
opsJson / mysql_api.c
Last active July 30, 2022 18:21
Easy MYSQL C API interface
#ifndef MYSQL_API_H
#define MYSQL_API_H
#include <mysql.h>
#include <stdio.h>
typedef struct MYSQL_CTX {
/* public */
unsigned int rows;
unsigned int columns;