xbmc
SimpleFS.h
1 /*
2  * Copyright (C) 2005-2013 Team XBMC
3  * http://kodi.tv
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 #pragma once
22 
23 #include <cstdint>
24 #include <cstdio>
25 #include <string>
26 
27 class CFile
28 {
29 public:
30  CFile()
31  {
32  m_file = NULL;
33  }
34 
35  ~CFile()
36  {
37  Close();
38  }
39 
40  bool Open(const std::string &file)
41  {
42  Close();
43  m_file = fopen(file.c_str(), "rb");
44  return NULL != m_file;
45  }
46 
47  bool OpenForWrite(const std::string &file, bool overwrite)
48  {
49  Close();
50  m_file = fopen(file.c_str(), "wb");
51  return NULL != m_file;
52  }
53  void Close()
54  {
55  if (m_file)
56  fclose(m_file);
57  m_file = NULL;
58  }
59 
60  uint64_t Read(void *data, uint64_t size)
61  {
62  if (fread(data, (size_t)size, 1, m_file) == 1)
63  return size;
64  return 0;
65  }
66 
67  uint64_t Write(const void *data, uint64_t size)
68  {
69  if (fwrite(data, (size_t)size, 1, m_file) == 1)
70  return size;
71  return 0;
72  }
73 
74  FILE *getFP()
75  {
76  return m_file;
77  }
78 
79  uint64_t GetFileSize()
80  {
81  long curPos = ftell(m_file);
82  uint64_t fileSize = 0;
83  if (fseek(m_file, 0, SEEK_END) == 0)
84  {
85  long size = ftell(m_file);
86  if (size >= 0)
87  fileSize = (uint64_t)size;
88  }
89 
90  // restore fileptr
91  Seek(curPos);
92 
93  return fileSize;
94  }
95 
96  uint64_t Seek(uint64_t offset)
97  {
98  uint64_t seekedBytes = 0;
99  int seekRet = fseek(m_file, offset, SEEK_SET);
100  if (seekRet == 0)
101  seekedBytes = offset;
102  return seekedBytes;
103  }
104 
105 private:
106  FILE* m_file;
107 };
Definition: SimpleFS.h:27