[go: nahoru, domu]

Skip to content

Commit

Permalink
use io streams for everything
Browse files Browse the repository at this point in the history
  • Loading branch information
jimm committed Jul 26, 2021
1 parent 730c553 commit b24b048
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 84 deletions.
39 changes: 22 additions & 17 deletions src/editor.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <iostream>
#include <iomanip>
#include <strstream>
#include <string.h>
#include <ctype.h>
#include <libgen.h>
Expand All @@ -23,9 +26,8 @@ int Editor::load_set_list_from_file(char *path) {
string name;
string comments;

if (_file->open(path, "r") == nullptr) {
fprintf(stderr, "error: can't open \"%s\" for reading: %s\n",
path, strerror(errno));
if (!_file->open(path, "r")) {
cerr << "error: can't open \"" << path << "\" for reading: " << strerror(errno) << endl;
return errno;
}

Expand All @@ -37,7 +39,7 @@ int Editor::load_set_list_from_file(char *path) {
// Set List name
name = _file->header_text(1);
if (slw.set_name(name) != 0)
fprintf(stderr, "warning: set list name \"%s\" is too long and will be truncated", name.c_str());
cerr << "warning: set list name \"" << name << "\" is too long and will be truncated" << endl;

_file->skip_blank_lines();
load_set_list_settings_from_file(slw);
Expand All @@ -54,13 +56,17 @@ int Editor::load_set_list_from_file(char *path) {
Slot &slot = _set_list.slots[slot_number];
SlotWrapper sw(slot);
if (sw.set_name(name) != 0)
fprintf(stderr, "warning: slot %03d name \"%s\" is too long and will be truncated",
slot_number, name.c_str());
cerr << "warning: slot " << setw(3) << setfill('0') << slot_number
<< " named \"" << name << "\" is too long and will be truncated"
<< endl;

string trimmed_comments = trimmed(comments);
if (sw.set_comments(trimmed_comments) != 0)
fprintf(stderr, "warning: slot %03d comments \"%s...\" are too long and will be truncated",
slot_number, comments.substr(0, 20).c_str());
cerr << "warning: slot " << setw(3) << setfill('0') << slot_number
<< " named \"" << name
<< "\" comments \""
<< comments.substr(0, 20) << "...\" are too long and will be truncated"
<< endl;

load_set_list_slot_settings_from_file(sw);

Expand Down Expand Up @@ -141,9 +147,8 @@ int Editor::save_set_list_to_file(char *path) {
char buf[BUFSIZ];
SetListWrapper slw(_set_list);

if (_file->open(path, "w") == nullptr) {
fprintf(stderr, "error: can't open \"%s\" for writing: %s\n",
path, strerror(errno));
if (!_file->open(path, "w")) {
cerr << "error: can't open \"" << path << "\" for writing: " << strerror(errno) << endl;
return errno;
}

Expand All @@ -168,16 +173,16 @@ int Editor::save_set_list_to_file(char *path) {
}

void Editor::save_set_list_settings_to_file(SetListWrapper &slw) {
char buf[BUFSIZ];
ostrstream ostr;

_file->table_headers("Setting", "Value");
_file->table_row("Slots/Page", slw.slots_per_page());
_file->table_row("EQ Bypass", _set_list.eq_bypass);
sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d", _set_list.band_levels[0],
_set_list.band_levels[1], _set_list.band_levels[2], _set_list.band_levels[3],
_set_list.band_levels[4], _set_list.band_levels[5], _set_list.band_levels[6],
_set_list.band_levels[7], _set_list.band_levels[8]);
_file->table_row("Band Levels", buf);
for (int i = 0 ; i < 9; ++i) {
if (i > 0) ostr << ',';
ostr << (int)_set_list.band_levels[i];
}
_file->table_row("Band Levels", ostr.str());
_file->table_row("Surface Mode", _set_list.control_surface_mode);
_file->table_row("Surface Asgn", _set_list.control_surface_assign_from);
_file->table_end();
Expand Down
12 changes: 6 additions & 6 deletions src/kronos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ Kronos::Kronos(byte chan, int input_device_num, int output_device_num)
if (input_device_num >= 0) { // negative means we're testing
PmError err = Pm_OpenInput(&input, input_device_num, 0, MIDI_BUFSIZ, 0, 0);
if (err != 0) {
fprintf(stderr, "error opening PortMidi input stream: %s\n", Pm_GetErrorText(err));
cerr << "error opening PortMidi input stream: " << Pm_GetErrorText(err) << endl;
exit(1);
}
}

if (output_device_num >= 0) {
PmError err = Pm_OpenOutput(&output, output_device_num, 0, MIDI_BUFSIZ, 0, 0, 0);
if (err != 0) {
fprintf(stderr, "error opening PortMidi output stream: %s\n", Pm_GetErrorText(err));
cerr << "error opening PortMidi output stream: " << Pm_GetErrorText(err) << endl;
exit(1);
}
}
Expand All @@ -85,7 +85,7 @@ Kronos::~Kronos() {
void Kronos::send_sysex(const byte * const sysex_bytes) {
PmError err = Pm_WriteSysEx(output, 0, (unsigned char *)sysex_bytes);
if (err != 0) {
fprintf(stderr, "error writing sysex: %s\n", Pm_GetErrorText(err));
cerr << "error writing sysex: " << Pm_GetErrorText(err) << endl;
exit(1);
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ void Kronos::read_sysex() {
}
}
else if ((time(0) - start) >= READ_SYSEX_TIMEOUT_SECS) {
fprintf(stderr, "timeout waiting for sysex\n");
cerr << "timeout waiting for sysex" << endl;
exit(1);
}
}
Expand Down Expand Up @@ -149,15 +149,15 @@ void Kronos::get(const byte * const request_sysex, const char * const func_name)
send_sysex(request_sysex);
read_sysex();
if (error_reply_seen()) {
fprintf(stderr, "Kronos::%s received an error response: %s\n", func_name, error_reply_message());
cerr << "Kronos::" << func_name << " received an error response: " << error_reply_message() << endl;
exit(1);
}
}

void Kronos::send_channel_message(byte status, byte data1, byte data2) {
PmError err = Pm_WriteShort(output, 0, Pm_Message(status, data1, data2));
if (err != 0) {
fprintf(stderr, "error writing channel message: %s\n", Pm_GetErrorText(err));
cerr << "error writing channel message: " << Pm_GetErrorText(err) << endl;
exit(1);
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/kronut.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <iomanip>
#include <getopt.h>
#include <ctype.h>
#include <iostream>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -9,8 +10,6 @@
#include "kronos.h"
#include "editor.h"

#define SET_LIST_UNDEFINED (-1)

using namespace std;

typedef unsigned char byte;
Expand Down Expand Up @@ -47,7 +46,7 @@ void close_midi() {
void init_midi() {
PmError err = Pm_Initialize();
if (err != 0) {
fprintf(stderr, "error initializing PortMidi: %s\n", Pm_GetErrorText(err));
cerr << "error initializing PortMidi: " << Pm_GetErrorText(err) << endl;
exit(1);
}

Expand Down Expand Up @@ -96,7 +95,7 @@ void parse_command_line(int argc, char * const *argv, struct opts &opts) {
case 'c':
opts.channel = atoi(optarg) - 1; // 0-15
if (opts.channel < 0 || opts.channel > 15) {
fprintf(stderr, "error: channel must be 1-16\n");
cerr << "error: channel must be 1-16" << endl;
usage(prog_name);
exit(1);
}
Expand All @@ -118,13 +117,13 @@ void parse_command_line(int argc, char * const *argv, struct opts &opts) {
}

void list_devices(const char * const type_name, bool show_inputs) {
printf("%s:\n", type_name);
cout << type_name << ':' << endl;
for (int i = 0; i < Pm_CountDevices(); ++i) {
const PmDeviceInfo *info = Pm_GetDeviceInfo(i);
if (show_inputs ? (info->input == 1) : (info->output == 1)) {
const char *name = info->name;
const char *q = (name[0] == ' ' || name[strlen(name)-1] == ' ') ? "\"" : "";
printf(" %2d: %s%s%s\n", i, q, name, q);
cout << " " << setw(2) << i << ": " << q << name << q << endl;
}
}
}
Expand Down Expand Up @@ -160,12 +159,12 @@ int main(int argc, char * const *argv) {
if (opts.input_num == -1)
opts.input_num = find_kronos_input_num();
if (opts.input_num == -1)
fprintf(stderr, "error: can't find Kronos input port number\n");
cerr << "error: can't find Kronos input port number" << endl;

if (opts.output_num == -1)
opts.output_num = find_kronos_output_num();
if (opts.output_num == -1)
fprintf(stderr, "error: can't find Kronos output port number\n");
cerr << "error: can't find Kronos output port number" << endl;

if (opts.input_num == -1 || opts.output_num == -1) {
usage(prog_name);
Expand Down
67 changes: 35 additions & 32 deletions src/set_list_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,37 @@ SetListFile::SetListFile(char header_char, char table_sep_sep_char)
{
}

FILE *SetListFile::open(const char * const path, const char *mode) {
bool SetListFile::open(const char * const path, const char *mode) {
errno = 0;
_fp = fopen(path, mode);
return _fp;
if (mode[0] == 'r') {
_in.open(path, std::ofstream::in);
return !_in.fail();
}
else {
_out.open(path, std::ofstream::out);
return !_out.fail();
}
}

void SetListFile::close() {
if (_fp != nullptr)
fclose(_fp);
if (_in.is_open())
_in.close();
if (_out.is_open())
_out.close();
}

// ================ writing ================

void SetListFile::header(int level, char *text) {
for (int i = 0; i < level; ++i)
fputc(_header_char, _fp);
fprintf(_fp, " %s\n\n", text);
_out << _header_char;
_out << ' ' << text << endl << endl;
}

void SetListFile::header(int level, string str) {
for (int i = 0; i < level; ++i)
fputc(_header_char, _fp);
fprintf(_fp, " %s\n\n", str.c_str());
_out << _header_char;
_out << str << endl << endl;
}


Expand All @@ -47,31 +55,31 @@ void SetListFile::text(string str) {
}

void SetListFile::text(KString &kstr) {
fprintf(_fp, "%s\n\n", kstr.str());
_out << kstr.str() << endl << endl;
}

void SetListFile::puts(char *text) {
fputs(text, _fp);
_out << text;
if (text[strlen(text) - 1] != '\n')
fputc('\n', _fp);
_out << endl;
}

void SetListFile::puts(string str) {
fputs(str.c_str(), _fp);
_out << str;
if (str[str.size() - 1] != '\n')
fputc('\n', _fp);
_out << endl;
}

// ================ writing tables ================

void SetListFile::table_separator() {
fputc('|', _fp);
_out << '|';
for (int i = 0; i < COL1_DATA_WIDTH + 2; ++i)
fputc('-', _fp);
fputc(_table_sep_sep_char, _fp);
_out << '-';
_out << _table_sep_sep_char;
for (int i = 0; i < COL2_DATA_WIDTH + 2; ++i)
fputc('-', _fp);
fprintf(_fp, "|\n");
_out << '-';
_out << '|' << endl;
}

void SetListFile::table_headers(const char * const h1, const char * const h2) {
Expand All @@ -81,14 +89,15 @@ void SetListFile::table_headers(const char * const h1, const char * const h2) {
}

void SetListFile::table_row(const char * const col1, const char * const col2) {
fprintf(_fp, "| %*s | %*s |\n", COL1_DATA_WIDTH, col1, COL2_DATA_WIDTH, col2);
_out << "| " << setw(COL1_DATA_WIDTH) << col1
<< " | " << setw(COL2_DATA_WIDTH) << col2
<< " |" << endl;
}

void SetListFile::table_row(const char * const col1, int value) {
char buf[16];

sprintf(buf, "%d", value);
table_row(col1, buf);
_out << "| " << setw(COL1_DATA_WIDTH) << col1
<< " | " << value
<< " |" << endl;
}

void SetListFile::table_end() {
Expand All @@ -102,14 +111,8 @@ void SetListFile::table_end() {
bool SetListFile::getline() {
char buf[BUFSIZ];

if (fgets(buf, BUFSIZ, _fp) == 0)
return false;
int len = strlen(buf);
if (buf[len-1] == '\n')
buf[len-1] = '\0';

_line = buf;
return true;
_in.getline(buf, BUFSIZ);
return !_in.eof();
}

void SetListFile::skip_blank_lines() {
Expand Down
7 changes: 4 additions & 3 deletions src/set_list_file.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef EDIT_FILE_H
#define EDIT_FILE_H

#include <stdio.h>
#include <fstream>
#include <string>
#include "kstring.h"

Expand All @@ -11,7 +11,7 @@ class SetListFile {
public:
SetListFile(char header_char, char table_sep_sep_char);

FILE *open(const char * const path, const char *mode);
bool open(const char * const path, const char *mode);
void close();

// writing
Expand Down Expand Up @@ -49,7 +49,8 @@ class SetListFile {
string table_col2();

protected:
FILE *_fp;
ifstream _in;
ofstream _out;
string _line;
char _header_char;
char _table_sep_sep_char;
Expand Down
3 changes: 2 additions & 1 deletion src/slot_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>
#include <strstream>
#include <iomanip>
#include "slot_wrapper.h"
Expand Down Expand Up @@ -55,7 +56,7 @@ void SlotWrapper::set_performance_type(SlotPerformanceType val) {
const char * const SlotWrapper::performance_type_name() {
int index = (int)performance_type();
if (index < 0 || index >= (sizeof(SLOT_PERF_TYPE_NAMES) / sizeof(const char * const))) {
fprintf(stderr, "error: illegal performance type value %d\n", index);
cerr << "error: illegal performance type value " << index << endl;
exit(1);
}
return SLOT_PERF_TYPE_NAMES[(int)performance_type()];
Expand Down
Loading

0 comments on commit b24b048

Please sign in to comment.