Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hex viewer #357

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions software/userinterface/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,26 @@ void Editor :: init(Screen *scr, Keyboard *key)

void Editor :: draw(void)
{
struct Line line;
struct Line line, *line_ptr;
int width = window->get_size_x();
for(int i=0;i<height;i++) {
window->move_cursor(0, i);
line = (*text)[i + first_line];
int line_idx = i + first_line;
line = (*text)[line_idx];
if (line.buffer) {
window->output_length(line.buffer, line.length);
window->repeat(' ', width - line.length);
draw(line_idx, &line);
} else {
window->repeat(' ', width);
}
}
}

void Editor :: draw(int line_idx, Line *line)
{
window->output_length(line->buffer, line->length);
window->repeat(' ', window->get_size_x() - line->length);
}

void Editor :: deinit()
{
if(window)
Expand Down Expand Up @@ -182,15 +188,20 @@ int Editor :: handle_key(uint8_t c)
draw();
}
break;
case KEY_F1: // F1 -> page up
case KEY_F1: // page up
case KEY_PAGEUP:
first_line -= height + 1;
if (first_line < 0) {
first_line = 0;
}
draw();
break;
case KEY_F7: // F7 -> page down
case KEY_F2: // start
case KEY_HOME:
first_line = 0;
draw();
break;
case KEY_F7: // page down
case KEY_PAGEDOWN:
first_line += height - 1;
if (first_line >= linecount - height) {
Expand All @@ -200,6 +211,11 @@ int Editor :: handle_key(uint8_t c)
}
draw();
break;
case KEY_F8: // end
case KEY_END:
first_line = linecount - height;
draw();
break;
case KEY_BACK: // backspace
break;
case KEY_SPACE: // space
Expand Down
14 changes: 12 additions & 2 deletions software/userinterface/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class UserInterface;
class Editor : public UIObject
{
UserInterface *user_interface;
void line_breakdown(const char *text_buffer, int buffer_size);
void draw();
public:
int line_length;
int height;
Expand All @@ -34,8 +32,20 @@ class Editor : public UIObject
virtual void init(Screen *win, Keyboard *k);
virtual void deinit(void);

virtual void line_breakdown(const char *text_buffer, int buffer_size);
virtual void draw();
virtual void draw(int line_idx, Line *line);
virtual int poll(int);
virtual int handle_key(uint8_t);
};

class HexEditor : public Editor
{
UserInterface *user_interface;
void line_breakdown(const char *text_buffer, int buffer_size);
void draw(int line_idx, Line *line);
public:
HexEditor(UserInterface *ui, const char *text_buffer, int max_len);
};

#endif
57 changes: 57 additions & 0 deletions software/userinterface/hex_editor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "editor.h"
#include <string.h>
#include <stdio.h>

static const char hex_chars[] = "0123456789ABCDEF";

HexEditor :: HexEditor(UserInterface *ui, const char *text_buffer, int max_len) : Editor(ui, text_buffer, max_len)
{
}

void HexEditor :: line_breakdown(const char *text_buffer, int buffer_size)
{
Line current;
int pos = 0;
linecount = 0;

text->clear_list();
while (pos < buffer_size) {
current.buffer = text_buffer + pos;
current.length = (buffer_size - pos > BYTES_PER_HEX_ROW) ? BYTES_PER_HEX_ROW : buffer_size - pos;;
text->append(current);
pos += current.length;
linecount++;
}
}

inline void add_hex_byte(char *buf, int offset, uint8_t byte)
{
buf[offset] = hex_chars[(byte >> 4) & 0x0F];
buf[offset + 1] = hex_chars[byte & 0x0F];
}

inline void add_hex_word(char *buf, int offset, uint16_t word)
{
add_hex_byte(buf, offset, (word >> 8) & 0xFF);
add_hex_byte(buf, offset + 2, word & 0xFF);
}

void HexEditor :: draw(int line_idx, Line *line)
{
#define HEX_COL_START 5
#define TXT_COL_START (HEX_COL_START + (3 * BYTES_PER_HEX_ROW))

char hex_buf[CHARS_PER_HEX_ROW];
memset(hex_buf, ' ', CHARS_PER_HEX_ROW);
add_hex_word(hex_buf, 0, line_idx * BYTES_PER_HEX_ROW);

for (int i = 0; i < line->length; i++) {
uint8_t c = (uint8_t) line->buffer[i];
add_hex_byte(hex_buf, HEX_COL_START + (3 * i), c);
// represent all non-printable characters as '.' based on the character set used by firmware version 3.10j
hex_buf[TXT_COL_START + i] = (char) ((c == 0 || c == 8 || c == 10 || c == 13 || (c >=20 && c <= 31) || (c >= 144 && c <= 159)) ? '.' : c);
}

window->output_length(hex_buf, CHARS_PER_HEX_ROW);
window->repeat(' ', window->get_size_x() - CHARS_PER_HEX_ROW);
}
31 changes: 26 additions & 5 deletions software/userinterface/user_file_interaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include "home_directory.h"
#include "subsys.h"
#include "editor.h"

#define MAX_FILE_SIZE_TO_VIEW 262144

