[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / filesystem / BlurayCallback.cpp
blob4849eef1a7865bb158e5223b258e48055367633f
1 /*
2 * Copyright (C) 2005-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 "BlurayCallback.h"
11 #include "FileItem.h"
12 #include "filesystem/Directory.h"
13 #include "filesystem/File.h"
14 #include "utils/URIUtils.h"
15 #include "utils/log.h"
17 using namespace XFILE;
19 struct SDirState
21 CFileItemList list;
22 int curr = 0;
25 void CBlurayCallback::bluray_logger(const char* msg)
27 CLog::Log(LOGDEBUG, "CBlurayCallback::Logger - {}", msg);
30 void CBlurayCallback::dir_close(BD_DIR_H *dir)
32 if (dir)
34 CLog::Log(LOGDEBUG, "CBlurayCallback - Closed dir ({})", fmt::ptr(dir));
35 delete static_cast<SDirState*>(dir->internal);
36 delete dir;
40 BD_DIR_H* CBlurayCallback::dir_open(void *handle, const char* rel_path)
42 std::string strRelPath(rel_path);
43 std::string* strBasePath = reinterpret_cast<std::string*>(handle);
44 if (!strBasePath)
46 CLog::Log(LOGDEBUG, "CBlurayCallback - Error opening dir, null handle!");
47 return nullptr;
50 std::string strDirname = URIUtils::AddFileToFolder(*strBasePath, strRelPath);
51 if (URIUtils::HasSlashAtEnd(strDirname))
52 URIUtils::RemoveSlashAtEnd(strDirname);
54 CLog::Log(LOGDEBUG, "CBlurayCallback - Opening dir {}", CURL::GetRedacted(strDirname));
56 SDirState *st = new SDirState();
57 if (!CDirectory::GetDirectory(strDirname, st->list, "", DIR_FLAG_DEFAULTS))
59 if (!CFile::Exists(strDirname))
60 CLog::Log(LOGDEBUG, "CBlurayCallback - Error opening dir! ({})",
61 CURL::GetRedacted(strDirname));
62 delete st;
63 return nullptr;
66 BD_DIR_H *dir = new BD_DIR_H;
67 dir->close = dir_close;
68 dir->read = dir_read;
69 dir->internal = (void*)st;
71 return dir;
74 int CBlurayCallback::dir_read(BD_DIR_H *dir, BD_DIRENT *entry)
76 SDirState* state = static_cast<SDirState*>(dir->internal);
78 if (state->curr >= state->list.Size())
79 return 1;
81 strncpy(entry->d_name, state->list[state->curr]->GetLabel().c_str(), sizeof(entry->d_name) - 1);
82 entry->d_name[sizeof(entry->d_name) - 1] = 0;
83 state->curr++;
85 return 0;
88 void CBlurayCallback::file_close(BD_FILE_H *file)
90 if (file)
92 delete static_cast<CFile*>(file->internal);
93 delete file;
97 int CBlurayCallback::file_eof(BD_FILE_H *file)
99 if (static_cast<CFile*>(file->internal)->GetPosition() == static_cast<CFile*>(file->internal)->GetLength())
100 return 1;
101 else
102 return 0;
105 BD_FILE_H * CBlurayCallback::file_open(void *handle, const char *rel_path)
107 std::string strRelPath(rel_path);
108 std::string* strBasePath = reinterpret_cast<std::string*>(handle);
109 if (!strBasePath)
111 CLog::Log(LOGDEBUG, "CBlurayCallback - Error opening dir, null handle!");
112 return nullptr;
115 std::string strFilename = URIUtils::AddFileToFolder(*strBasePath, strRelPath);
117 BD_FILE_H *file = new BD_FILE_H;
119 file->close = file_close;
120 file->seek = file_seek;
121 file->read = file_read;
122 file->write = file_write;
123 file->tell = file_tell;
124 file->eof = file_eof;
126 CFile* fp = new CFile();
127 if (fp->Open(strFilename))
129 file->internal = (void*)fp;
130 return file;
133 CLog::Log(LOGDEBUG, "CBlurayCallback - Error opening file! ({})", CURL::GetRedacted(strFilename));
135 delete fp;
136 delete file;
138 return nullptr;
141 int64_t CBlurayCallback::file_seek(BD_FILE_H *file, int64_t offset, int32_t origin)
143 return static_cast<CFile*>(file->internal)->Seek(offset, origin);
146 int64_t CBlurayCallback::file_tell(BD_FILE_H *file)
148 return static_cast<CFile*>(file->internal)->GetPosition();
151 int64_t CBlurayCallback::file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
153 return static_cast<int64_t>(static_cast<CFile*>(file->internal)->Read(buf, static_cast<size_t>(size)));
156 int64_t CBlurayCallback::file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
158 return static_cast<int64_t>(static_cast<CFile*>(file->internal)->Write(buf, static_cast<size_t>(size)));