Skip to content

Commit

Permalink
More work towards eliminating printf in favor of fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
indigodarkwolf committed Sep 23, 2024
1 parent 2e5bf2b commit 548447a
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 263 deletions.
52 changes: 0 additions & 52 deletions src/boxmon/boxmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,54 +107,6 @@ bool boxmon_do_console_command(const std::string &line)
return true;
}

void boxmon_console_print(char const *format, ...)
{
if (Console_suppress_output) {
return;
}

va_list arglist;
va_start(arglist, format);

char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, arglist);
Console_history.push_back({ boxmon::message_severity::output, buffer });

va_end(arglist);
}

void boxmon_warning_print(char const *format, ...)
{
if (Console_suppress_warnings) {
return;
}

va_list arglist;
va_start(arglist, format);

char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, arglist);
Console_history.push_back({ boxmon::message_severity::warning, buffer });

va_end(arglist);
}

void boxmon_error_print(char const *format, ...)
{
if (Console_suppress_errors) {
return;
}

va_list arglist;
va_start(arglist, format);

char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, arglist);
Console_history.push_back({ boxmon::message_severity::error, buffer });

va_end(arglist);
}

const std::vector<boxmon::console_line_type> &boxmon_get_console_history()
{
return Console_history;
Expand All @@ -172,9 +124,5 @@ void boxmon_clear_console_history()

void boxmon_console_print(boxmon::message_severity severity, const std::string& message)
{
if (Console_suppress_errors) {
return;
}

Console_history.push_back({ severity, message });
}
84 changes: 31 additions & 53 deletions src/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::tuple<void *, size_t> files_load(const std::filesystem::path &path)
}

struct x16file {
char path[PATH_MAX];
std::filesystem::path path;

SDL_RWops *file;
size_t size;
Expand All @@ -103,17 +103,15 @@ static bool get_tmp_name(char *path_buffer, const char *original_path, char cons
return true;
}

bool file_is_compressed_type(char const *path)
bool file_is_compressed_type(const std::filesystem::path &path)
{
int len = (int)strlen(path);

if (strcmp(path + len - 3, ".gz") == 0 || strcmp(path + len - 3, "-gz") == 0) {
return true;
} else if (strcmp(path + len - 2, ".z") == 0 || strcmp(path + len - 2, "-z") == 0 || strcmp(path + len - 2, "_z") == 0 || strcmp(path + len - 2, ".Z") == 0) {
return true;
}

return false;
const std::string path_string = path.generic_string();
return path_string.ends_with(".gz")
|| path_string.ends_with("-gz")
|| path_string.ends_with(".z")
|| path_string.ends_with("-z")
|| path_string.ends_with("_z")
|| path_string.ends_with(".Z");
}

const char *file_find_extension(const char *path, const char *mark)
Expand Down Expand Up @@ -149,32 +147,28 @@ void files_shutdown()
}
}

