From 153a5c4c3a7fdb73c3672d67a3de083dc3946100 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 20 May 2021 14:14:08 -0700 Subject: [PATCH] Factor out "between" calculation to macro and func FILESYSTEM_mountAssets() has a big comment describing the magic numbers needed to grab FILENAME from a string that looks like "levels/FILENAME.vvvvvv". Instead of doing that (and having to write a comment every time the similar happens), I've written a macro (and helper function) instead that does the same thing, but clearly conveys the intent. I mean, just look at the diff. Using VVV_between() is much better than having to read that comment, and the corresponding SDL_strlcpy(). --- desktop_version/src/FileSystemUtils.cpp | 15 +-------------- desktop_version/src/UtilityClass.cpp | 15 +++++++++++++++ desktop_version/src/UtilityClass.h | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 91dcc99d..1160f63f 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -325,25 +325,12 @@ void FILESYSTEM_loadZip(const char* filename) void FILESYSTEM_mountAssets(const char* path) { - const size_t path_size = SDL_strlen(path); char filename[MAX_PATH]; char zip_data[MAX_PATH]; const char* zip_normal; char dir[MAX_PATH]; - /* path is going to look like "levels/LEVELNAME.vvvvvv". - * We want LEVELNAME, which entails starting from index 7 - * (which is how long "levels/" is) - * and then grabbing path_size-14 characters - * (14 chars because "levels/" and ".vvvvvv" are both 7 chars). - * We also add 1 when calculating the amount of bytes to grab - * to account for the null terminator. - */ - SDL_strlcpy( - filename, - &path[7], - VVV_min((path_size - 14) + 1, sizeof(filename)) - ); + VVV_between(path, "levels/", filename, ".vvvvvv"); SDL_snprintf( zip_data, diff --git a/desktop_version/src/UtilityClass.cpp b/desktop_version/src/UtilityClass.cpp index 315533c0..e060283d 100644 --- a/desktop_version/src/UtilityClass.cpp +++ b/desktop_version/src/UtilityClass.cpp @@ -344,3 +344,18 @@ void VVV_fillstring( SDL_memset(buffer, fillchar, buffer_size - 1); buffer[buffer_size - 1] = '\0'; } +void _VVV_between( + const char* original, + const size_t left_length, + char* middle, + const size_t right_length, + const size_t middle_size +) { + size_t middle_length = SDL_strlen(original); + middle_length -= left_length + right_length; + SDL_strlcpy( + middle, + &original[left_length], + VVV_min(middle_length + 1, middle_size) + ); +} diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index e099e09b..b2d22b6e 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -45,6 +45,28 @@ void VVV_fillstring( puts(message); \ } +/* Don't call this directly; use the VVV_between macro. */ +void _VVV_between( + const char* original, + const size_t left_length, + char* middle, + const size_t right_length, + const size_t middle_size +); + +/* If original is "LEFTMIDDLERIGHT", VVV_between(original, "LEFT", buffer, "RIGHT") + * will put "MIDDLE" into buffer - assuming that sizeof(buffer) refers to length + * of buffer and not length of pointer to buffer. + */ +#define VVV_between(original, left, middle, right) \ + _VVV_between( \ + original, \ + SDL_arraysize(left) - 1, \ + middle, \ + SDL_arraysize(right) - 1, \ + sizeof(middle) \ + ) + //helperClass class UtilityClass