38 R__LOG_WARNING(RFileLog()) <<
"ROOT::RFile only supports ROOT files. The preferred file extension is \".root\"";
43enum class ENameCycleError {
52struct RNameCycleResult {
54 std::optional<std::int16_t> fCycle;
55 ENameCycleError fError;
59static const char *
ToString(ENameCycleError err)
61 static const char *
const kErrorStr[] = {
"",
"",
"invalid syntax",
"cycle is too large",
"name is empty"};
62 static_assert(std::size(
kErrorStr) ==
static_cast<std::size_t
>(ENameCycleError::kCOUNT));
63 return kErrorStr[
static_cast<std::size_t
>(err)];
71 return ENameCycleError::kInvalidSyntax;
72 if (res * 10 > std::numeric_limits<std::int16_t>::max())
73 return ENameCycleError::kCycleTooLarge;
78 assert(res < std::numeric_limits<std::int16_t>::max());
79 out =
static_cast<std::int16_t
>(res);
81 return ENameCycleError::kNoError;
95 result.fError = ENameCycleError::kNameEmpty;
101 result.fError = ENameCycleError::kInvalidSyntax;
108 result.fError = ENameCycleError::kAnyCycle;
142 return "path cannot be empty";
144 if (path.back() ==
'/')
145 return "path cannot end with a '/'";
148 for (
char ch : path) {
151 valid &= !(ch < 33 || ch ==
'.');
154 return "path cannot contain control characters, whitespaces or dots";
161 const auto len = path.length();
170 const auto it = std::unique(path.begin(), path.end(), [](
char a,
char b) { return (a ==
'/' && b ==
'/'); });
171 path.erase(it, path.end());
174 const auto nesting = std::count(path.begin(), path.end(),
'/');
176 return "pathView contains too many levels of nesting";
192 auto tfile = std::unique_ptr<TFile>(
TFile::Open(std::string(path).c_str(),
"READ_WITHOUT_GLOBALREGISTRATION"));
193 if (!tfile || tfile->IsZombie())
196 auto rfile = std::unique_ptr<RFile>(
new RFile(std::move(tfile)));
205 auto tfile = std::unique_ptr<TFile>(
TFile::Open(std::string(path).c_str(),
"UPDATE_WITHOUT_GLOBALREGISTRATION"));
206 if (!tfile || tfile->IsZombie())
209 auto rfile = std::unique_ptr<RFile>(
new RFile(std::move(tfile)));
218 auto tfile = std::unique_ptr<TFile>(
TFile::Open(std::string(path).c_str(),
"RECREATE_WITHOUT_GLOBALREGISTRATION"));
219 if (!tfile || tfile->IsZombie())
222 auto rfile = std::unique_ptr<RFile>(
new RFile(std::move(tfile)));
231 std::string
fullPath = std::string(path);
259 if (res.fError != ENameCycleError::kAnyCycle) {
260 if (res.fError != ENameCycleError::kNoError) {
263 }
else if (res.fCycle && *res.fCycle != key->
GetCycle()) {
293 }
else if (key && !
GetROOT()->IsBatch()) {
294 R__LOG_WARNING(RFileLog()) <<
"Tried to get object '" << path <<
"' of type " <<
cls->GetName()
295 <<
" but that path contains an object of type " << key->
GetClassName();
311 if (path.find_first_of(
';') != std::string_view::npos) {
313 R__FAIL(
"Invalid object path '" + path +
314 "': character ';' is used to specify an object cycle, which only makes sense when reading."));
320 if (!
fFile->IsWritable())
331 return ROOT::Join(
"/", std::span<const std::string>{
tokens.data(), idx + 1});
359 existing->GetClassName() +
" with another object of type " +
cls->GetName()));
374 return fFile->Write();
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
static const char * ToString(ENameCycleError err)
static ENameCycleError DecodeNumericCycle(const char *str, std::optional< std::int16_t > &out)
static void CheckExtension(std::string_view path)
static RNameCycleResult DecodeNameCycle(std::string_view nameCycleRaw)
static std::string ValidateAndNormalizePath(std::string &path)
This function first validates, then normalizes the given path in place.
#define R__LOG_WARNING(...)
#define R__LOG_ERROR(...)
const char Option_t
Option string (const char)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
An interface to read from, or write to, a ROOT file, as well as performing other common operations.
void Close()
Flushes the RFile if needed and closes it, disallowing any further reading or writing.
static constexpr int kMaxPathNesting
static std::unique_ptr< RFile > Recreate(std::string_view path)
Opens the file for reading/writing, overwriting it if it already exists.
std::unique_ptr< TFile > fFile
size_t Flush()
Writes all objects and the file structure to disk.
static std::unique_ptr< RFile > Update(std::string_view path)
Opens the file for updating, creating a new one if it doesn't exist.
TKey * GetTKey(std::string_view path) const
Given path, returns the TKey corresponding to the object at that path (assuming the path is fully spl...
static std::unique_ptr< RFile > Open(std::string_view path)
Opens the file for reading.
void PutUntyped(std::string_view path, const std::type_info &type, const void *obj, std::uint32_t flags)
Writes obj to file, without taking its ownership.
RFile(std::unique_ptr< TFile > file)
void * GetUntyped(std::string_view path, const std::type_info &type) const
Gets object path from the file and returns an owning pointer to it.
Base class for all ROOT issued exceptions.
A log configuration for a channel, e.g.
TClass instances represent classes, structs and namespaces in the ROOT type system.
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
TDirectory::TContext keeps track and restore the current directory.
Describe directory structure in memory.
virtual TDirectory * GetDirectory(const char *namecycle, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory using apath.
virtual Int_t WriteObjectAny(const void *, const char *, const char *, Option_t *="", Int_t=0)
virtual TKey * GetKey(const char *, Short_t=9999) const
virtual TKey * FindKey(const char *) const
virtual TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE)
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Book space in a file, create I/O buffers, to fill them, (un)compress them.
virtual const char * GetClassName() const
virtual void * ReadObjectAny(const TClass *expectedClass)
To read an object (non deriving from TObject) from the file.
Short_t GetCycle() const
Return cycle number associated to this key.
ROOT::RLogChannel & RFileLog()
bool EndsWith(std::string_view string, std::string_view suffix)
std::string Join(const std::string &sep, StringCollection_t &&strings)
Concatenate a list of strings with a separator.
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.