File: FileIO.hpp

package info (click to toggle)
vbindiff 3.0-beta4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 588 kB
  • ctags: 370
  • sloc: cpp: 2,296; sh: 787; perl: 108; makefile: 96; lisp: 13
file content (97 lines) | stat: -rw-r--r-- 2,796 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//--------------------------------------------------------------------
// $Id: FileIO.hpp 4759 2008-07-26 03:19:24Z cjm $
//--------------------------------------------------------------------
//
//   Visual Binary Diff
//   Copyright 1997-2005 by Christopher J. Madsen
//
//   Support functions for Posix file I/O
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------

#ifndef INCLUDED_FILEIO_HPP

#define INCLUDED_FILEIO_HPP

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

typedef int      File;
typedef off_t    FPos;
typedef ssize_t  Size;

const int
  SeekEnd = SEEK_END,
  SeekPos = SEEK_SET;

const File InvalidFile = -1;

//--------------------------------------------------------------------
inline const char* ErrorMsg()
{
  return strerror(errno);
} // end ErrorMsg

//--------------------------------------------------------------------
inline File OpenFile(const char* path, bool writable=false)
{
  return open(path, (writable ? O_RDWR : O_RDONLY));
} // end OpenFile

//--------------------------------------------------------------------
inline void CloseFile(File file)
{
  close(file);
} // end CloseFile

//--------------------------------------------------------------------
bool WriteFile(File file, const void* buffer, Size count)
{
  const char* ptr = reinterpret_cast<const char*>(buffer);

  while (count > 0) {
    Size bytesWritten = write(file, ptr, count);
    if (bytesWritten < 1) {
      if (errno == EINTR)
        bytesWritten = 0;
      else
        return false;
    }

    ptr   += bytesWritten;
    count -= bytesWritten;
  } // end while more to write

  return true;
} // end WriteFile

//--------------------------------------------------------------------
inline Size ReadFile(File file, void* buffer, Size count)
{
  return read(file, buffer, count);
} // end ReadFile

//--------------------------------------------------------------------
inline FPos SeekFile(File file, FPos position, int whence=SeekPos)
{
  return lseek(file, position, whence);
} // end SeekFile

#endif // INCLUDED_FILEIO_HPP

// Local Variables:
//     c-file-style: "cjm"
// End: