adds fileout

This commit is contained in:
SuperNovaa41 2025-02-06 14:04:06 -05:00
parent f780f45c93
commit 1f3e2fca84
3 changed files with 28 additions and 16 deletions

View File

@ -1,7 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "include/hex.h" #include "include/hex.h"
@ -32,27 +31,26 @@ void convert_text_to_hex(hex_chunk_t* chunk)
} }
} }
// TODO: clean up the text formatting to not print it every time void display_hex_chunk(hex_chunk_t* chunk, FILE* stream)
void display_hex_chunk(hex_chunk_t* chunk)
{ {
int i, j; int i, j;
printf("%08x: \x1b[32m", chunk->line * 16); fprintf(stream, "%08x: \x1b[32m", chunk->line * 16);
for (i = 0; i < HEX_LINE_LEN; i += 4) { for (i = 0; i < HEX_LINE_LEN; i += 4) {
for (j = 0; j < 4; j += 2) { for (j = 0; j < 4; j += 2) {
if (((chunk->hex + i) + j)[0] == '0' && ((chunk->hex + i) + j)[1] == 'a') if (((chunk->hex + i) + j)[0] == '0' && ((chunk->hex + i) + j)[1] == 'a')
printf("\x1b[33m%2.2s\x1b[32m", chunk->hex + i + j); fprintf(stream, "\x1b[33m%2.2s\x1b[32m", chunk->hex + i + j);
else else
printf("%2.2s", chunk->hex + i + j); fprintf(stream, "%2.2s", chunk->hex + i + j);
} }
printf(" "); fprintf(stream, " ");
} }
for (i = 0; i < TEXT_LINE_LEN; i++) { for (i = 0; i < TEXT_LINE_LEN; i++) {
if (chunk->text[i] == '\n' || chunk->text[i] == EOF) if (chunk->text[i] == '\n' || chunk->text[i] == EOF)
printf("\x1b[33m.\x1b[32m"); fprintf(stream, "\x1b[33m.\x1b[32m");
else else
printf("%c", chunk->text[i]); fprintf(stream, "%c", chunk->text[i]);
} }
puts("\x1b[0m"); fprintf(stream, "\x1b[0m\n");
} }

View File

@ -1,3 +1,5 @@
#include <stdio.h>
#ifndef HEX_H #ifndef HEX_H
#define HEX_H #define HEX_H
@ -13,6 +15,6 @@ typedef struct {
void free_hex_chunk(hex_chunk_t* chunk); void free_hex_chunk(hex_chunk_t* chunk);
void add_text_to_chunk(char* src, char** dst); void add_text_to_chunk(char* src, char** dst);
void convert_text_to_hex(hex_chunk_t* chunk); void convert_text_to_hex(hex_chunk_t* chunk);
void display_hex_chunk(hex_chunk_t* chunk); void display_hex_chunk(hex_chunk_t* chunk, FILE* stream);
#endif #endif

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "include/hex.h" #include "include/hex.h"
#include "include/file.h" #include "include/file.h"
@ -10,7 +11,7 @@ int get_hex_lines(int len)
int out; int out;
out = len / 16; out = len / 16;
if (len % 16 != 0) if (len % 16 != 0) // if we have a less than a full line remaining
out++; out++;
return out; return out;
@ -19,13 +20,24 @@ int get_hex_lines(int len)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
char* file_content; char* file_content;
FILE* stream;
bool outfile;
int hex_lines, i; int hex_lines, i;
if (argc != 2) {
if (argc < 2) {
fprintf(stderr, "Usage: %s [filename]\n", argv[0]); fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
stream = stdout;
outfile = false;
if (argc == 3) {
stream = fopen(argv[2], "w");
outfile = true;
}
read_file_to_buf(argv[1], &file_content); read_file_to_buf(argv[1], &file_content);
hex_lines = get_hex_lines(strlen(file_content)); hex_lines = get_hex_lines(strlen(file_content));
@ -39,10 +51,10 @@ int main(int argc, char* argv[])
} }
for (i = 0; i < hex_lines; i++) for (i = 0; i < hex_lines; i++)
display_hex_chunk(&(lines[i])); display_hex_chunk(&(lines[i]), stream);
if (outfile)
fclose(stream);
free(file_content); free(file_content);
for (i = 0; i < hex_lines; i++) for (i = 0; i < hex_lines; i++)
free_hex_chunk(&(lines[i])); free_hex_chunk(&(lines[i]));