readline from char[]
Derek Parnell
derek at psych.ward
Sun May 6 04:37:25 PDT 2007
On Sun, 6 May 2007 11:31:14 +0200, Jan Hanselaer wrote:
> Hi
>
> Is it possible to read lines from a char[] buffer? I ask this because
> reading lines directly from a file (with a BufferedStream) seems to be very
> slow.
> So when I can first load my file
>
> char[] file = cast(char[])std.file.read("filename");
>
> and then read lines from file that would go faster I presume.
>
> But I don't see a way to do this.
>
> Anyone with an idea for this problem?
>
Here are two routines that I use ...
// -----------------
import std.file;
import std.string;
enum GetOpt
{
Exists = 'e', // Must exist otherwise Get fails.
Always = 'a' // Get always returns something, even if it's just
// empty lines for a missing file.
}
// Read an entire file into a string.
char[] GetText(char[] pFileName, GetOpt pOpt = GetOpt.Always)
{
char[] lFileText;
if (std.file.exists( pFileName))
{
lFileText = cast(char[]) std.file.read(pFileName);
// Ensure last line has a EOL.
if ( (lFileText.length == 0) ||
(lFileText[$-1] != '\n'))
lFileText ~= std.path.linesep;
}
else if (pOpt == GetOpt.Exists)
{
throw new Exception( std.string.format("File '%s' not found.",
pFileName));
}
return lFileText;
}
// Read a entire file in to a set of lines (strings).
char[][] GetTextLines(char[] pFileName, GetOpt pOpt = GetOpt.Always)
{
char[][] lLines;
char[] lText;
lText = GetText(pFileName, pOpt);
lLines = std.string.splitlines( lText );
return lLines;
}
--
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell
More information about the Digitalmars-d-learn
mailing list