Skip to content

Commit

Permalink
Check for proper ascii characters and minor argument improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Bertele <[email protected]>
  • Loading branch information
DevManu-de committed Apr 20, 2021
1 parent 4cb3bc6 commit bb0e3dc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# hexify
is a program for displaying files in hex and also giving an ascii letter to each hex number in a nice terminal gui.
## Description
![img](img/20210331.175128.jpg)
![img](img/20210420.173900.jpg)

## Usage
hexify currently only has 2 ways to open a file.
- hexify \<file-name>
- hexify -f \<file-name>

### Other flags
- hexify -r \<float> `Bigger = more ascii characters`
- hexify -r \<float> `Bigger = more ascii characters (0-1)`
- hexify -h
- hexify -v

Expand Down
Binary file removed img/20210331.175128.jpg
Binary file not shown.
Binary file added img/20210420.173900.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions log.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
==678197== Memcheck, a memory error detector
==678197== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==678197== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==678197== Command: ./output/hexify src/hexify.c
==678197== Parent PID: 287716
==678197==
==678197==
==678197== HEAP SUMMARY:
==678197== in use at exit: 1,070,567 bytes in 375 blocks
==678197== total heap usage: 391 allocs, 16 frees, 1,088,628 bytes allocated
==678197==
==678197== LEAK SUMMARY:
==678197== definitely lost: 0 bytes in 0 blocks
==678197== indirectly lost: 0 bytes in 0 blocks
==678197== possibly lost: 0 bytes in 0 blocks
==678197== still reachable: 1,070,567 bytes in 375 blocks
==678197== suppressed: 0 bytes in 0 blocks
==678197== Reachable blocks (those to which a pointer was found) are not shown.
==678197== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==678197==
==678197== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
29 changes: 12 additions & 17 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static WINDOW *divider;
static WINDOW *hex;
static WINDOW *lines;

/* Characters_drawn is a variable that updated every time gui_draw_hex is ran
/* Characters_drawn is a variable that updated every time gui_draw_hex was executed
* it contains the amount of characters that were drawn 1 hex number is 1 character drawn */
static size_t characters_drawn;
/* Contains the amount of hex numbers that are in one row */
Expand Down Expand Up @@ -99,31 +99,26 @@ void gui_draw_hex(byte *file, size_t file_current_offset, size_t file_size) {
wmove(text, 0, 0);
/* Clear hex from previous text */
wclrtobot(hex);
wclrtobot(text);
wclrtobot(lines);
size_t i; /* Jumps from line to line */
for (i = 0; i < file_draw_size; i += hex_per_line, ++line) {
/* Prints the characters in and byte numbers */
for (size_t j = 0; (j < hex_per_line) && (i + j < file_draw_size); ++j) {
/* Display the hex numbers */
/* Display the hex numbers and 0 if number is negativ*/
wprintw(hex, "%02x", file[i+j] < 0 ? 0 : file[i+j]);
if (j < hex_per_line-1)
if (j < hex_per_line-1) {
wprintw(hex, " ");
else
} else {
wprintw(hex, "\n");
}
++characters_drawn;
/* Determine if an tab or line break character was encountered */
switch (file[i+j]) {
case '\n':
wprintw(text, "\\n");
continue;
case '\t':
wprintw(text, "\\t");
continue;
case '\r':
wprintw(text, "\\r");
continue;
/* If character is allowed or just an irrelevant number */
if (file[i+j] >= '!' && file[i+j] <= '~') {
wprintw(text, "%c", file[i+j]);
} else {
wprintw(text, ".");
}
/* Print the ascii character */
wprintw(text, "%c", file[i+j]);
}
/* Print a newline at the text window */
wprintw(text, "\n");
Expand Down
21 changes: 7 additions & 14 deletions src/hexify.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ void version();

int main(int argc, char *argv[]) {

char *file_name;
float ratio = 0.24;

if (argc < 2) {
help();
} else if (argc == 2) {
file_name = strdup(argv[1]);
}

char *file_name = strdup(argv[1]);
float ratio = 0.3;

static struct option long_options[] = {
{"file", required_argument, NULL, 'f'},
{"ratio", required_argument, NULL, 'r'},
Expand All @@ -31,17 +33,14 @@ int main(int argc, char *argv[]) {

int opts;
while ((opts = getopt_long(argc, argv, "f:r:hv", long_options, NULL)) != -1) {

switch (opts) {

case 'f':
xfree(file_name);
file_name = strdup(optarg);
break;
case 'r':
ratio = strtof(optarg, NULL);
if (ratio <= 0 || ratio >= 1) {
die(RATERR, "ratio (%s) must be betweem 0 and 1", optarg);
if (ratio <= 0.0f || ratio >= 1.0f) {
die(RATERR, "ratio (%s) must be between 0 and 1", optarg);
}
break;
case 'h':
Expand All @@ -53,7 +52,6 @@ int main(int argc, char *argv[]) {
default:
die(ARGERR, NULL);
break;

}

}
Expand All @@ -79,21 +77,17 @@ int main(int argc, char *argv[]) {

int inp;
while ((inp = getchar()) != 'q') {

switch (inp) {

case -1:
/* trigger a refresh when terminal got resized */
gui_init(ratio);
gui_draw_hex(file_content, file_current_offset, file_size);
gui_draw_title("Open file: %s", file_name);
break;

case 27:
getchar();
inp = getchar();
switch (inp) {

case 'A':
/* Arrow up */
draw_cursor_up(&file_current_offset, file_content, file_size);
Expand All @@ -110,7 +104,6 @@ int main(int argc, char *argv[]) {
/* Arrow left */
draw_cursor_left(&file_current_offset, file_content, file_size);
break;

}
}
}
Expand Down

0 comments on commit bb0e3dc

Please sign in to comment.