Section Files
Files utility functions
Class
Class | Summary |
---|---|
INI | Rough INI configuration file reader and writer. |
Summary
Return type | Function and summary |
---|---|
table | FileReadLines(string path, int stringEncoding, int crlf, boolean errorIfNotFound) Syntaxic sugar to read all lines of a file, and return a table of them. |
FileWriteLines(string path, table lines, function formatFunc, int stringEncoding, int crlf) Syntaxic sugar to writes a table of strings as lines in a file. | |
table | GetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath)BrowseFolder(...) needs a callback function, and has a bug: here is a fixed and simplified version. |
string | ScriptReadVersion(string file) Read the version of a script. |
string, string | SplitPath(string path) Split a path into directory and file, no file if path ends by a '/'. |
Functions
GetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath)
BrowseFolder(...)
needs a callback function, and has a bug: here is a fixed and simplified version.
Parameter | Type | Default | Description |
---|---|---|---|
folder | string | Folder name | |
mask | string | "*.*" | Mask to search file, e.g. "*.myr", "a*.*" |
recursive | boolean | false | true to browse sub-folders |
fullPath | boolean | false | true returns the full path, false returns only file names |
- Return
- table: File names: array of string, in alphabetic order
FileReadLines(string path, int stringEncoding, int crlf, boolean errorIfNotFound)
Syntaxic sugar to read all lines of a file, and return a table of them.
Note:
- encoding is supposed to be UTF-8 BOM and lines terminated by Macintosh CR.
- Convenient for small files when full content have to been read and stored.
Parameter | Type | Default | Description |
---|---|---|---|
path | string | The full path of the file to read. | |
stringEncoding | int | UTF8_STR_ENCODING | String encoding constant from MSDefine, e.g. UNKNOWN_STR_ENCODING |
crlf | int | MAC_CR | Carriage return constant from MSDefine, MAC_CR, LINUX_CR, WIN_CR, UNKNOWN_CR |
errorIfNotFound | boolean | (defaut=true) Throw error if file is not found, else return an empty table. |
- Return
- table: Table of string, one item per line
- Errors
- if argument is not a string
- if file can't be read
- See
- FileWriteLines
- Example
local lines = FileReadLines("/path/to/myConfigFile.txt") print(lines[1]) -- first line of the config file
FileWriteLines(string path, table lines, function formatFunc, int stringEncoding, int crlf)
Syntaxic sugar to writes a table of strings as lines in a file.
Collection and Map are also handled, and an optional formating function can transform lines before writing. Notes:
- The file is overwritten,
- Encoding is UTF-8 BOM and lines are terminated by Macintosh CR.
Parameter | Type | Default | Description |
---|---|---|---|
path | string | The full path of the file to write | |
lines | table | The lines to write, can be a table, a Collection or a Map | |
formatFunc | function | nil | Formatting function that receive two arguments and must return a string that will be written into the file. Arguments are:
nil then table and Collection prints their values, Map prints key and values separated by a space char. |
stringEncoding | int | UTF8_STR_ENCODING | String encoding constant from MSDefine, e.g. HTML_STR_ENCODING |
crlf | int | MAC_CR | Carriage return constant from MSDefine, MAC_CR, LINUX_CR, WIN_CR, UNKNOWN_CR |
- Errors
- if arguments have bad type.
- if file can't be written.
- See
- FileReadLines
- Examples
local lines = FileReadLines("/path/to/myConfigFile.txt") lines[1] = "0" -- change a value in the config file FileWriteLines("/path/to/myConfigFile.txt", lines)
local map = Map:new("number", "string", SORT_ASCENDING) map:put(1, "one"); map:put(2, "two"); map:put(3, "three") FileWriteLines("/path/to/file/count.txt", map, function(key,value) return tostring(key).."="..strupper(value); end) ) -- outputs 3 lines 1=ONE, 2=TWO and 3=THREE
ScriptReadVersion(string file)
Read the version of a script.
Look for the --VERSION: x.y in first 30 lines of source code.
Parameter | Type | Default | Description |
---|---|---|---|
file | string | Script file name (or full path) |
- Return
- string: A version in the x.y.z or yyyyMMdd form (as source code), or
nil
if file doesn't exist or version not found.
SplitPath(string path)
Split a path into directory and file, no file if path ends by a '/'.
Parameter | Type | Default | Description |
---|---|---|---|
path | string | path |
- Returns
- string: directory
- string: file name