Line data Source code
1 : /******************************************************************************* 2 : * Project: libopencad 3 : * Purpose: OpenSource CAD formats support library 4 : * Author: Alexandr Borzykh, mush3d at gmail.com 5 : * Author: Dmitry Baryshnikov, bishop.dev@gmail.com 6 : * Language: C++ 7 : ******************************************************************************* 8 : * The MIT License (MIT) 9 : * 10 : * Copyright (c) 2016 Alexandr Borzykh 11 : * Copyright (c) 2016 NextGIS, <info@nextgis.com> 12 : * 13 : * Permission is hereby granted, free of charge, to any person obtaining a copy 14 : * of this software and associated documentation files (the "Software"), to 15 : *deal in the Software without restriction, including without limitation the 16 : *rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 17 : *sell copies of the Software, and to permit persons to whom the Software is 18 : * furnished to do so, subject to the following conditions: 19 : * 20 : * The above copyright notice and this permission notice shall be included in 21 : *all copies or substantial portions of the Software. 22 : * 23 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 : *FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 29 : *IN THE SOFTWARE. 30 : *******************************************************************************/ 31 : #include "vsilfileio.h" 32 : 33 9 : VSILFileIO::VSILFileIO(const char *pszFilePath) 34 9 : : CADFileIO(pszFilePath), m_oFileStream(nullptr) 35 : { 36 9 : } 37 : 38 18 : VSILFileIO::~VSILFileIO() 39 : { 40 9 : if (m_oFileStream) 41 1 : VSILFileIO::Close(); 42 18 : } 43 : 44 0 : const char *VSILFileIO::ReadLine() 45 : { 46 : // TODO: getline 47 0 : return nullptr; 48 : } 49 : 50 0 : bool VSILFileIO::Eof() const 51 : { 52 0 : return VSIFEofL(m_oFileStream) == 0 ? false : true; 53 : } 54 : 55 9 : bool VSILFileIO::Open(int mode) 56 : { 57 : // NOTE: now support only read mode 58 9 : if (mode & OpenMode::out) 59 0 : return false; 60 : 61 9 : std::string sOpenMode = "r"; 62 9 : if (mode & OpenMode::binary) 63 9 : sOpenMode = "rb"; 64 : 65 9 : m_oFileStream = VSIFOpenL(m_soFilePath.c_str(), sOpenMode.c_str()); 66 : 67 9 : if (m_oFileStream != nullptr) 68 9 : m_bIsOpened = true; 69 : 70 9 : return m_bIsOpened; 71 : } 72 : 73 9 : bool VSILFileIO::Close() 74 : { 75 9 : bool bRet = VSIFCloseL(m_oFileStream) == 0 ? true : false; 76 9 : m_oFileStream = nullptr; 77 9 : return bRet; 78 : } 79 : 80 458 : int VSILFileIO::Seek(long offset, CADFileIO::SeekOrigin origin) 81 : { 82 458 : int nWhence = 0; 83 458 : switch (origin) 84 : { 85 8 : case SeekOrigin::CUR: 86 8 : nWhence = SEEK_CUR; 87 8 : break; 88 0 : case SeekOrigin::END: 89 0 : nWhence = SEEK_END; 90 0 : break; 91 450 : case SeekOrigin::BEG: 92 450 : nWhence = SEEK_SET; 93 450 : break; 94 : } 95 : 96 458 : return VSIFSeekL(m_oFileStream, offset, nWhence) == 0 ? 0 : 1; 97 : } 98 : 99 0 : long int VSILFileIO::Tell() 100 : { 101 0 : return static_cast<long>(VSIFTellL(m_oFileStream)); 102 : } 103 : 104 716 : size_t VSILFileIO::Read(void *ptr, size_t size) 105 : { 106 716 : return VSIFReadL(static_cast<char *>(ptr), 1, size, m_oFileStream); 107 : } 108 : 109 0 : size_t VSILFileIO::Write(void * /*ptr*/, size_t /*size*/) 110 : { 111 : // unsupported 112 0 : return 0; 113 : } 114 : 115 26 : void VSILFileIO::Rewind() 116 : { 117 26 : VSIRewindL(m_oFileStream); 118 26 : }