textfile

Top  Previous  Next

textfile

o=new("textfile"[,filename$])

 

The textfile class provides line-oriented text file processing capabilities.  Each line in the file is considered a record, and lines are separated by linefeeds ($0A$) on Unix-like systems, and carriage return-linefeed ($0D0A$) sequences on Windows systems.

 

If filename$ is provided, the file's existing content is loaded, or it is created if necessary.  If no filename$ is provided, a temporary file is created that is erased when the object is destroyed.

 

Properties

 

All properties are read-only.

 

filename$ contains the file name of the text file being managed.

lines contains the number of lines in the file.

found is an unkeyed collection object that holds the results of find operations.  Each element of the collection is a matching line from the file.

 

 

Methods

append(row$) appends the line row$ to the end of the file.

delline(line) removes the line specified from the file, shifting remaining rows up.

find(search$[,nocase[,invert]]) initializes the found collection object, then fills it with text lines whose data contain the text search$.  If nocase is true (1), then the search is case-insensitive.  If invert is true (1), then records that do not contain search$ are added, rather than those that do.  The function returns the number of records found.

findreg(regex$[,nocase[,invert]]) initializes the found collection object, then fills it with text lines whose data match the regular expression regex$.  If nocase is true (1), then the search is case-insensitive.  If invert is true (1), then records that do not match regex$ are added, rather than those that do. The function returns the number of records found.

findwhere(whereexpr$,dlm$,quotes) initializes the found collection, then searches records that match the where expression.  For this search, records are assumed to be in delimited format, with the supplied dlm$ value as the delimiter.  If quotes is true (1), then fields are parsed assuming they may be quoted to protect delimiter values in field values.

 

The structure of the where expression is of a Boolean expression using fields, values, comparison operators, parentheses, and AND or OR, using #number syntax to represent field numbers.  All fields are assumed to be string data, but you can use num() to convert strings to numbers, so long as the string is an unpunctuated numeric value.  Here are some examples:

 

#2<>"" - field 2 not null

 

#2="100" and (num(#3)>=0 and num(#3)<10000) - field 2 is "100" and field 3 is between 0 and 10000

getline$(line) returns the line number specified.

insline(line,row$) inserts the line row$ into the file at the line specified.  Lines are 1-based, so insline(1,"text line") inserts "text line" at the beginning of the file.  If line is more than the number of lines in the file, new lines are added to ensure the file has at least that many lines.

purge() removes all lines from the file.

putline(line,row$) replaces the line specified with the contents in row$.  If the line exceeds the number of lines in the file, lines are added as necessary.