Re: String Appender Fails (Memory Allocation Failure)

2011-07-14 Thread Loopback

Anybody got an idea?


Re: tiny web server in D

2011-07-14 Thread Dr.Smith
Thank you Adam,
Your code is comprehensive.  I will read it closely for ideas.
I seek a minimalist approach for locally run applications.


Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread bearophile
> In GCC there is a way to (sometimes) do it, see the __builtin_constant_p here:
> http://www.delorie.com/gnu/docs/gcc/gcc_81.html

Google code search gives about 8,100 answers:
http://www.google.com/codesearch#search/&q=%22__builtin_constant_p%22&type=cs

Bye,
bearophile


Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread bearophile
scarrow:

> I'd really like to figure out how to have Hash("foo") be static and 
> Hash(variable) be dynamic.

In GCC there is a way to (sometimes) do it, see the __builtin_constant_p here:
http://www.delorie.com/gnu/docs/gcc/gcc_81.html

Time ago I have asked for something similar in D too, because I think it allows 
to implement a poor's man manual "partial compilation" using templates. Maybe 
it's worth asking about this in the main D newsgroup. But before asking, it's 
better to have few use cases examples :-)

Bye,
bearophile


Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread scarrow
I think invoking a template to call the pure function (StaticEval!(Hash("foo"))
isn't much different from StaticHash!("foo").  You still have to explicitly know
whether you're dealing with a compile time constant or not.  I'd really like to
figure out how to have Hash("foo") be static and Hash(variable) be dynamic.


Re: Lazy and delegates - out-of-scopeness - what happens?

2011-07-14 Thread Jacob Carlborg

On 2011-07-14 20:42, Cecil ward wrote:

If I pass lazy expressions or delegates to as arguments to functions, what 
happens with variables mentioned in the
lazy expression or in the delegate body if those variables are on the stack or 
otherwise out of scope, or are objects
that may be freed? Does the compiler partially protect the programmer from 
mistakes here?

If anyone can comment on how these features are implemented in generated code 
wrt stack-allocated objects that
are _in_ lexical scope in a delegate evaluation context, then I'd be interested 
to hear about this.


They all local variables (or similar) that a delegate references will be 
allocated on the heap, this is also known as a closure: 
http://en.wikipedia.org/wiki/Closure_%28computer_science%29


--
/Jacob Carlborg


Lazy and delegates - out-of-scopeness - what happens?

2011-07-14 Thread Cecil ward
If I pass lazy expressions or delegates to as arguments to functions, what 
happens with variables mentioned in the
lazy expression or in the delegate body if those variables are on the stack or 
otherwise out of scope, or are objects
that may be freed? Does the compiler partially protect the programmer from 
mistakes here?

If anyone can comment on how these features are implemented in generated code 
wrt stack-allocated objects that
are _in_ lexical scope in a delegate evaluation context, then I'd be interested 
to hear about this.


Re: how to raw read a struct or value?

2011-07-14 Thread Graham Fawcett
On Thu, 14 Jul 2011 13:05:33 -0400, Steven Schveighoffer wrote:

> On Thu, 14 Jul 2011 09:36:47 -0400, Graham Fawcett 
> wrote:
> 
>> On Thu, 14 Jul 2011 09:01:29 +, hasnoth wrote:
>>
>>> auto file = io.File("test.txt", "rb");
>>>   auto fp = file.getFP();
>>>   int x;
>>>   io.fread(&x, x.sizeof, 1, fp);
>>>
>>>   what's the “d method” to do that?
>>
>> The same method:
>>
>> import core.stdc.stdio : fread;
>> fread(&x, x.sizeof, 1, fp);
>>
>> or, if you are using an array, you can use File.rawRead:
>>
>> int[10] xx;
>> f.rawRead(xx);
> 
> In fact, you can read a struct/int this way too:
> 
> f.rawRead((&x)[0..1]);
> 
> -Steve

Aha! that makes sense. Thanks, Steve.

Graham


Re: how to raw read a struct or value?

2011-07-14 Thread Steven Schveighoffer
On Thu, 14 Jul 2011 09:36:47 -0400, Graham Fawcett   
wrote:



On Thu, 14 Jul 2011 09:01:29 +, hasnoth wrote:


auto file = io.File("test.txt", "rb");
  auto fp = file.getFP();
  int x;
  io.fread(&x, x.sizeof, 1, fp);

  what's the “d method” to do that?


The same method:

import core.stdc.stdio : fread;
fread(&x, x.sizeof, 1, fp);

or, if you are using an array, you can use File.rawRead:

int[10] xx;
f.rawRead(xx);


In fact, you can read a struct/int this way too:

f.rawRead((&x)[0..1]);

-Steve


Re: Problems with static linking of c libraries

2011-07-14 Thread Trass3r

Am 14.07.2011, 16:36 Uhr, schrieb Danny Arends :


Hey all,

I'm trying to build a D application which statically links in the the  
blas and lapack libraries
(from http://icl.cs.utk.edu/lapack-for-windows/clapack/index.html ).  
When downloading the

pre-build libraries from the website I link them in using:


You save yourself a lot of hassle if you just stick with dlls on Windoze.
Converting prebuilt libs has never really worked for me.
And compiling them with dmc is a nightmare.

But creating an import library from a dll with implib worked really well  
so far no matter how the code was compiled.


Problems with static linking of c libraries

2011-07-14 Thread Danny Arends
Hey all,

I'm trying to build a D application which statically links in the the blas
and lapack libraries
(from http://icl.cs.utk.edu/lapack-for-windows/clapack/index.html ). When
downloading the
pre-build libraries from the website I link them in using:

pragma(lib, "blas");
pragma(lib, "lapack");

Compiling isn't a problem but then optlink complains:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
blas.lib
 Error 43: Not a Valid Library File

First I thought this was due to the fact that I was running on windows 2008
(32 bit version) but
I also tried on windows XP professional, with the same result...

I decided to build the libraries myself using Cmake and VisualStudio 2010,
this however also
has the same result:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
blas.lib
 Error 43: Not a Valid Library File

I am able to build the packages using the MinGW toolchain (using GNU C/CPP)
however I then end up with
libblas.a and liblapack.a files, which causes the linker to not find the
libraries (under windows it searches for
the .lib extension). renaming them leads again to the msg seen above (Not a
Valid Library)

Any help on how to statically link these 2 packages in my executable using
windows ?

Kid regards,
Danny

Ps. I need to avoid the use of dynamic linking


Re: tiny web server in D

2011-07-14 Thread Adam Ruppe
std.socket is too low level for serving webpages. It just provides
the means to talk on the network, but doesn't do any application
protocols like http.

I've written a little http server in D, but it uses linux system
calls instead of std.socket, so it only works on linux.

http://arsdnet.net/dcode

Check out httpd.d and netman.d in there. Also, my cgi.d can work
together with them to serve web apps through the mini web server.


But as you can see, a lot of the code is parsing and writing http.

When you go to a web site, your browser sends something like this
to the server:

GET /index.html HTTP/1.1
Host: mysite.com

Then, the server replies:

HTTP/1.1 200 OK
Content-Length: 13
Content-Type: text/plain
Connection: close

Hello, world!


Then the connection is closed and the get is complete.


Re: tiny web server in D

2011-07-14 Thread Dr.Smith
The program as such can regurgitate a web page provided these additional lines:

string webpage = "index.html";
string output = "HTTP/1.1 200 OK\r\n Content-Type: text/html; 
charset=UTF-8\r\n\r
\n" ~ to!string(read(webpage) ~ "\r\n";

This does not serve a page as localhost:port/webpage.html, but merely displays
output on localhost:port

I've been exploring std.socket for an efficient solution.  It might also be
desirable to get the program to open the port only on request.


Re: wrapping a void* by a ubyte[] array?

2011-07-14 Thread Daniel Murphy
Cast to ubyte* and slice?

ubyte[] array = (cast(ubyte*)pointer)[0..length];

"teo"  wrote in message 
news:ivn0n8$14ig$1...@digitalmars.com...
> Is there any way of wrapping a void* by a ubyte[] array? The void* comes
> from mmap. 




Re: wrapping a void* by a ubyte[] array?

2011-07-14 Thread Simen Kjaeraas

On Thu, 14 Jul 2011 17:07:20 +0200, teo  wrote:


Is there any way of wrapping a void* by a ubyte[] array? The void* comes
from mmap.


byte[] array = cast(ubyte[])mmap(addr, length, ...)[0..length;]

--
  Simen


Re: Problems with static linking of c libraries

2011-07-14 Thread Loopback

On 2011-07-14 17:06, Danny Arends wrote:

Wow THANX,
Using the COFF2OMF tool at least got me a step further, now it finds and 
accepts the lib files,
however, it now fails with the following error:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
lapack.lib(lapack)  Offset B24AEH Record Type 009D
  Error 16: Index Range
--- errorlevel 1

Any suggestions, on how to fix this error ?


My own success have varied depending on which tool that I have used.
COFF2OMF seems to be the one most appropriate with old libraries (such
as VC++ 6.0 and earlier). Coffimplib only works with a few libraries,
(in my own experience) and I have had least success with this tool,
whilst objconv works in most cases. I would recommended to try them
all and see which one that works best for you. If none of them works,
try to use them on different compiled libraries (the one's on the
website and the one's that you have compiled locally etc..).


Re: Problems with static linking of c libraries

2011-07-14 Thread Danny Arends
Wow THANX,
Using the COFF2OMF tool at least got me a step further, now it finds and 
accepts the lib files,
however, it now fails with the following error:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
lapack.lib(lapack)  Offset B24AEH Record Type 009D
 Error 16: Index Range
--- errorlevel 1

Any suggestions, on how to fix this error ?


wrapping a void* by a ubyte[] array?

2011-07-14 Thread teo
Is there any way of wrapping a void* by a ubyte[] array? The void* comes 
from mmap.


Re: Problems with static linking of c libraries

2011-07-14 Thread Loopback

You need to convert the libraries to OMF format from COFF.
These are some tools which can do this:

Coffimplib: http://www.digitalmars.com/ctg/coffimplib.html
Coff2OMF: http://www.digitalmars.com/ctg/coff2omf.html
ObjConv: http://www.agner.org/optimize/objconv.zip


Problems with static linking of c libraries

2011-07-14 Thread Danny Arends
Hey all,

I'm trying to build a D application which statically links in the the blas and 
lapack libraries
(from http://icl.cs.utk.edu/lapack-for-windows/clapack/index.html ). When 
downloading the
pre-build libraries from the website I link them in using:

pragma(lib, "blas");
pragma(lib, "lapack");

Compiling isn't a problem but then optlink complains:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
blas.lib
 Error 43: Not a Valid Library File

First I thought this was due to the fact that I was running on windows 2008 (32 
bit version) but
I also tried on windows XP professional, with the same result...

I decided to build the libraries myself using Cmake and VisualStudio 2010, this 
however also
has the same result:

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
blas.lib
 Error 43: Not a Valid Library File

I am able to build the packages using the MinGW toolchain (using GNU C/CPP) 
however I then end up with
libblas.a and liblapack.a files, which causes the linker to not find the 
libraries (under windows it searches for
the .lib extension). renaming them leads again to the msg seen above (Not a 
Valid Library)

Any help on how to statically link these 2 packages in my executable using 
windows ?

Kind regards,
Danny


Re: how to raw read a struct or value?

2011-07-14 Thread Graham Fawcett
On Thu, 14 Jul 2011 09:01:29 +, hasnoth wrote:

> auto file = io.File("test.txt", "rb");
>   auto fp = file.getFP();
>   int x;
>   io.fread(&x, x.sizeof, 1, fp);
> 
>   what's the “d method” to do that?

The same method:

import core.stdc.stdio : fread;
fread(&x, x.sizeof, 1, fp);

or, if you are using an array, you can use File.rawRead:

int[10] xx;
f.rawRead(xx);

Graham


Re: A problem with const

2011-07-14 Thread Daniel Murphy
I don't think there's a bug report specifically on this.

"bearophile"  wrote in message 
news:ivmigl$98a$1...@digitalmars.com...
> Daniel Murphy:
>
>> Yeah, type deduction with modifiers is inconsistent.
>>
>> In some cases matching T to const(U) gives U == tailconst(T), but not in
>> others.
>> The problem exists with pointers, arrays, and (if we ever get it) Michel
>> Fortin's const(Object)ref.
>
> Is this already in Bugzilla, or do you want me to create a new Bugzilla 
> enhancement request with my examples?
>
> Bye,
> bearophile 




Re: A problem with const

2011-07-14 Thread bearophile
Daniel Murphy:

> Yeah, type deduction with modifiers is inconsistent.
> 
> In some cases matching T to const(U) gives U == tailconst(T), but not in 
> others.
> The problem exists with pointers, arrays, and (if we ever get it) Michel 
> Fortin's const(Object)ref.

Is this already in Bugzilla, or do you want me to create a new Bugzilla 
enhancement request with my examples?

Bye,
bearophile


Re: A problem with const

2011-07-14 Thread Daniel Murphy
Yeah, type deduction with modifiers is inconsistent.

In some cases matching T to const(U) gives U == tailconst(T), but not in 
others.
The problem exists with pointers, arrays, and (if we ever get it) Michel 
Fortin's const(Object)ref.

A big part of the problem is that it can match with implicit conversions, 
but the type is never actually converted.

eg.
is(shared(int*) T : const(U), U) should match and give U == 
shared(const(int)*)

"bearophile"  wrote in message 
news:ivmd15$2vp2$1...@digitalmars.com...
>A D2 program:
>
>
> T[] foo(T)(const T[] x) {
>//static assert(is(U == int)); // false
>static assert(is(T == const(int)));
>return new T[1];
> }
> U[] bar(U)(const U[] y) {
>static assert(is(U == int));
>return foo(y);
> }
> void main() {
>bar([1]);
> }
>
>
> DMD 2.054 gives:
> test.d(8): Error: cannot implicitly convert expression (foo(y)) of type 
> const(int)[] to int[]
> test.d(11): Error: template instance test.bar!(int) error instantiating
>
>
> Do you know why T of foo is const(int) instead of int? Isn't the "const" 
> of foo(T)(const T[] x) de-structuring the const nature of x? Is is 
> possible to change/"fix" this?
>
> This causes me problems because many of my functions have "in" arguments. 
> When they call each other they don't compile, as in this example (here I 
> have used "const" arguments just for clarity).
>
>
> I have had to use code like this:
>
> Unqual![] foo(T)(const T[] x) {
>return new Unqual!T[1];
> }
> U[] bar(U)(const U[] y) {
>return foo(y);
> }
> void main() {
>bar([1]);
> }
>
> Bye and thank you,
> bearophile 




A problem with const

2011-07-14 Thread bearophile
A D2 program:


T[] foo(T)(const T[] x) {
//static assert(is(U == int)); // false
static assert(is(T == const(int)));
return new T[1];
}
U[] bar(U)(const U[] y) {
static assert(is(U == int));
return foo(y);
}
void main() {
bar([1]);
}


DMD 2.054 gives:
test.d(8): Error: cannot implicitly convert expression (foo(y)) of type 
const(int)[] to int[]
test.d(11): Error: template instance test.bar!(int) error instantiating


Do you know why T of foo is const(int) instead of int? Isn't the "const" of 
foo(T)(const T[] x) de-structuring the const nature of x? Is is possible to 
change/"fix" this?

This causes me problems because many of my functions have "in" arguments. When 
they call each other they don't compile, as in this example (here I have used 
"const" arguments just for clarity).


I have had to use code like this:

Unqual![] foo(T)(const T[] x) {
return new Unqual!T[1];
}
U[] bar(U)(const U[] y) {
return foo(y);
}
void main() {
bar([1]);
}

Bye and thank you,
bearophile


how to raw read a struct or value?

2011-07-14 Thread hasnoth

  auto file = io.File("test.txt", "rb");
  auto fp = file.getFP();
  int x;
  io.fread(&x, x.sizeof, 1, fp);

  what's the �d method� to do that?