Position printing and editing
This commit is contained in:
40
hex.c
40
hex.c
@@ -1,35 +1,40 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO: ASCII/Position printing
|
// TODO: Position printing
|
||||||
// TODO: Stop appending garbage at file end
|
// TODO: Stop appending garbage at file end
|
||||||
// TODO: Simplify/clean some code (useless condition/loop guard brackets)
|
// TODO: Skip repeating lines
|
||||||
|
// TODO: Coloring printable ASCII characters in output
|
||||||
|
// TODO: Stop scanning always for cmd & loc (for q and s commands)
|
||||||
|
|
||||||
#define BYTES 1024
|
#define BYTES 1024
|
||||||
#define BYTES_PER_LINE 20
|
#define BYTES_PER_LINE 20
|
||||||
|
|
||||||
void print_hex(unsigned char* buf, int byteno)
|
void print_hex(unsigned char* buf, int byteno, int pos)
|
||||||
{
|
{
|
||||||
for (int i=0; i<byteno; i++)
|
for (int i=0; i<byteno; i++)
|
||||||
{
|
{
|
||||||
|
// Print line number here
|
||||||
if (i % BYTES_PER_LINE == 0)
|
if (i % BYTES_PER_LINE == 0)
|
||||||
{
|
{
|
||||||
|
//printf("%03d: ", lineno);
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
{
|
{
|
||||||
printf(" ");
|
printf(" ");
|
||||||
for (int j=i-BYTES_PER_LINE; j<i; j++)
|
for (int j=i-BYTES_PER_LINE; j<i; j++)
|
||||||
{
|
{
|
||||||
if (isprint(buf[j]))
|
if (isprint(buf[j])) printf("%c", buf[j]);
|
||||||
{
|
else printf(".");
|
||||||
printf("%c", buf[j]);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf(".");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
puts("");
|
puts("");
|
||||||
|
// Here, we could print positions in hex but I prefer using integers.
|
||||||
|
//printf("%04X ", i);
|
||||||
|
|
||||||
|
if (pos == 0) printf("%06d: ", i);
|
||||||
|
else printf("%06d: ", pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%.2X ", buf[i]);
|
printf("%.2X ", buf[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,10 +42,7 @@ void print_hex(unsigned char* buf, int byteno)
|
|||||||
int padding = BYTES_PER_LINE - (byteno % BYTES_PER_LINE);
|
int padding = BYTES_PER_LINE - (byteno % BYTES_PER_LINE);
|
||||||
if (padding < BYTES_PER_LINE)
|
if (padding < BYTES_PER_LINE)
|
||||||
{
|
{
|
||||||
for (int i=0; i<padding; i++)
|
for (int i=0; i<padding; i++) printf(" ");
|
||||||
{
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,25 +63,26 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
{
|
{
|
||||||
printf("Usage: %s <file>\n", argv[0]);
|
printf("Usage: %s <file>\nInline commands:\n\tpX - print position X\n\teX - edit position X\n\ts - save modified buffer\n\tq - quit\n", argv[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* f = fopen(argv[1], "r");
|
FILE* f = fopen(argv[1], "r");
|
||||||
unsigned char buf[BYTES];
|
unsigned char buf[BYTES];
|
||||||
int byteno = fread(buf, 1, BYTES, f);
|
int byteno = fread(buf, 1, BYTES, f);
|
||||||
print_hex(buf, byteno);
|
print_hex(buf, byteno, 0);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
char cmd;
|
char cmd;
|
||||||
int loc = 0;
|
int loc;
|
||||||
|
|
||||||
scanf("%c%d", &cmd, &loc);
|
scanf("%c%d", &cmd, &loc);
|
||||||
switch(cmd)
|
switch(cmd)
|
||||||
{
|
{
|
||||||
case 'p':
|
case 'p':
|
||||||
case 'P':
|
case 'P':
|
||||||
print_hex(buf + loc, BYTES_PER_LINE);
|
print_hex(buf + loc, BYTES_PER_LINE, loc);
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
case 'E':
|
case 'E':
|
||||||
@@ -99,7 +102,6 @@ int main(int argc, char** argv)
|
|||||||
case 'q':
|
case 'q':
|
||||||
case 'Q':
|
case 'Q':
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user