diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2019-08-29 17:17:37 +0200 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2019-08-29 17:17:37 +0200 |
| commit | ba8db654fffee58a2d3ee3914c8fc48c663e2d80 (patch) | |
| tree | e31318e35c38ab179fb6eb950a872cf6748657cc | |
| parent | 62e39e5559acdad536418c35575985a9c55de801 (diff) | |
Fix one compiler warning (STR 3529)
fluid/undo.cxx: fix warning [-Wformat-truncation=]
This fix also removes some static variables and simplifies the
function undo_filename(). It does no longer copy the full filename
string back to a given buffer. Now it returns a pointer to the
internal filename string.
Summary: fix compiler warning, save memory, simplify a function, and
speed up code by not copying data unnecessarily.
| -rw-r--r-- | fluid/undo.cxx | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/fluid/undo.cxx b/fluid/undo.cxx index fc23e60a1..98dff5ea2 100644 --- a/fluid/undo.cxx +++ b/fluid/undo.cxx @@ -54,26 +54,34 @@ int undo_save = -1; // Last undo level that was saved static int undo_paused = 0; // Undo checkpointing paused? -// Return the undo filename -static char *undo_filename(int level, char *buf, int bufsize) { - static char undo_path[FL_PATH_MAX] = ""; // Undo path - - - if (!undo_path[0]) fluid_prefs.getUserdataPath(undo_path, sizeof(undo_path)); +// Return the undo filename. +// The filename is constructed in a static internal buffer and +// this buffer is overwritten by every call of this function. +// The return value is a pointer to this internal string. +static char *undo_filename(int level) { + static char undo_path[FL_PATH_MAX] = ""; // Undo path + static unsigned int undo_path_len = 0; // length w/o filename + + if (!undo_path_len) { + fluid_prefs.getUserdataPath(undo_path, sizeof(undo_path)); + undo_path_len = strlen(undo_path); + } - snprintf(buf, bufsize, "%sundo_%d_%d.fl", undo_path, getpid(), level); - return buf; + // append filename: "undo_PID_LEVEL.fl" + snprintf(undo_path + undo_path_len, + sizeof(undo_path) - undo_path_len - 1, + "undo_%d_%d.fl", getpid(), level); + return undo_path; } // Redo menu callback void redo_cb(Fl_Widget *, void *) { - char filename[FL_PATH_MAX]; // Undo checkpoint file if (undo_current >= undo_last) return; undo_suspend(); - if (!read_file(undo_filename(undo_current + 1, filename, sizeof(filename)), 0)) { + if (!read_file(undo_filename(undo_current + 1), 0)) { // Unable to read checkpoint file, don't redo... undo_resume(); return; @@ -91,16 +99,15 @@ void redo_cb(Fl_Widget *, void *) { // Undo menu callback void undo_cb(Fl_Widget *, void *) { - char filename[FL_PATH_MAX]; // Undo checkpoint file if (undo_current <= 0) return; if (undo_current == undo_last) { - write_file(undo_filename(undo_current, filename, sizeof(filename))); + write_file(undo_filename(undo_current)); } undo_suspend(); - if (!read_file(undo_filename(undo_current - 1, filename, sizeof(filename)), 0)) { + if (!read_file(undo_filename(undo_current - 1), 0)) { // Unable to read checkpoint file, don't undo... undo_resume(); return; @@ -119,16 +126,15 @@ void undo_cb(Fl_Widget *, void *) { // Save current file to undo buffer void undo_checkpoint() { - char filename[FL_PATH_MAX]; // Undo checkpoint filename - -// printf("undo_checkpoint(): undo_current=%d, undo_paused=%d, modflag=%d\n", -// undo_current, undo_paused, modflag); + // printf("undo_checkpoint(): undo_current=%d, undo_paused=%d, modflag=%d\n", + // undo_current, undo_paused, modflag); // Don't checkpoint if undo_suspend() has been called... if (undo_paused) return; // Save the current UI to a checkpoint file... - if (!write_file(undo_filename(undo_current, filename, sizeof(filename)))) { + const char *filename = undo_filename(undo_current); + if (!write_file(filename)) { // Don't attempt to do undo stuff if we can't write a checkpoint file... perror(filename); return; @@ -150,12 +156,10 @@ void undo_checkpoint() { // Clear undo buffer void undo_clear() { - char filename[FL_PATH_MAX]; // Undo checkpoint filename - // Remove old checkpoint files... for (int i = 0; i <= undo_max; i ++) { - fl_unlink(undo_filename(i, filename, sizeof(filename))); + fl_unlink(undo_filename(i)); } // Reset current, last, and save indices... |
