adds fileout
This commit is contained in:
parent
f780f45c93
commit
1f3e2fca84
18
src/hex.c
18
src/hex.c
@ -1,7 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.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)
|
||||
void display_hex_chunk(hex_chunk_t* chunk, FILE* stream)
|
||||
{
|
||||
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 (j = 0; j < 4; j += 2) {
|
||||
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
|
||||
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++) {
|
||||
if (chunk->text[i] == '\n' || chunk->text[i] == EOF)
|
||||
printf("\x1b[33m.\x1b[32m");
|
||||
fprintf(stream, "\x1b[33m.\x1b[32m");
|
||||
else
|
||||
printf("%c", chunk->text[i]);
|
||||
fprintf(stream, "%c", chunk->text[i]);
|
||||
}
|
||||
puts("\x1b[0m");
|
||||
fprintf(stream, "\x1b[0m\n");
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef HEX_H
|
||||
#define HEX_H
|
||||
|
||||
@ -13,6 +15,6 @@ typedef struct {
|
||||
void free_hex_chunk(hex_chunk_t* chunk);
|
||||
void add_text_to_chunk(char* src, char** dst);
|
||||
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
|
||||
|
22
src/main.c
22
src/main.c
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "include/hex.h"
|
||||
#include "include/file.h"
|
||||
@ -10,7 +11,7 @@ int get_hex_lines(int len)
|
||||
int out;
|
||||
|
||||
out = len / 16;
|
||||
if (len % 16 != 0)
|
||||
if (len % 16 != 0) // if we have a less than a full line remaining
|
||||
out++;
|
||||
|
||||
return out;
|
||||
@ -19,13 +20,24 @@ int get_hex_lines(int len)
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
char* file_content;
|
||||
FILE* stream;
|
||||
bool outfile;
|
||||
int hex_lines, i;
|
||||
|
||||
if (argc != 2) {
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
|
||||
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);
|
||||
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++)
|
||||
display_hex_chunk(&(lines[i]));
|
||||
display_hex_chunk(&(lines[i]), stream);
|
||||
|
||||
|
||||
|
||||
if (outfile)
|
||||
fclose(stream);
|
||||
free(file_content);
|
||||
for (i = 0; i < hex_lines; i++)
|
||||
free_hex_chunk(&(lines[i]));
|
||||
|
Loading…
x
Reference in New Issue
Block a user