FileParser

FileParser is the abstract root of Blackchirp’s file-parser hierarchy. Every class that imports an external data file — spectroscopic line catalogs, generic two-column XY data, anything else added later — derives from this interface. The class defines four pure-virtual hook points that identify the format and a small set of protected helpers covering the file-system patterns shared by every concrete parser.

Subclass authors implement FileParser::canParse() to recognize their format (typically a suffix check followed by a structural sniff of the first lines), FileParser::formatName() and FileParser::formatDescription() for user-facing labels, and FileParser::fileExtensions() for QFileDialog glob patterns. The protected helpers — FileParser::isFileReadable(), FileParser::hasMatchingExtension(), and FileParser::readFileHeader() — let canParse implementations share a single readable-file check, a case-insensitive suffix match, and an N-line peek without each parser re-implementing them.

The actual parse method that returns data lives on the format-specific subclass because the value type differs by family: CatalogParser adds a parse() CatalogData hook for spectroscopic line catalogs, and GenericXYParser adds its own parse() GenericXYData directly. Concrete parsers register themselves with FileParserRegistry during application startup; the registry then dispatches incoming files to the first parser whose canParse returns true. The user-facing import workflow that consumes these parsers is described in Overlays.

API Reference

class FileParser

Abstract interface for file-format parsers.

Subclassed by CatalogParser, GenericXYParser

Public Functions

virtual ~FileParser() = default
virtual bool canParse(const QString &filePath, const QVariantMap &hints = QVariantMap()) const = 0

Test whether this parser recognizes the given file.

Implementations typically combine an extension check (hasMatchingExtension) with a structural sniff of the first few lines (readFileHeader) to keep the test cheap.

Parameters:
  • filePath – Absolute or relative path to the candidate file.

  • hints – Optional format-specific hints (delimiter overrides, column indices, etc.). Each subclass documents which keys it consumes.

Returns:

true when the parser is willing to handle the file.

virtual QString formatName() const = 0

Short, human-readable format identifier.

Used in file-dialog filters and in user-visible error messages. Examples: "SPCAT", "XIAM", "GenericXY".

virtual QStringList fileExtensions() const = 0

Glob patterns this parser accepts for file-dialog filters.

Patterns include the leading wildcard and dot (e.g. "*.cat"). :cpp:func:FileParserRegistry::fileDialogFilter joins these into a single QFileDialog filter string.

virtual QString formatDescription() const = 0

One-line description of the format suitable for tooltips.

Protected Functions

bool isFileReadable(const QString &filePath) const

Verify that filePath exists, is a regular file, and is readable.

Returns:

true when QFileInfo reports the file is readable.

bool hasMatchingExtension(const QString &filePath, const QStringList &extensions) const

Test whether the file’s suffix matches one of extensions.

Comparison is case-insensitive. extensions entries should be of the form ".cat" (with the leading dot, no glob wildcard).

QStringList readFileHeader(const QString &filePath, int headerLines = 10) const

Read the first headerLines lines of filePath as text.

Returns an empty list if the file is unreadable. Used by canParse() implementations that need to peek at the file body without reading the entire file.