Added input-processing program source `mkinput.c`

This swaps the rows and columns of the inputted file argument, and writes the result directly to stdout.

Fortune for day3's current commit: Half curse − 半凶
master
Avril 3 years ago
parent 6decefdc5b
commit 7e535cf9a2
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,17 +0,0 @@
#pragma once
namespace input {
constexpr const auto COLS = 6;
constexpr const char DATA[][COLS] = {
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
};
constexpr const auto ROWS = sizeof(DATA)/sizeof(DATA[0]);
}

@ -1,10 +1,12 @@
//! -pipe -std=gnu17 -O3 -msse -fwhole-program -flto -fno-strict-aliasing -Wall -Wstrict-aliasing -Werror -Wl,-flto -Wl,-O3 -o //! -pipe -std=gnu17 -O3 -msse -fwhole-program -flto -fno-strict-aliasing -Wall -Wstrict-aliasing -Werror -Wl,-flto -Wl,-O3 -o
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -14,29 +16,16 @@
if(__builtin_expect(!_expect__expr, 0)) { perror("Fatal error: `" #expr "` failed: " msg); exit(1); } \ if(__builtin_expect(!_expect__expr, 0)) { perror("Fatal error: `" #expr "` failed: " msg); exit(1); } \
_expect__expr; }) _expect__expr; })
union openfd { #define report(...) ({ fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); false; })
struct { #define throw(...) return report(__VA_ARGS__)
off_t len;
FILE* file;
} buf;
struct {
off_t len;
int fd;
} raw;
};
typedef struct mappedfd { typedef struct mappedfd {
int fd; int fd;
size_t len; size_t len;
uintptr_t origin; void* origin;
} mmap_t; } mmap_t;
inline static void* map_ptr(const mmap_t* map)
{
return (void*)map->origin;
}
bool filesz(int fd, off_t* restrict size) bool filesz(int fd, off_t* restrict size)
{ {
if(fd<0) return false; if(fd<0) return false;
@ -46,107 +35,156 @@ bool filesz(int fd, off_t* restrict size)
return true; return true;
} }
bool bopen(const char* name, const char* perm, union openfd* restrict file) bool map_stdin(mmap_t* restrict map, size_t sz)
{ {
FILE* f = fopen(name, perm); size_t size;
if(!f) return false; if(! (size = sz)) {
if(!filesz(fileno(f), &file->buf.len)) return (fclose(f), false); off_t osz;
file->buf.file = f; if(!filesz(STDIN_FILENO, &osz)) return false;
size = (size_t)osz;
}
void* origin = mmap(NULL, size, PROT_READ, MAP_PRIVATE, STDIN_FILENO, 0);
if(origin == MAP_FAILED) return false;
*map = (mmap_t) {
.fd = STDIN_FILENO,
.len = size,
.origin = origin,
};
return true; return true;
} }
bool ropen(const char* name, int perm, union openfd* restrict file) bool map_input(const char* file, mmap_t *restrict map)
{ {
int fd = open(name, perm, 0); int fd = open(file, O_RDONLY);
if(fd<0) return false; if(fd<0) return false;
if(!filesz(fd, &file->raw.len)) return (close(fd), false);
file->raw.fd = fd;
return true;
}
bool btor(union openfd* restrict file) off_t _sz;
{ if(!filesz(fd, &_sz)) return (close(fd), false);
if(fflush(file->buf.file)) return false; size_t sz = (size_t)_sz;
int fd = fileno(file->buf.file);
file->raw.len = file->buf.len; void* origin = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
file->raw.fd = fd; if(origin == MAP_FAILED) return (close(fd), false);
*map = (mmap_t){
.fd = fd,
.len = sz,
.origin = origin,
};
return true; return true;
} }
inline bool rclose(union openfd file) bool map_stdout(mmap_t* restrict map, size_t size)
{ {
return close(file.raw.fd) != 0; char link[64];
} int fd;
snprintf(link, sizeof(link), "/proc/%d/fd/1", getpid());
inline void bclose(union openfd file) fd = open(link, O_RDWR);
{ if(fd < 0) return report("failed to open fd 1 for rdwr (path was `%s')", link);
fclose(file.buf.file);
}
bool map_raw(union openfd* restrict file, mmap_t* restrict map) //dup2(fd, STDOUT_FILENO);
{ //close(fd);
void* origin = mmap(NULL, (size_t)file->raw.len, PROT_READ, MAP_SHARED, file->raw.fd, 0); // !!! normal stdout dead beyond this point
if(origin == MAP_FAILED) return false; if(ftruncate(fd, size)<0) return (close(fd), report("failed to truncate stdout (temp %d) to size %lu", fd, size));
*map = (mmap_t){ if(dup2(fd, STDOUT_FILENO)<0) return (close(fd), report("failed to dup2 %d -> %d", fd, STDOUT_FILENO));
.fd = file->raw.fd, close(fd);
.len = (size_t)file->raw.len,
.origin = (uintptr_t)origin,
};
file->raw.fd = -1;
return true;
}
bool map_clone(const mmap_t* from, mmap_t* restrict to) void* origin = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, STDOUT_FILENO, 0);
{ if(origin == MAP_FAILED) return report("failed to map stdout as write + shared at size %lu", size);
void* origin = mmap((void*)from->origin, from->len, PROT_READ|PROT_WRITE, MAP_PRIVATE, from->fd, 0);
if(origin == MAP_FAILED) return false; *map = (mmap_t){
*to = (mmap_t){ .fd = STDOUT_FILENO,
.fd = -1, .len = size,
.len = from->len, .origin = origin,
.origin = (uintptr_t)origin,
}; };
return true; return true;
} }
void map_close(mmap_t map) void map_close(mmap_t map)
{ {
munmap((void*)map.origin, map.len); munmap(map.origin, map.len);
if(map.fd >= 0) close(map.fd); if(map.fd >= 0) close(map.fd);
} }
inline bool map_buf(union openfd* restrict file, mmap_t* restrict map) const unsigned char VALID[255] = {
['0' ... '9'] = 1,
['\n'] = 2,
};
inline bool is_number(const char* str)
{ {
if(!btor(file)) return false; register unsigned char vl;
return map_raw(file, map); while( (vl = (unsigned char)*str++) ) if(VALID[vl] != 1) return false;
return true;
} }
bool process(const mmap_t* io) size_t get_width(const char* str)
{ {
char* input = map_ptr(io);
size_t size = io->len;
size_t i=0;
unsigned char v;
while( (v = VALID[(unsigned char) *str++]) ) {
i+=1;
if(v == 2) break;
}
return i;
}
bool process(const mmap_t* imap, const mmap_t* omap, size_t *restrict osize)
{
const char* input = imap->origin;
char* output = omap->origin;
uintptr_t _start = (uintptr_t)output;
size_t size = omap->len;
size_t width = get_width(input) - 1; //XXX: This isn't working, because of newlines and bullshit like that. Idk how to fix it...
size_t height = size / (width + 1);
fprintf(stderr, "size: %lu, w: %lu, h: %lu\n", size, width, height);
#define getat(ar, x, y) ((ar)[(((y) * (width+1)) + (x))])
assert(size % (width + 1)== 0);
for(size_t x = 0;x<width;x++) {
for(size_t y = 0; y<height; y++)
*output++ = getat(input, x, y);
*output++ = '\n';
}
*osize = (size_t)(((uintptr_t)output) - _start);
return true;
#undef getat
}
mmap_t open_input(char*const* argv)
{
const char* in = argv[1];
mmap_t input;
if(!in) expect(map_stdin(&input, 0), "failed to map stdin with no size hint");
else if(is_number(in)) expect(map_stdin(&input, strtoul(in, NULL, 10)), "failed to map stdin at that size");
else expect(map_input(in, &input), "failed to map input file");
return input;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if(argc < 1) return 1;
int rc = 0; int rc = 0;
union openfd input; mmap_t input;
expect(ropen(argv[1], O_RDONLY, &input), "couldn't open input file"); mmap_t output;
input = open_input(argv); //expect(map_stdin(&input), "failed to map stdin");
expect(map_stdout(&output, input.len), "failed to map stdout");
mmap_t read; expect(input.len == output.len, "invalid i/o lengths");
expect(map_raw(&input, &read), "failed to map file"); size_t fout;
mmap_t write; expect(process(&input, &output, &fout), "failed to process");
expect(map_clone(&read, &write), "failed to clone page"); fprintf(stderr, "output size %lu -> %lu\n", output.len, fout);
map_close(input);
if(!process(&write)) rc = (perror("failed to process"), 1); if(msync(output.origin, fout, MS_SYNC)) perror("failed to sync. output may be corrupted");
munmap(output.origin, output.len);
if(ftruncate(output.fd, fout)) perror("failed to truncate file to correct output size");
close(output.fd);
map_close(write);
map_close(read);
return rc; return rc;
} }

