windows/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 <io.h>
50 #include <fcntl.h>
51 #include <share.h>
52 #include <sys/stat.h>
53 
54 // The following define the platform independent open method 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 _SH_DENYWR
65 #define RX_SH_DENYRD _SH_DENYRD
66 #define RX_SH_DENYRW _SH_DENYRW
67 #define RX_SH_DENYNO _SH_DENYNO
68 // shareMode flags
69 #define RX_S_IWRITE _S_IWRITE
70 #define RX_S_IREAD _S_IREAD
71 
72 #define BLOCK_THRESHOLD (32*1024)
73 
74 
75 class SysFile
76 {
77 public:
79 
80  enum
81  {
82  DEFAULT_BUFFER_SIZE = 4096, // default size for buffering
83  LINE_POSITIONING_BUFFER = 512 // buffer size for line movement
84  };
85 
86 #define LINE_TERMINATOR "\r\n"
87 
88  bool open(const char *name, int openFlags, int openMode, int shareMode);
89  bool open(int handle);
90  void reset();
91  void setStdIn();
92  void setStdOut();
93  void setStdErr();
94  void setBuffering(bool buffer, size_t length);
95  bool close();
96  bool flush();
97  bool read(char *buf, size_t len, size_t &bytesRead);
98  bool write(const char *data, size_t len, size_t &bytesWritten);
99  bool putChar(char ch);
100  bool ungetc(char ch);
101  bool getChar(char &ch);
102  bool puts(const char *data, size_t &bytesWritten);
103  bool gets(char *buffer, size_t len, size_t &bytesRead);
104  bool setPosition(int64_t location, int64_t &position);
105  bool seek(int64_t offset, int direction, int64_t &position);
106  bool getPosition(int64_t &position);
107  bool getSize(int64_t &size);
108  bool getSize(const char *name, int64_t &size);
109  bool getTimeStamp(const char *&time);
110  bool getTimeStamp(const char *name, const char *&time);
111  bool putLine(const char *buffer, size_t len, size_t &bytesWritten);
112  bool hasData();
113  bool countLines(int64_t &count);
114  bool countLines(int64_t start, int64_t end, int64_t &lastLine, int64_t &count);
115  bool nextLine(size_t &bytesRead);
116  bool seekForwardLines(int64_t startPosition, int64_t &lineCount, int64_t &endPosition);
117  inline bool isTransient() { return transient; }
118  inline bool isDevice() { return device; }
119  inline bool isReadable() { return readable; }
120  inline bool isWriteable() { return writeable; }
121  inline bool isOpen() { return fileHandle != -1; }
122 
123  inline bool error() { return errInfo != 0; }
124  inline int errorInfo() { return errInfo; }
125  inline void clearErrors() { errInfo = 0; }
126  inline bool atEof() { return !hasBufferedInput() && eof(fileHandle) == 1; }
127  inline bool hasBufferedInput() { return buffered && (bufferedInput > bufferPosition); }
128  inline uintptr_t getHandle() { return (uintptr_t)fileHandle; }
129 
130 protected:
132  int writeData(const char *data, size_t length);
133 
134  int fileHandle; // separate file handle
135  int errInfo; // last error info
136  bool openedHandle; // true if we opened the handle.
137  int flags; // open flag information
138  int mode; // mode flags
139  int share; // sharing mode flags
140  char *filename; // the input file name
141  bool buffered; // the buffering indicator
142  bool transient; // this is a transient stream
143  bool device; // this stream is a device.
144  bool writeable; // stream is capable of output
145  bool readable; // stream is capable in input
146  bool isTTY; // a keyboard based stream.
147  char *buffer; // our read/write buffer.
148  size_t bufferSize; // the size of the buffer
149  size_t bufferPosition; // current read/write position in buffer
150  size_t bufferedInput; // amount of data in the buffer
151  bool writeBuffered; // false == read, true == write
152  bool append; // opened in append mode
153  int64_t filePointer; // current file pointer location
154  int ungetchar; // a pushed back character value
155 };
156 
157 #endif
158 
159 
void setStdIn()
bool putChar(char ch)
bool putLine(const char *buffer, size_t len, size_t &bytesWritten)
void clearErrors()
bool writeable
bool getSize(const char *name, int64_t &size)
char * buffer
bool openedHandle
size_t bufferSize
bool getTimeStamp(const char *name, const char *&time)
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
bool buffered
int fileHandle
int writeData(const char *data, size_t length)
bool isReadable()
bool close()
bool puts(const char *data, size_t &bytesWritten)
bool getPosition(int64_t &position)
size_t bufferPosition
uintptr_t getHandle()
void setBuffering(bool buffer, size_t length)
bool getTimeStamp(const char *&time)
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 writeBuffered
bool countLines(int64_t &count)
char * filename
bool write(const char *data, size_t len, size_t &bytesWritten)
bool countLines(int64_t start, int64_t end, int64_t &lastLine, int64_t &count)
bool open(int handle)
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()
UINT_PTR uintptr_t
signed __int64 int64_t