1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/**********************************************************************
*
* Project: CPL - Common Portability Library
* Purpose: Implement VSI large file api for stdout
* Author: Even Rouault, <even dot rouault at spatialys.com>
*
**********************************************************************
* Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "cpl_vsi.h"
#include <cstddef>
#include <cstdio>
#include <cstring>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "cpl_error.h"
#include "cpl_vsi_virtual.h"
#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif
static VSIWriteFunction pWriteFunction = fwrite;
static FILE *pWriteStream = stdout;
/************************************************************************/
/* VSIStdoutSetRedirection() */
/************************************************************************/
/** Set an alternative write function and output file handle instead of
* fwrite() / stdout.
*
* @param pFct Function with same signature as fwrite()
* @param stream File handle on which to output. Passed to pFct.
*
* @since GDAL 2.0
*/
void VSIStdoutSetRedirection(VSIWriteFunction pFct, FILE *stream)
{
pWriteFunction = pFct;
pWriteStream = stream;
}
//! @cond Doxygen_Suppress
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
class VSIStdoutFilesystemHandler final : public VSIFilesystemHandler
{
CPL_DISALLOW_COPY_ASSIGN(VSIStdoutFilesystemHandler)
public:
VSIStdoutFilesystemHandler() = default;
VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
bool bSetError,
CSLConstList /* papszOptions */) override;
int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags) override;
bool SupportsRead(const char * /* pszPath */) override
{
return false;
}
};
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutHandle */
/* ==================================================================== */
/************************************************************************/
class VSIStdoutHandle final : public VSIVirtualHandle
{
CPL_DISALLOW_COPY_ASSIGN(VSIStdoutHandle)
vsi_l_offset m_nOffset = 0;
public:
VSIStdoutHandle() = default;
~VSIStdoutHandle() override = default;
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
int Eof() override;
int Flush() override;
int Close() override;
};
/************************************************************************/
/* Seek() */
/************************************************************************/
int VSIStdoutHandle::Seek(vsi_l_offset nOffset, int nWhence)
{
if (nOffset == 0 && (nWhence == SEEK_END || nWhence == SEEK_CUR))
return 0;
if (nWhence == SEEK_SET && nOffset == Tell())
return 0;
CPLError(CE_Failure, CPLE_NotSupported, "Seek() unsupported on /vsistdout");
return -1;
}
/************************************************************************/
/* Tell() */
/************************************************************************/
vsi_l_offset VSIStdoutHandle::Tell()
{
return m_nOffset;
}
/************************************************************************/
/* Flush() */
/************************************************************************/
int VSIStdoutHandle::Flush()
{
if (pWriteStream == stdout)
return fflush(stdout);
else
return 0;
}
/************************************************************************/
/* Read() */
/************************************************************************/
size_t VSIStdoutHandle::Read(void * /* pBuffer */, size_t /* nSize */,
size_t /* nCount */)
{
CPLError(CE_Failure, CPLE_NotSupported, "Read() unsupported on /vsistdout");
return 0;
}
/************************************************************************/
/* Write() */
/************************************************************************/
size_t VSIStdoutHandle::Write(const void *pBuffer, size_t nSize, size_t nCount)
{
size_t nRet = pWriteFunction(pBuffer, nSize, nCount, pWriteStream);
m_nOffset += nSize * nRet;
return nRet;
}
/************************************************************************/
/* Eof() */
/************************************************************************/
int VSIStdoutHandle::Eof()
{
return 0;
}
/************************************************************************/
/* Close() */
/************************************************************************/
int VSIStdoutHandle::Close()
{
return Flush();
}
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* Open() */
/************************************************************************/
VSIVirtualHandle *
VSIStdoutFilesystemHandler::Open(const char * /* pszFilename */,
const char *pszAccess, bool /* bSetError */,
CSLConstList /* papszOptions */)
{
if (strchr(pszAccess, 'r') != nullptr || strchr(pszAccess, '+') != nullptr)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Read or update mode not supported on /vsistdout");
return nullptr;
}
#ifdef WIN32
if (strchr(pszAccess, 'b') != nullptr)
setmode(fileno(stdout), O_BINARY);
#endif
return new VSIStdoutHandle;
}
/************************************************************************/
/* Stat() */
/************************************************************************/
int VSIStdoutFilesystemHandler::Stat(const char * /* pszFilename */,
VSIStatBufL *pStatBuf, int /* nFlags */)
{
memset(pStatBuf, 0, sizeof(VSIStatBufL));
return -1;
}
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutRedirectFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
class VSIStdoutRedirectFilesystemHandler final : public VSIFilesystemHandler
{
public:
VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
bool bSetError,
CSLConstList /* papszOptions */) override;
int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags) override;
bool SupportsRead(const char * /* pszPath */) override
{
return false;
}
};
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutRedirectHandle */
/* ==================================================================== */
/************************************************************************/
class VSIStdoutRedirectHandle final : public VSIVirtualHandle
{
VSIVirtualHandle *m_poHandle = nullptr;
CPL_DISALLOW_COPY_ASSIGN(VSIStdoutRedirectHandle)
public:
explicit VSIStdoutRedirectHandle(VSIVirtualHandle *poHandle);
~VSIStdoutRedirectHandle() override;
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
int Eof() override;
int Flush() override;
int Close() override;
};
/************************************************************************/
/* VSIStdoutRedirectHandle() */
/************************************************************************/
VSIStdoutRedirectHandle::VSIStdoutRedirectHandle(VSIVirtualHandle *poHandle)
: m_poHandle(poHandle)
{
}
/************************************************************************/
/* ~VSIStdoutRedirectHandle() */
/************************************************************************/
VSIStdoutRedirectHandle::~VSIStdoutRedirectHandle()
{
delete m_poHandle;
}
/************************************************************************/
/* Seek() */
/************************************************************************/
int VSIStdoutRedirectHandle::Seek(vsi_l_offset /* nOffset */, int /* nWhence */)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Seek() unsupported on /vsistdout_redirect");
return -1;
}
/************************************************************************/
/* Tell() */
/************************************************************************/
vsi_l_offset VSIStdoutRedirectHandle::Tell()
{
return m_poHandle->Tell();
}
/************************************************************************/
/* Flush() */
/************************************************************************/
int VSIStdoutRedirectHandle::Flush()
{
return m_poHandle->Flush();
}
/************************************************************************/
/* Read() */
/************************************************************************/
size_t VSIStdoutRedirectHandle::Read(void * /* pBuffer */, size_t /* nSize */,
size_t /* nCount */)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Read() unsupported on /vsistdout_redirect");
return 0;
}
/************************************************************************/
/* Write() */
/************************************************************************/
size_t VSIStdoutRedirectHandle::Write(const void *pBuffer, size_t nSize,
size_t nCount)
{
return m_poHandle->Write(pBuffer, nSize, nCount);
}
/************************************************************************/
/* Eof() */
/************************************************************************/
int VSIStdoutRedirectHandle::Eof()
{
return m_poHandle->Eof();
}
/************************************************************************/
/* Close() */
/************************************************************************/
int VSIStdoutRedirectHandle::Close()
{
return m_poHandle->Close();
}
/************************************************************************/
/* ==================================================================== */
/* VSIStdoutRedirectFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* Open() */
/************************************************************************/
VSIVirtualHandle *VSIStdoutRedirectFilesystemHandler::Open(
const char *pszFilename, const char *pszAccess, bool /* bSetError */,
CSLConstList /* papszOptions */)
{
if (strchr(pszAccess, 'r') != nullptr || strchr(pszAccess, '+') != nullptr)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Read or update mode not supported on /vsistdout_redirect");
return nullptr;
}
VSIVirtualHandle *poHandle = reinterpret_cast<VSIVirtualHandle *>(
VSIFOpenL(pszFilename + strlen("/vsistdout_redirect/"), pszAccess));
if (poHandle == nullptr)
return nullptr;
return new VSIStdoutRedirectHandle(poHandle);
}
/************************************************************************/
/* Stat() */
/************************************************************************/
int VSIStdoutRedirectFilesystemHandler::Stat(const char * /* pszFilename */,
VSIStatBufL *pStatBuf,
int /* nFlags */)
{
memset(pStatBuf, 0, sizeof(VSIStatBufL));
return -1;
}
//! @endcond
/************************************************************************/
/* VSIInstallStdoutHandler() */
/************************************************************************/
/*!
\brief Install /vsistdout/ file system handler
A special file handler is installed that allows writing to the standard
output stream.
The file operations available are of course limited to Write().
A variation of this file system exists as the /vsistdout_redirect/ file
system handler, where the output function can be defined with
VSIStdoutSetRedirection().
\verbatim embed:rst
See :ref:`/vsistdout/ documentation <vsistdout>`
\endverbatim
@since GDAL 1.8.0
*/
void VSIInstallStdoutHandler()
{
VSIFileManager::InstallHandler("/vsistdout/",
new VSIStdoutFilesystemHandler);
VSIFileManager::InstallHandler("/vsistdout_redirect/",
new VSIStdoutRedirectFilesystemHandler);
}
|