@ -0,0 +1,12 @@
1110010111110010111001000110100101010010101111011111111101001011111011100100111100111110110100010011111111000101000100010000001001110101110111011100110000000100001000101111100010010000001111000100110000101110111101010111001010111101110001001000001010010001000011111010100110000010000010011111010101110110100000011010011000001101101001101110101100011000111011011000011111111010100011110000000001000110000111011001011011111001011001001100100101011010010110011010010101001111110100001000110010110000100100111110011000001111010001111000011011000001100111001001110101011111101101010101011101000001000101110010010000000001000000000011000001111100000000001100111000100000100111100101001111111011101011101000010100010100011010101001001001010101011010110101111100101100001010000100110011111100110111110101100101110000111011110011011010101000111001111111011110101000011111101111100111100001011011011110110111010100100000001111010011010001100110101111001100111000000101010111000000110111011001111100101101001010
0001010011100000011000000001100100111001100001111011101010000000100000001001010001011001101011101011101011101110101101110101011000001000001000010101101101100011111101000011101000011110110001111011111110011001011011000101000101000011011101011111000011110110000000011001101010001010100010100110010010011011101111100010001110000000011011010010011010010011000011100000100110111101011001011111001111011010111111000000001011000101100001100111111100001001000001001010001100101101101100010101101000111011001101100101001010100010101100000111101111101011100000100100100001110001111110010011110110111011101101110011110000011000111111100110111001110101011111100001111111110111000000110010111010010101101001110010010000010100000001000011100101100100001010101100111100111111001111000100100101011000010000111101111011000100110000111011011010111100001001111110110011000101010100011101011011100010011011110101101100010100110000001111101100010111100110110011100111110000100100001001110000110000011110100101001011010101
1010101101110010010010000110011001101000001110110111111110010100001111111010111110111010100001101101010100011011111110111010000100000011000111101110001100010001001011011111110110011110101011110011011010001000010110010100000110100010000101010100100000100010100011101100100010110101110100011011010100000011110111101110101010100111111101010000101011101010100011000010101011110110010100110111101100010011011101101111010001001111111010100110010001000001010100000111011111001110110000011100101110000101100011101111011000010100010011001010011111101011001010000011101000101100000101001101001001110101000110101101010010011111100010101111000111011001000000100111011000101100100100110100111000110101011100010010001001010100010101110100101010100011111000111100010001100001111111111001010011101011110100110111001111100000011111101010000110011111101101100100010110100001110000011110011100011110100011110010110111111111100111000011100010010100111110001010010001100110011011111110010111100010100000100000010111100010
0101100101110101110001011000111100010100000110011000011111100110111101011000100000011001111111101110000110001101110010000010100100001111101111010110010110001010100111000101011101000010100100110110100001101100000011101100011011110111010001010010111010001110100001100110110010111000100001101010101001110100001000001110101010101100111001100000101011001011011111010110101011011010010101100011010001101100110000111101110010011111110100101001111001011101101000011001111010111000110011001110111011100000000011110001111101110100101011011101111011110011010110101100110101010011000001010111001010010100100011000101001001011101100010101100011000110011010010010101001010011110010001001001000001111101101011111000100100111001010101000110010100111100001001110111000101101011001010010011101101001100011101111101111011011000011110010010110011100100001101001011011011010010100100111100110110010001110011110100010101000101111111011101101001101011000110001101101001110001111110001001011101010110001000110111001101000111
1010000101001111011100100111111111111101011111101101010111101000010111111100011001101101110010101110110111010100001001000111110111001000000111010000111111101111100101101100110111100111101100000010010100110111100011010000001010110011001101110101111111001101011001001000000010001101000100001001010110001111011001101111100100011011110111011110100001010000101101011000000110011001011100000011000000010110010101100001111000000001001010101101101111001001011100110100111000101101001001110111011110001100001110111001010010110010011100001001010101110101101100000000100100000110101100011100010000110110000110111101110011111101001010010111000000011000101011111110011111010101110110011101111100011010001110111010001101110010100110111111000100001110001100100111001100010011111111110110100010111010011011001010111100000110111001011001001011010111101101011101001010010001101100111111101110001101100001000001101001110101000011000000001101100000010110001010101111010110111010001101100100100101000101110101010111100110
1000111111000001110111101010000100100100000111100101001111110111111001100100100101011011111101001100000111001111111101001100101001110100001000111001100100101001110000101101001111101100001100001000110110101000110111110001111010110100001001100100001001100101001011001011001010100111111110101101000101101001010101110100110111010110011100110001001010111010110100001010110101001110000100111001000011010101010011001001011000000100001110110011100100110100110010111000101001101101110000110100011110010010101001011010001011100010100110001111011000001000010011111010010101010101001000001001001010100110111101010110000100001011000000100011100011110001110101100111101010101100111010011000111001001111100001000001000111111001101110100100010000101101101000111110001010100111000011001010011101000000000000101001101100110110100111110000010110100001010000011101101101011001111111011100010110001011100011110110101110101110011000000010101110011000010110101011100001010100110000110001000110000001111101011110011001000110
1110110100100010100010111011101111000011100101101111010111001010110110101000101000101010110000011010011001110000111011011011101101111101010000010101001001111100001111110011100001000010010000111101000101100100110001100011001101100011100000110011100010101101011110100110100110100111110000110011000100011001111111100001111111000101101101000100010011110110100101100010001010000110101100111101101010100101010000011000010010001011000110010111011010001110010000110111000010000001010111011011100000001010001010001000001101111000101101010011101010000011110101001101011011100000000111000100010111001111000101001100011010010000101111010101001000101010000111111111100011011011110110010111110000010000111000011011010010110100111100101111101001100011110101010111000100010111011010001100100110010101110101110000000110011110011010111001010110110110001111010001100010110010010101111000100100100011001110111011000010001000010001111111111010110011110010011011111001111110000101001001001111100110111111011010100111100010
1101001000110000010010010111001000001000000110010010010001101101110011011001100010100110100110111100111110011100100100110001100100010001000001100111010001110100010110000000001110011110101010101010001100111000011101101011000100001000000111011110000010010100101001000010110001101001101111001110100111010101000001101110000000000011001111011000111000101100100110100100001100111111110011010110010101001101001001000000010111010000001000101001000011101110111010000000100010100010011001001001010000100010111100100101100010111000000000001011111110101101111111000001011111001001101101101010101011110010100100110111011111101101100111101110000010110100011111000101110000111010001111101001010011000011110101000010000100010101111101100011110100001011000101101001001110101010111011111100110011110001111010011111001011001011100110010001011111110001001111110101011011101101001011110001011010100110011011001111001010101010011100010100001000111111110100001011100100001010000001110101010110111101000111000000000001101101
1010111110000001101111001100101011101011111010011000100010110010010000000000110000100111010000111001000000001011011010010111100010100101110101010101010011111110011010011100101011101111011110010000000001011110010011100100101001010100011001011110000100010101100110101110110111001000101001111001000001100000110111111101110011101111001010000101110001011000001111101001111111110110001010010000101010011001111011111110101001110001100110101010010110101011001010000101000100100111010001101011100101001101011100101001001000011001001010110110001101110011010001100110101001110001011011111011101011010100011101101010011000100001010000101011011010000100001000000000000111000100111111000100000101110111101000111011000110000000001101010000010000100000100000100110110111101110011011101101000101101100110010011100101001100101101100111001111011110011101101001001001000011111010000011100011100001100010110110000000010100100001110011111000101000101010001011100111011011011000100110011100111100010111001000001000100010000
0100100100010010111111100000111100110101101110100011100111010000011100111010000011110111101001010010001000100011111000110010011110110110111011000111011001010110011001000111011110000001001111000001101001000111101001110101100101010101011011011001011001011001010001101101010011111110010010000100110100011101110011010100000110110011110100110011010011000100001000001011000110001001110110001100110100111101100110100010100101100011010101110100001001010010100011101101100111111111001011001010110000001001110010101100011101011010011011111110101010111110001000000011000100110010110100000001010000111001101100101010101110101101111011001001101101111000001110100010000100100111100011000111001001111110011101111001101000100010011110110101011111011100010110010110010110100001111101111010101110101100111100110100000001010111111001100010111111001110000110111011001101110100001111101011111111111101000110110011111000101000010010101010000011111011000111000110111001000011110010100111110101110011101100001101000010000100
0110010100001101100010010101110011010010100010010110110101110000010101000000110001001001110101100011111001101101010000010101110110110110001010010101100101111010110010101101011001101000001000011111001101010101011010010110001100101111011000000010100000111111100000001101101000101001010001110110110010101011101101001011100001110110100100000001110010100011110100011100000111000100101001011010011110000101111011100011111001111100011000111001011011011001010011111100101100001110100111000000100011011100100111001110000101001101000011001010000101101110110001011101011100111110010011101010001001000001010010110010001011101110101111101011001010011100000011111100001010111100001011111100110101010101000100001101010100110010001110010110110110110000101010101101001111011110111101010000000010011101101101111111100111011111001000000000101110110110101100010110100110100111111110111011100010010100001011100001100001010000001001100101011011011111100011001001110100100100101000001101010001011010101000101110101010111111
1110101100101110010100010101001100110110010101001001110011110010111000100001110011010111001011000111101001110011001100011000100111010111000000111001110101101010110011101111010010111100011000001001111110101101011100110100011000110100101010010001101000001110000011100001011111010011000001000110001111011110000110111011000001010101000111010001011001011111000011010000100100110100000010000110010010001011111010011100001011011011101011001011011110111111111101001001100010101101111011011101000010011011100111100111101010101101011111101010110100101110000111101000001100010011011001001111100100010101100010011101011111110011111110111011100111011001110110011110001000110100010011000111110001010010110100110011111011010011100101011001100000111001110100110111010010001100010010110000001000010000110010011110001110100011001010011011001110011111100001111000001110101110011101110000101100001100011110000100101101100101001100100011010001100001100001100101010000101100010010111011101101011010000110000010000100011101
Loading…
Cancel
Save