ATLAS Offline Software
Loading...
Searching...
No Matches
XMLParserUtilities.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
3 */
10
11#ifndef XMLCoreParser_XMLParserUtilities_H
12#define XMLCoreParser_XMLParserUtilities_H
13
14#include <expat.h>
15#include <cstdlib> //getenv
16#include <string>
17#include <source_location>
18#include <memory>
19#include <iostream>
20
21
22namespace XmlParser{
24 std::string
25 xmlFileName(const std::string & fname, const std::string & prefix="");
26
28 inline bool
30 static const bool enabled = (std::getenv("XMLDEBUG") != nullptr);
31 return enabled;
32 }
33
35 inline std::string
36 label(const std::source_location loc = std::source_location::current()){
37 return loc.function_name();
38 }
39
42 void operator()(XML_Parser p) const noexcept {
43 if (p) {
44 XML_ParserFree(p);
45 }
46 }
47 };
48
49 using XMLParserPtr = std::unique_ptr<std::remove_pointer_t<XML_Parser>, XMLParserDeleter>;
50
53 XML_Parser p = XML_ParserCreate(nullptr);
54 if (!p) {
55 std::cout << "ExpatCoreParser::Couldn't allocate memory for parser" << std::endl;
56 std::abort();
57 }
58 return XMLParserPtr{p};
59 }
60
61 inline std::string
62 rtrim(const XML_Char* s, int len){
63 while (len > 0 && s[len - 1] == '\n') {
64 --len;
65 }
66 return (len > 0) ? std::string{s, static_cast<std::size_t>(len)} : std::string{};
67 }
68}
69
70
71#endif
std::string xmlFileName(const std::string &fname, const std::string &prefix)
find the xml file locally or on the datapath, return the full filename
bool debug_enabled()
true if XML debug mode is enabled in the environment
XMLParserPtr make_parser()
Create an RAII XMLParser pointer.
std::unique_ptr< std::remove_pointer_t< XML_Parser >, XMLParserDeleter > XMLParserPtr
RAII XMLParser pointer.
std::string label(const std::source_location loc=std::source_location::current())
report function name in a string, for debugging
std::string rtrim(const XML_Char *s, int len)
Trim newline from end of a XML_Char * , return the trimmed string as std::string.
deleter for a unique_ptr<XML_Parser>
void operator()(XML_Parser p) const noexcept