cwk_path_change_extension

(since v1.2.0)
Changes the extension of a file path.

Description

size_t cwk_path_change_extension(const char *path, const char *new_extension,
  char *buffer, size_t buffer_size);

This function changes the extension of a file name. The function will append an extension if the basename does not have an extension, or use the extension as a basename if the path does not have a basename. This function will not write out more than the specified buffer can contain. However, the generated string is always null-terminated - even if not the whole path is written out. The function returns the total number of characters the complete buffer would have, even if it was not written out completely. The path may be the same memory address as the buffer.

Note: This function does not normalize the resulting path. You can use cwk_path_normalize to do so.

Note: If the new_extension parameter starts with a dot, the first dot will be ignored when the new extension is appended.

Parameters

Return Value

Returns the total size which the output would have if it was not truncated.

Example

#include <cwalk.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char buffer[FILENAME_MAX];

  cwk_path_set_style(CWK_STYLE_WINDOWS);

  cwk_path_change_extension("C:\\test.txt", "md", buffer,
    sizeof(buffer));

  printf("The new path: '%s'", buffer);

  return EXIT_SUCCESS;
}

Ouput:

The new path: 'C:\test.md'

Changelog

Version Description
v1.2.0 The function is introduced.