comparison Framework/Common/IResult.cpp @ 298:8f655f308de9

added IResult::Print()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Jul 2021 14:39:27 +0200
parents
children 16aac0287485
comparison
equal deleted inserted replaced
297:86265ef5f3e3 298:8f655f308de9
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "IResult.h"
23
24 #include "Utf8StringValue.h"
25
26 #include <Compatibility.h>
27 #include <OrthancException.h>
28
29 #include <cassert>
30 #include <list>
31 #include <vector>
32 #include <ostream>
33
34 namespace OrthancDatabases
35 {
36 static void PrintSeparators(std::ostream& stream,
37 char c,
38 size_t count)
39 {
40 for (size_t i = 0; i < count; i++)
41 {
42 stream << c;
43 }
44 }
45
46
47 static void PrintHeader(std::ostream& stream,
48 const std::vector<size_t>& maxWidth)
49 {
50 for (size_t i = 0; i < maxWidth.size(); i++)
51 {
52 stream << '+';
53 PrintSeparators(stream, '-', maxWidth[i] + 2);
54 }
55
56 stream << '+' << std::endl;
57 }
58
59
60 void IResult::Print(std::ostream& stream,
61 IResult& result)
62 {
63 typedef std::list< std::vector<std::string> > Table;
64
65 Table table;
66
67 const size_t columns = result.GetFieldsCount();
68
69 std::vector<size_t> maxWidth(columns);
70
71 while (!result.IsDone())
72 {
73 table.push_back(std::vector<std::string>(columns));
74
75 for (size_t i = 0; i < columns; i++)
76 {
77 std::string value;
78
79 try
80 {
81 std::unique_ptr<IValue> converted(
82 result.GetField(i).Convert(ValueType_Utf8String));
83 value = dynamic_cast<Utf8StringValue&>(*converted).GetContent();
84 }
85 catch (Orthanc::OrthancException&)
86 {
87 value = "?";
88 }
89
90 if (value.size() > maxWidth[i])
91 {
92 maxWidth[i] = value.size();
93 }
94
95 table.back() [i] = value;
96 }
97
98 result.Next();
99 }
100
101 PrintHeader(stream, maxWidth);
102
103 for (Table::const_iterator it = table.begin(); it != table.end(); ++it)
104 {
105 assert(it->size() == maxWidth.size());
106
107 for (size_t i = 0; i < it->size(); i++)
108 {
109 const std::string& value = (*it) [i];
110
111 stream << "| " << value << ' ';
112
113 for (size_t j = value.size(); j < maxWidth[i]; j++)
114 {
115 stream << ' ';
116 }
117 }
118
119 stream << '|' << std::endl;
120 }
121
122 PrintHeader(stream, maxWidth);
123 }
124 }