![]() |
CARTA Backend
The backend component of CARTA
|
#include "File.h"
#include <spdlog/fmt/fmt.h>
#include <fstream>
#include <regex>
#include "String.h"
Functions | |
uint32_t | GetMagicNumber (const std::string &filename) |
Reads the magic number from the beginning of a file. | |
bool | IsCompressedFits (const std::string &filename) |
Checks whether a given file is a compressed FITS file. | |
bool | IsRemoteHttpFile (const std::string &filename) |
Checks whether a given filename represents a remote HTTP or HTTPS URL. | |
bool | IsGzMagicNumber (uint32_t magic_number) |
Determines if a given magic number corresponds to a gzip-compressed file. | |
int | GetNumItems (const std::string &path) |
Counts the number of items in a given directory. | |
fs::path | SearchPath (std::string filename) |
Searches for a file in the system's PATH environment variable. | |
CARTA::FileType | GuessImageType (const std::string &path_string, bool check_content) |
Determines the image file type based on its content or extension. | |
CARTA::FileType | GuessRegionType (const std::string &path_string, bool check_content) |
Determines the region file type based on its content or extension. | |
CARTA::CatalogFileType | GuessTableType (const std::string &path_string, bool check_content) |
Determines the catalog table file type based on its content or file extension. | |
uint32_t GetMagicNumber | ( | const std::string & | filename | ) |
Reads the magic number from the beginning of a file.
This function opens the specified file and reads the first 4 bytes as a uint32_t
magic number.
0
without error messages. int GetNumItems | ( | const std::string & | path | ) |
Counts the number of items in a given directory.
This function iterates over the contents of the specified directory and counts the number of files and subdirectories present. If the directory does not exist or cannot be accessed, the function returns -1
.
-1
is returned. CARTA::FileType GuessImageType | ( | const std::string & | path_string, |
bool | check_content | ||
) |
Determines the image file type based on its content or extension.
This function attempts to identify the type of an image file either by checking its magic number (file signature) or examining its file extension. If check_content
is set to true
, the function inspects the file's magic number to classify it as FITS or HDF5. If check_content
is false
, it relies on common file extensions.
check_content
is enabled, compressed FITS files (.fits.gz
) are identified by their decompressed filename extension.CARTA::FileType GuessRegionType | ( | const std::string & | path_string, |
bool | check_content | ||
) |
Determines the region file type based on its content or extension.
This function attempts to identify the type of a region file used in astronomical imaging analysis by either checking its file content or examining its file extension. If check_content
is true
, it reads the first line of the file to identify known headers (e.g., #CRTF
or # Region file format: DS9
). Otherwise, it determines the file type based on its extension.
#CRTF
, while DS9 region files may include # Region file format: DS9
as an optional header.CARTA::CatalogFileType GuessTableType | ( | const std::string & | path_string, |
bool | check_content | ||
) |
Determines the catalog table file type based on its content or file extension.
This function attempts to classify the type of a catalog table file (e.g., FITS table or VOTable) either by checking its magic number (if check_content
is true
) or, if content checking is disabled, by inspecting the file extension.
check_content
is enabled, the function may attempt to read the file's magic number. Ensure the file is accessible to avoid potential I/O errors.bool IsCompressedFits | ( | const std::string & | filename | ) |
Checks whether a given file is a compressed FITS file.
This function first determines if the file is gzip-compressed by examining its magic number. If the file is gzip-compressed, it then checks whether the original filename (before compression) has a .fits
extension.
bool IsGzMagicNumber | ( | uint32_t | magic_number | ) |
Determines if a given magic number corresponds to a gzip-compressed file.
This function checks whether the provided 32-bit magic number matches the gzip file signature (0x1f8b
). The magic number is formatted as a hexadecimal string and examined to see if it ends with "8b1f".
bool IsRemoteHttpFile | ( | const std::string & | filename | ) |
Checks whether a given filename represents a remote HTTP or HTTPS URL.
This function uses a regular expression to determine if the provided filename starts with "http://" or "https://", indicating that it is a remote file accessible via HTTP.
fs::path SearchPath | ( | std::string | filename | ) |
Searches for a file in the system's PATH
environment variable.
This function retrieves the PATH
environment variable, splits it into individual directory paths, and searches for the specified file within those directories. If the file is found, its full path is returned. If not found or if an error occurs, an empty fs::path
is returned.
PATH
are separated by colons (:
), which is standard on UNIX-like systems.PATH
environment variable is not set, this function may behave unexpectedly.