StreamNative.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 */
40 /* */
41 /* Stream declarations and includes */
42 /* */
43 /******************************************************************************/
44 #ifndef StreamNative_Included
45 #define StreamNative_Included
46 
47 #include "SysFileSystem.hpp"
48 #include "SysFile.hpp"
49 
50 #define nbt_line_end "\n"
51 #define nbt_line_end_size 1
52 
53 #define std_line_end "\n"
54 #define std_line_end_size 1
55 
56 #define ctrl_z 0x1a /* end-of-file marker */
57 #define nl '\n' /* new line character */
58 #define cr '\r' /* carriage return character */
59 
60 
61 /*****************************************************************************/
62 /* Stream structure to hold information pertinent to all streams */
63 /*****************************************************************************/
64 
66 {
67  friend class LineBuffer;
68 public:
69  enum
70  {
71  DefaultBufferSize = 512 // default read buffer size
72  };
73 
74  typedef enum
75  {
76  StreamUnknown = 0, // stream is in unknown state
77  StreamReady = 1, // stream is in ready state
78  StreamNotready = 2, // stream is in a notready condition
79  StreamEof = 3, // stream is in an eof condition
80  StreamError = 4, // stream has had an error condition
81  } StreamState;
82 
83  StreamInfo(RexxObjectPtr s, const char *inputName);
85  {
86  context = c;
87  defaultResult = d;
88  }
89  char *allocateBuffer(size_t length);
90  char *getDefaultBuffer(size_t &length);
91  char *extendBuffer(size_t &length);
92  void freeBuffer();
93  bool open(int openFlags, int openMode, int sharedFlag);
94  int64_t size();
95  void notreadyError();
96  void notreadyError(int error_code, RexxObjectPtr result = NULL);
97  void raiseException(int err);
98  void raiseException(int err, RexxObjectPtr sub1);
99  void raiseException(int err, RexxObjectPtr sub1, RexxObjectPtr sub2);
100  void raiseException(int err, RexxObjectPtr sub1, RexxObjectPtr sub2, RexxObjectPtr sub3);
101  void raiseException(int err, RexxObjectPtr sub1, RexxObjectPtr sub2, RexxObjectPtr sub3, RexxObjectPtr sub4);
102  void raiseException(int err, RexxObjectPtr sub1, RexxObjectPtr sub2, RexxObjectPtr sub3, RexxObjectPtr sub4, RexxObjectPtr sub5);
103  void eof(RexxObjectPtr);
104  void eof();
105  void checkEof();
106  void checkStreamType();
107  void close();
108  const char *openStd(const char *options);
109  const char *handleOpen(const char *options);
110  void resetFields();
111  void implicitOpen(int type);
112  void readSetup();
113  void writeSetup();
114  RexxStringObject readLine(char *buffer, size_t length, bool update_position);
115  void resolveStreamName();
116  void writeBuffer(const char *data, size_t length, size_t &bytesWritten);
117  void writeLine(const char *data, size_t length, size_t &bytesWritten);
118  void readBuffer(char *data, size_t length, size_t &bytesRead);
119  void completeLine(size_t writeLength);
120  void writeFixedLine(const char *data, size_t length);
121  void setPosition(int64_t position, int64_t &newPosition);
122  void setPosition(int64_t offset, int style, int64_t &newPosition);
123  void setReadPosition(int64_t position);
124  void setWritePosition(int64_t position);
125  void setCharReadPosition(int64_t position);
126  void setLineReadPosition(int64_t position);
127  void setCharWritePosition(int64_t position);
128  void setLineWritePosition(int64_t position);
131  void lineReadIncrement();
132  void resetLinePositions();
133  RexxStringObject charin(bool setPosition, int64_t position, size_t read_length);
134  size_t charout(RexxStringObject data, bool setPosition, int64_t position);
135  RexxStringObject linein(bool setPosition, int64_t position, size_t count);
136  int arrayin(RexxArrayObject r);
137  int64_t lines(bool quick);
138  int64_t chars();
139  int lineout(RexxStringObject data, bool setPosition, int64_t position);
140  const char *streamClose();
141  const char *streamFlush();
142  const char *streamOpen(const char *options);
143  void setHandle(int fh);
144  int64_t streamPosition(const char *options);
146  int64_t seekLinePosition(int64_t offset, int direction, int64_t &current_line, int64_t &current_position);
147  int64_t setLinePosition(int64_t new_line, int64_t &current_line, int64_t &current_position);
148  int64_t queryLinePosition(int64_t current_position);
149  RexxObjectPtr queryStreamPosition(const char *options);
152  int64_t readForwardByLine(int64_t offset, int64_t &current_line, int64_t &current_position);
153  int64_t seekToVariableLine(int64_t offset, int64_t& current_line, int64_t &current_position);
155  const char *getQualifiedName();
156  const char *streamExists();
158  const char *getStreamType();
160  const char *getTimeStamp();
161  const char *getState();
163  int64_t countStreamLines(int64_t currentLinePosition, int64_t currentPosition);
164  inline void setStandard() { stdstream = true; }
165 
166 
167 protected:
168  RexxMethodContext *context; // our current execution context
169  RexxObjectPtr self; // our real Rexx object instance
170  RexxObjectPtr defaultResult; // default result used for notready conditions.
171  // specified stream name
173  // fully resolved stream name
175  int64_t charReadPosition; // current character position
176  int64_t charWritePosition; // current write position
177  int64_t lineReadPosition; // current read line number
178  int64_t lineWritePosition; // current write line number
179  int64_t lineReadCharPosition; // current line read position
180  int64_t lineWriteCharPosition; // current line write position
181  int64_t stream_line_size; // emulated stream size (lines)
182  StreamState state; // current stream state
183  int errorInfo; // error information about the last notready condition
184  size_t binaryRecordLength; // binary file record length
185 
186  char *bufferAddress; // current read buffer
187  size_t bufferLength; // current read buffer size
188 
189  SysFile fileInfo; // system specific file implementation
190 
191  // the various flag state settings
192  bool read_only;
195  bool append;
196  bool nobuffer;
197  bool stdstream; // true if a standard I/O stream
198  bool last_op_was_read; // still needed?
199  bool opened_as_handle; // given a handle directly
200  bool transient; // non-persistent stream
201  bool record_based; // uses fixed-length, non-terminated records
202  bool isopen; // the stream is open
203 };
204 
205 #endif
void checkStreamType()
void setLineReadPosition(int64_t position)
void resolveStreamName()
const char * getTimeStamp()
int64_t lineReadPosition
void resetFields()
int64_t charWritePosition
void setHandle(int fh)
const char * streamOpen(const char *options)
void freeBuffer()
bool open(int openFlags, int openMode, int sharedFlag)
void writeSetup()
void readSetup()
StreamInfo(RexxObjectPtr s, const char *inputName)
void implicitOpen(int type)
SysFile fileInfo
void lineReadIncrement()
RexxStringObject readVariableLine()
int64_t setLinePositions()
char * allocateBuffer(size_t length)
int64_t lineReadCharPosition
int64_t readForwardByLine(int64_t offset, int64_t &current_line, int64_t &current_position)
int64_t lineWriteCharPosition
int64_t seekLinePosition(int64_t offset, int direction, int64_t &current_line, int64_t &current_position)
const char * streamExists()
bool opened_as_handle
int64_t getLineWritePosition()
RexxMethodContext * context
char stream_name[SysFileSystem::MaximumFileNameBuffer]
friend class LineBuffer
void notreadyError()
const char * handleOpen(const char *options)
RexxStringObject charin(bool setPosition, int64_t position, size_t read_length)
void resetLinePositions()
void setContext(RexxMethodContext *c, RexxObjectPtr d)
int64_t charReadPosition
void writeBuffer(const char *data, size_t length, size_t &bytesWritten)
int lineout(RexxStringObject data, bool setPosition, int64_t position)
RexxObjectPtr defaultResult
void checkEof()
const char * streamFlush()
size_t binaryRecordLength
int64_t seekToVariableLine(int64_t offset, int64_t &current_line, int64_t &current_position)
const char * openStd(const char *options)
char qualified_name[SysFileSystem::MaximumFileNameLength]
int64_t getLineReadPosition()
void setCharWritePosition(int64_t position)
int64_t queryLinePosition(int64_t current_position)
RexxStringObject getDescription()
void setCharReadPosition(int64_t position)
void writeFixedLine(const char *data, size_t length)
RexxObjectPtr getStreamSize()
StreamState state
char * getDefaultBuffer(size_t &length)
int64_t countStreamLines(int64_t currentLinePosition, int64_t currentPosition)
void setLineWritePosition(int64_t position)
const char * getStreamType()
RexxObjectPtr queryHandle()
void appendVariableLine(RexxArrayObject r)
RexxObjectPtr queryStreamPosition(const char *options)
void raiseException(int err)
int64_t lineWritePosition
size_t bufferLength
RexxStringObject readLine(char *buffer, size_t length, bool update_position)
int64_t stream_line_size
void completeLine(size_t writeLength)
void setStandard()
void setPosition(int64_t position, int64_t &newPosition)
bool last_op_was_read
char * bufferAddress
const char * getState()
int64_t chars()
int64_t setLinePosition(int64_t new_line, int64_t &current_line, int64_t &current_position)
int64_t streamPosition(const char *options)
int arrayin(RexxArrayObject r)
char * extendBuffer(size_t &length)
void setWritePosition(int64_t position)
int64_t lines(bool quick)
void writeLine(const char *data, size_t length, size_t &bytesWritten)
int64_t size()
int64_t getLineSize()
RexxStringObject linein(bool setPosition, int64_t position, size_t count)
void readBuffer(char *data, size_t length, size_t &bytesRead)
size_t charout(RexxStringObject data, bool setPosition, int64_t position)
const char * getQualifiedName()
const char * streamClose()
void setReadPosition(int64_t position)
int type
Definition: cmdparse.cpp:383
struct _RexxStringObject * RexxStringObject
Definition: rexx.h:128
struct _RexxArrayObject * RexxArrayObject
Definition: rexx.h:130
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
signed __int64 int64_t