Interface: MemStream

The MemStream Core Interface was first introduced in gMax 1.0 and is available in MAXScript since 3ds Max 5. MemStream provides fast stream parsing by loading the entire file into memory.

The memStreamMgr and memStream interfaces provide a large set of methods for parsing the stream.

memStreamMgr - Interface

Methods

<Interface>openFile <filename>fname

Returns a memStream interface where the content of the specified file is the input stream to be processed. If the file cannot be opened, a value of undefined is returned.

<Interface>openString <filename>string

Returns a memStream interface where the content of the string is the input stream to be processed.

<void>close <Interface>memStream

Closes the input stream for the memStream, freeing the memory associated with this stream.

memStream - Interface

 

The memStream Interface provides methods for parsing the input stream. This interface is returned from the memStreamMgr.openString() and memStreamMgr.openFile() methods.

Methods

<filename>getFileName()

Returns the file name that the memStream was created from. Returns "" if the interface was created via memStreamMgr.openString().

<void>skipSpace()

Skips leading whitespace characters (space, tab, and newlines) starting at the current stream cursor position. Following this call the stream cursor position will be at a non-whitespace character, or at the end of the stream.

<string>readChar()

Reads a single character from the stream, returning it as a string.

<string>peekChar()

Reads a single character from the stream, returning it as a string. Does not increment the stream cursor position.

<string>readLine()

Reads all characters from the current stream cursor position until a newline and returns it as a string.

<string>readToken()

Reads a token from the given stream returns it as a string. A token is defined by a sequence of characters surrounded by whitespace or the stream start or end. Leading whitespace and one line "//" style comments are automatically skipped.

The maximum allowed length of the returned string is 5120 characters. If a token larger than this length is read, the string returned contains the first 5120 characters of the token. The stream cursor position will be at the next character.

<string>peekToken()

Same as <memStream>.readToken except it does not increment the stream cursor position.

<void>unReadToken()

Resets the stream cursor position to right before the last readToken operation.

<string>readBlock <string>open <string>close

Reads a block of tokens from the 'open' token until the 'close' token, automatically skipping nested open/close token pairs, and returns the contents as a string. This string will contain the initial 'open' token and the final 'close' token. The initial 'open' token must be the next token in the input stream. If it is not, a value of ‘undefined’ will be returned.

Following this call, the stream cursor position will be at the beginning of the next token, or at the end of the stream if there are no more tokens following the 'close' token. The comparison of tokens against the 'open' and 'close' tokens is case-insensitive.

Note

that the open and close strings represent tokens, and not character strings.

For example:

b2=memStreamMgr.openstring "{aaaaa}"

b2.readblock "{" "}"

will return undefined, because the '{' and '}' are not surrounded by whitespace, and so are not found as tokens (the entire content of the memStream is a single token).

If the 'open' token is not the next token in the input stream, a value of 'undefined' will be returned, and the stream cursor position will be unchanged.

If the 'open' token is the next token in the input stream, but the 'close' token is not found, returns a string starting at the open token, and ending at the end of stream -1 character.

The maximum allowed length of the block is 5120 characters. If a block is found, but is larger than this length, a value of 'undefined' is returned. However, the stream cursor position will be at the end of the block.

<void>skipBlock <string>open <string>close

Same as readBlock except does not return the block of tokens (this is a little faster).

<integer>pos()

Returns the current stream cursor position.

<void>seek <integer>offset <enum>origin

origin enums: {#seek_set|#seek_cur|#seek_end}

Moves stream cursor position 'offset' number of characters from the base specified.

#seek_set -- start of file

#seek_cur -- current file position

#seek_end -- end of file (works with negative offset values)

<integer>size()

Returns the size of the input stream

<bool>eos()

Returns true if the current stream cursor position is at or past the end of the input stream, false otherwise.

Example

ms = memStreamMgr.openString "token! //comment!\n { { nested bracket pair } } "

ms.readToken()

ms.readBlock "{" "}"

ms.eos()

memStreamMgr.close ms

Result

<MixinInterface:memStream>

"token!"

"{ { nested bracket pair } }"

true

OK

Notes

The read or peek methods currently do not protect against reading past the end of the input stream.

The seek method currently does not protect against setting position past end of the input stream.

The readBlock and skipBlock methods performs case-insensitive comparisons on the open and close token strings.