[go: nahoru, domu]

Skip to content

Commit

Permalink
--skip-empty option; 'h' hexdump output format
Browse files Browse the repository at this point in the history
  • Loading branch information
jimm committed Sep 1, 2021
1 parent fd12354 commit 3d22cda
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ Finally, there is a table containing the slot's settings.

That's it.

# To Do
# Random Notes

Comment brightness (adjusted from with the full-screen slot comment display)
is a global setting. It is not stored with the slot or the set list, so
Kronut can't save it or let you adjust it.

- Find out where slot brightness is stored and expose that value. Or is it
in the set list?
# To Do

- Find out where slot MIDI track is. It's only for songs.

Expand Down
60 changes: 57 additions & 3 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
#include "set_list_file.h"

Editor::Editor(int format)
: _file_format(format)
{
if (format == EDITOR_FORMAT_ORG_MODE)
switch (_file_format) {
case EDITOR_FORMAT_ORG_MODE:
_file = new OrgModeSetListFile();
else
break;
case EDITOR_FORMAT_MARKDOWN:
_file = new MarkdownSetListFile();
break;
case EDITOR_FORMAT_HEXDUMP:
_file = nullptr;
break;
}
}

Editor::~Editor() {
if (_file != nullptr) delete _file;
}

int Editor::load_set_list_from_file(const char * const path) {
Expand Down Expand Up @@ -139,7 +151,10 @@ void Editor::load_set_list_slot_settings_from_file(SlotWrapper &sw) {
}
}

