|
Romfile Object (V1.1) |
Top Previous Next |
The romfile object allows you to access resource files that you have added to your project. Resource files appear in the "Resource Files" folder of your project tree. These are files that are not processed by the compiler in any way, just added to the compiled binary file.
You can use resource files to store some permanent data that is too large to fit in a constant or when it is just more convenient for you to handle this data as a separate file.
The romfile.open method allows you to open the file you need. Only one file can be opened at any given time and there is no need to close it. The size of the file you have opened can be checked through the romfile.size read-only property.
|
If you attempt to open a non-existent file its size will be returned as 0. This is how you know that the file does not exist! |
There is a file pointer, accessible through the romfile.pointer property. When you (re)open the file the pointer is at file position 1 (first character in the file). You read the data from the file using the romfile.getdata method. As you read from the file, the pointer is moving- each time by the number of characters you've just read out. You can also move the pointer to the new position by writing desired value into the romfile.pointer property.
|
The pointer is a variable of word type, therefore, you can only access up to 65534 bytes in each file. You can add larger files to the project but only first 65534 bytes of each file will be accessible from your application. |
The romfile.find method is used to find desired substring in the resource file. This allows you to organize quick search for the file portion you need, which is very useful if you keep some table data in the resource file (again, you can only search within first 65534 bytes of the file).
For example, supposing you have the following data in the file <settings.txt>:
IP,4,4,127.0.0.1 Port,0,65535,1001 Protocol,0,1,0 ... ...
|
This sample data represents the list of settings (user-definable parameters) that your program keeps somewhere in the non-volatile memory (i.e. stor object). Each line of the file describes the setting name, minimum value (length), maximum value (length), and default value.
Now, supposing you need to extract the string describing the "Port" setting. Here is the code:
dim w as word dim s as string
romfile.open("settings.txt")
if romfile.size=0 then 'File does not exist! Do something about it! end if
'look for the 'port' substring w=romfile.find(1,"Port",1) if w=0 then 'Line not found- do something about it! end if romfile.pointer=w 'set the pointer
'now look for the end of this line w=romfile.find(w,chr(13),1) 'we assume that the CR (ASCII=13) will definitely be encountered
'OK, now read s=romfile.getdata(w-romfile.pointer)
's now contains desired line from the file <settings.txt> ... ...
|
Notice, that with the romfile object characters are counted from 1, not 0.
Sometimes, you will need to open the file not for the purpose of accessing it, but for the purpose of passing a reference to this file to another object. Such reference "pointer" is provided by the romfile.offset read-only property.