Merge pull request #25808 from CastagnaIT/fix_url_parse
[xbmc.git] / xbmc / filesystem / PipeFile.cpp
blobf732843c89bef1d7253232291a8ce52d7f676430
1 /*
2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "PipeFile.h"
11 #include "PipesManager.h"
12 #include "URL.h"
14 #include <mutex>
16 using namespace XFILE;
18 CPipeFile::CPipeFile() : m_pipe(NULL)
22 CPipeFile::~CPipeFile()
24 Close();
27 int64_t CPipeFile::GetPosition()
29 return m_pos;
32 int64_t CPipeFile::GetLength()
34 return m_length;
37 void CPipeFile::SetLength(int64_t len)
39 m_length = len;
42 bool CPipeFile::Open(const CURL& url)
44 std::string name = url.Get();
45 m_pipe = PipesManager::GetInstance().OpenPipe(name);
46 if (m_pipe)
47 m_pipe->AddListener(this);
48 return (m_pipe != NULL);
51 bool CPipeFile::Exists(const CURL& url)
53 std::string name = url.Get();
54 return PipesManager::GetInstance().Exists(name);
57 int CPipeFile::Stat(const CURL& url, struct __stat64* buffer)
59 return -1;
62 int CPipeFile::Stat(struct __stat64* buffer)
64 if (!buffer)
65 return -1;
67 *buffer = {};
68 buffer->st_size = m_length;
69 return 0;
72 ssize_t CPipeFile::Read(void* lpBuf, size_t uiBufSize)
74 if (!m_pipe)
75 return -1;
77 if (uiBufSize > SSIZE_MAX)
78 uiBufSize = SSIZE_MAX;
80 return m_pipe->Read((char *)lpBuf,(int)uiBufSize);
83 ssize_t CPipeFile::Write(const void* lpBuf, size_t uiBufSize)
85 if (!m_pipe)
86 return -1;
88 // m_pipe->Write return bool. either all was written or not.
89 return m_pipe->Write((const char *)lpBuf,uiBufSize) ? uiBufSize : -1;
92 void CPipeFile::SetEof()
94 if (!m_pipe)
95 return ;
96 m_pipe->SetEof();
99 bool CPipeFile::IsEof()
101 if (!m_pipe)
102 return true;
103 return m_pipe->IsEof();
106 bool CPipeFile::IsEmpty()
108 if (!m_pipe)
109 return true;
110 return m_pipe->IsEmpty();
113 int64_t CPipeFile::Seek(int64_t iFilePosition, int iWhence)
115 return -1;
118 void CPipeFile::Close()
120 if (m_pipe)
122 m_pipe->RemoveListener(this);
123 PipesManager::GetInstance().ClosePipe(m_pipe);
125 m_pipe = NULL;
128 bool CPipeFile::IsClosed()
130 return (m_pipe == NULL);
133 void CPipeFile::Flush()
135 if (m_pipe)
136 m_pipe->Flush();
139 bool CPipeFile::OpenForWrite(const CURL& url, bool bOverWrite)
141 std::string name = url.Get();
143 m_pipe = PipesManager::GetInstance().CreatePipe(name);
144 if (m_pipe)
145 m_pipe->AddListener(this);
146 return (m_pipe != NULL);
149 bool CPipeFile::Delete(const CURL& url)
151 return false;
154 bool CPipeFile::Rename(const CURL& url, const CURL& urlnew)
156 return false;
159 int CPipeFile::IoControl(EIoControl, void* param)
161 return -1;
164 std::string CPipeFile::GetName() const
166 if (!m_pipe)
167 return "";
168 return m_pipe->GetName();
171 void CPipeFile::OnPipeOverFlow()
173 std::unique_lock<CCriticalSection> lock(m_lock);
174 for (size_t l=0; l<m_listeners.size(); l++)
175 m_listeners[l]->OnPipeOverFlow();
178 int64_t CPipeFile::GetAvailableRead()
180 return m_pipe->GetAvailableRead();
183 void CPipeFile::OnPipeUnderFlow()
185 for (size_t l=0; l<m_listeners.size(); l++)
186 m_listeners[l]->OnPipeUnderFlow();
189 void CPipeFile::AddListener(IPipeListener *l)
191 std::unique_lock<CCriticalSection> lock(m_lock);
192 for (size_t i=0; i<m_listeners.size(); i++)
194 if (m_listeners[i] == l)
195 return;
197 m_listeners.push_back(l);
200 void CPipeFile::RemoveListener(IPipeListener *l)
202 std::unique_lock<CCriticalSection> lock(m_lock);
203 std::vector<XFILE::IPipeListener *>::iterator i = m_listeners.begin();
204 while(i != m_listeners.end())
206 if ( (*i) == l)
207 i = m_listeners.erase(i);
208 else
209 ++i;
213 void CPipeFile::SetOpenThreshold(int threshold)
215 m_pipe->SetOpenThreshold(threshold);