/src/dcmtk/dcmdata/libsrc/dcistrms.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: DcmStdinStream and related classes, |
19 | | * implements streamed input from stdin. |
20 | | * |
21 | | */ |
22 | | |
23 | | #include "dcmtk/config/osconfig.h" |
24 | | #include "dcmtk/dcmdata/dcistrms.h" |
25 | | #include "dcmtk/dcmdata/dcerror.h" |
26 | | |
27 | | BEGIN_EXTERN_C |
28 | | #include <fcntl.h> |
29 | | #ifdef HAVE_IO_H |
30 | | #include <io.h> |
31 | | #endif |
32 | | END_EXTERN_C |
33 | | |
34 | | // size of the buffer we use to read from stdin |
35 | 0 | #define DCMSTDINSTREAMBUFSIZE 32768 |
36 | | |
37 | | DcmStdinStream::DcmStdinStream() |
38 | 0 | : DcmInputStream(&producer_) // safe because DcmInputStream only stores pointer |
39 | 0 | , producer_() |
40 | 0 | , buf_(new unsigned char[DCMSTDINSTREAMBUFSIZE]) |
41 | 0 | { |
42 | | #ifdef _WIN32 |
43 | | // Set "stdin" to binary mode |
44 | | int result = setmode(fileno(stdin), O_BINARY); |
45 | | if (result == -1) DCMDATA_ERROR("Failed to switch stdin to binary mode"); |
46 | | #endif |
47 | 0 | } |
48 | | |
49 | | DcmStdinStream::~DcmStdinStream() |
50 | 0 | { |
51 | 0 | delete[] buf_; |
52 | 0 | } |
53 | | |
54 | | DcmInputStreamFactory *DcmStdinStream::newFactory() const |
55 | 0 | { |
56 | | // we don't support delayed loading from stdin streams |
57 | 0 | return NULL; |
58 | 0 | } |
59 | | |
60 | | void DcmStdinStream::fillBuffer() |
61 | 0 | { |
62 | | // if there are a few bytes left in the previous buffer, make the |
63 | | // buffer producer make a backup copy |
64 | 0 | producer_.releaseBuffer(); |
65 | | |
66 | | // read the next block from stdin |
67 | 0 | size_t numBytes = fread(buf_, 1, DCMSTDINSTREAMBUFSIZE, stdin); |
68 | | |
69 | | // make the buffer available to the buffer producer |
70 | 0 | producer_.setBuffer(buf_, OFstatic_cast(offile_off_t, numBytes)); |
71 | | |
72 | | // check if we are at the end of stream, and if so, notify the buffer producer |
73 | 0 | if (feof(stdin)) producer_.setEos(); |
74 | | |
75 | | // if there is a compression filter, the following call will |
76 | | // cause it to feed the compression engine with data from the |
77 | | // new buffer. |
78 | 0 | skip(0); |
79 | 0 | } |
80 | | |