Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / cores / DllLoader / exports / util / EmuFileWrapper.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21
22 #ifndef __EMU_FILE_WRAPPER_H__
23 #define __EMU_FILE_WRAPPER_H__
24
25 #include <stdio.h>
26
27 #include "system.h"
28 #include "threads/CriticalSection.h"
29
30 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN) && !defined(TARGET_FREEBSD) && !defined(TARGET_ANDROID) && !defined(__UCLIBC__)
31 #define _file _fileno
32 #elif defined(__UCLIBC__)
33 #define _file __filedes
34 #endif
35
36 #define MAX_EMULATED_FILES    50
37 #define FILE_WRAPPER_OFFSET   0x00000100
38
39 namespace XFILE
40 {
41   class CFile;
42 }
43
44 typedef struct stEmuFileObject
45 {
46   bool    used;
47   FILE    file_emu;
48   XFILE::CFile*  file_xbmc;
49   CCriticalSection *file_lock;
50   int mode;
51 } EmuFileObject;
52
53 class CEmuFileWrapper
54 {
55 public:
56   CEmuFileWrapper();
57   ~CEmuFileWrapper();
58   
59   /**
60    * Only to be called when shutting down xbmc
61    */
62   void CleanUp();
63   
64   EmuFileObject* RegisterFileObject(XFILE::CFile* pFile);
65   void UnRegisterFileObjectByDescriptor(int fd);
66   void UnRegisterFileObjectByStream(FILE* stream);
67   void LockFileObjectByDescriptor(int fd);
68   bool TryLockFileObjectByDescriptor(int fd);
69   void UnlockFileObjectByDescriptor(int fd);
70   EmuFileObject* GetFileObjectByDescriptor(int fd);  
71   EmuFileObject* GetFileObjectByStream(FILE* stream);  
72   XFILE::CFile* GetFileXbmcByDescriptor(int fd);
73   XFILE::CFile* GetFileXbmcByStream(FILE* stream);
74   static int GetDescriptorByStream(FILE* stream);
75   FILE* GetStreamByDescriptor(int fd);
76   static bool DescriptorIsEmulatedFile(int fd);
77   static bool StreamIsEmulatedFile(FILE* stream);
78 private:
79   EmuFileObject m_files[MAX_EMULATED_FILES];
80   CCriticalSection m_criticalSection;
81 };
82
83 extern CEmuFileWrapper g_emuFileWrapper;
84
85 #endif
86