Skip to content

Commit

Permalink
Improved Type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeulen committed Sep 17, 2024
1 parent 3083958 commit 5b3ca15
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/whats_new/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version 0.24

* Add support for sm files
* Add support for CZ CF3 FCS files
* Improved type tttr file type inference to mitigate crashes


Version 0.23
Expand Down
6 changes: 6 additions & 0 deletions ext/python/FileCheck.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%module tttrlib
%{
#include "FileCheck.h"
%}

%include "FileCheck.h"
15 changes: 2 additions & 13 deletions ext/python/TTTR.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,8 @@ def __init__(self, *args, **kwargs):
import pathlib
if isinstance(args[0], str) or isinstance(args[0], pathlib.Path):
if len(args) == 1:
suffix = str(pathlib.Path(args[0]).suffix)[1:].upper()
obj = str(pathlib.Path(args[0]).absolute())
if suffix == 'HT3' or suffix == 'PTU':
tttr_container_type = suffix
elif suffix == 'HDF' or suffix == 'H5' or suffix == 'HDF5':
tttr_container_type = 'HDF'
else:
raise ValueError(
"The file type '{}' does not allow to determine "
"the container format. Specify the 'tttr_container_type' "
"parameter.".format(suffix)
)
this = _tttrlib.new_TTTR(obj, tttr_container_type)
obj = str(pathlib.Path(args[0]).absolute().as_posix())
this = _tttrlib.new_TTTR(obj)
else:
this = _tttrlib.new_TTTR(*args, **kwargs)
else:
Expand Down
1 change: 1 addition & 0 deletions ext/python/tttrlib.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

%include "info.h"
%include "misc_types.i"
%include "FileCheck.i"

%include "TTTRHeader.i"
%include "TTTRRange.i"
Expand Down
93 changes: 93 additions & 0 deletions include/FileCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef TTTRLIB_FILECHECK_H
#define TTTRLIB_FILECHECK_H

#include <iostream>
#include <fstream>
#include <array>

#include "TTTRHeader.h"

/**
* @brief Checks if the given file is an HDF5 file.
*
* This function opens the specified file and performs checks to determine if
* it conforms to the HDF5 file format.
*
* @param filename The name of the file to check.
* @return true if the file is an HDF5 file, false otherwise.
*/
bool isHDF5File(const std::string& filename);

/**
* @brief Checks if the given file is an SM file.
*
* This function opens the specified file and reads the first 64 bits to determine
* if the file is an SM file, specifically checking if the value equals 2.
*
* @param filename The name of the file to check.
* @return true if the file is an SM file, false otherwise.
*/
bool isSMFile(const std::string& filename);

/**
* @brief Checks if the given file is a PTU file.
*
* This function opens the specified file and reads the first 8 bytes (Magic) to
* verify if the file is a PTU file by checking if it starts with "PQTTTR".
*
* @param filename The name of the file to check.
* @return true if the file is a PTU file, false otherwise.
*/
bool isPTUFile(const std::string& filename);

/**
* @brief Checks if the given file is an HT3 file.
*
* This function opens the specified file and reads the header to check if the
* file format version is "1.0", which is required for HT3 files.
*
* @param filename The name of the file to check.
* @return true if the file is an HT3 file, false otherwise.
*/
bool isHT3File(const std::string& filename);

/**
* @brief Checks if the given file is a BH132 file.
*
* This function opens the specified file and reads the header to determine if
* it is a BH132 file by checking if the `macro_time_clock` is in the range (0, 10000)
* and if the `invalid` flag is set to true.
*
* @param filename The name of the file to check.
* @return true if the file is a BH132 file, false otherwise.
*/
bool isBH132File(const std::string& filename);


/**
* @brief Determines the type of the TTTR file based on its content.
*
* This function uses various type-checking functions to infer the type of the
* given TTTR file. It performs checks in a specific order to identify the file type.
* Returns a container identifier corresponding to the file type.
*
* @param fn The name of the file to check.
* @return An integer representing the type of the TTTR file, or -1 if the type cannot be determined.
*/
int inferTTTRFileType(const char* fn);


/**
* @brief Determines if the given file is a Carl Zeiss Confocor3 (CZ Confocor3) raw data file.
*
* This function reads the header of the file and checks for specific patterns
* to determine if the file is of type CZ Confocor3. It performs the necessary checks
* and returns true if the file type is identified, otherwise false.
*
* @param filename The name of the file to check.
* @return True if the file is a CZ Confocor3 file, false otherwise.
*/
bool isCZConfocor3File(const std::string& filename);


#endif //TTTRLIB_FILECHECK_H
20 changes: 20 additions & 0 deletions include/TTTR.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "Histogram.h"
#include "TTTRHeader.h"
#include "FileCheck.h"
#include "TTTRMask.h"
#include "TTTRRecordReader.h"
#include "TTTRRecordTypes.h"
Expand Down Expand Up @@ -683,6 +684,25 @@ class TTTR : public std::enable_shared_from_this<TTTR>{
*/
TTTR(const char *filename, int container_type, bool read_input);

/**
* @brief Constructs a TTTR object based on the provided filename by inferring the file type.
*
* This constructor initializes a TTTR object by automatically determining the file type
* from the given filename. It utilizes the `inferTTTRFileType` function to identify
* the type of TTTR file and then calls the existing constructor with the inferred container type.
* The internal state related to the container type is initialized based on the file type inference.
*
* The constructor performs the following actions:
* - Infers the container type from the filename using `inferTTTRFileType`.
* - Initializes the TTTR object using the existing constructor `TTTR(const char *fn, int container_type, bool some_flag)`.
* - Sets the container type string based on the inferred container type.
* - Handles cases where the inferred container type is not supported by setting the container type string to `"Unknown"`
* and logging an error message.
*
* @param filename The path to the TTTR file to be analyzed. The file type is inferred based on this filename.
*/
TTTR(const char *filename);

/*!
* Constructor for TTTR object that reads the content of the file.
*
Expand Down
Loading

0 comments on commit 5b3ca15

Please sign in to comment.