[release] version bump to 13.0 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)
31 #define _file _fileno
32 #endif
33
34 #define MAX_EMULATED_FILES    50
35 #define FILE_WRAPPER_OFFSET   0x00000100
36
37 namespace XFILE
38 {
39   class CFile;
40 }
41
42 typedef struct stEmuFileObject
43 {
44   bool    used;
45   FILE    file_emu;
46   XFILE::CFile*  file_xbmc;
47   CCriticalSection *file_lock;
48   int mode;
49 } EmuFileObject;
50
51 class CEmuFileWrapper
52 {
53 public:
54   CEmuFileWrapper();
55   ~CEmuFileWrapper();
56   
57   /**
58    * Only to be called when shutting down xbmc
59    */
60   void CleanUp();
61   
62   EmuFileObject* RegisterFileObject(XFILE::CFile* pFile);
63   void UnRegisterFileObjectByDescriptor(int fd);
64   void UnRegisterFileObjectByStream(FILE* stream);
65   void LockFileObjectByDescriptor(int fd);
66   bool TryLockFileObjectByDescriptor(int fd);
67   void UnlockFileObjectByDescriptor(int fd);
68   EmuFileObject* GetFileObjectByDescriptor(int fd);  
69   EmuFileObject* GetFileObjectByStream(FILE* stream);  
70   XFILE::CFile* GetFileXbmcByDescriptor(int fd);
71   XFILE::CFile* GetFileXbmcByStream(FILE* stream);
72   static int GetDescriptorByStream(FILE* stream);
73   FILE* GetStreamByDescriptor(int fd);
74   static bool DescriptorIsEmulatedFile(int fd);
75   static bool StreamIsEmulatedFile(FILE* stream);
76 private:
77   EmuFileObject m_files[MAX_EMULATED_FILES];
78   CCriticalSection m_criticalSection;
79 };
80
81 extern CEmuFileWrapper g_emuFileWrapper;
82
83 #endif
84