mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-11-21 16:33:51 +01:00
[svn r135] * Merged DMD 1.025 *
* Fixed a minor linking order mishap * * Added an command line option -annotate * * Fixed some problems with running optimizations * * Added std.stdio and dependencies to lphobos (still not 100% working, but compiles and links) * * Fixed problems with passing aggregate types to variadic functions * * Added initial code towards full GC support, currently based on malloc and friends, not all the runtime calls the GC yet for memory * * Fixed problems with resolving nested function context pointers for some heavily nested cases * * Redid function argument passing + other minor code cleanups, still lots to do on this end... *
This commit is contained in:
+1578
File diff suppressed because it is too large
Load Diff
+18
-11
@@ -170,15 +170,12 @@ private TypeInfo primitiveTypeInfo(Mangle m)
|
||||
ti = typeid(idouble);break;
|
||||
case Mangle.Tireal:
|
||||
ti = typeid(ireal);break;
|
||||
/+
|
||||
// No complex in LLVMDC yes
|
||||
case Mangle.Tcfloat:
|
||||
ti = typeid(cfloat);break;
|
||||
case Mangle.Tcdouble:
|
||||
ti = typeid(cdouble);break;
|
||||
case Mangle.Tcreal:
|
||||
ti = typeid(creal);break;
|
||||
+/
|
||||
case Mangle.Tchar:
|
||||
ti = typeid(char);break;
|
||||
case Mangle.Twchar:
|
||||
@@ -450,7 +447,8 @@ formattedPrint("The answer is %s:", x, 6);
|
||||
*/
|
||||
|
||||
void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
{ int j;
|
||||
{ //printf("doFormat(...)\n");
|
||||
int j;
|
||||
TypeInfo ti;
|
||||
Mangle m;
|
||||
uint flags;
|
||||
@@ -503,7 +501,7 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
|
||||
void putstr(char[] s)
|
||||
{
|
||||
//printf("flags = x%x\n", flags);
|
||||
//printf("flags = 0x%x\n", flags);
|
||||
int prepad = 0;
|
||||
int postpad = 0;
|
||||
int padding = field_width - (strlen(prefix) + s.length);
|
||||
@@ -539,7 +537,8 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
|
||||
void putreal(real v)
|
||||
{
|
||||
//printf("putreal %Lg\n", vreal);
|
||||
//printf("putreal %Lg\n", vreal); // no 80 bit float
|
||||
//printf("putreal %g\n", vreal);
|
||||
|
||||
switch (fc)
|
||||
{
|
||||
@@ -593,9 +592,11 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
format[i + 0] = '*';
|
||||
format[i + 1] = '.';
|
||||
format[i + 2] = '*';
|
||||
format[i + 3] = 'L';
|
||||
format[i + 4] = fc;
|
||||
format[i + 5] = 0;
|
||||
format[i + 3] = fc;
|
||||
format[i + 4] = 0;
|
||||
//format[i + 3] = 'L'; // no 80 bit yet
|
||||
//format[i + 4] = fc;
|
||||
//format[i + 5] = 0;
|
||||
if (!(flags & FLprecision))
|
||||
precision = -1;
|
||||
while (1)
|
||||
@@ -638,7 +639,8 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
auto tiSave = ti;
|
||||
auto mSave = m;
|
||||
ti = valti;
|
||||
//printf("\n%.*s\n", valti.classinfo.name);
|
||||
auto className = valti.classinfo.name;
|
||||
printf("\n%.*s\n", className.length, className.ptr);
|
||||
m = getMan(valti);
|
||||
while (len--)
|
||||
{
|
||||
@@ -831,10 +833,12 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
goto Lcomplex;
|
||||
|
||||
case Mangle.Tsarray:
|
||||
//printf("static array\n");
|
||||
putArray(argptr, (cast(TypeInfo_StaticArray)ti).len, (cast(TypeInfo_StaticArray)ti).next);
|
||||
return;
|
||||
|
||||
case Mangle.Tarray:
|
||||
//printf("dynamic array\n");
|
||||
int mi = 10;
|
||||
if (ti.classinfo.name.length == 14 &&
|
||||
ti.classinfo.name[9..14] == "Array")
|
||||
@@ -863,6 +867,7 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
return;
|
||||
}
|
||||
|
||||
//printf("primitive type\n");
|
||||
while (1)
|
||||
{
|
||||
m2 = cast(Mangle)ti.classinfo.name[mi];
|
||||
@@ -897,6 +902,7 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
continue;
|
||||
|
||||
default:
|
||||
//printf("primitive type default handling\n");
|
||||
TypeInfo ti2 = primitiveTypeInfo(m2);
|
||||
if (!ti2)
|
||||
goto Lerror;
|
||||
@@ -1058,9 +1064,10 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, void* argptr)
|
||||
}
|
||||
|
||||
|
||||
//printf("arguments length: %u\n", arguments.length);
|
||||
for (j = 0; j < arguments.length; )
|
||||
{ ti = arguments[j++];
|
||||
//printf("test1: '%.*s' %d\n", ti.classinfo.name, ti.classinfo.name.length);
|
||||
//printf("test1: '%.*s' %d\n", ti.classinfo.name.length, ti.classinfo.name.ptr, ti.classinfo.name.length);
|
||||
//ti.print();
|
||||
|
||||
flags = 0;
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 1999-2006 by Digital Mars, www.digitalmars.com
|
||||
* Written by Walter Bright
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* o The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* o Altered source versions must be plainly marked as such, and must not
|
||||
* be misrepresented as being the original software.
|
||||
* o This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The garbage collector normally works behind the scenes without needing any
|
||||
* specific interaction. These functions are for advanced applications that
|
||||
* benefit from tuning the operation of the collector.
|
||||
* Macros:
|
||||
* WIKI=Phobos/StdGc
|
||||
*/
|
||||
|
||||
module std.gc;
|
||||
|
||||
import gcstats;
|
||||
|
||||
/**
|
||||
* Add p to list of roots. Roots are references to memory allocated by the
|
||||
collector that are maintained in memory outside the collector pool. The garbage
|
||||
collector will by default look for roots in the stacks of each thread, the
|
||||
registers, and the default static data segment. If roots are held elsewhere,
|
||||
use addRoot() or addRange() to tell the collector not to free the memory it
|
||||
points to.
|
||||
*/
|
||||
void addRoot(void *p); // add p to list of roots
|
||||
|
||||
/**
|
||||
* Remove p from list of roots.
|
||||
*/
|
||||
void removeRoot(void *p); // remove p from list of roots
|
||||
|
||||
/**
|
||||
* Add range to scan for roots.
|
||||
*/
|
||||
void addRange(void *pbot, void *ptop); // add range to scan for roots
|
||||
|
||||
/**
|
||||
* Remove range.
|
||||
*/
|
||||
void removeRange(void *pbot); // remove range
|
||||
|
||||
/**
|
||||
* Mark a gc allocated block of memory as possibly containing pointers.
|
||||
*/
|
||||
void hasPointers(void* p);
|
||||
|
||||
/**
|
||||
* Mark a gc allocated block of memory as definitely NOT containing pointers.
|
||||
*/
|
||||
void hasNoPointers(void* p);
|
||||
|
||||
/**
|
||||
* Mark a gc allocated block of memory pointed to by p as being populated with
|
||||
* an array of TypeInfo ti (as many as will fit).
|
||||
*/
|
||||
void setTypeInfo(TypeInfo ti, void* p);
|
||||
|
||||
/**
|
||||
* Allocate nbytes of uninitialized data.
|
||||
* The allocated memory will be scanned for pointers during
|
||||
* a gc collection cycle, unless
|
||||
* it is followed by a call to hasNoPointers().
|
||||
*/
|
||||
void[] malloc(size_t nbytes);
|
||||
|
||||
/**
|
||||
* Resize allocated memory block pointed to by p to be at least nbytes long.
|
||||
* It will try to resize the memory block in place.
|
||||
* If nbytes is 0, the memory block is free'd.
|
||||
* If p is null, the memory block is allocated using malloc.
|
||||
* The returned array may not be at the same location as the original
|
||||
* memory block.
|
||||
* The allocated memory will be scanned for pointers during
|
||||
* a gc collection cycle, unless
|
||||
* it is followed by a call to hasNoPointers().
|
||||
*/
|
||||
void[] realloc(void* p, size_t nbytes);
|
||||
|
||||
/**
|
||||
* Attempt to enlarge the memory block pointed to by p
|
||||
* by at least minbytes beyond its current capacity,
|
||||
* up to a maximum of maxbytes.
|
||||
* Returns:
|
||||
* 0 if could not extend p,
|
||||
* total size of entire memory block if successful.
|
||||
*/
|
||||
size_t extend(void* p, size_t minbytes, size_t maxbytes);
|
||||
|
||||
/**
|
||||
* Returns capacity (size of the memory block) that p
|
||||
* points to the beginning of.
|
||||
* If p does not point into the gc memory pool, or does
|
||||
* not point to the beginning of an allocated memory block,
|
||||
* 0 is returned.
|
||||
*/
|
||||
size_t capacity(void* p);
|
||||
|
||||
/**
|
||||
* Set gc behavior to match that of 1.0.
|
||||
*/
|
||||
void setV1_0();
|
||||
|
||||
/***********************************
|
||||
* Run a full garbage collection cycle.
|
||||
*
|
||||
* The collector normally runs synchronously with a storage allocation request
|
||||
(i.e. it never happens when in code that does not allocate memory). In some
|
||||
circumstances, for example when a particular task is finished, it is convenient
|
||||
to explicitly run the collector and free up all memory used by that task. It
|
||||
can also be helpful to run a collection before starting a new task that would
|
||||
be annoying if it ran a collection in the middle of that task. Explicitly
|
||||
running a collection can also be done in a separate very low priority thread,
|
||||
so that if the program is idly waiting for input, memory can be cleaned up.
|
||||
*/
|
||||
|
||||
void fullCollect();
|
||||
|
||||
/***********************************
|
||||
* Run a generational garbage collection cycle.
|
||||
* Takes less time than a fullcollect(), but isn't
|
||||
* as effective.
|
||||
*/
|
||||
|
||||
void genCollect();
|
||||
|
||||
void genCollectNoStack();
|
||||
|
||||
/**
|
||||
* Minimizes physical memory usage
|
||||
*/
|
||||
void minimize();
|
||||
|
||||
/***************************************
|
||||
* disable() temporarily disables garbage collection cycle, enable()
|
||||
* then reenables them.
|
||||
*
|
||||
* This is used for brief time critical sections of code, so the amount of time
|
||||
* it will take is predictable.
|
||||
* If the collector runs out of memory while it is disabled, it will throw an
|
||||
* std.outofmemory.OutOfMemoryException.
|
||||
* The disable() function calls can be nested, but must be
|
||||
* matched with corresponding enable() calls.
|
||||
* By default collections are enabled.
|
||||
*/
|
||||
|
||||
void disable();
|
||||
|
||||
/**
|
||||
* ditto
|
||||
*/
|
||||
void enable();
|
||||
|
||||
void getStats(out GCStats stats);
|
||||
|
||||
/***************************************
|
||||
* Get handle to the collector.
|
||||
*/
|
||||
|
||||
void* getGCHandle();
|
||||
|
||||
/***************************************
|
||||
* Set handle to the collector.
|
||||
*/
|
||||
|
||||
void setGCHandle(void* p);
|
||||
|
||||
void endGCHandle();
|
||||
|
||||
extern (C)
|
||||
{
|
||||
void gc_init();
|
||||
void gc_term();
|
||||
}
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
/* This is for use with variable argument lists with extern(D) linkage. */
|
||||
|
||||
/* Modified for LLVMDC (LLVM D Compiler) by Tomas Lindquist Olsen, 2007 */
|
||||
|
||||
module std.stdarg;
|
||||
|
||||
alias void* va_list;
|
||||
|
||||
T va_arg(T)(inout va_list vp)
|
||||
T va_arg(T)(ref va_list vp)
|
||||
{
|
||||
static assert((T.sizeof & (T.sizeof -1)) == 0);
|
||||
va_list vptmp = cast(va_list)((cast(size_t)vp + T.sizeof - 1) & ~(T.sizeof - 1));
|
||||
size_t size = T.sizeof > size_t.sizeof ? size_t.sizeof : T.sizeof;
|
||||
va_list vptmp = cast(va_list)((cast(size_t)vp + size - 1) & ~(size - 1));
|
||||
vp = vptmp + T.sizeof;
|
||||
return *cast(T*)vptmp;
|
||||
}
|
||||
|
||||
+538
-34
@@ -1,44 +1,548 @@
|
||||
|
||||
// Written in the D programming language.
|
||||
|
||||
/* Written by Walter Bright and Andrei Alexandrescu
|
||||
* www.digitalmars.com
|
||||
* Placed in the Public Domain.
|
||||
*/
|
||||
|
||||
/********************************
|
||||
* Standard I/O functions that extend $(B std.c.stdio).
|
||||
* $(B std.c.stdio) is automatically imported when importing
|
||||
* $(B std.stdio).
|
||||
* Macros:
|
||||
* WIKI=Phobos/StdStdio
|
||||
*/
|
||||
|
||||
module std.stdio;
|
||||
|
||||
import std.traits;
|
||||
public import std.c.stdio;
|
||||
|
||||
void _writef(T)(T t) {
|
||||
static if (is(T == char)) {
|
||||
printf("%c", t);
|
||||
import std.format;
|
||||
import std.utf;
|
||||
import std.string;
|
||||
import std.gc;
|
||||
import std.c.stdlib;
|
||||
import std.c.string;
|
||||
import std.c.stddef;
|
||||
|
||||
|
||||
version (DigitalMars)
|
||||
{
|
||||
version (Windows)
|
||||
{
|
||||
// Specific to the way Digital Mars C does stdio
|
||||
version = DIGITAL_MARS_STDIO;
|
||||
}
|
||||
else static if (is(T : char[])) {
|
||||
printf("%.*s", t.length, t.ptr);
|
||||
}
|
||||
else static if (is(T : long)) {
|
||||
printf("%ld", t);
|
||||
}
|
||||
else static if (is(T : ulong)) {
|
||||
printf("%lu", t);
|
||||
}
|
||||
else static if (is(T : real)) {
|
||||
printf("%f", t);
|
||||
}
|
||||
else static if (is(T : Object)) {
|
||||
_writef(t.toString());
|
||||
}
|
||||
else static if(isArray!(T)) {
|
||||
_writef('[');
|
||||
if (t.length) {
|
||||
_writef(t[0]);
|
||||
foreach(v; t[1..$]) {
|
||||
_writef(','); _writef(v);
|
||||
}
|
||||
}
|
||||
_writef(']');
|
||||
}
|
||||
else static assert(0, "Cannot writef:"~T.tostring);
|
||||
}
|
||||
|
||||
void writef(T...)(T t)
|
||||
version (DIGITAL_MARS_STDIO)
|
||||
{
|
||||
foreach(v;t) _writef(v);
|
||||
}
|
||||
void writefln(T...)(T t)
|
||||
else
|
||||
{
|
||||
writef(t, '\n');
|
||||
// Specific to the way Gnu C does stdio
|
||||
version = GCC_IO;
|
||||
import std.c.linux.linux;
|
||||
}
|
||||
|
||||
version (DIGITAL_MARS_STDIO)
|
||||
{
|
||||
extern (C)
|
||||
{
|
||||
/* **
|
||||
* Digital Mars under-the-hood C I/O functions
|
||||
*/
|
||||
int _fputc_nlock(int, FILE*);
|
||||
int _fputwc_nlock(int, FILE*);
|
||||
int _fgetc_nlock(FILE*);
|
||||
int _fgetwc_nlock(FILE*);
|
||||
int __fp_lock(FILE*);
|
||||
void __fp_unlock(FILE*);
|
||||
}
|
||||
alias _fputc_nlock FPUTC;
|
||||
alias _fputwc_nlock FPUTWC;
|
||||
alias _fgetc_nlock FGETC;
|
||||
alias _fgetwc_nlock FGETWC;
|
||||
|
||||
alias __fp_lock FLOCK;
|
||||
alias __fp_unlock FUNLOCK;
|
||||
}
|
||||
else version (GCC_IO)
|
||||
{
|
||||
/* **
|
||||
* Gnu under-the-hood C I/O functions; see
|
||||
* http://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams
|
||||
*/
|
||||
extern (C)
|
||||
{
|
||||
int fputc_unlocked(int, FILE*);
|
||||
int fputwc_unlocked(wchar_t, FILE*);
|
||||
int fgetc_unlocked(FILE*);
|
||||
int fgetwc_unlocked(FILE*);
|
||||
void flockfile(FILE*);
|
||||
void funlockfile(FILE*);
|
||||
ssize_t getline(char**, size_t*, FILE*);
|
||||
ssize_t getdelim (char**, size_t*, int, FILE*);
|
||||
}
|
||||
|
||||
alias fputc_unlocked FPUTC;
|
||||
alias fputwc_unlocked FPUTWC;
|
||||
alias fgetc_unlocked FGETC;
|
||||
alias fgetwc_unlocked FGETWC;
|
||||
|
||||
alias flockfile FLOCK;
|
||||
alias funlockfile FUNLOCK;
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(0, "unsupported C I/O system");
|
||||
}
|
||||
|
||||
|
||||
/*********************
|
||||
* Thrown if I/O errors happen.
|
||||
*/
|
||||
class StdioException : Exception
|
||||
{
|
||||
uint errno; // operating system error code
|
||||
|
||||
this(char[] msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
this(uint errno)
|
||||
{ char* s = strerror(errno);
|
||||
super(std.string.toString(s).dup);
|
||||
}
|
||||
|
||||
static void opCall(char[] msg)
|
||||
{
|
||||
throw new StdioException(msg);
|
||||
}
|
||||
|
||||
static void opCall()
|
||||
{
|
||||
throw new StdioException(getErrno());
|
||||
}
|
||||
}
|
||||
|
||||
private
|
||||
void writefx(FILE* fp, TypeInfo[] arguments, void* argptr, int newline=false)
|
||||
{ int orientation;
|
||||
|
||||
orientation = fwide(fp, 0);
|
||||
|
||||
/* Do the file stream locking at the outermost level
|
||||
* rather than character by character.
|
||||
*/
|
||||
FLOCK(fp);
|
||||
scope(exit) FUNLOCK(fp);
|
||||
|
||||
if (orientation <= 0) // byte orientation or no orientation
|
||||
{
|
||||
void putc(dchar c)
|
||||
{
|
||||
if (c <= 0x7F)
|
||||
{
|
||||
FPUTC(c, fp);
|
||||
}
|
||||
else
|
||||
{ char[4] buf;
|
||||
char[] b;
|
||||
|
||||
b = std.utf.toUTF8(buf, c);
|
||||
for (size_t i = 0; i < b.length; i++)
|
||||
FPUTC(b[i], fp);
|
||||
}
|
||||
}
|
||||
|
||||
std.format.doFormat(&putc, arguments, argptr);
|
||||
if (newline)
|
||||
FPUTC('\n', fp);
|
||||
}
|
||||
else if (orientation > 0) // wide orientation
|
||||
{
|
||||
version (Windows)
|
||||
{
|
||||
void putcw(dchar c)
|
||||
{
|
||||
assert(isValidDchar(c));
|
||||
if (c <= 0xFFFF)
|
||||
{
|
||||
FPUTWC(c, fp);
|
||||
}
|
||||
else
|
||||
{ wchar[2] buf;
|
||||
|
||||
buf[0] = cast(wchar) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800);
|
||||
buf[1] = cast(wchar) (((c - 0x10000) & 0x3FF) + 0xDC00);
|
||||
FPUTWC(buf[0], fp);
|
||||
FPUTWC(buf[1], fp);
|
||||
}
|
||||
}
|
||||
}
|
||||
else version (linux)
|
||||
{
|
||||
void putcw(dchar c)
|
||||
{
|
||||
FPUTWC(c, fp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(0);
|
||||
}
|
||||
|
||||
std.format.doFormat(&putcw, arguments, argptr);
|
||||
if (newline)
|
||||
FPUTWC('\n', fp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Arguments are formatted per the
|
||||
* $(LINK2 std_format.html#format-string, format strings)
|
||||
* and written to $(B stdout).
|
||||
*/
|
||||
|
||||
void writef(...)
|
||||
{
|
||||
writefx(stdout, _arguments, _argptr, 0);
|
||||
}
|
||||
|
||||
/***********************************
|
||||
* Same as $(B writef), but a newline is appended
|
||||
* to the output.
|
||||
*/
|
||||
|
||||
void writefln(...)
|
||||
{
|
||||
writefx(stdout, _arguments, _argptr, 1);
|
||||
}
|
||||
|
||||
/***********************************
|
||||
* Same as $(B writef), but output is sent to the
|
||||
* stream fp instead of $(B stdout).
|
||||
*/
|
||||
|
||||
void fwritef(FILE* fp, ...)
|
||||
{
|
||||
writefx(fp, _arguments, _argptr, 0);
|
||||
}
|
||||
|
||||
/***********************************
|
||||
* Same as $(B writefln), but output is sent to the
|
||||
* stream fp instead of $(B stdout).
|
||||
*/
|
||||
|
||||
void fwritefln(FILE* fp, ...)
|
||||
{
|
||||
writefx(fp, _arguments, _argptr, 1);
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* Read line from stream fp.
|
||||
* Returns:
|
||||
* null for end of file,
|
||||
* char[] for line read from fp, including terminating '\n'
|
||||
* Params:
|
||||
* fp = input stream
|
||||
* Throws:
|
||||
* $(B StdioException) on error
|
||||
* Example:
|
||||
* Reads $(B stdin) and writes it to $(B stdout).
|
||||
---
|
||||
import std.stdio;
|
||||
|
||||
int main()
|
||||
{
|
||||
char[] buf;
|
||||
while ((buf = readln()) != null)
|
||||
writef("%s", buf);
|
||||
return 0;
|
||||
}
|
||||
---
|
||||
*/
|
||||
char[] readln(FILE* fp = stdin)
|
||||
{
|
||||
char[] buf;
|
||||
readln(fp, buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* Read line from stream fp and write it to buf[],
|
||||
* including terminating '\n'.
|
||||
*
|
||||
* This is often faster than readln(FILE*) because the buffer
|
||||
* is reused each call. Note that reusing the buffer means that
|
||||
* the previous contents of it need to be copied if needed.
|
||||
* Params:
|
||||
* fp = input stream
|
||||
* buf = buffer used to store the resulting line data. buf
|
||||
* is resized as necessary.
|
||||
* Returns:
|
||||
* 0 for end of file, otherwise
|
||||
* number of characters read
|
||||
* Throws:
|
||||
* $(B StdioException) on error
|
||||
* Example:
|
||||
* Reads $(B stdin) and writes it to $(B stdout).
|
||||
---
|
||||
import std.stdio;
|
||||
|
||||
int main()
|
||||
{
|
||||
char[] buf;
|
||||
while (readln(stdin, buf))
|
||||
writef("%s", buf);
|
||||
return 0;
|
||||
}
|
||||
---
|
||||
*/
|
||||
size_t readln(FILE* fp, inout char[] buf)
|
||||
{
|
||||
version (DIGITAL_MARS_STDIO)
|
||||
{
|
||||
FLOCK(fp);
|
||||
scope(exit) FUNLOCK(fp);
|
||||
|
||||
if (__fhnd_info[fp._file] & FHND_WCHAR)
|
||||
{ /* Stream is in wide characters.
|
||||
* Read them and convert to chars.
|
||||
*/
|
||||
static assert(wchar_t.sizeof == 2);
|
||||
buf.length = 0;
|
||||
int c2;
|
||||
for (int c; (c = FGETWC(fp)) != -1; )
|
||||
{
|
||||
if ((c & ~0x7F) == 0)
|
||||
{ buf ~= c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c >= 0xD800 && c <= 0xDBFF)
|
||||
{
|
||||
if ((c2 = FGETWC(fp)) != -1 ||
|
||||
c2 < 0xDC00 && c2 > 0xDFFF)
|
||||
{
|
||||
StdioException("unpaired UTF-16 surrogate");
|
||||
}
|
||||
c = ((c - 0xD7C0) << 10) + (c2 - 0xDC00);
|
||||
}
|
||||
std.utf.encode(buf, c);
|
||||
}
|
||||
}
|
||||
if (ferror(fp))
|
||||
StdioException();
|
||||
return buf.length;
|
||||
}
|
||||
|
||||
auto sz = std.gc.capacity(buf.ptr);
|
||||
//auto sz = buf.length;
|
||||
buf = buf.ptr[0 .. sz];
|
||||
if (fp._flag & _IONBF)
|
||||
{
|
||||
/* Use this for unbuffered I/O, when running
|
||||
* across buffer boundaries, or for any but the common
|
||||
* cases.
|
||||
*/
|
||||
L1:
|
||||
char *p;
|
||||
|
||||
if (sz)
|
||||
{
|
||||
p = buf.ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
sz = 64;
|
||||
p = cast(char*) std.gc.malloc(sz);
|
||||
std.gc.hasNoPointers(p);
|
||||
buf = p[0 .. sz];
|
||||
}
|
||||
size_t i = 0;
|
||||
for (int c; (c = FGETC(fp)) != -1; )
|
||||
{
|
||||
if ((p[i] = c) != '\n')
|
||||
{
|
||||
i++;
|
||||
if (i < sz)
|
||||
continue;
|
||||
buf = p[0 .. i] ~ readln(fp);
|
||||
return buf.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = p[0 .. i + 1];
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
if (ferror(fp))
|
||||
StdioException();
|
||||
buf = p[0 .. i];
|
||||
return i;
|
||||
}
|
||||
else
|
||||
{
|
||||
int u = fp._cnt;
|
||||
char* p = fp._ptr;
|
||||
int i;
|
||||
if (fp._flag & _IOTRAN)
|
||||
{ /* Translated mode ignores \r and treats ^Z as end-of-file
|
||||
*/
|
||||
char c;
|
||||
while (1)
|
||||
{
|
||||
if (i == u) // if end of buffer
|
||||
goto L1; // give up
|
||||
c = p[i];
|
||||
i++;
|
||||
if (c != '\r')
|
||||
{
|
||||
if (c == '\n')
|
||||
break;
|
||||
if (c != 0x1A)
|
||||
continue;
|
||||
goto L1;
|
||||
}
|
||||
else
|
||||
{ if (i != u && p[i] == '\n')
|
||||
break;
|
||||
goto L1;
|
||||
}
|
||||
}
|
||||
if (i > sz)
|
||||
{
|
||||
buf = cast(char[])std.gc.malloc(i);
|
||||
std.gc.hasNoPointers(buf.ptr);
|
||||
}
|
||||
if (i - 1)
|
||||
memcpy(buf.ptr, p, i - 1);
|
||||
buf[i - 1] = '\n';
|
||||
if (c == '\r')
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if (i == u) // if end of buffer
|
||||
goto L1; // give up
|
||||
auto c = p[i];
|
||||
i++;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
if (i > sz)
|
||||
{
|
||||
buf = cast(char[])std.gc.malloc(i);
|
||||
std.gc.hasNoPointers(buf.ptr);
|
||||
}
|
||||
memcpy(buf.ptr, p, i);
|
||||
}
|
||||
fp._cnt -= i;
|
||||
fp._ptr += i;
|
||||
buf = buf[0 .. i];
|
||||
return i;
|
||||
}
|
||||
}
|
||||
else version (GCC_IO)
|
||||
{
|
||||
if (fwide(fp, 0) > 0)
|
||||
{ /* Stream is in wide characters.
|
||||
* Read them and convert to chars.
|
||||
*/
|
||||
FLOCK(fp);
|
||||
scope(exit) FUNLOCK(fp);
|
||||
version (Windows)
|
||||
{
|
||||
buf.length = 0;
|
||||
int c2;
|
||||
for (int c; (c = FGETWC(fp)) != -1; )
|
||||
{
|
||||
if ((c & ~0x7F) == 0)
|
||||
{ buf ~= c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c >= 0xD800 && c <= 0xDBFF)
|
||||
{
|
||||
if ((c2 = FGETWC(fp)) != -1 ||
|
||||
c2 < 0xDC00 && c2 > 0xDFFF)
|
||||
{
|
||||
StdioException("unpaired UTF-16 surrogate");
|
||||
}
|
||||
c = ((c - 0xD7C0) << 10) + (c2 - 0xDC00);
|
||||
}
|
||||
std.utf.encode(buf, c);
|
||||
}
|
||||
}
|
||||
if (ferror(fp))
|
||||
StdioException();
|
||||
return buf.length;
|
||||
}
|
||||
else version (linux)
|
||||
{
|
||||
buf.length = 0;
|
||||
for (int c; (c = FGETWC(fp)) != -1; )
|
||||
{
|
||||
if ((c & ~0x7F) == 0)
|
||||
buf ~= c;
|
||||
else
|
||||
std.utf.encode(buf, cast(dchar)c);
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
if (ferror(fp))
|
||||
StdioException();
|
||||
return buf.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
char *lineptr = null;
|
||||
size_t n = 0;
|
||||
auto s = getdelim(&lineptr, &n, '\n', fp);
|
||||
scope(exit) free(lineptr);
|
||||
if (s < 0)
|
||||
{
|
||||
if (ferror(fp))
|
||||
StdioException();
|
||||
buf.length = 0; // end of file
|
||||
return 0;
|
||||
}
|
||||
buf = buf.ptr[0 .. std.gc.capacity(buf.ptr)];
|
||||
if (s <= buf.length)
|
||||
{
|
||||
buf.length = s;
|
||||
buf[] = lineptr[0 .. s];
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = lineptr[0 .. s].dup;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
/** ditto */
|
||||
size_t readln(inout char[] buf)
|
||||
{
|
||||
return readln(stdin, buf);
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user