Class Reference file

This class enables reading and writing files to disk. More...

Member

string baseName()
string canonicalPath()
void close()
string currentPath()
bool exists()
void find(string path, string s)
void findDir(string path, string s)
void findNext()
string filename
string homePath()
bool isDir(string base)
bool link(string linkname)
bool mkDir(string baseDir, string name)
bool open(bool mode=true)
string path()
string projectPath()
string read()
intList readBinary()
bool remove()
bool rename(string newName)
bool rmDir(string baseDir, string name)
void setCurrentPath(string path)
void setCodec(string codec)
string suffix()
string tempPath()
void write(string s)
void writeBinary(intList list)

Detailed Description

graph LR f("file f") f--read-->s(string) f--readBinary-->il(intList) click f "/layoutscript/api/file" click s "/layoutscript/api/string" click il "/layoutscript/api/intlist" classDef thisClas fill:#eaf5fc,stroke:#3c7faa,stroke-width:4px; classDef clas fill:#eaf5fc,stroke:#3c7faa,stroke-width:1px; class f thisClas class s,il clas

A file is a local object for reading and writing disk files and for a few directory helpers. Construct file f, set filename, call open, then read or write, then close. open(true) is read-only; open(false) is write-only. read returns the whole file as a string; readBinary returns an intList with one int per byte. find and findDir search under a path and store a match in filename; findNext continues that search. Static members homePath, tempPath, projectPath, mkDir, rmDir, and isDir do not need an open file. This class exists only for LayoutEditor C++ macros. Python scripts use the language's own file and path libraries.

Example:

 file f;

 // add platform specific path like "/home/username/filename" or "c:/my Files/filename.txt"
 string s="poly_on_23.txt";
 f.filename=s;
 bool b=false;

 //open for output
 f.open(b);

 s="data";

 // write string
 f.write(s);

 // close file
 f.close();

Member Function Documentation


string file::baseName()

Returns: the base name of the file without the path and suffix.


string file::canonicalPath()

Returns: the canonical path of the file without any '.' and '..'. (introduced with release 20210102)


void file::close()

Closes the file.


string file::currentPath()

Returns: the current working path.


bool file::exists()

Set filename before calling this method.

Returns: true if that file exists, otherwise false.

 file f;
 f.filename="myfile.txt";
 if (f.exists()) {
 // do something
 }

void file::find(string path, string s)

Finds a file matching search string s under path. If a match is found, filename is set. Otherwise filename is set to an empty string.

Parameter:
path path
s text

void file::findDir(string path, string s)

Finds a folder matching search string s under path. If a match is found, filename is set. Otherwise filename is set to an empty string.

Parameter:
path path
s text

void file::findNext()

Finds another match after find or findDir. If a match is found, filename is set. Otherwise filename is set to an empty string.


string file::filename

Name of the file to operate with. Set this before open, exists, remove, or rename.


static string file::homePath()

Returns: the user's home path.


static bool file::isDir(string base)

Returns: true if base is an existing directory, otherwise false.


bool file::link(string linkname)

Creates linkname as a link to the current filename.

Returns: true if the link was created, otherwise false.


static bool file::mkDir(string baseDir, string name)

Creates the folder name inside baseDir.

Returns: true if successful, otherwise false.

Parameter:
baseDir baseDir
name name

bool file::open(bool mode=true)

Opens the file named in filename. If mode is true, the file is opened read-only. If mode is false, the file is opened write-only.

Returns: true if the operation was successful, otherwise false.


string file::path()

Returns: the file's path without the file name.


static string file::projectPath()

Returns: the path where the LayoutEditor was started from. In most applications this is the project folder. (introduced with release 20201229)


string file::read()

Returns: the whole file as a string.


intList file::readBinary()

Returns: the whole file as an intList. Each byte becomes one int.

 intList list;
 file f;
 f.filename="data.bin";
 bool b=true;
//open for input
 f.open(b);
 list=f.readBinary();
// close file
 f.close();

bool file::remove()

Removes the file specified by filename.

Returns: true if successful, otherwise false.


bool file::rename(string newName)

Renames the file specified by filename to newName.

Returns: true if successful, otherwise false.


static bool file::rmDir(string baseDir, string name)

Removes the folder name inside baseDir.

Returns: true if successful, otherwise false.

Parameter:
baseDir baseDir
name name

void file::setCurrentPath(string path)

Sets the default working directory to path.


void file::setCodec(string codec)

Uses codec to encode input and output. Many codecs are supported, such as UTF-8, UTF-16, ISO-8859-13.


string file::suffix()

Returns: the suffix of the file.


static string file::tempPath()

Returns: the system's temporary-files path.


void file::write(string s)

Overwrites the file with string s.


void file::writeBinary(intList list)

Overwrites the file with intList list. Each int is written as one byte.

 file f;
 f.filename="data.bin";
 bool b=false;
 //open for output
 f.open(b);
 intList list;
 list.append(1);
 list.append(32);
 list.append(67);
 f.writeBinary(list);
// close file
 f.close();