SC Readfile/Writefile Line parameter.

Features wanted...
Post Reply
eil
Posts: 1870
Joined: 13 Jan 2011 19:44

SC Readfile/Writefile Line parameter.

Post by eil »

currently there are 2 ways to handle files containing text: either operate them as ini structured or as raw text with full/bit-by-bit load. it could be great if Readfile/Writefile allow to specify a line in text that is target.
if i understand it correct, that's smt like counting how many line-brakers to skip = so if the target is text in line 16, then 15 line-brakers are skipped and all text between 15-16 brakers is target for operation.

there are already numerous scripts that had to deal with this task in different ways(regex, sorting, search, etc), and those would benefit from such enhance for sure.
examples:
- operating with numerous files of similar structure to fast get particular information;
- creating "settings" for scripts without actually need to use .ini structure;
- lots of manipulations with simple text files, paper folder structure, modifying other script files.
Win 7 SP1 x64 100% 1366x768|1900x1080

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC Readfile/Writefile Line parameter.

Post by highend »

Write a simple function that uses a double gettoken to get a specific line?
Don't know why somebody would use a sort or search for this kind of stuff (while regex could at least be used)...

You could also easily add ranges if necessary via this approach...
One of my scripts helped you out? Please donate via Paypal

eil
Posts: 1870
Joined: 13 Jan 2011 19:44

Re: SC Readfile/Writefile Line parameter.

Post by eil »

if that all could be simple for any user(non pro-scripter) i wouldn't try to propose. :roll:
besides there are already "start reading from specific bite of file" and "read from specific key in ini", so why not expand this functionality for more comfortable operations. :whistle:
Win 7 SP1 x64 100% 1366x768|1900x1080

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC Readfile/Writefile Line parameter.

Post by highend »

start reading from specific bite of file" and "read from specific key in ini"
The second one is a structured (and therefore: well defined) document, that's something completely different from an unstructured "any type, any codepage" type of file.
What XY does in the first place is just reading from a specific pointer position onwards. That's something that the OS provides with file seek...

The rest is about understanding what is read there (and that can be rather difficult (ascii, utf-8, unicode with variable byte lengths for characters)).

I already gave you the most important part of that function: A double gettoken() that defines a range (which ofc can be a single line only).
The rest is just checking for boundaries and setting them appropriately so that the "flags" parameter doesn't exceed these boundaries, because
that function would error out in that case.

Code: Select all

function GetRangeFromFile($file, $range="", $codepage="", $lb=<crlf>) {
    $content = readfile($file, , , $codepage);
    ... 8 lines of code, checking and setting boundaries for $first and $last + catching corner cases ...
    return gettoken(gettoken($content, $first, $lb, , 2), ($last - $first) + 1, $lb, , 1);
}
And the whole thing can be feed with:
$range = "" -> Return everything
$range = 5 -> Return line number 5
$range = "3-20" -> Return line 3 up to line 20 and if the file has only 15 lines, it returns line 3 up to 15...
etc...
One of my scripts helped you out? Please donate via Paypal

Post Reply