summaryrefslogtreecommitdiff
path: root/im/src/im_sysfile_unix.cpp
blob: 2065bb6b654c748f58d70f233080f5c227d86cc4 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/** \file
 * \brief System Dependent Binary File Access (UNIX)
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_sysfile_unix.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $
 */

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "im_util.h"
#include "im_binfile.h"


class imBinSystemFile: public imBinFileBase
{
protected:
  int FileHandle, 
      Error;

  unsigned long ReadBuf(void* pValues, unsigned long pSize);
  unsigned long WriteBuf(void* pValues, unsigned long pSize);

public:
  virtual void Open(const char* pFileName);
  virtual void New(const char* pFileName);
  virtual void Close();

  unsigned long FileSize();
  int HasError() const;
  void SeekTo(unsigned long pOffset);
  void SeekOffset(long pOffset);
  void SeekFrom(long pOffset);
  unsigned long Tell() const;
  int EndOfFile() const;
};

imBinFileBase* iBinSystemFileNewFunc()
{
  return new imBinSystemFile();
}

void imBinSystemFile::Open(const char* pFileName)
{
  int mode = O_RDONLY;
#ifdef O_BINARY
    mode |= O_BINARY;
#endif        
  this->FileHandle = open(pFileName, mode, 0);
  if (this->FileHandle < 0) 
    this->Error = errno;
  else
    this->Error = 0;
  InitByteOrder(imBinCPUByteOrder());
  this->IsNew = 0;
}

void imBinSystemFile::New(const char* pFileName)
{
  int mode = O_WRONLY | O_CREAT | O_TRUNC;           
#ifdef O_BINARY
    mode |= O_BINARY;
#endif        
  this->FileHandle = open(pFileName, mode, 0666); // User/Group/Other can read and write
  if (this->FileHandle < 0) 
    this->Error = errno;
  else
    this->Error = 0;
  InitByteOrder(imBinCPUByteOrder());
  this->IsNew = 1;
}

void imBinSystemFile::Close()
{
  assert(this->FileHandle > -1);
  int ret = close(this->FileHandle);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
}

int imBinSystemFile::HasError() const
{
  if (this->FileHandle < 0 || this->Error) return 1;
  return 0;
}

unsigned long imBinSystemFile::ReadBuf(void* pValues, unsigned long pSize)
{
  assert(this->FileHandle > -1);
	int ret = read(this->FileHandle, pValues, (size_t)pSize);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
  return ret < 0? 0: ret;
}
                             
unsigned long imBinSystemFile::WriteBuf(void* pValues, unsigned long pSize)
{
  assert(this->FileHandle > -1);
  int ret = write(this->FileHandle, pValues, (size_t)pSize);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
  return ret < 0? 0: ret;
}

void imBinSystemFile::SeekTo(unsigned long pOffset)
{
  assert(this->FileHandle > -1);
  int ret = lseek(this->FileHandle, pOffset, SEEK_SET);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
}

void imBinSystemFile::SeekOffset(long pOffset)
{
  assert(this->FileHandle > -1);
  int ret = lseek(this->FileHandle, pOffset, SEEK_CUR);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
}

void imBinSystemFile::SeekFrom(long pOffset)
{
  assert(this->FileHandle > -1);
  int ret = lseek(this->FileHandle, pOffset, SEEK_END);
  if (ret < 0)
    this->Error = errno;
  else
    this->Error = 0;
}

unsigned long imBinSystemFile::Tell() const
{
  assert(this->FileHandle > -1);
  long offset = lseek(this->FileHandle, 0L, SEEK_CUR);
  return offset < 0? 0: offset;
}

unsigned long imBinSystemFile::FileSize()
{
  assert(this->FileHandle > -1);
  long lCurrentPosition = lseek(this->FileHandle, 0L, SEEK_CUR);
  long lSize = lseek(this->FileHandle, 0L, SEEK_END);
  lseek(this->FileHandle, lCurrentPosition, SEEK_SET);
  return lSize < 0? 0: lSize;
}

int imBinSystemFile::EndOfFile() const
{
  assert(this->FileHandle > -1);
  long lCurrentPosition = lseek(this->FileHandle, 0L, SEEK_CUR);
  long lSize = lseek(this->FileHandle, 0L, SEEK_END);
  lseek(this->FileHandle, lCurrentPosition, SEEK_SET);
  return lCurrentPosition == lSize? 1: 0;
}



class imBinSystemFileHandle: public imBinSystemFile
{
public:
  virtual void Open(const char* pFileName);
  virtual void New(const char* pFileName);
  virtual void Close();
};

imBinFileBase* iBinSystemFileHandleNewFunc()
{
  return new imBinSystemFileHandle();
}

void imBinSystemFileHandle::Open(const char* pFileName)
{
  // the file was successfully opened already by the client

  int *s = (int*)pFileName;
  this->FileHandle = s[0];
  InitByteOrder(imBinCPUByteOrder());
  this->IsNew = 0;
  this->Error = 0;
}

void imBinSystemFileHandle::New(const char* pFileName)
{
  // the file was successfully opened already the client

  int *s = (int*)pFileName;
  this->FileHandle = s[0];
  InitByteOrder(imBinCPUByteOrder());
  this->IsNew = 1;
  this->Error = 0;
}

void imBinSystemFileHandle::Close()
{
  // does nothing, the client must close the file
}