1 : /**********************************************************************
2 : * $Id: cpl_vsil_stdout.cpp 20025 2010-07-11 17:13:02Z rouault $
3 : *
4 : * Project: CPL - Common Portability Library
5 : * Purpose: Implement VSI large file api for stdout
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2010, Even Rouault
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "cpl_port.h"
31 : #include "cpl_error.h"
32 : #include "cpl_vsi_virtual.h"
33 :
34 : #include <stdio.h>
35 : #ifdef WIN32
36 : #include <io.h>
37 : #include <fcntl.h>
38 : #endif
39 :
40 : CPL_CVSID("$Id: cpl_vsil_stdout.cpp 20025 2010-07-11 17:13:02Z rouault $");
41 :
42 : /************************************************************************/
43 : /* ==================================================================== */
44 : /* VSIStdoutFilesystemHandler */
45 : /* ==================================================================== */
46 : /************************************************************************/
47 :
48 : class VSIStdoutFilesystemHandler : public VSIFilesystemHandler
49 879 : {
50 : public:
51 : virtual VSIVirtualHandle *Open( const char *pszFilename,
52 : const char *pszAccess);
53 : virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf );
54 : };
55 :
56 : /************************************************************************/
57 : /* ==================================================================== */
58 : /* VSIStdoutHandle */
59 : /* ==================================================================== */
60 : /************************************************************************/
61 :
62 : class VSIStdoutHandle : public VSIVirtualHandle
63 2 : {
64 : public:
65 :
66 : virtual int Seek( vsi_l_offset nOffset, int nWhence );
67 : virtual vsi_l_offset Tell();
68 : virtual size_t Read( void *pBuffer, size_t nSize, size_t nMemb );
69 : virtual size_t Write( const void *pBuffer, size_t nSize, size_t nMemb );
70 : virtual int Eof();
71 : virtual int Flush();
72 : virtual int Close();
73 : };
74 :
75 : /************************************************************************/
76 : /* Seek() */
77 : /************************************************************************/
78 :
79 0 : int VSIStdoutHandle::Seek( vsi_l_offset nOffset, int nWhence )
80 :
81 : {
82 0 : CPLError(CE_Failure, CPLE_NotSupported, "Seek() unsupported on /vsistdout");
83 0 : return -1;
84 : }
85 :
86 : /************************************************************************/
87 : /* Tell() */
88 : /************************************************************************/
89 :
90 0 : vsi_l_offset VSIStdoutHandle::Tell()
91 : {
92 0 : return ftell(stdout);
93 : }
94 :
95 : /************************************************************************/
96 : /* Flush() */
97 : /************************************************************************/
98 :
99 0 : int VSIStdoutHandle::Flush()
100 :
101 : {
102 0 : return fflush( stdout );
103 : }
104 :
105 : /************************************************************************/
106 : /* Read() */
107 : /************************************************************************/
108 :
109 0 : size_t VSIStdoutHandle::Read( void * pBuffer, size_t nSize, size_t nCount )
110 :
111 : {
112 0 : CPLError(CE_Failure, CPLE_NotSupported, "Read() unsupported on /vsistdout");
113 0 : return 0;
114 : }
115 :
116 : /************************************************************************/
117 : /* Write() */
118 : /************************************************************************/
119 :
120 : size_t VSIStdoutHandle::Write( const void * pBuffer, size_t nSize,
121 3 : size_t nCount )
122 :
123 : {
124 3 : return fwrite(pBuffer, nSize, nCount, stdout);
125 : }
126 :
127 : /************************************************************************/
128 : /* Eof() */
129 : /************************************************************************/
130 :
131 0 : int VSIStdoutHandle::Eof()
132 :
133 : {
134 0 : return feof(stdout);
135 : }
136 :
137 : /************************************************************************/
138 : /* Close() */
139 : /************************************************************************/
140 :
141 1 : int VSIStdoutHandle::Close()
142 :
143 : {
144 1 : return 0;
145 : }
146 :
147 : /************************************************************************/
148 : /* ==================================================================== */
149 : /* VSIStdoutFilesystemHandler */
150 : /* ==================================================================== */
151 : /************************************************************************/
152 :
153 : /************************************************************************/
154 : /* Open() */
155 : /************************************************************************/
156 :
157 : VSIVirtualHandle *
158 : VSIStdoutFilesystemHandler::Open( const char *pszFilename,
159 1 : const char *pszAccess )
160 :
161 : {
162 1 : if ( strchr(pszAccess, 'r') != NULL ||
163 : strchr(pszAccess, '+') != NULL )
164 : {
165 : CPLError(CE_Failure, CPLE_NotSupported,
166 0 : "Read or update mode not supported on /vsistdout");
167 0 : return NULL;
168 : }
169 :
170 : #ifdef WIN32
171 : if ( strchr(pszAccess, 'b') != NULL )
172 : setmode( fileno( stdout ), O_BINARY );
173 : #endif
174 :
175 1 : return new VSIStdoutHandle;
176 : }
177 :
178 : /************************************************************************/
179 : /* Stat() */
180 : /************************************************************************/
181 :
182 : int VSIStdoutFilesystemHandler::Stat( const char * pszFilename,
183 0 : VSIStatBufL * pStatBuf )
184 :
185 : {
186 0 : memset( pStatBuf, 0, sizeof(VSIStatBufL) );
187 :
188 0 : return -1;
189 : }
190 :
191 : /************************************************************************/
192 : /* VSIInstallStdoutHandler() */
193 : /************************************************************************/
194 :
195 447 : void VSIInstallStdoutHandler()
196 :
197 : {
198 447 : VSIFileManager::InstallHandler( "/vsistdout/", new VSIStdoutFilesystemHandler );
199 447 : }
|