flac: Input file name size calculated just one time
authorAndrey Astafyev <dev@246060.ru>
Sat, 30 Nov 2019 02:08:58 +0000 (05:08 +0300)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sun, 8 Dec 2019 09:34:57 +0000 (20:34 +1100)
src/flac/main.c

index 8eb246e..c6c5b89 100644 (file)
@@ -1694,6 +1694,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        const char *outfilename = get_encoded_outfilename(infilename); /* the final name of the encoded file */
        /* internal_outfilename is the file we will actually write to; it will be a temporary name if infilename==outfilename */
        char *internal_outfilename = 0; /* NULL implies 'use outfilename' */
+       size_t infilename_length;
 
        if(0 == outfilename) {
                flac__utils_printf(stderr, 1, "ERROR: filename too long: %s", infilename);
@@ -1714,21 +1715,22 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
        if(!option_values.force_raw_format) {
                /* first set format based on name */
-               if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".wav"))
+               infilename_length = strlen(infilename);
+               if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".wav"))
                        input_format = FORMAT_WAVE;
-               else if(strlen(infilename) >= 5 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-5), ".rf64"))
+               else if(infilename_length >= 5 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-5), ".rf64"))
                        input_format = FORMAT_RF64;
-               else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".w64"))
+               else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".w64"))
                        input_format = FORMAT_WAVE64;
-               else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".aif"))
+               else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".aif"))
                        input_format = FORMAT_AIFF;
-               else if(strlen(infilename) >= 5 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-5), ".aiff"))
+               else if(infilename_length >= 5 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-5), ".aiff"))
                        input_format = FORMAT_AIFF;
-               else if(strlen(infilename) >= 5 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-5), ".flac"))
+               else if(infilename_length >= 5 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-5), ".flac"))
                        input_format = FORMAT_FLAC;
-               else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".oga"))
+               else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".oga"))
                        input_format = FORMAT_OGGFLAC;
-               else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".ogg"))
+               else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".ogg"))
                        input_format = FORMAT_OGGFLAC;
 
                /* attempt to guess the file type based on the first 12 bytes */
@@ -2036,6 +2038,7 @@ int decode_file(const char *infilename)
        FileFormat output_format = FORMAT_WAVE;
        decode_options_t decode_options;
        const char *outfilename = get_decoded_outfilename(infilename);
+       size_t infilename_length;
 
        if(0 == outfilename) {
                flac__utils_printf(stderr, 1, "ERROR: filename too long: %s", infilename);
@@ -2084,11 +2087,12 @@ int decode_file(const char *infilename)
                        return usage_error("ERROR: --keep-foreign-metadata can only be used with WAVE, Wave64, RF64, or AIFF output\n");
        }
 
+       infilename_length = strlen(infilename);
        if(option_values.use_ogg)
                treat_as_ogg = true;
-       else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".oga"))
+       else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".oga"))
                treat_as_ogg = true;
-       else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".ogg"))
+       else if(infilename_length >= 4 && 0 == FLAC__STRCASECMP(infilename+(infilename_length-4), ".ogg"))
                treat_as_ogg = true;
        else
                treat_as_ogg = false;