int Editor::save_set_list_to_file(const char * const path) {
int Editor::save_set_list_to_file(const char * const path, bool skip_empty_slots) {
if (_file_format == EDITOR_FORMAT_HEXDUMP)
return hexdump(path);

char buf[BUFSIZ];
SetListWrapper slw(_set_list);

Expand All @@ -156,6 +171,9 @@ int Editor::save_set_list_to_file(const char * const path) {
Slot &slot = _set_list.slots[i];
SlotWrapper sw(slot);

if (skip_empty_slots && sw.is_empty())
continue;

_file->header(2, sw.name());
if (sw.comments().size() > 0)
_file->text(trimmed(sw.comments()));
Expand Down Expand Up @@ -196,6 +214,42 @@ void Editor::save_set_list_slot_settings_to_file(SlotWrapper &sw) {
_file->table_end();
}

int Editor::hexdump(const char * const path) {
ofstream out;

out.open(path, std::ofstream::out);
if (out.fail()) {
cerr << "error opening " << path << " for output" << endl;
exit(1);
}

byte *bytes = (byte *)&_set_list;
size_t size = sizeof(_set_list);
size_t offset = 0;

// TODO this duplicates dump_hex code in utils.cpp
while (size > 0) {
int chunk_len = 8 > size ? size : 8;
out << setw(8) << setfill('0') << hex << offset << ' ';
out << " ";
for (int i = 0; i < chunk_len; ++i)
out << ' ' << setw(2) << setfill('0') << hex << (int)bytes[i];
for (int i = chunk_len; i < 8; ++i)
out << " ";
out << ' ';
for (int i = 0; i < chunk_len; ++i)
out << (char)((bytes[i] >= 32 && bytes[i] < 127) ? bytes[i] : '.');
out << endl;
bytes += chunk_len;
size -= chunk_len;
offset += chunk_len;
}

out.close();

return 0;
}

string Editor::trimmed(string s) {
char buf[BUFSIZ], *p;

Expand Down
7 changes: 6 additions & 1 deletion src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#define EDITOR_FORMAT_ORG_MODE 0
#define EDITOR_FORMAT_MARKDOWN 1
#define EDITOR_FORMAT_HEXDUMP 2

class SetListWrapper;
class SlotWrapper;
Expand All @@ -18,13 +19,15 @@ class SetListFile;
class Editor {
public:
Editor(int format);
~Editor();

int load_set_list_from_file(const char * const path);
int save_set_list_to_file(const char * const path);
int save_set_list_to_file(const char * const path, bool skip_empty_slots);

SetList &set_list() { return _set_list; }

protected:
int _file_format;
int _set_list_number;
SetList _set_list;
SetListFile *_file;
Expand All @@ -35,6 +38,8 @@ class Editor {
void load_set_list_settings_from_file(SetListWrapper &slw);
void load_set_list_slot_settings_from_file(SlotWrapper &sw);

int hexdump(const char * const path);

string trimmed(string s);
void init_set_list();
};
Expand Down
29 changes: 25 additions & 4 deletions src/kronut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct opts {
int input_num;
int output_num;
int format;
bool skip_empty_slots;
bool debug;
} opts;

Expand Down Expand Up @@ -61,9 +62,10 @@ void usage(const char *prog_name) {
<< " [-c N] [-i N] [-o N] [-f FORMAT] [-d] [-h] COMMAND [args]" << endl
<< endl
<< " -c, --channel N Kronos general MIDI channel (1-16, default 1)" << endl
<< " -f, --format FMT Format: \"o\" (Org Mode, default) or \"m\" (Markdown)" << endl
<< " -f, --format FMT Format: \"o\" (Org Mode, default), \"m\" (Markdown), \"h\" (hexdump)" << endl
<< " -i, --input N Input number (default: attempts to find it automatically)" << endl
<< " -o, --output N Output number (default: attempts to find it automatically)" << endl
<< " -s, --skip-empty Skips empty slots when saving" << endl
<< endl
<< "Commands:" << endl
<< endl
Expand All @@ -88,6 +90,7 @@ void parse_command_line(int argc, char * const *argv, struct opts &opts) {
{"format", required_argument, 0, 'f'},
{"input", required_argument, 0, 'i'},
{"output", required_argument, 0, 'o'},
{"skip-empty", no_argument, 0, 's'},
{"debug", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
Expand All @@ -96,7 +99,8 @@ void parse_command_line(int argc, char * const *argv, struct opts &opts) {
opts.input_num = opts.output_num = -1;
opts.format = EDITOR_FORMAT_ORG_MODE;
opts.debug = false;
while ((ch = getopt_long(argc, argv, "c:f:i:o:dh", longopts, 0)) != -1) {
opts.skip_empty_slots = false;
while ((ch = getopt_long(argc, argv, "c:f:i:o:sdh", longopts, 0)) != -1) {
switch (ch) {
case 'c':
opts.channel = atoi(optarg) - 1; // 0-15
Expand All @@ -107,14 +111,31 @@ void parse_command_line(int argc, char * const *argv, struct opts &opts) {
}
break;
case 'f':
opts.format = optarg[0] == 'm' ? EDITOR_FORMAT_MARKDOWN : EDITOR_FORMAT_ORG_MODE;
switch (optarg[0]) {
case 'm':
opts.format = EDITOR_FORMAT_MARKDOWN;
break;
case 'o':
opts.format = EDITOR_FORMAT_ORG_MODE;
break;
case 'h':
opts.format = EDITOR_FORMAT_HEXDUMP;
break;
default:
cerr << "error: format must be 'm', 'o', or 'h'" << endl;
usage(prog_name);
exit(1);
}
break;
case 'i':
opts.input_num = atoi(optarg);
break;
case 'o':
opts.output_num = atoi(optarg);
break;
case 's':
opts.skip_empty_slots = true;
break;
case 'd':
opts.debug = true;
break;
Expand Down Expand Up @@ -207,7 +228,7 @@ int main(int argc, char * const *argv) {
break;
case 's':
kronos.read_set_list(set_list_num, editor.set_list());
editor.save_set_list_to_file(path);
editor.save_set_list_to_file(path, opts.skip_empty_slots);
break;
default:
usage(prog_name);
Expand Down
12 changes: 12 additions & 0 deletions src/slot_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ const char * const SLOT_FONT_SHORT_NAMES[5] = {
"S", "XS", "M", "L", "XL"
};

bool SlotWrapper::is_empty() {
return name() == ""
&& performance_type() == pt_program
&& performance_bank() == 0
&& performance_index() == 0
&& hold_time() == 6
&& volume() == 127
&& keyboard_track() == 0
&& comments() == "";

}

string SlotWrapper::name() {
return chars_to_string(slot.name, SLOT_NAME_LEN);
}
Expand Down
2 changes: 2 additions & 0 deletions src/slot_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class SlotWrapper : public StructWrapper {

SlotWrapper(Slot &s) : slot(s) {}

bool is_empty();

string name();
int set_name(string str);

Expand Down
5 changes: 3 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ void dump_hex(byte *bytes, size_t size, const char * const msg) {
size_t offset = 0;
while (size > 0) {
int chunk_len = 8 > size ? size : 8;
cout << setw(8) << setfill('0') << offset;
cout << setw(8) << setfill('0') << hex << offset << ' ';
cout << " ";
for (int i = 0; i < chunk_len; ++i)
cout << setw(2) << setfill('0') << bytes[i];
cout << ' ' << setw(2) << setfill('0') << hex << (int)bytes[i];
for (int i = chunk_len; i < 8; ++i)
cout << " ";
cout << ' ';
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ char * c_str(char *p, size_t len);

void dump_hex(byte *bytes, size_t size, const char * const msg);

#endif /* UTILS_H */
#endif /* UTILS_H */

0 comments on commit 3d22cda

Please sign in to comment.