implements uppercase flag

This commit is contained in:
SuperNovaa41 2025-02-06 22:38:06 -05:00
parent 14680d2aeb
commit 20b24245d6
3 changed files with 41 additions and 29 deletions

View File

@ -1,9 +1,24 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "include/hex.h"
extern struct flags flags;
void init_flags(struct flags* flags)
{
flags->autoskip = false;
flags->binary = false;
flags->cols = 16;
flags->octets = 2;
flags->len = -1; // -1 means til EOF
flags->uppercase = false;
flags->decimaloffset = false;
}
void free_hex_chunk(hex_chunk_t* chunk)
{
free(chunk->text);
@ -22,12 +37,13 @@ void convert_text_to_hex(hex_chunk_t* chunk)
{
int i, j;
chunk->hex = malloc(sizeof(char) * (HEX_LINE_LEN + 1));
for (i = 0, j = 0; i < HEX_LINE_LEN; i += 2, j += 1) {
if (chunk->text[j] == '\0')
if (chunk->text[j] == '\0') {
snprintf(chunk->hex + i, 3, " ");
else
snprintf(chunk->hex + i, 3, "%02x", chunk->text[j]);
} else {
snprintf(chunk->hex + i, 3, flags.uppercase ? "%02X ": "%02x", chunk->text[j]);
}
}
}

View File

@ -1,4 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef HEX_H
#define HEX_H
@ -12,6 +14,21 @@ typedef struct {
char* hex;
} hex_chunk_t;
struct flags {
bool autoskip; // should we condense null lines into *
bool binary; // do binary dump instead of hex
uint cols; // choose the amount of columns to display (default 16)
uint octets; // number of octets per line (default 2)
int len; // max len to stop at
bool uppercase; // do uppercase hex chars
bool decimaloffset; // do decimal offset instead of hex
};
// TODO: implement flags and stuff
void init_flags(struct flags* flags);
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);

View File

@ -6,31 +6,6 @@
#include "include/hex.h"
#include "include/file.h"
struct flags {
bool autoskip; // should we condense null lines into *
bool binary; // do binary dump instead of hex
uint cols; // choose the amount of columns to display (default 16)
uint octets; // number of octets per line (default 2)
int len; // max len to stop at
bool uppercase; // do uppercase hex chars
bool decimaloffset; // do decimal offset instead of hex
};
// TODO: implement flags and stuff
void init_flags(struct flags* flags)
{
flags->autoskip = false;
flags->binary = false;
flags->cols = 16;
flags->octets = 2;
flags->len = -1; // -1 means til EOF
flags->uppercase = false;
flags->decimaloffset = false;
}
int get_hex_lines(int len)
{
int out;
@ -42,6 +17,8 @@ int get_hex_lines(int len)
return out;
}
struct flags flags;
int main(int argc, char* argv[])
{
char* file_content;
@ -49,6 +26,8 @@ int main(int argc, char* argv[])
bool outfile;
int hex_lines, i;
init_flags(&flags);
if (argc < 2) {
fprintf(stderr, "Usage: %s [filename]\n", argv[0]);