SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
sddspp_read_demo.cc
Go to the documentation of this file.
1/**
2 * @file sddspp_read_demo.cc
3 * @brief Demonstrates generic SDDS++ layout and page inspection.
4 *
5 * This is the C++ counterpart of SDDSlib/demo/sdds_read_demo.c. It reads any
6 * supported SDDS file and prints its parameters, arrays, and columns.
7 *
8 * @copyright
9 * - (c) 2026 The University of Chicago
10 *
11 * @license
12 * This file is distributed under the terms of the Software License Agreement
13 * found in the file LICENSE included with this distribution.
14 */
15
16#include "SDDS.hpp"
17
18#include <exception>
19#include <filesystem>
20#include <iomanip>
21#include <iostream>
22#include <string>
23#include <type_traits>
24#include <variant>
25
26namespace {
27
28/** @brief Prints one value from an SDDS variant. */
29template <class T>
30void printValue(const T &value) {
31 if constexpr (std::is_floating_point_v<T>)
32 std::cout << std::scientific << std::setprecision(15) << value;
33 else
34 std::cout << value;
35}
36
37/** @brief Prints an SDDS scalar without switching on a numeric type ID. */
38void printScalar(const sdds::Scalar &value) {
39 std::visit([](const auto &item) { printValue(item); }, value);
40}
41
42/** @brief Prints all elements in an SDDS values vector. */
43void printValues(const sdds::Values &values) {
44 std::visit([](const auto &items) {
45 for (const auto &item : items) {
46 std::cout << " ";
47 printValue(item);
48 std::cout << '\n';
49 }
50 }, values);
51}
52
53/** @brief Prints a decoded page using its immutable layout definitions. */
54void printPage(const sdds::Page &page) {
55 std::cout << "Page " << page.number() << " (" << page.rowCount() << " rows)\n";
56
57 for (const auto &definition : page.layout().parameters) {
58 std::cout << " parameter " << definition.name << " ("
59 << sdds::typeName(definition.type) << ") = ";
60 printScalar(page.parameter(definition.name));
61 std::cout << '\n';
62 }
63
64 for (const auto &definition : page.layout().arrays) {
65 const auto &array = page.array(definition.name);
66 std::cout << " array " << definition.name << " ("
67 << sdds::typeName(definition.type) << ") dimensions=";
68 for (std::size_t index = 0; index < array.dimensions.size(); ++index) {
69 if (index)
70 std::cout << 'x';
71 std::cout << array.dimensions[index];
72 }
73 std::cout << '\n';
74 printValues(array.values);
75 }
76
77 for (const auto &definition : page.layout().columns) {
78 std::cout << " column " << definition.name << " ("
79 << sdds::typeName(definition.type) << ")\n";
80 printValues(page.column(definition.name));
81 }
82}
83
84} // namespace
85
86/**
87 * @brief Reads and displays an SDDS file.
88 * @param argc Argument count.
89 * @param argv Argument vector containing the input filename.
90 * @return Zero on success, nonzero on failure.
91 */
92int main(int argc, char **argv) {
93 if (argc != 2) {
94 std::cerr << "usage: " << argv[0] << " input.sdds\n";
95 return 1;
96 }
97
98 const std::filesystem::path input = argv[1];
99 try {
100 auto reader = sdds::Reader::open(input);
101 const auto &layout = reader.layout();
102 std::cout << "SDDS version " << layout.version << ", "
103 << (layout.data.mode == sdds::DataMode::Ascii ? "ASCII" : "binary")
104 << " data\n";
105 while (auto page = reader.next())
106 printPage(*page);
107 reader.close();
108 } catch (const std::exception &error) {
109 std::cerr << "Unable to read " << input << ": " << error.what() << '\n';
110 return 1;
111 }
112 return 0;
113}
int main(int argc, char **argv)
Reads and displays an SDDS file.