Nebula3 Study Notes (5): Io System

Source: Internet
Author: User
Tags xml reader

  Io SubsystemThe IO System of nebula3 is a huge improvement compared with Nebula1 and 2. The main design objectives of the new system are as follows:
  • Use a more standard mechanism, such as URI to locate resources, and Mime Type to differentiate data formats
  • A flexible stream model that does not care whether data comes from files, memory, HTTP connections, or other places
  • Data Types that do not read or write data from a stream are also more convenient. For example, the XML format data to be read is from the file/memory/network.
  • In addition, the new stream and read/write classes can be registered to the IO system at runtime.
  • Compared with the specific Io functions on the system platform, C lib functions such as fopen () have additional performance or memory loss. therefore, the I/O functions of a specific platform must be used without compromising performance while ensuring portability.
I/O subsystem concepts:
  • A hubIO: ConsoleThe object is connected to the console processor for text input and output. this ensures that all the nebula3 text outputs pass through a centralized inbound and outbound channel. A specific console processor can process text output in a specific way (for example, output to stdout, game console, log file, or network connection ).
  • Redirection characterAs the path alias. the general functions are similar to those of Nebula1 and 2, except for the inspiration from the amigaos redirection. A new feature of the nebula3 redirection is that they can be used as the URI alias. for example, the redirection character "Textures:" can be defined as "http://www.radonlabs.de/textures", which simplifies the resource path "Textures: mytexture. DDS "will be interpreted as this absolute path:" http://www.radonlabs.de/textures/mytexture.dds "(too Nb, put the texture on the site to load? Haha, it must be nice to use built-in advertising)
  • Stream)As the basic data access channel. it provides the basic API functions open ()/close ()/read ()/write (), but may completely hide the transmission and storage channels. typical examples include IO: filestream, IO: memorystream, or net: httpstream.
  • Stream ReaderAnd writerIt is connected to the stream and implements easy-to-use interfaces to read and write data formats. for example, you can connect IO: xmlreader to IO: filestream to read XML data from the file system, or connect IO: httpstream to read XML data from the HTTP connection.
