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