// member
int UserFileInteraction::fetch_context_items(BrowsableDirEntry *br, IndexedList<Action *> &list)
Expand All @@ -30,9 +33,10 @@ int UserFileInteraction::fetch_context_items(BrowsableDirEntry *br, IndexedList<
list.append(new Action("Enter", UserFileInteraction::S_enter, 0));
count++;
}
if ((info->size <= 262144) && (!(info->attrib & AM_DIR))) {
if ((info->size <= MAX_FILE_SIZE_TO_VIEW) && (!(info->attrib & AM_DIR))) {
list.append(new Action("View", UserFileInteraction::S_view, 0));
count++;
list.append(new Action("Hex View", UserFileInteraction::S_hex_view, 0));
count += 2;
}
if (info->is_writable()) {
list.append(new Action("Rename", UserFileInteraction::S_rename, 0));
Expand Down Expand Up @@ -131,7 +135,7 @@ int UserFileInteraction::S_delete(SubsysCommand *cmd)
return 0;
}

int UserFileInteraction::S_view(SubsysCommand *cmd)
int _view(SubsysCommand *cmd, EditorType editor_type)
{
FileManager *fm = FileManager::getFileManager();
File *f = 0;
Expand All @@ -141,14 +145,31 @@ int UserFileInteraction::S_view(SubsysCommand *cmd)
uint32_t size = f->get_size();
char *text_buf = new char[size + 1];
FRESULT fres = f->read(text_buf, size, &transferred);
printf("Res = %d. Read text buffer: %d bytes\n", fres, transferred);
printf("Res = %d. Read text buffer: %d bytes. File size: %d bytes\n", fres, transferred, size);
text_buf[transferred] = 0;
cmd->user_interface->run_editor(text_buf, transferred);
switch (editor_type) {
case HEX_EDITOR:
cmd->user_interface->run_hex_editor(text_buf, transferred);
break;
default:
cmd->user_interface->run_editor(text_buf, transferred);
break;
}
delete text_buf;
}
return 0;
}

int UserFileInteraction::S_view(SubsysCommand *cmd)
{
return _view(cmd, TEXT_EDITOR);
}

int UserFileInteraction::S_hex_view(SubsysCommand *cmd)
{
return _view(cmd, HEX_EDITOR);
}

int UserFileInteraction::S_createDir(SubsysCommand *cmd)
{
char buffer[64];
Expand Down
3 changes: 3 additions & 0 deletions software/userinterface/user_file_interaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "subsys.h"
#include "filemanager.h"

enum EditorType { TEXT_EDITOR, HEX_EDITOR};

class Path;
class Action;
class BrowsableDirEntry;
Expand All @@ -26,6 +28,7 @@ class UserFileInteraction : public SubSystem, ObjectWithMenu {
static int S_rename(SubsysCommand *cmd);
static int S_delete(SubsysCommand *cmd);
static int S_view(SubsysCommand *cmd);
static int S_hex_view(SubsysCommand *cmd);
static int S_createDir(SubsysCommand *cmd);
static int S_runApp(SubsysCommand *cmd);

Expand Down
19 changes: 14 additions & 5 deletions software/userinterface/userinterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,24 @@ void UserInterface :: hide_progress(void)
delete status_box;
}

void UserInterface :: run_editor(const char *text_buf, int max_len)
void UserInterface :: run_editor(Editor *editor)
{
Editor *edit = new Editor(this, text_buf, max_len);
edit->init(screen, keyboard);
editor->init(screen, keyboard);
int ret;
do {
ret = edit->poll(0);
ret = editor->poll(0);
} while(!ret);
edit->deinit();
editor->deinit();
}

void UserInterface :: run_editor(const char *text_buf, int max_len)
{
run_editor(new Editor(this, text_buf, max_len));
}

void UserInterface :: run_hex_editor(const char *text_buf, int max_len)
{
run_editor(new HexEditor(this, text_buf, max_len));
}

int UserInterface :: enterSelection()
Expand Down
8 changes: 8 additions & 0 deletions software/userinterface/userinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#define CFG_USERIF_ULTICOPY_NAME 0x0B
#define CFG_USERIF_FILENAME_OVERFLOW_SQUEEZE 0x0C

#define BYTES_PER_HEX_ROW 8
#define CHARS_PER_HEX_ROW 37

class Editor;
class HexEditor;
class UserInterface : public ConfigurableObject, public HostClient
{
private:
Expand All @@ -45,6 +50,8 @@ class UserInterface : public ConfigurableObject, public HostClient
void set_screen_title(void);
bool pollFocussed(void);
bool buttonDownFor(uint32_t ms);
void run_editor(Editor *);

UIStatusBox *status_box;
public:
int color_border, color_bg, color_fg, color_sel, color_sel_bg, config_save, filename_overflow_squeeze;
Expand Down Expand Up @@ -83,6 +90,7 @@ class UserInterface : public ConfigurableObject, public HostClient
int getPreferredType(void);

void run_editor(const char *, int);
void run_hex_editor(const char *, int);
void swapDisk(void);

UIObject *get_root_object(void) { return ui_objects[0]; }
Expand Down
1 change: 1 addition & 0 deletions target/u64/nios2/ultimate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ SRCS_CC = u2p_init.cc \
ui_elements.cc \
user_file_interaction.cc \
editor.cc \
hex_editor.cc \
tree_browser.cc \
tree_browser_state.cc \
config_menu.cc \
Expand Down