Code:
- D getRecord PR 10I 0
- D file 10I 0 VALUE
- D record 65535A VARYING
- D OPTIONS(*VARSIZE)
- D maxSize 10I 0 VALUE
- D recordStart...
- D 10A VARYING
- D CONST
- D recordEnd...
- D 10A VARYING
- D CONST
-
- /copy QCOPYSRC,UTTIFS
-
- D fd...
- D S like(open)
- D reclen...
- D S like(getRecord)
- D record...
- D S 500A varying
- D start...
- D S z
- D end...
- D S z
- D runtime...
- D S 11A dtaara('RUNTIME')
- /free
- start = %timestamp();
-
- fd = open('/home/adamg/readtest/onebig' : O_RDONLY + O_TEXTDATA);
-
- recLen = getRecord(fd : record : %size(record) -2 : '<B>' : '<E>');
- dow (reclen > 0);
- recLen = getRecord(fd : record : %size(record) -2 : '<B>' : '<E>');
- enddo;
-
- callp close(fd);
-
- end = %timestamp();
-
- in *lock runTime;
- runTime = %char(%diff(end : start : *ms));
- out runTime;
-
- *inlr = *on;
- return;
- /end-free
-
- P*--------------------------------------------------
- P* Procedure name: getRecord
- P* Purpose: Retrieve one event or alarm record from the file
- P* Returns: The number of bytes in the record, -1 if an error o...
- P* ccurred
- P* Parameter: file => The file from which to read
- P* Parameter: record => The variable which will receive the record
- P* Parameter: maxSize => The maximum number of bytes that the rec...
- P* ord variable can hold
- P*--------------------------------------------------
- P getRecord B
- D getRecord PI 10I 0
- D file 10I 0 VALUE
- D record 65535A VARYING
- D OPTIONS(*VARSIZE)
- D maxSize 10I 0 VALUE
- D recordStart...
- D 10A VARYING
- D CONST
- D recordEnd...
- D 10A VARYING
- D CONST
-
-
- D* Local fields
- D buffer...
- D S 1024A static
- D bufferPosition...
- D S 5I 0 static
- D bytesInBuffer...
- D S 5I 0 static
- D readTo...
- D S *
- D recordStarted...
- D S N
- D reloadRequested...
- D S N
- D bytesInRecord S 10I 0
- D readFromFile...
- D S like(bytesInBuffer)
- D unreadCount...
- D S like(bytesInBuffer)
-
- /free
- // "clear out" the variable that will receive the record
- %len(record) = 0;
-
- dow (1 = 1);
- // reload the buffer when necessary
- if (bufferPosition >= bytesInBuffer OR reloadRequested);
- reloadRequested = *OFF;
- unreadCount = bytesInBuffer-bufferPosition;
- if (unreadCount < 0);
- unreadCount = 0;
- endif;
-
- if (unreadCount > 0);
- // if there are still some characters which haven't been
- // read due to a forced reload of the buffer, they need to be
- // put to the beginning of the buffer
- // At this point, bufferPosition points the the last character
- // that was processed, so we want bufferPosition+1 to the end
- buffer = %subst(buffer : bufferPosition + 1);
- endif;
-
- // fill the buffer starting after any characters that are left
- // over from the last read
- bufferPosition = 0;
- readTo = %addr(buffer) + unreadCount;
- readFromFile = read(file : readTo : %size(buffer) - unreadCount);
- if (readFromFile < 1);
- // error reading file or end of file, return error code
- buffer = *blanks;
- bytesInBuffer = 0;
- bufferPosition = 0;
- return -1;
- endif;
-
- bytesInBuffer = unreadCount + readFromFile;
-
- endif;
-
- // read the next section of the buffer
- if (NOT recordStarted);
- if ((bytesInBuffer-bufferPosition) < %len(recordStart));
- // not enough bytes left in buffer for start of record sequence,
- // so reload the buffer with unread part still included
- reloadRequested = *on;
- iter;
- endif;
-
- bufferPosition += 1;
- if (recordStart =
- %subst(buffer : bufferPosition : %len(recordStart))
- );
- recordStarted = *ON;
-
- // start of record sequence should not be included in the
- // returned record
- bufferPosition += %len(recordStart) - 1;
-
- endif;
- iter;
-
- endif;
-
- // check for end of record
- if ((bytesInBuffer-bufferPosition) < %len(recordEnd));
- // not enough bytes left in buffer for end of record sequence,
- // so reload the buffer with unread part still included
- reloadRequested = *on;
- iter;
- endif;
-
- bufferPosition += 1;
- if (recordEnd =
- %subst(buffer : bufferPosition : %len(recordEnd))
- );
- // found end of record, so exit the read loop
- bufferPosition += %len(recordEnd) - 1;
- leave;
- endif;
-
- // this character belongs in the record, so add it
- bytesInRecord += 1;
- if (%len(record) < maxSize);
- record += %subst(buffer : bufferPosition : 1);
- endif;
- enddo;
-
- return bytesInRecord;
-
- /end-free
- P getRecord E
|
|