/src/dcmtk/dcmdata/libsrc/dcostrms.cc
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 2002-2025, OFFIS e.V. |
4 | | * All rights reserved. See COPYRIGHT file for details. |
5 | | * |
6 | | * This software and supporting documentation were developed by |
7 | | * |
8 | | * OFFIS e.V. |
9 | | * R&D Division Health |
10 | | * Escherweg 2 |
11 | | * D-26121 Oldenburg, Germany |
12 | | * |
13 | | * |
14 | | * Module: dcmdata |
15 | | * |
16 | | * Author: Jasper den Otter, Marco Eichelberg |
17 | | * |
18 | | * Purpose: DcmStdoutStream and related classes, |
19 | | * implements streamed output to stdout. |
20 | | * |
21 | | */ |
22 | | |
23 | | #include "dcmtk/config/osconfig.h" |
24 | | #include "dcmtk/dcmdata/dcostrms.h" |
25 | | #include "dcmtk/dcmdata/dcerror.h" |
26 | | #include "dcmtk/ofstd/ofconsol.h" |
27 | | |
28 | | BEGIN_EXTERN_C |
29 | | #include <fcntl.h> |
30 | | #ifdef HAVE_IO_H |
31 | | #include <io.h> |
32 | | #endif |
33 | | END_EXTERN_C |
34 | | |
35 | | |
36 | | DcmStdoutConsumer::DcmStdoutConsumer(const OFFilename & /* filename */) |
37 | 0 | : DcmConsumer() |
38 | 0 | , file_() |
39 | 0 | , status_(EC_Normal) |
40 | 0 | { |
41 | | #ifdef _WIN32 |
42 | | // Set "stdout" to binary mode |
43 | | int result = setmode(fileno(stdout), O_BINARY); |
44 | | if (result == -1) DCMDATA_ERROR("Failed to switch stdout to binary mode"); |
45 | | #endif |
46 | 0 | } |
47 | | |
48 | | DcmStdoutConsumer::DcmStdoutConsumer(FILE *file) |
49 | 0 | : DcmConsumer() |
50 | 0 | , file_(file) |
51 | 0 | , status_(EC_Normal) |
52 | 0 | { |
53 | 0 | } |
54 | | |
55 | | DcmStdoutConsumer::~DcmStdoutConsumer() |
56 | 0 | { |
57 | 0 | } |
58 | | |
59 | | OFBool DcmStdoutConsumer::good() const |
60 | 0 | { |
61 | 0 | return status_.good(); |
62 | 0 | } |
63 | | |
64 | | OFCondition DcmStdoutConsumer::status() const |
65 | 0 | { |
66 | 0 | return status_; |
67 | 0 | } |
68 | | |
69 | | OFBool DcmStdoutConsumer::isFlushed() const |
70 | 0 | { |
71 | 0 | return OFTrue; |
72 | 0 | } |
73 | | |
74 | | offile_off_t DcmStdoutConsumer::avail() const |
75 | 0 | { |
76 | | // since we cannot report "unlimited", let's claim that we can still write 2GB. |
77 | | // Note that offile_off_t is a signed type. |
78 | 0 | return 2147483647L; |
79 | 0 | } |
80 | | |
81 | | offile_off_t DcmStdoutConsumer::write(const void *buf, offile_off_t buflen) |
82 | 0 | { |
83 | 0 | if (buflen && buf) |
84 | 0 | return OFstatic_cast(offile_off_t, fwrite(buf, 1, OFstatic_cast(size_t, buflen), stdout)); |
85 | 0 | else return 0; |
86 | 0 | } |
87 | | |
88 | | void DcmStdoutConsumer::flush() |
89 | 0 | { |
90 | 0 | fflush(stdout); |
91 | 0 | } |
92 | | |
93 | | /* ======================================================================= */ |
94 | | |
95 | | DcmStdoutStream::DcmStdoutStream(const OFFilename &filename) |
96 | 0 | : DcmOutputStream(&consumer_) // safe because DcmStdoutStream only stores pointer |
97 | 0 | , consumer_(filename) |
98 | 0 | { |
99 | 0 | } |
100 | | |
101 | | DcmStdoutStream::DcmStdoutStream(FILE *file) |
102 | 0 | : DcmOutputStream(&consumer_) // safe because DcmStdoutStream only stores pointer |
103 | 0 | , consumer_(file) |
104 | 0 | { |
105 | 0 | } |
106 | | |
107 | | DcmStdoutStream::~DcmStdoutStream() |
108 | 0 | { |
109 | | // last attempt to flush stream before file is closed |
110 | 0 | flush(); |
111 | | #ifdef DEBUG |
112 | | if (! isFlushed()) |
113 | | { |
114 | | DCMDATA_WARN("closing unflushed DcmStdoutStream, loss of data!"); |
115 | | } |
116 | | #endif |
117 | 0 | } |