Interface: MemStream

Interfaces > Core Interfaces > MemStream

The MemStream Core Interface was first introduced in gMax 1.0 and is available in MAXScript since 3ds Max 5.

It 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>MemStreamMgr.openFile <filename>fname favor_type:<enum> code_page:<integer>

favor_type enums: {#FAVOR_ACP|#FAVOR_UTF8}
favor_type default value: #FAVOR_ACP
code_page default value: 0

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.

The optional keyword arguments favor_type: and code_page: available in 3ds Max 2013 and higher provide control over the stream Encoding.

ACP stands for ANSI Code Page

UTF8 stands for UCS Transformation Format - 8 bit, where UCS stands for Universal Character Set.

   

<Interface>MemStreamMgr.openString <filename>string 	 

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

   

<void>MemStreamMgr.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><Interface:MemStream>.getFileName() 

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

   

<void><Interface:MemStream>.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><Interface:MemStream>.readChar() 

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

   

<string><Interface:MemStream>.peekChar() 

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

   

<string><Interface:MemStream>.readLine() 

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

   

<string><Interface:MemStream>.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><Interface:MemStream>.peekToken() 

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

   

<void><Interface:MemStream>.unReadToken() 

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

   

<string><Interface:MemStream>.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 ’r;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:

The open and close strings represent tokens, and not character strings.

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><Interface:MemStream>.skipBlock <string>open <string>close 

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

   

<integer><Interface:MemStream>.pos() 

Returns the current stream cursor position.

   

<void><Interface:MemStream>.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
NOTE:

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.