50
Makefile
Normal file
50
Makefile
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# Compiler and flags
|
||||||
|
CC := clang
|
||||||
|
CFLAGS := -Wall -Wextra -g -xc -std=c99 -I./include
|
||||||
|
LDFLAGS :=
|
||||||
|
|
||||||
|
EXEC_NAME := $(notdir $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
|
||||||
|
|
||||||
|
# Directories
|
||||||
|
SRC_DIR := src
|
||||||
|
OBJ_DIR := obj
|
||||||
|
BIN_DIR := bin
|
||||||
|
|
||||||
|
# Find all source files recursively
|
||||||
|
SRCS := $(shell find $(SRC_DIR) -name '*.c')
|
||||||
|
# Generate object file paths by replacing src/ with obj/ and .c with .o
|
||||||
|
OBJS := $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
|
||||||
|
# Final executable name
|
||||||
|
EXEC := $(BIN_DIR)/$(EXEC_NAME)
|
||||||
|
|
||||||
|
# First rule is the default rule
|
||||||
|
.PHONY: all
|
||||||
|
all: $(EXEC)
|
||||||
|
|
||||||
|
# Rule to create directories if they don't exist
|
||||||
|
$(BIN_DIR) $(OBJ_DIR):
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
# Rule to create object files
|
||||||
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
@mkdir -p $(dir $@)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Rule to create the executable
|
||||||
|
$(EXEC): $(OBJS) | $(BIN_DIR)
|
||||||
|
$(CC) $(OBJS) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
# Clean rule to remove generated files
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(OBJ_DIR) $(BIN_DIR)
|
||||||
|
|
||||||
|
# Debug rule to print variables
|
||||||
|
.PHONY: debug
|
||||||
|
debug:
|
||||||
|
@echo "Source files:" $(SRCS)
|
||||||
|
@echo "Object files:" $(OBJS)
|
||||||
|
|
||||||
|
.PHONY: compiledb
|
||||||
|
bear: clean
|
||||||
|
bear -- make
|
||||||
94
src/main.c
Normal file
94
src/main.c
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define DEFAULT_WIDTH 64
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
|
||||||
|
const char *emoji_map[256] = {
|
||||||
|
"😀","😁","😂","🤣","😃","😄","😅","😆",
|
||||||
|
"😉","😊","😋","😎","😍","😘","🥰","😗",
|
||||||
|
"😙","😚","🙂","🤗","🤩","🤔","🫡","🤨",
|
||||||
|
"😐","😑","😶","🙄","😏","😣","😥","😮",
|
||||||
|
"🤐","😯","😪","😫","🥱","😴","😌","😛",
|
||||||
|
"😜","😝","🤤","😒","😓","😔","😕","🙃",
|
||||||
|
"🫠","🤑","😲","☹","🙁","😖","😞","😟",
|
||||||
|
"😤","😢","😭","😦","😧","🥺","😨","😩",
|
||||||
|
"🤯","😬","😰","😱","🥵","🥶","😳","🤪",
|
||||||
|
"😵","🥴","😠","😡","🤬","😷","🤒","🤕",
|
||||||
|
"🤢","🤮","🤧","😇","🥳","🥸","😈","👿",
|
||||||
|
"👹","👺","💀","☠","👻","👽","👾","🤖",
|
||||||
|
"💩","😺","😸","😹","😻","😼","😽","🙀",
|
||||||
|
"😿","😾","🙈","🙉","🙊","🐵","🐒","🦍",
|
||||||
|
"🦧","🐶","🐕","🦮","🐕","🐩","🐺","🦊",
|
||||||
|
"🦝","🐱","🐈","🐈","🦁","🐯","🐅","🐆",
|
||||||
|
"🐴","🐎","🦄","🦓","🦌","🦬","🐮","🐂",
|
||||||
|
"🐃","🐄","🐷","🐖","🐗","🐽","🐏","🐑",
|
||||||
|
"🐐","🐪","🐫","🦙","🦒","🐘","🦣","🦏",
|
||||||
|
"🦛","🐭","🐁","🐀","🐹","🐰","🐇","🐿",
|
||||||
|
"🦫","🦔","🦇","🐻","🐻","🐨","🐼","🦥",
|
||||||
|
"🦦","🦨","🦘","🦡","🐾","🦃","🐔","🐓",
|
||||||
|
"🐣","🐤","🐥","🐦","🐧","🕊","🦅","🦆",
|
||||||
|
"🦢","🦉","🦤","🪶","🦩","🦚","🦜","🐸",
|
||||||
|
"🐊","🐢","🦎","🐍","🐲","🐉","🦕","🦖",
|
||||||
|
"🐳","🐋","🐬","🦭","🐟","🐠","🐡","🦈",
|
||||||
|
"🐙","🪼","🦑","🦐","🦞","🦀","🐚","🪸",
|
||||||
|
"🌌","🦋","🐛","🐜","🐝","🪲","🐞","🦗",
|
||||||
|
"🪳","🕷","🕸","🦂","🦟","🪰","🪱","🦠",
|
||||||
|
"💐","🌸","💮","🏵","🌹","🥀","🌺","🌻",
|
||||||
|
"🌼","🌷","🪷","🌱","🪴","🌲","🌳","🌴",
|
||||||
|
"🌵","🌾","🌿","☘","🍀","🍁","🍂","🍃"
|
||||||
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
static void usage() {
|
||||||
|
fprintf(stderr, "Usage: binvis <filename> [width]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 2) {
|
||||||
|
usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *path = argv[1];
|
||||||
|
int width = -1;
|
||||||
|
|
||||||
|
if (argc >= 3) {
|
||||||
|
width = atoi(argv[2]);
|
||||||
|
if (width < 0 || width % 8 != 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Width MUST be a positive number that is divisible by 8. "
|
||||||
|
"Provided width was: %d\n",
|
||||||
|
width);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
width = DEFAULT_WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
if (!file) {
|
||||||
|
perror("fopen");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int c, count = 0;
|
||||||
|
while ((c = fgetc(file)) != EOF) {
|
||||||
|
uint8_t uc = (uint8_t)c;
|
||||||
|
if (uc != 0) {
|
||||||
|
printf("%s", emoji_map[uc]);
|
||||||
|
count++;
|
||||||
|
if (count % width == 0) {
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user