Merge pull request #5095 from koying/fixdroidappcrash
[vuplus_xbmc] / xbmc / filesystem / SAPFile.cpp
1 /*
2  *  SAP-Announcement Support for XBMC
3  *      Copyright (c) 2008 elupus (Joakim Plate)
4  *      Copyright (C) 2008-2013 Team XBMC
5  *      http://xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "SAPFile.h"
24 #include "SAPDirectory.h"
25 #include "threads/SingleLock.h"
26 #include "URL.h"
27
28 #include <sys/stat.h>
29 #include <vector>
30
31 using namespace std;
32 using namespace XFILE;
33
34 //////////////////////////////////////////////////////////////////////
35 // Construction/Destruction
36 //////////////////////////////////////////////////////////////////////
37
38 CSAPFile::CSAPFile()
39 {}
40
41 CSAPFile::~CSAPFile()
42 {
43 }
44
45 bool CSAPFile::Open(const CURL& url)
46 {
47   CStdString path = url.Get();
48
49   CSingleLock lock(g_sapsessions.m_section);
50   for(vector<CSAPSessions::CSession>::iterator it = g_sapsessions.m_sessions.begin(); it != g_sapsessions.m_sessions.end(); it++)
51   {
52     if(it->path == path)
53     {
54       m_len = it->payload.length();
55       m_stream.str(it->payload);
56       m_stream.seekg(0);
57       break;
58     }
59   }
60   if(m_len == 0)
61     return false;
62
63   return true;
64 }
65
66 bool CSAPFile::Exists(const CURL& url)
67 {
68   CStdString path = url.Get();
69
70   CSingleLock lock(g_sapsessions.m_section);
71   for(vector<CSAPSessions::CSession>::iterator it = g_sapsessions.m_sessions.begin(); it != g_sapsessions.m_sessions.end(); it++)
72   {
73     if(it->path == path)
74       return true;
75   }
76   return false;
77 }
78
79 int CSAPFile::Stat(const CURL& url, struct __stat64* buffer)
80 {
81   CStdString path = url.Get();
82
83   if(path == "smb://")
84   {
85     if(buffer)
86     {
87       memset(buffer, 0, sizeof(struct __stat64));
88       buffer->st_mode = _S_IFDIR;
89     }
90     return 0;
91   }
92
93
94   CSingleLock lock(g_sapsessions.m_section);
95   for(vector<CSAPSessions::CSession>::iterator it = g_sapsessions.m_sessions.begin(); it != g_sapsessions.m_sessions.end(); it++)
96   {
97     if(it->path == path)
98     {
99       if(buffer)
100       {
101         memset(buffer, 0, sizeof(*buffer));
102
103         buffer->st_size = it->payload.size();
104         buffer->st_mode = _S_IFREG;
105       }
106       return 0;
107     }
108   }
109   return -1;
110
111 }
112
113
114 unsigned int CSAPFile::Read(void *lpBuf, int64_t uiBufSize)
115 {
116   return (unsigned int)m_stream.readsome((char*)lpBuf, (streamsize)uiBufSize);
117 }
118
119 void CSAPFile::Close()
120 {
121 }
122
123 //*********************************************************************************************
124 int64_t CSAPFile::Seek(int64_t iFilePosition, int iWhence)
125 {
126   switch (iWhence)
127   {
128     case SEEK_SET:
129       m_stream.seekg((int)iFilePosition, ios_base::beg);
130       break;
131     case SEEK_CUR:
132       m_stream.seekg((int)iFilePosition, ios_base::cur);
133       break;
134     case SEEK_END:
135       m_stream.seekg((int)iFilePosition, ios_base::end);
136       break;
137     default:
138       return -1;
139   }
140   return m_stream.tellg();
141 }
142
143 //*********************************************************************************************
144 int64_t CSAPFile::GetLength()
145 {
146   return m_len;
147 }
148
149 //*********************************************************************************************
150 int64_t CSAPFile::GetPosition()
151 {
152   return m_stream.tellg();
153 }
154
155 bool CSAPFile::Delete(const CURL& url)
156 {
157   CStdString path = url.Get();
158
159   CSingleLock lock(g_sapsessions.m_section);
160   for(vector<CSAPSessions::CSession>::iterator it = g_sapsessions.m_sessions.begin(); it != g_sapsessions.m_sessions.end(); it++)
161   {
162     if(it->path == path)
163     {
164       g_sapsessions.m_sessions.erase(it);
165       return true;
166     }
167   }
168   return false;
169 }
170
171 bool CSAPFile::Rename(const CURL& url, const CURL& urlnew)
172 {
173   return false;
174 }
175
176 int CSAPFile::IoControl(EIoControl request, void* param)
177 {
178   if(request == IOCTRL_SEEK_POSSIBLE)
179     return 1;
180
181   return -1;
182 }