unix/SysFile.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel SysFile.hpp */
40 /* */
41 /* System support for File operations. */
42 /* */
43 /******************************************************************************/
44 
45 #ifndef Included_SysFile
46 #define Included_SysFile
47 
48 #include "rexxapitypes.h"
49 #include <fcntl.h>
50 #if defined(__OpenBSD__)
51 #include <sys/stat.h>
52 #endif
53 
54 // The following define the platform independent open flags
55 // openFlags argument flags
56 #define RX_O_RDONLY O_RDONLY
57 #define RX_O_WRONLY O_WRONLY
58 #define RX_O_RDWR O_RDWR
59 #define RX_O_CREAT O_CREAT
60 #define RX_O_EXCL O_EXCL
61 #define RX_O_TRUNC O_TRUNC
62 #define RX_O_APPEND O_APPEND
63 // openMode flags
64 #define RX_SH_DENYWR 0
65 #define RX_SH_DENYRD 0
66 #define RX_SH_DENYRW 0
67 #define RX_SH_DENYNO 0
68 // shareMode flags
69 #define RX_S_IWRITE (S_IWUSR | S_IWGRP | S_IWOTH)
70 #define RX_S_IREAD (S_IRUSR | S_IRGRP | S_IROTH)
71 
72 class SysFile
73 {
74 public:
75  SysFile();
76 
77  static const int stdinHandle;
78  static const int stdoutHandle;
79  static const int stderrHandle;
80 
81  enum
82  {
83  DEFAULT_BUFFER_SIZE = 4096, // default size for buffering
84  LINE_POSITIONING_BUFFER = 512 // buffer size for line movement
85  };
86 
87 #define LINE_TERMINATOR "\n"
88 
89  bool open(const char *name, int openFlags, int openMode, int shareMode);
90  bool open(int handle);
91  void reset();
92  void setStdIn();
93  void setStdOut();
94  void setStdErr();
95  void setBuffering(bool buffer, size_t length);
96  bool close();
97  bool flush();
98  bool read(char *buf, size_t len, size_t &bytesRead);
99  bool write(const char *data, size_t len, size_t &bytesWritten);
100  bool putChar(char ch);
101  bool ungetc(char ch);
102  bool getChar(char &ch);
103  bool puts(const char *data, size_t &bytesWritten);
104  bool gets(char *buffer, size_t len, size_t &bytesRead);
105  bool setPosition(int64_t location, int64_t &position);
106  bool seek(int64_t offset, int direction, int64_t &position);
107  bool getPosition(int64_t &position);
108  bool getSize(int64_t &size);
109  bool getSize(const char *name, int64_t &size);
110  bool getTimeStamp(const char *&time);
111  bool getTimeStamp(const char *name, const char *&time);
112  bool putLine(const char *buffer, size_t len, size_t &bytesWritten);
113  bool hasData();
114  bool countLines(int64_t &count);
115  bool countLines(int64_t start, int64_t end, int64_t &lastLine, int64_t &count);
116  bool nextLine(size_t &bytesRead);
117  bool seekForwardLines(int64_t startPosition, int64_t &lineCount, int64_t &endPosition);
118  inline bool isTransient() { return transient; }
119  inline bool isDevice() { return device; }
120  inline bool isReadable() { return readable; }
121  inline bool isWriteable() { return writeable; }
122  inline bool isOpen() { return fileHandle != -1; }
123  inline bool isStdIn() { return fileHandle == stdinHandle; }
124 
125  inline bool error() { return errInfo != 0; }
126  inline int errorInfo() { return errInfo; }
127  inline void clearErrors() { errInfo = 0; }
128  inline bool atEof() { return !hasBufferedInput() && fileeof; }
129  inline bool hasBufferedInput() { return buffered && (bufferedInput > bufferPosition); }
130  inline uintptr_t getHandle() { return (uintptr_t)fileHandle; }
131 
132 protected:
133  void getStreamTypeInfo();
134 
135  int fileHandle; // separate file handle
136  int errInfo; // last error info
137  bool openedHandle; // true if we opened the handle.
138  int flags; // open flag information
139  int mode; // mode flags
140  int share; // sharing mode flags
141  const char *filename; // the input file name
142  bool buffered; // the buffering indicator
143  bool transient; // this is a transient stream
144  bool device; // this stream is a device.
145  bool writeable; // stream is capable of output
146  bool readable; // stream is capable in input
147  bool isTTY; // a keyboard based stream.
148  char *buffer; // our read/write buffer.
149  size_t bufferSize; // the size of the buffer
150  size_t bufferPosition; // current read/write position in buffer
151  size_t bufferedInput; // amount of data in the buffer
152  bool writeBuffered; // false == read, true == write
153  bool append; // opened in append mode
154  int64_t filePointer; // current file pointer location
155  int ungetchar; // a pushed back character value
156  bool fileeof; // have we reached eof?
157 };
158 
159 #endif
160 
161 
void setStdIn()
bool putChar(char ch)
bool putLine(const char *buffer, size_t len, size_t &bytesWritten)
void clearErrors()
int errorInfo()
bool writeable
char * buffer
bool openedHandle
bool atEof()
size_t bufferSize
const char * filename
bool isStdIn()
void setStdErr()
bool isDevice()
bool gets(char *buffer, size_t len, size_t &bytesRead)
bool nextLine(size_t &bytesRead)
bool getSize(int64_t &size)
bool seek(int64_t offset, int direction, int64_t &position)
bool seekForwardLines(int64_t startPosition, int64_t &lineCount, int64_t &endPosition)
bool isWriteable()
bool hasBufferedInput()
@ LINE_POSITIONING_BUFFER
@ DEFAULT_BUFFER_SIZE
bool flush()
int64_t filePointer
static const int stdinHandle
bool buffered
static const int stderrHandle
int fileHandle
bool isReadable()
bool close()
bool puts(const char *data, size_t &bytesWritten)
bool getPosition(int64_t &position)
bool fileeof
size_t bufferPosition
uintptr_t getHandle()
void setBuffering(bool buffer, size_t length)
bool getTimeStamp(const char *&time)
static const int stdoutHandle
bool open(const char *name, int openFlags, int openMode, int shareMode)
bool getChar(char &ch)
bool read(char *buf, size_t len, size_t &bytesRead)
bool error()
bool writeBuffered
bool countLines(int64_t &count)
bool write(const char *data, size_t len, size_t &bytesWritten)
bool setPosition(int64_t location, int64_t &position)
bool hasData()
bool readable
bool isTransient()
size_t bufferedInput
void getStreamTypeInfo()
bool ungetc(char ch)
void setStdOut()
void reset()
bool isOpen()
UINT_PTR uintptr_t
signed __int64 int64_t