x16file *x16open(const char *path, const char *attribs)
x16file *x16open(const std::filesystem::path &path, const char *attribs)
{
x16file *f = (x16file *)malloc(sizeof(x16file));
strcpy(f->path, path);
x16file *f = new x16file;
f->path = path;

if (file_is_compressed_type(path)) {
char tmp_path[PATH_MAX];
if (!get_tmp_name(tmp_path, path, ".tmp")) {
fmt::print("Path too long, cannot create temp file: {}\n", path);
goto error;
}
std::filesystem::path tmp_path = path / ".tmp";

gzFile zfile = gzopen(path, "rb");
gzFile zfile = gzopen(path.generic_string().c_str(), "rb");
if (zfile == Z_NULL) {
fmt::print("Could not open file for decompression: {}\n", path);
fmt::print("Could not open file for decompression: {}\n", path.generic_string());
goto error;
}

SDL_RWops *tfile = SDL_RWFromFile(tmp_path, "wb");
SDL_RWops *tfile = SDL_RWFromFile(tmp_path.generic_string().c_str(), "wb");
if (tfile == NULL) {
gzclose(zfile);
fmt::print("Could not open file for write: {}\n", tmp_path);
fmt::print("Could not open file for write: {}\n", tmp_path.generic_string());
goto error;
}

fmt::print("Decompressing {}\n", path);
fmt::print("Decompressing {}\n", path.generic_string());

const int buffer_size = 16 * 1024 * 1024;
char *buffer = (char *)malloc(buffer_size);
Expand All @@ -198,14 +192,14 @@ x16file *x16open(const char *path, const char *attribs)
gzclose(zfile);
free(buffer);

f->file = SDL_RWFromFile(tmp_path, attribs);
f->file = SDL_RWFromFile(tmp_path.generic_string().c_str(), attribs);
if (f->file == NULL) {
unlink(tmp_path);
std::filesystem::remove(tmp_path);
goto error;
}
f->size = total_read;
} else {
f->file = SDL_RWFromFile(path, attribs);
f->file = SDL_RWFromFile(path.generic_string().c_str(), attribs);
if (f->file == NULL) {
goto error;
}
Expand All @@ -232,26 +226,10 @@ void x16close(x16file *f)
SDL_RWclose(f->file);

if (file_is_compressed_type(f->path)) {
char tmp_path[PATH_MAX];
if (!get_tmp_name(tmp_path, f->path, ".tmp")) {
fmt::print("Path too long, cannot create temp file: {}\n", f->path);

if (f == open_files) {
open_files = f->next;
} else {
for (x16file *fi = open_files; fi != NULL; fi = fi->next) {
if (fi->next == f) {
fi->next = f->next;
break;
}
}
}
free(f);
return;
}
std::filesystem::path tmp_path = f->path / ".tmp";

if (f->modified == false) {
unlink(tmp_path);
std::filesystem::remove(tmp_path);
if (f == open_files) {
open_files = f->next;
} else {
Expand All @@ -266,10 +244,10 @@ void x16close(x16file *f)
return;
}

gzFile zfile = gzopen(f->path, "wb6");
gzFile zfile = gzopen(f->path.generic_string().c_str(), "wb6");
if (zfile == Z_NULL) {
fmt::print("Could not open file for compression: {}\n", f->path);
unlink(tmp_path);
fmt::print("Could not open file for compression: {}\n", f->path.generic_string());
std::filesystem::remove(tmp_path);
if (f == open_files) {
open_files = f->next;
} else {
Expand All @@ -284,15 +262,15 @@ void x16close(x16file *f)
return;
}

SDL_RWops *tfile = SDL_RWFromFile(tmp_path, "rb");
SDL_RWops *tfile = SDL_RWFromFile(tmp_path.generic_string().c_str(), "rb");
if (tfile == NULL) {
gzclose(zfile);
fmt::print("Could not open file for read: {}\n", tmp_path);
fmt::print("Could not open file for read: {}\n", tmp_path.generic_string());

if (zfile != Z_NULL) {
gzclose(zfile);
}
unlink(tmp_path);
std::filesystem::remove(tmp_path);
if (f == open_files) {
open_files = f->next;
} else {
Expand All @@ -307,7 +285,7 @@ void x16close(x16file *f)
return;
}

fmt::print("Recompressing {}\n", f->path);
fmt::print("Recompressing {}\n", f->path.generic_string());

const size_t buffer_size = 16 * 1024 * 1024;
char *buffer = (char *)malloc(buffer_size);
Expand Down Expand Up @@ -336,7 +314,7 @@ void x16close(x16file *f)
gzclose(zfile);
}

unlink(tmp_path);
std::filesystem::remove(tmp_path);
}

if (f == open_files) {
Expand Down
6 changes: 3 additions & 3 deletions src/files.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#pragma once

#include "zlib.h"
#include <string>
#include <filesystem>

struct x16file;

#define XSEEK_SET 0
#define XSEEK_END 1
#define XSEEK_CUR 2

bool file_is_compressed_type(char const *path);
bool file_is_compressed_type(const std::filesystem::path &path);
const char *file_find_extension(const char *path, const char *mark);

void files_shutdown();

x16file *x16open(const char *path, const char *attribs);
x16file *x16open(const std::filesystem::path &path, const char *attribs);
void x16close(x16file *f);

size_t x16size(x16file *f);
Expand Down
8 changes: 2 additions & 6 deletions src/hypercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ void hypercalls_update()
if (start == 0x0801) {
keyboard_add_text("RUN\r");
} else {
char sys_text[10];
sprintf(sys_text, "SYS$%04X\r", start);
keyboard_add_text(sys_text);
keyboard_add_text(fmt::format("SYS${:04X}", start));
}
}
}
Expand All @@ -341,9 +339,7 @@ void hypercalls_update()
}

if (Options.run_test) {
char test_text[256];
sprintf(test_text, "TEST %d\r", Options.test_number);
keyboard_add_text(test_text);
keyboard_add_text(fmt::format("TEST {:d}\r", Options.test_number));
}

Has_boot_tasks = false;
Expand Down
2 changes: 1 addition & 1 deletion src/ieee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ static void set_activity(bool active)

static void set_error(int e, int t, int s)
{
error_len = snprintf(error, sizeof(error), "%02x,%s,%02d,%02d\r", e, error_string(e), t, s);
error_len = static_cast<int>(fmt::format_to_n(error, sizeof(error), "{:02x},{:s},{:02d},{:02d}\r", e, error_string(e), t, s).out - error);
error_pos = 0;
uint8_t cbdos_flags = get_kernal_cbdos_flags();
if (e < 0x10 || e == 0x73) {
Expand Down
12 changes: 6 additions & 6 deletions src/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ void keyboard_add_event(const bool down, const SDL_Scancode scancode)
}
}

void keyboard_add_text(char const *const text)
void keyboard_add_text(const std::string &text)
{
size_t text_len = strlen(text);
const size_t text_len = text.length();
char * text_copy = new char[text_len + 1];

strcpy(text_copy, text);
strcpy(text_copy, text.c_str());

keyboard_event evt;
evt.data.text_input.file_chars = text_copy;
Expand All @@ -184,11 +184,11 @@ void keyboard_add_text(char const *const text)
Keyboard_event_list.push_back(evt);
}

void keyboard_add_file(char const *const path)
void keyboard_add_file(const std::filesystem::path &path)
{
x16file *file = x16open(path, "r");
if (file == nullptr) {
fmt::print("Cannot open text file {}!\n", path);
fmt::print("Cannot open text file {}!\n", path.generic_string());
return;
}

Expand All @@ -199,7 +199,7 @@ void keyboard_add_file(char const *const path)

const size_t read_size = x16read(file, file_text, sizeof(uint8_t), static_cast<unsigned int>(file_size));
if (read_size != file_size) {
fmt::print("File read error on {}\n", path);
fmt::print("File read error on {}\n", path.generic_string());
delete[] file_text;
} else {
file_text[read_size] = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
// All rights reserved. License: 2-clause BSD

# include <SDL_keycode.h>
#include<filesystem>

void keyboard_process();

void keyboard_add_event(const bool down, const SDL_Scancode scancode);
void keyboard_add_text(char const *const text);
void keyboard_add_file(char const *const path);
void keyboard_add_text(const std::string &text);
void keyboard_add_file(const std::filesystem::path &path);

uint8_t keyboard_get_next_byte();

Expand Down
Loading

0 comments on commit 548447a

Please sign in to comment.