test: Reimplemented SDLTest_CommonUsage() to restore binary compatibility.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 12 Feb 2020 13:09:38 -0500
changeset 13513af27f52d379a
parent 13512 d85e27d937eb
child 13514 529fc1a611e8
test: Reimplemented SDLTest_CommonUsage() to restore binary compatibility.

Fixes Bugzilla #4975.
include/SDL_test_common.h
src/test/SDL_test_common.c
     1.1 --- a/include/SDL_test_common.h	Wed Feb 12 13:08:29 2020 -0500
     1.2 +++ b/include/SDL_test_common.h	Wed Feb 12 13:09:38 2020 -0500
     1.3 @@ -156,6 +156,19 @@
     1.4  void SDLTest_CommonLogUsage(SDLTest_CommonState * state, const char *argv0, const char **options);
     1.5  
     1.6  /**
     1.7 + * \brief Returns common usage information
     1.8 + *
     1.9 + * You should (probably) be using SDLTest_CommonLogUsage() instead, but this
    1.10 + *  function remains for binary compatibility. Strings returned from this
    1.11 + *  function are valid until SDLTest_CommonQuit() is called, in which case
    1.12 + *  those strings' memory is freed and can no longer be used.
    1.13 + *
    1.14 + * \param state The common state describing the test window to create.
    1.15 + * \returns String with usage information
    1.16 + */
    1.17 +const char *SDLTest_CommonUsage(SDLTest_CommonState * state);
    1.18 +
    1.19 +/**
    1.20   * \brief Open test window.
    1.21   *
    1.22   * \param state The common state describing the test window to create.
     2.1 --- a/src/test/SDL_test_common.c	Wed Feb 12 13:08:29 2020 -0500
     2.2 +++ b/src/test/SDL_test_common.c	Wed Feb 12 13:09:38 2020 -0500
     2.3 @@ -521,6 +521,65 @@
     2.4      }
     2.5  }
     2.6  
     2.7 +static const char *
     2.8 +BuildCommonUsageString(char **pstr, const char **strlist, const int numitems, const char **strlist2, const int numitems2)
     2.9 +{
    2.10 +    char *str = *pstr;
    2.11 +    if (!str) {
    2.12 +        size_t len = SDL_strlen("[--trackmem]") + 2;
    2.13 +        int i;
    2.14 +        for (i = 0; i < numitems; i++) {
    2.15 +            len += SDL_strlen(strlist[i]) + 1;
    2.16 +        }
    2.17 +        if (strlist2) {
    2.18 +            for (i = 0; i < numitems2; i++) {
    2.19 +                len += SDL_strlen(strlist2[i]) + 1;
    2.20 +            }
    2.21 +        }
    2.22 +        str = (char *) SDL_calloc(1, len);
    2.23 +        if (!str) {
    2.24 +            return "";  /* oh well. */
    2.25 +        }
    2.26 +        SDL_strlcat(str, "[--trackmem] ", len);
    2.27 +        for (i = 0; i < numitems-1; i++) {
    2.28 +            SDL_strlcat(str, strlist[i], len);
    2.29 +            SDL_strlcat(str, " ", len);
    2.30 +        }
    2.31 +        SDL_strlcat(str, strlist[i], len);
    2.32 +        if (strlist2) {
    2.33 +            SDL_strlcat(str, " ", len);
    2.34 +            for (i = 0; i < numitems2-1; i++) {
    2.35 +                SDL_strlcat(str, strlist2[i], len);
    2.36 +                SDL_strlcat(str, " ", len);
    2.37 +            }
    2.38 +            SDL_strlcat(str, strlist2[i], len);
    2.39 +        }
    2.40 +        *pstr = str;
    2.41 +    }
    2.42 +    return str;
    2.43 +}
    2.44 +
    2.45 +static char *common_usage_video = NULL;
    2.46 +static char *common_usage_audio = NULL;
    2.47 +static char *common_usage_videoaudio = NULL;
    2.48 +
    2.49 +const char *
    2.50 +SDLTest_CommonUsage(SDLTest_CommonState * state)
    2.51 +{
    2.52 +
    2.53 +    switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
    2.54 +        case SDL_INIT_VIDEO:
    2.55 +            return BuildCommonUsageString(&common_usage_video, video_usage, SDL_arraysize(video_usage), NULL, 0);
    2.56 +        case SDL_INIT_AUDIO:
    2.57 +            return BuildCommonUsageString(&common_usage_audio, audio_usage, SDL_arraysize(audio_usage), NULL, 0);
    2.58 +        case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
    2.59 +            return BuildCommonUsageString(&common_usage_videoaudio, video_usage, SDL_arraysize(video_usage), audio_usage, SDL_arraysize(audio_usage));
    2.60 +        default:
    2.61 +            return "[--trackmem]";
    2.62 +    }
    2.63 +}
    2.64 +
    2.65 +
    2.66  SDL_bool
    2.67  SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, const int argc, char **argv)
    2.68  {
    2.69 @@ -1852,6 +1911,13 @@
    2.70  {
    2.71      int i;
    2.72  
    2.73 +    SDL_free(common_usage_video);
    2.74 +    SDL_free(common_usage_audio);
    2.75 +    SDL_free(common_usage_videoaudio);
    2.76 +    common_usage_video = NULL;
    2.77 +    common_usage_audio = NULL;
    2.78 +    common_usage_videoaudio = NULL;
    2.79 +
    2.80      SDL_free(state->windows);
    2.81      if (state->targets) {
    2.82          for (i = 0; i < state->num_windows; ++i) {