Here is a good code example that reflects the power of the nebula3 Input/Output System:
1: IO: fileserver: instance()-> Copyfile(Http://www.radonlabs.de/index.html","Temp: index.html");
This line of code copies a file from the HTTP server to the temporary directory of the user. add a few more lines of code. You can create a stream object pointing to an HTML file on the HTTP server and connect an XML reader to the stream, then you can parse HTML without storing intermediate files. Standard redirection characterNebula3 initializes the following redirection characters:
  • Home:Point to the application directory. In general, under "C:/Program Files". nebula3 regards this directory as read-only, so that it can run without administrator permissions.
  • User:This point points to the user directory currently logged on, which generally refers to "C:/Documents and Settings/[username]". nebula3 automatically creates a local directory to prevent different programs from overwriting their data. therefore, it is generally safe to write data into the user directory. this area can be used to save game data and configurations, or the persistent data that the program needs to call.
  • Temp:This point points to the temporary directory of the current user, which is generally writable, but do not assume that the data still exists at the next Startup Program.
  • Bin:This directs to the directory of the executable files of the application. It can be the same as home, or it may be different. This Directory should also be treated as read-only.
Other redirection characters can be defined when the program is running. some abstract resource paths, such as textuers, sound, and data, are usually defined. in this way, the resource path can only change the definition of the redirection operator, but not replace all the paths. another advantage of the redirection is that it reduces the length of the path string and saves memory usage in a certain program. Uri (Uniform Resource Identifier)The resource location in nebula3 is usually defined by Uri. Uri generally includes the following parts, some of which are optional:
  • Mode (protocol ?), For example, "http:", "file:", etc. .. nebula3 does not have any hard-coded mode, but is bound with the stream class and registered with IO: streamserver single-piece
  • An optional user information field. This is a user name and password used for HTTP or FTP Host Authentication.
  • A host name, such as "www.radonlabs.de"
  • An optional port number after the Host Name
  • A local path pointing to a resource on the host
  • An optional segment, usually pointing to a location inside the resource
  • An optional query section generally contains parameters of a PHP script or similar dynamic response mechanism.
IO: The URI class is used to pass the URI and parse the URI string to each part of it. it is worth noting that the URI object occupies more memory than the string, so sometimes it is better to save the URI in the string and use the IO: URI class only when splitting is required. here are some Uri examples:
1: file: // C:/temp/bla.txt 2: file: // Samba/temp/bla.txt 3: http://www.radonlabs.de/index.html4: http: // User: password@www.myserver.com: 8080/index.html # Main
By using the relocate operator, the path name is greatly simplified. to reference a program directory file, you can use "Home: bla.txt", which is equivalent to file: // C:/program files/[MyApp]/bla.txt. Stream, reader, and writerStream provides an interface for storing and transmitting raw data. A stream object provides the traditional open ()/close ()/read ()/write ()/seek () interfaces, some of which also provide memory ing, in this way, data can be read and written directly through memory access. stream objects use an IO: URI object to define their resource locations. generally, a URI format is mapped to a specific stream object. for example, the "http:" URI format is generally mapped to the Net: httpstream class, while the "file:" format is mapped to the IO: filestream class. this ing is used by streamserver to construct a stream object and match a URI. A nebula3 application registers this ing through the streamserver: Register () method, which is also a registration method for new stream objects and Uri formats. let's take a look at the important classes:
  • IO: filestream: provides the ability to access the host File System
  • IO: memorystream: a dynamic memory buffer with stream Interfaces
  • IO: httpstream: provides a stream interface to access HTTP server files.
The stream reader and writer classes provide some comfortable interfaces dedicated to processing specific data formats. Here there are some stream reader and Writer:
  • IO: binaryreader/iobinarywriter: read/write binary data
  • IO: textreader/iotextwriter: read/write text data
  • IO: xmlreader/ioxmlwriter: reads and writes data in XML format.
  • Messaging: messagereader/messagingmessagewriter: Message serialization
Here is a simple example of using xmlreader to access files from the HTTP server.
1: Using namespace Io;2: 3: PTR <stream> stream = streamserver: instance()-> Createstream(Http://www.radonlabs.de/index.html");4: PTR <xmlreader> xmlreader = xmlreader: Create();5: xmlreader-> setstream(Stream);6:If (Xmlreader-> open())7:{8:// Parse content here using the xmlreader Interface9:}
  File Server)Nebula3 IO: The fileserver class provides a single piece for accessing the file system of the host for some global operations, such as defining the redirection, copying, deleting, and checking whether the file exists, listing the directory content, and so on. this code snippet describes some useful methods for fileserver:
Using namespace IO; using namespace util; fileserver * FS = fileserver: instance (); // check if a file or directory existsbool fileexists = FS-> fileexists ("home: bla.txt "); bool direxists = FS-> directoryexists (" Temp: Bla/blub "); // resolve a path with assigns into an absolute filesystem // path, this is sometimes necessary to interface with // 3rd party libraries which don't understand nebula3 paths directlystring abspath = FS-> resolveassings ("User: MyApp/savegames "); // create a directory, note that all missing subdirectories will // be created as wellfs-> createdirectory ("User: MyApp/savegames "); // copy and delete filesfs-> copyfile ("Home: movie. MPG "," Temp: movie. MPG "); FS-> deletefile (" Temp: movie. MPG "); // list files in a directory matching a patternarray <string> files = FS-> listfiles (" Temp :","*. TXT "); // list all subdirectories in temp: array <string> dirs = FS-> listdirectories (" Temp :","*");
  ConsoleGenerally, IO: console is not directly called. n_printf (), n_error (), n_dbgout (), n_warning ()@_@

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.