Stream

The stream provides an interface to deal with binary and text data. The API below allows you to load, store data on disk, enables you to quickly compress stream (LZ) and allows you to manipulate the data stream as you see fit.

Find and replace functionalities are also available along with other convenient helper function to efficiently interact even with a large data stream. The interface below provides you the key functionalities of the stream available for Lua.

See Also

Static Variables


Name Description
size Total size in bytes of the current Stream.
cursor Current location of the cursor within the Stream.
data Current data contained in the active Stream.
name The filename of the Stream.

Functions


Name Description
CompressLZ Compress the current Stream data using LZ compression.
DecompressLZ Decompress an LZ data Stream.
Store Save the current data Stream to a binary file.
StoreText Save the current data Stream to a text file.
SetData Manually set Stream data.
Load Load an existing file from disk to the active Stream.
Clear Clear the current Stream and reset the size and data back to zero.
Reset Clear and flush the internal Stream cache.
Clean Cleanup the Stream; reset and clear the Stream and optionally flush the cache.
Reset Clear and flush the internal Stream cache.
Alloc Pre-allocate the active Stream.
Write Write data to the active Stream.
Append Append data at the end of the Stream.
Insert Write data to the active Stream.
Replace Find and replace a specific string within the Stream data.
GetLineCount Return the amount of lines contains in the Stream text data.
Find Find a specific string within the Stream text data.

Scripting


The Stream scripting interface is similar to the one used for Pose; contrarily to the other available API they require you to initialize and free the memory object. However, the rest of the API interactions with the scripting and plugins languages are standard to the rest of the NRG API.

Find below a quick example to learn how to use the Stream interface:

  • Lua
  • C/C++
-- Create a new Stream
local str = Stream.Init()
-- Load an existing text file.
if not str:Load("../../mytext.txt") then        
    print("The file could not be loaded." )
else
    -- Get the total line count.
    print("Line Count:" .. str:GetLineCount() )     
    -- Convert the bytes to a string.
    local w = {}        
    for i=1,table.getn(str.data) do 
        w[i] = string.char(str.data[i]) 
    end
    -- Print the string.
    print( table.concat(w) )        
    -- Compress the Stream.
    print("Compression Ratio:" .. string.format( "%.2f%%", str:CompressLZ() * 100.0 ) )     
    -- Save to compressed Stream on disk.
    if not str:Store( "../../mytext.lz" ) then      
        print("Cannot save compressed file.")
    else
        print("Saved...")
    end     
end
-- You are responsible to destroy the Stream to avoid leaks.
str = nil
// Create a new Stream
Stream str = StreamCreate();
// Load an existing text file.
if( !StreamLoad( &str, "C:/Users/rom/Desktop/mytext.txt" ) ) {  
    printf("The file could not be loaded\n" );
}
else {
    // Get the total line count.
    printf("Line Count:%d\n", StreamGetLineCount( &str ) );
    // Print the Stream as a string.
    printf("%s", ( char * )str.data );
    // Compress the Stream.
    float ratio = StreamCompressLZ( &str );         
    printf("Compression Ratio:%.2f%%\n", ratio * 100.0f );
    // Save to compressed Stream on disk.
    if( !StreamStore( &str,"C:/Users/rom/Desktop/mytext.lz" ) ) {
        printf("Cannot save compressed file.\n");   
    }
    else {
        printf("Saved...\n");
    }
}   
// You are responsible to destroy the Stream to avoid leaks.
StreamDestroy( &str );




NRG - API 2022.1.377663 - Fri Feb 4 2022
Copyright © 2022 nrgcore.com. All Rights Reserved. Terms of Service - Privacy Policy - EULA