C Standard Library Extensions: Get various names (filenames, dir names,...)

C Standard Library Extensions 6.2.0

- Main Page
- Modules
- Classes
- Files

Functions
Get various names (filenames, dir names,...)

## Functions
char * | qfits_get_dir_name (const char *filename)
| Find the directory name in the given string.
char * | qfits_get_base_name (const char *filename)
| Find out the base name of a file (i.e. without prefix path)
char * | qfits_get_root_name (const char *filename)
| Find out the root part of a basename (name without extension).
char * | qfits_get_ext_name (const char *filename)
| Find out the extension of a file name.

## Detailed Description

The following functions are useful to cut out a filename into its components. All functions work with statically allocated memory, i.e. the pointers they return are not permanent but likely to be overwritten at each function call. If you need a returned value later on, you should store it into a local variable.

Example:

char * s ;
s = qfits_get_dir_name("/mnt/cdrom/data/image.fits")

s contains now "/mnt/cdrom/data" but will loose these contents at the next function call. To retain its value, you can either do:

char s[1024];
strcpy(s, qfits_get_dir_name("/mnt/cdrom/data/image.fits"));

or:

char * s;
s = strdup(qfits_get_dir_name("/mnt/cdrom/data/image.fits"));
...
free(s);

## Function Documentation

char* qfits_get_base_name | ( | const char * | filename | ) |

Find out the base name of a file (i.e. without prefix path)

Parameters:

  filename | Full path name to scan.

Returns:
  Pointer to char within the input string.

Provide a full path name and you get in return a pointer to a statically allocated string containing the name of the file only, without prefixing directory names. If the input string does not contain a slash (i.e. it is not a full path), the returned string is a copy of the input string.

This function does not check for the existence of the path or the file.

Examples:

        qfits_get_base_name("/cdrom/data/image.fits") returns "image.fits"
        qfits_get_base_name("filename.fits") returns "filename.fits"

char* qfits_get_dir_name | ( | const char * | filename | ) |

Find the directory name in the given string.

Parameters:

  filename | Full path name to scan.

Returns:
  Pointer to statically allocated string.

Provide a full path name and you get in return a pointer to a statically allocated string containing the name of the directory only, without trailing slash. If the input string does not contain a slash (i.e. it is not a full path), the returned string is '.', corresponding to the current working directory. Since the returned string is statically allocated, do not try to free it or modify it.

This function does not check for the existence of the path or the file.

Examples:

        qfits_get_dir_name("/cdrom/data/image.fits") returns "/cdrom/data"
        qfits_get_dir_name("filename.fits") returns "."

char* qfits_get_ext_name | ( | const char * | filename | ) |

Find out the extension of a file name.

Parameters:

  filename | File name without path prefix.

Returns:
  Pointer to char within the input string.

Find out the extension of a given file name. Notice that the input character string must not contain a path prefix (typically, you feed in the output of `qfits_get_base_name`).

Works with all kinds of extensions: returns the part of the string after the last dot. If no dot is found in the input string, NULL is returned.

Examples:

        qfits_get_ext_name("filename.fits") returns "fits"
        qfits_get_ext_name("hello.c") returns "c"
        qfits_get_ext_name("readme") returns NULL

char* qfits_get_root_name | ( | const char * | filename | ) |

Find out the root part of a basename (name without extension).

Parameters:

  filename | File name to scan.

Returns:
  Pointer to statically allocated string.

Find out the root part of a file name, i.e. the file name without extension. Since in Unix a file name can have several dots, only a number of extensions are supported. This includes:

- .fits and .FITS
- .tfits and .TFITS
- .paf and .PAF
- .ascii and .ASCII
- .dat and .DAT
- .txt and .TXT

This function does not check for the existence of the path or the file.

Examples:

        qfits_get_root_name("/cdrom/filename.fits") returns "/cdrom/filename"
        qfits_get_root_name("filename.paf") returns "filename"
        qfits_get_root_name("filename") returns "filename"
        qfits_get_root_name("filename.ext") returns "filename.ext"

Since the returned string is statically allocated in this module, do not try to free it or modify its contents.