Skip to content

Commit bb0e3dc

Browse files
committed
Check for proper ascii characters and minor argument improvements
Signed-off-by: Manuel Bertele <manuel.bertele2003@gmail.com>
1 parent 4cb3bc6 commit bb0e3dc

6 files changed

Lines changed: 42 additions & 33 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# hexify
22
is a program for displaying files in hex and also giving an ascii letter to each hex number in a nice terminal gui.
33
## Description
4-
![img](img/20210331.175128.jpg)
4+
![img](img/20210420.173900.jpg)
55

66
## Usage
77
hexify currently only has 2 ways to open a file.
88
- hexify \<file-name>
99
- hexify -f \<file-name>
1010

1111
### Other flags
12-
- hexify -r \<float> `Bigger = more ascii characters`
12+
- hexify -r \<float> `Bigger = more ascii characters (0-1)`
1313
- hexify -h
1414
- hexify -v
1515

img/20210331.175128.jpg

-883 KB
Binary file not shown.

img/20210420.173900.jpg

876 KB
Loading

log.log

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
==678197== Memcheck, a memory error detector
2+
==678197== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
3+
==678197== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
4+
==678197== Command: ./output/hexify src/hexify.c
5+
==678197== Parent PID: 287716
6+
==678197==
7+
==678197==
8+
==678197== HEAP SUMMARY:
9+
==678197== in use at exit: 1,070,567 bytes in 375 blocks
10+
==678197== total heap usage: 391 allocs, 16 frees, 1,088,628 bytes allocated
11+
==678197==
12+
==678197== LEAK SUMMARY:
13+
==678197== definitely lost: 0 bytes in 0 blocks
14+
==678197== indirectly lost: 0 bytes in 0 blocks
15+
==678197== possibly lost: 0 bytes in 0 blocks
16+
==678197== still reachable: 1,070,567 bytes in 375 blocks
17+
==678197== suppressed: 0 bytes in 0 blocks
18+
==678197== Reachable blocks (those to which a pointer was found) are not shown.
19+
==678197== To see them, rerun with: --leak-check=full --show-leak-kinds=all
20+
==678197==
21+
==678197== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

src/draw.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static WINDOW *divider;
1111
static WINDOW *hex;
1212
static WINDOW *lines;
1313

14-
/* Characters_drawn is a variable that updated every time gui_draw_hex is ran
14+
/* Characters_drawn is a variable that updated every time gui_draw_hex was executed
1515
* it contains the amount of characters that were drawn 1 hex number is 1 character drawn */
1616
static size_t characters_drawn;
1717
/* Contains the amount of hex numbers that are in one row */
@@ -99,31 +99,26 @@ void gui_draw_hex(byte *file, size_t file_current_offset, size_t file_size) {
9999
wmove(text, 0, 0);
100100
/* Clear hex from previous text */
101101
wclrtobot(hex);
102+
wclrtobot(text);
103+
wclrtobot(lines);
102104
size_t i; /* Jumps from line to line */
103105
for (i = 0; i < file_draw_size; i += hex_per_line, ++line) {
104106
/* Prints the characters in and byte numbers */
105107
for (size_t j = 0; (j < hex_per_line) && (i + j < file_draw_size); ++j) {
106-
/* Display the hex numbers */
108+
/* Display the hex numbers and 0 if number is negativ*/
107109
wprintw(hex, "%02x", file[i+j] < 0 ? 0 : file[i+j]);
108-
if (j < hex_per_line-1)
110+
if (j < hex_per_line-1) {
109111
wprintw(hex, " ");
110-
else
112+
} else {
111113
wprintw(hex, "\n");
114+
}
112115
++characters_drawn;
113-
/* Determine if an tab or line break character was encountered */
114-
switch (file[i+j]) {
115-
case '\n':
116-
wprintw(text, "\\n");
117-
continue;
118-
case '\t':
119-
wprintw(text, "\\t");
120-
continue;
121-
case '\r':
122-
wprintw(text, "\\r");
123-
continue;
116+
/* If character is allowed or just an irrelevant number */
117+
if (file[i+j] >= '!' && file[i+j] <= '~') {
118+
wprintw(text, "%c", file[i+j]);
119+
} else {
120+
wprintw(text, ".");
124121
}
125-
/* Print the ascii character */
126-
wprintw(text, "%c", file[i+j]);
127122
}
128123
/* Print a newline at the text window */
129124
wprintw(text, "\n");

src/hexify.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ void version();
1414

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

17+
char *file_name;
18+
float ratio = 0.24;
19+
1720
if (argc < 2) {
1821
help();
22+
} else if (argc == 2) {
23+
file_name = strdup(argv[1]);
1924
}
2025

21-
char *file_name = strdup(argv[1]);
22-
float ratio = 0.3;
23-
2426
static struct option long_options[] = {
2527
{"file", required_argument, NULL, 'f'},
2628
{"ratio", required_argument, NULL, 'r'},
@@ -31,17 +33,14 @@ int main(int argc, char *argv[]) {
3133

3234
int opts;
3335
while ((opts = getopt_long(argc, argv, "f:r:hv", long_options, NULL)) != -1) {
34-
3536
switch (opts) {
36-
3737
case 'f':
38-
xfree(file_name);
3938
file_name = strdup(optarg);
4039
break;
4140
case 'r':
4241
ratio = strtof(optarg, NULL);
43-
if (ratio <= 0 || ratio >= 1) {
44-
die(RATERR, "ratio (%s) must be betweem 0 and 1", optarg);
42+
if (ratio <= 0.0f || ratio >= 1.0f) {
43+
die(RATERR, "ratio (%s) must be between 0 and 1", optarg);
4544
}
4645
break;
4746
case 'h':
@@ -53,7 +52,6 @@ int main(int argc, char *argv[]) {
5352
default:
5453
die(ARGERR, NULL);
5554
break;
56-
5755
}
5856

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

8078
int inp;
8179
while ((inp = getchar()) != 'q') {
82-
8380
switch (inp) {
84-
8581
case -1:
8682
/* trigger a refresh when terminal got resized */
8783
gui_init(ratio);
8884
gui_draw_hex(file_content, file_current_offset, file_size);
8985
gui_draw_title("Open file: %s", file_name);
9086
break;
91-
9287
case 27:
9388
getchar();
9489
inp = getchar();
9590
switch (inp) {
96-
9791
case 'A':
9892
/* Arrow up */
9993
draw_cursor_up(&file_current_offset, file_content, file_size);
@@ -110,7 +104,6 @@ int main(int argc, char *argv[]) {
110104
/* Arrow left */
111105
draw_cursor_left(&file_current_offset, file_content, file_size);
112106
break;
113-
114107
}
115108
}
116109
}

0 commit comments

Comments
 (0)