does some more cleanup
This commit is contained in:
parent
1eda825308
commit
6a700092f9
40
src/main.c
40
src/main.c
@ -115,6 +115,26 @@ static int get_hex_lines(int len)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int get_seek(int len_content)
|
||||||
|
{
|
||||||
|
uint seek = 0;
|
||||||
|
|
||||||
|
if (flags.seek >= 0) {
|
||||||
|
if (flags.seek > len_content)
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
seek = flags.seek;
|
||||||
|
} else {
|
||||||
|
if ((flags.seek * -1) > len_content) {
|
||||||
|
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
seek = (len_content - (flags.seek * -1));
|
||||||
|
flags.offset += seek;
|
||||||
|
}
|
||||||
|
|
||||||
|
return seek;
|
||||||
|
}
|
||||||
|
|
||||||
static void do_text_parse(hex_chunk_t** lines, bool interactive)
|
static void do_text_parse(hex_chunk_t** lines, bool interactive)
|
||||||
{
|
{
|
||||||
char* file_content = NULL;
|
char* file_content = NULL;
|
||||||
@ -129,28 +149,15 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive)
|
|||||||
read_file_to_buf(flags.files[0], &file_content);
|
read_file_to_buf(flags.files[0], &file_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags.seek >= 0) {
|
|
||||||
if (flags.seek > (int) strlen(file_content))
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
seek = flags.seek;
|
|
||||||
} else {
|
|
||||||
if ((flags.seek * -1) > (int) strlen(file_content)) {
|
|
||||||
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
seek = (strlen(file_content) - (flags.seek * -1));
|
|
||||||
flags.offset += seek;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
seek = get_seek(strlen(file_content));
|
||||||
filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len;
|
filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len;
|
||||||
|
|
||||||
flags.len = filesize;
|
flags.len = filesize;
|
||||||
|
|
||||||
hex_lines = get_hex_lines(filesize);
|
hex_lines = get_hex_lines(filesize);
|
||||||
|
|
||||||
*lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1));
|
*lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1));
|
||||||
|
|
||||||
|
|
||||||
for (i = 0; i < hex_lines; i++) {
|
for (i = 0; i < hex_lines; i++) {
|
||||||
(*lines)[i].line = i;
|
(*lines)[i].line = i;
|
||||||
add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text));
|
add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text));
|
||||||
@ -189,8 +196,6 @@ static void do_display(hex_chunk_t** lines)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char* filename;
|
char* filename;
|
||||||
|
|
||||||
|
|
||||||
bool fileout = false;
|
bool fileout = false;
|
||||||
FILE* stream = stdout;
|
FILE* stream = stdout;
|
||||||
|
|
||||||
@ -199,8 +204,6 @@ static void do_display(hex_chunk_t** lines)
|
|||||||
fileout = true;
|
fileout = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
|
||||||
|
|
||||||
if (flags.c_style) {
|
if (flags.c_style) {
|
||||||
parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename),
|
parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename),
|
||||||
&filename);
|
&filename);
|
||||||
@ -208,6 +211,7 @@ static void do_display(hex_chunk_t** lines)
|
|||||||
printf("unsigned char %s[] = {\n", filename);
|
printf("unsigned char %s[] = {\n", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i = 0;
|
||||||
while ((*lines)[i].line != -1)
|
while ((*lines)[i].line != -1)
|
||||||
display_hex_chunk(&((*lines)[i++]), stream);
|
display_hex_chunk(&((*lines)[i++]), stream);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user