ICP  1
SingleFile.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "SingleFile.h"
3 
4 SingleFile::SingleFile() : m_file(NULL)
5 {
6  setLoggerName("SingleFile");
7 }
8 
10 {
11  close();
12 }
13 
14 void SingleFile::removeFile(const std::string& path)
15 {
16  std::remove(path.c_str());
17 }
18 
19 int SingleFile::open(const std::string& path, Mode mode, int shflag)
20 {
21  std::string fmode;
22  if (mode == IFile::ReadOnly)
23  {
24  fmode = "rbN";
25  }
26  else if (mode == IFile::ReadWriteCreate)
27  {
28  if (access(path.c_str(), 0) == 0)
29  {
30  fmode = "r+bcN";
31  }
32  else
33  {
34  fmode = "wbcN";
35  }
36  }
37  m_file = _fsopen(path.c_str(), fmode.c_str(), shflag);
38  return (m_file != NULL ? 0 : 1);
39 }
40 
41 size_t SingleFile::write(const void* data, size_t element_size, size_t nelement)
42 {
43  return fwrite(data, element_size, nelement, m_file);
44 }
45 
46 size_t SingleFile::read(void* data, size_t element_size, size_t nelement)
47 {
48  return fread(data, element_size, nelement, m_file);
49 }
50 
52 {
53  int ret = 0;
54  if (m_file != NULL)
55  {
56  ret = fclose(m_file);
57  m_file = NULL;
58  }
59  return ret;
60 }
61 
62 int SingleFile::seek(int64_t offset, int whence)
63 {
64  return fseek(m_file, offset, whence);
65 }
66 
68 {
69  return _chsize(fileno(m_file), 0);
70 }
71 
72 bool SingleFile::isOpen() const
73 {
74  return m_file != NULL;
75 }
76 
78 {
79  return fflush(m_file);
80 }
81 
size_t read(void *data, size_t element_size, size_t nelement)
Definition: SingleFile.cpp:46
FILE * m_file
Definition: SingleFile.h:9
int flush()
Definition: SingleFile.cpp:77
bool isOpen() const
Definition: SingleFile.cpp:72
int open(const std::string &path, Mode mode, int shflag)
Definition: SingleFile.cpp:19
size_t write(const void *data, size_t element_size, size_t nelement)
Definition: SingleFile.cpp:41
int discardContents()
Definition: SingleFile.cpp:67
static void removeFile(const std::string &path)
Definition: SingleFile.cpp:14
int close()
Definition: SingleFile.cpp:51
int seek(int64_t offset, int whence)
Definition: SingleFile.cpp:62
void setLoggerName(const std::string &logger_name)
Definition: IsisBase.h:17