Mistake of opening of a file having a name in cp1251.

2015-04-03 Thread MGW via Digitalmars-d-learn

Greetings to all!

I work on Windows with cp1251 and I have a mistake in the program:

import std.stdio;
int main (string [] args) {
string nameFile = `«Ёлки с объектами №876».txt`;
File f = File (nameFile, w);
f.writeln (Greetings!);
return 0;
}

This mistake of a kind:

std.exception. ErrnoException@std\stdio.d (372): Cannot open file 
` ┬л╨Б ╨╗╨║╨╕ ╨Ю ╨▒

╤К ╨╡╨║╤ 876 ┬╗. txt ' in mode ` w ' (Invalid argument)

For correction of this mistake I had to change a file std\stdio.d,
having added in it function fromUtf8toAnsiW (). The piece of an 
source code

of a file std\stdio.d with changes is presented more low.

private FILE* fopen(in char[] name, in char[] mode = r) 
@trusted // nothrow @nogc - mgw отключено из-за fromUtf8toAnsiW

{
import std.internal.cstring : tempCString;

version(Windows)
{
// 02.04.2015 8:31:19 repair for dmd 2.067.0
		wchar* fromUtf8toAnsiW(in char[] s, uint codePage = 0) @trusted 
 {
			import std.c.windows.windows: WideCharToMultiByte, 
GetLastError;

import std.windows.syserror: sysErrorString;
import std.conv: to;
char[] result, rez; int readLen; auto ws = 
std.utf.toUTF16z(s);
			result.length = WideCharToMultiByte(codePage, 0, ws, -1, null, 
0, null, null);

if (result.length)  {
readLen = WideCharToMultiByte(codePage, 0, ws, -1, 
result.ptr,	to!int(result.length), null, null);
for(int i; i != result.length; i++) { rez ~= result[i]; rez 
~= 0; } rez ~= 0; rez ~= 0;

}
if (!readLen || readLen != result.length)   {
throw new Exception(Couldn't convert string:  ~ 
sysErrorString(GetLastError()));

}
return cast(wchar*)rez.ptr;
}

import std.internal.cstring : tempCStringW;
// return _wfopen(name.tempCStringW(), 
mode.tempCStringW());
return _wfopen(fromUtf8toAnsiW(name), 
mode.tempCStringW());

}
else version(Posix)
{
import core.sys.posix.stdio : fopen;
return fopen(name.tempCString(), mode.tempCString());
}
else
{
return .fopen(name.tempCString(), mode.tempCString());
}
}

With the given correction all works correctly.

Question.
Whether correctly such change from the point of view of 
programming canons on D ?


Re: Mistake of opening of a file having a name in cp1251.

2015-04-03 Thread Danny via Digitalmars-d-learn
According to the documentation 
https://msdn.microsoft.com/de-de/library/yeby3zcb.aspx, _wfopen 
already takes a wide-character string, not an ANSI string.


So

 return _wfopen(name.tempCStringW(), mode.tempCStringW());

would be the correct way. All these weird ansi versions are 
Windows 98 era legacy, they aren't commonly used anymore.


Please also try whether your C runtime implements _wfopen 
correctly or otherwise your file name is somehow broken (maybe 
it's on a FAT filesystem etc). For that, please try opening it in 
a C program using _wfopen(_T(filenamehere), r);


For comparison, try to create a file with the same name on an 
NTFS filesystem and try opening it in a C program using 
_wfopen(_T(filenamehere), r);


Does that work?

Also, what version and flavour (DMD, GDC, LDC) of D do you use?


linking C library with D, creating a OpenVG D port

2015-04-03 Thread ddos via Digitalmars-d-learn

hi!

i'm trying to run OpenVG examples in D. So far i have compiled 
ShivaVG (an implementation of OpenVG standard) in C++ and created 
a shared library. Now i'd like to link my OpenVG.lib with a D 
program.
(the library was compiled with msvc2013x86, i'd like to avoid 
switching compiler)


to link the library i added my C++ lib to my dub file,
libs: [OpenVG],
dmd complains:  Error 43: Not a Valid Library File

after some googling i found out i have to convert/create my lib 
file in the appropriate format to be used with dmd, so i used 
implib to create a new lib from my dll. viewing this lib no 
exports are visible with dumpbin, dumpbin /EXPORTS OpenVG_D.lib 
also reports a warning: OpenVG_D.lib : warning LNK4048: Invalid 
format file; ignored
maybe i have to use another tool here to view the exports? but 
dumpbin can view pe and coff, dmd uses coff i think so this 
should work i guess


if i try to compile my application it now tells me about the 
obviously missing export,  Error 42: Symbol Undefined 
_vgCreateContextS




Re: Mistake of opening of a file having a name in cp1251.

2015-04-03 Thread MGW via Digitalmars-d-learn
The decision, which I have applied using function 
fromUtf8toAnsiW() already works correctly.


I use dmd 2.067.0


Re: linking C library with D, creating a OpenVG D port

2015-04-03 Thread ddos via Digitalmars-d-learn

progress ... i think
in some forum posts i've read 64bit dmd uses a differnt linker 
which supports coff

atleast i can now link my app in 64bit mode without errors
dmd -m64 source/app.d OpenVG.lib

also an exported test function prints to stdout, so my problem is 
solved for x64 :)


if anyone could explain how this could be done with 32bit builds 
i'd appreciate it

why does dmd use different linkers for 32 and 64bit anyway?


Re: linking C library with D, creating a OpenVG D port

2015-04-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 4/04/2015 1:00 a.m., ddos wrote:

progress ... i think
in some forum posts i've read 64bit dmd uses a differnt linker which
supports coff
atleast i can now link my app in 64bit mode without errors
dmd -m64 source/app.d OpenVG.lib

also an exported test function prints to stdout, so my problem is solved
for x64 :)

if anyone could explain how this could be done with 32bit builds i'd
appreciate it
why does dmd use different linkers for 32 and 64bit anyway?


In the beginning, Symantec had their own c/c++ compiler. That compiler 
was made by a humble but brilliant developer called Walter Bright. At 
the time the Microsoft toolchain was horribly incompatible between 
versions. In fact it was so bad, that a linker was made specifically for it.
At the time OMF was the standard binary format for Windows. Not long 
after PE-COFF was transitioned to for Windows 95.
Not long after this, Symantec dropped their toolchain and that clever 
developer negotiated the rights to it and in the process created a 
little company.


Furthering this, that clever developer didn't enjoy c++ not nearly as 
much as what others think of it. Instead choose to create a new language 
using the existing toolchain.


Fast forward about 12 years. The compiler still used that old linker but 
only for 32bit on Windows. But now the need was strong for 64bit 
support. After successful addition of 64bit support of *nix, Windows was 
worked on. Now nobody wanted to add 64bit support to that old old linker 
as it was written in an arcane dialect of assembly. Almost nobody 
understood it.


So the clever people, implemented it using the standard toolchain on the 
Windows platform. Instead of their own custom one. However it used 
PE-COFF for binary format. Unfortunately the need was not great to add 
it also for 32bit. Causing a great divide. That divide has since been 
resolved and will be forgotten about after one more major release.


Disclaimer: not 100% accurate, bunch of stuff I missed out. But more or 
less accurate timeline.


Re: Mistake of opening of a file having a name in cp1251.

2015-04-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/3/15 2:26 AM, MGW wrote:

Greetings to all!

I work on Windows with cp1251 and I have a mistake in the program:

import std.stdio;
int main (string [] args) {
 string nameFile = `«Ёлки с объектами №876».txt`;
 File f = File (nameFile, w);
 f.writeln (Greetings!);
 return 0;
}


Please see if this will fix your problem, currently open PR: 
https://github.com/D-Programming-Language/phobos/pull/3138


Seems like the same issue

-Steve



Placing variable/array in a particular section

2015-04-03 Thread Jens Bauer via Digitalmars-d-learn
Today I finally succeeded in building my first Hello World D 
program (after fixing the endian problem).


Is there a way of setting the target section for a variable or an 
array ?


Eg. the equivalent way of doing this using gcc is:

__attribute__((section(.isr_vector))) VectorFunc g_pfnVectors[] 
= { ... };


-I need this functionality, because on microcontrollers, it's 
necessary to control where in RAM / flash memory the data is 
written.


I could of course do this using C or assembly language, but I'd 
like to use pure D only, if possible.


Re: D1 operator overloading in D2

2015-04-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/30/15 11:25 AM, Steven Schveighoffer wrote:


I'll put in a doc PR to reference the D1 documentation.


https://github.com/D-Programming-Language/dlang.org/pull/953

-Steve


C++ to D - recursion with std.variant

2015-04-03 Thread Dennis Ritchie via Digitalmars-d-learn

Hi,
Is it possible to write on D recursion using std.variant?

-
#include boost/variant.hpp
#include iostream

struct Nil {};
auto nil = Nil{};

template typename T
struct Cons;

template typename T
using List = boost::variantNil,
boost::recursive_wrapperConsT;

template typename T
struct Cons {
Cons(T val, ListT list) : head(val), tail(list) {}

T head;
ListT tail;
};

template typename T
class length_visitor : public boost::static_visitorsize_t {
public:
int operator()(Nil) const {
return 0;
}

int operator()(const ConsT c) const {
return 1 + length(c.tail);
}
};

template typename T
auto cons(T head, ListT tail) {
return ListT(ConsT(head, tail));
}

template typename T
auto cons(T head, Nil) {
return ListT(ConsT(head, ListT(Nil{})));
}

template typename T
size_t length(const ListT list) {
return boost::apply_visitor(length_visitorT(), list);
}

int main() {

auto l = cons(3, cons(2, cons(1, nil)));
std::cout  length(l)  std::endl; // prints 3

return 0;
}
-
http://ideone.com/qBuOvJ


Issue with free() for linked list implementation

2015-04-03 Thread Kitt via Digitalmars-d-learn
Hello. I’m trying to write my own version of a list that doesn’t 
rely on the garbage collector. I’m working on a very bare bones 
implementation using malloc and free, but I’m running into an 
exception when I attempt to call free. Here is a very minimal 
code sample to illustrate the issue:


// Some constant values we can use
static const int two = 2, ten = 10;

// Get memory for two new nodes
Node* head = cast(Node*)malloc(two.sizeof);
Node* node1 = cast(Node*)malloc(ten.sizeof);

// Initialize the nodes
node1.value = ten;
node1.next = null;
head.value = two;   
head.next = node1;

// Attempt to free the head node
Node* temp = head.next;
head.next = null;
free(head); // Exception right here
head = temp;

Note, if I comment out the line ‘head.next = node1’, this code 
works. Does anyone know what I’m doing wrong with my manual 
memory management?


Re: Issue with free() for linked list implementation

2015-04-03 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 3 April 2015 at 22:08:52 UTC, Kitt wrote:
Thanks for the help =) I guess I've been in C# land at work for 
way too long now, my low level C skills are evaporating!


I've written a straight forward linked list implementation here:

https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d

Even though I'm using the GC to manage memory, maybe it will help 
you.


Re: Issue with free() for linked list implementation

2015-04-03 Thread Namespace via Digitalmars-d-learn

On Friday, 3 April 2015 at 22:02:13 UTC, Kitt wrote:
Hello. I’m trying to write my own version of a list that 
doesn’t rely on the garbage collector. I’m working on a very 
bare bones implementation using malloc and free, but I’m 
running into an exception when I attempt to call free. Here is 
a very minimal code sample to illustrate the issue:


// Some constant values we can use
static const int two = 2, ten = 10;

// Get memory for two new nodes
Node* head = cast(Node*)malloc(two.sizeof);
Node* node1 = cast(Node*)malloc(ten.sizeof);

// Initialize the nodes
node1.value = ten;
node1.next = null;
head.value = two;   
head.next = node1;

// Attempt to free the head node
Node* temp = head.next;
head.next = null;
free(head); // Exception right here
head = temp;

Note, if I comment out the line ‘head.next = node1’, this code 
works. Does anyone know what I’m doing wrong with my manual 
memory management?


Why did you allocate only 2 / 10 bytes and not Node.sizeof bytes?
Since your Node struct has at least one pointer (nexT) and a 
value (I assume of type int) you must allocate at least 8 bytes 
for one Node. I'm sure that is at least one of your problems.


Re: Issue with free() for linked list implementation

2015-04-03 Thread Kitt via Digitalmars-d-learn

On Friday, 3 April 2015 at 22:06:06 UTC, Namespace wrote:

On Friday, 3 April 2015 at 22:02:13 UTC, Kitt wrote:
Hello. I’m trying to write my own version of a list that 
doesn’t rely on the garbage collector. I’m working on a very 
bare bones implementation using malloc and free, but I’m 
running into an exception when I attempt to call free. Here is 
a very minimal code sample to illustrate the issue:


// Some constant values we can use
static const int two = 2, ten = 10;

// Get memory for two new nodes
Node* head = cast(Node*)malloc(two.sizeof);
Node* node1 = cast(Node*)malloc(ten.sizeof);

// Initialize the nodes
node1.value = ten;
node1.next = null;
head.value = two;   
head.next = node1;

// Attempt to free the head node
Node* temp = head.next;
head.next = null;
free(head); // Exception right here
head = temp;

Note, if I comment out the line ‘head.next = node1’, this code 
works. Does anyone know what I’m doing wrong with my manual 
memory management?


Why did you allocate only 2 / 10 bytes and not Node.sizeof 
bytes?
Since your Node struct has at least one pointer (nexT) and a 
value (I assume of type int) you must allocate at least 8 bytes 
for one Node. I'm sure that is at least one of your problems.


Wow, I can't even begin to explain how red my cheeks are right 
now. You're completely right; I have no idea what my head was 
thinking. Sure enough, call malloc with the correct type, and the 
error goes away =P


Thanks for the help =) I guess I've been in C# land at work for 
way too long now, my low level C skills are evaporating!


Re: Issue with free() for linked list implementation

2015-04-03 Thread Namespace via Digitalmars-d-learn
Wow, I can't even begin to explain how red my cheeks are right 
now. You're completely right; I have no idea what my head was 
thinking. Sure enough, call malloc with the correct type, and 
the error goes away =P


Thanks for the help =) I guess I've been in C# land at work for 
way too long now, my low level C skills are evaporating!


Sometimes you wonder how simple such a problem can be. :D My 
pleasure.


Re: Issue with free() for linked list implementation

2015-04-03 Thread Namespace via Digitalmars-d-learn

On Friday, 3 April 2015 at 22:38:00 UTC, Gary Willoughby wrote:

On Friday, 3 April 2015 at 22:08:52 UTC, Kitt wrote:
Thanks for the help =) I guess I've been in C# land at work 
for way too long now, my low level C skills are evaporating!


I've written a straight forward linked list implementation here:

https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d

Even though I'm using the GC to manage memory, maybe it will 
help you.


Good idea to link to some existing code. Here is mine:
https://github.com/Dgame/m3/blob/master/source/m3/List.d


Re: lambda code

2015-04-03 Thread Vlad Levenfeld via Digitalmars-d-learn

On Thursday, 2 April 2015 at 19:27:21 UTC, John Colvin wrote:
On Wednesday, 1 April 2015 at 23:29:00 UTC, Vlad Levenfeld 
wrote:

On Tuesday, 31 March 2015 at 13:25:47 UTC, John Colvin wrote:
On Tuesday, 31 March 2015 at 12:49:36 UTC, Vlad Levenfeld 
wrote:
Is there any way (or could there be any way, in the future) 
of getting the code from lambda expressions as a string?


I've noticed that if I have an error with a lambda that 
looks like, say

x=x+a

the error message will come up referring to it as
(x) = x + a

so some level of processing has already been done on the 
expression. Can I get at any of it during compilation? It 
would be useful for automatic program rewriting.


Short answer: no. .codeof for functions is something I've 
wanted for ages, but no movement so far.


:(


On a more positive note, there's probably an OK way of achieving
your particular goal without this. Do you have an example?


Well I was just thinking of turning

  r[].map!(v = v.xy*2).zip (s[]).map!((v,t) = vec2(v.x*cos(t), 
v.y*sin(t))).to_vertex_shader ();


  or something like that, into a shader program.

Right now I have to do it with strings:

  r[].vertex_shader!(`v`, q{
vec2 u = v.xy*2;
gl_Position = vec2(v.x*cos(t), v.y*sin(t));
  });

I just keep thinking that, if I have programs composed of 
individual processing stages, like


  auto aspect_ratio_correction (T,U)(T computation, U canvas) {
return
  zip (computation, repeat (canvas.aspect_ratio, 
computation.length))

.map!((v, a_r) = v/a_r);
  }

then it's so that I can put them in UFCS chains, so

  vec2[] vertices;
  float time;
  Display display;

  auto kernel = some_program (vertices[], time)
.aspect_ratio_correction (display);

is able to be run on the cpu or gpu and this decision must be 
made lazily:


  kernel[].array; // cpu
  kernel[].computed_on_gpu.array; // compute on gpu, read back to 
cpu


So I'd like to turn place of execution into a lazily evaluated 
range adaptor, and maybe reduce the need to keep different 
cpu/gpu code for the same algorithms. This seems impossible 
without something like .codeof or, better yet, ASTs.


I can already unwrap the type of a composed range to get at how 
its constructed, but I don't get any information on the functions 
that are involved with higher-order function adaptors. The idea 
of doing compile-time restructuring of these ufcs chains is 
interesting to me, but I feel like I only have half of what I 
need to give it a proper try.


Re: Placing variable/array in a particular section

2015-04-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 4/04/2015 3:08 a.m., Jens Bauer wrote:

On Friday, 3 April 2015 at 13:58:21 UTC, Jens Bauer wrote:

On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:

On 4/04/2015 2:12 a.m., Jens Bauer wrote:

Is there a way of setting the target section for a variable or an
array ?


Supposedly gdc supports it.

[0] http://wiki.dlang.org/GDC/Using_GDC Extensions-Attributes
[1]
https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes



I tried to use some of the examples, but I got the following error:

src/start.d:7:10: error: module attribute is in file 'gcc/attribute.d'
which cannot be read
   import gcc.attribute;
  ^


Uhm, it seems that druntime is required for that; unfortunately it's not
ported to the Cortex-M platform.
I found the attribute.d file and tried a quick copy-and-paste, but no
luck there.


Yeah, contact the GDC guys, they should be able to help you.
Either the GDC newsgroup or as an issue on the compiler's project.

Atleast at this point, keep an open mind. Think of what you are doing as 
testing the current state :)


Re: Placing variable/array in a particular section

2015-04-03 Thread Jens Bauer via Digitalmars-d-learn

On Friday, 3 April 2015 at 13:58:21 UTC, Jens Bauer wrote:

On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:

On 4/04/2015 2:12 a.m., Jens Bauer wrote:
Is there a way of setting the target section for a variable 
or an array ?


Supposedly gdc supports it.

[0] http://wiki.dlang.org/GDC/Using_GDC Extensions-Attributes
[1] 
https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes


I tried to use some of the examples, but I got the following 
error:


src/start.d:7:10: error: module attribute is in file 
'gcc/attribute.d' which cannot be read

   import gcc.attribute;
  ^


Uhm, it seems that druntime is required for that; unfortunately 
it's not ported to the Cortex-M platform.
I found the attribute.d file and tried a quick copy-and-paste, 
but no luck there.


Re: Placing variable/array in a particular section

2015-04-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 4/04/2015 2:12 a.m., Jens Bauer wrote:

Today I finally succeeded in building my first Hello World D program
(after fixing the endian problem).

Is there a way of setting the target section for a variable or an array ?

Eg. the equivalent way of doing this using gcc is:

__attribute__((section(.isr_vector))) VectorFunc g_pfnVectors[] = {
... };

-I need this functionality, because on microcontrollers, it's necessary
to control where in RAM / flash memory the data is written.

I could of course do this using C or assembly language, but I'd like to
use pure D only, if possible.


Supposedly gdc supports it.

[0] http://wiki.dlang.org/GDC/Using_GDC Extensions-Attributes
[1] 
https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes


Re: Placing variable/array in a particular section

2015-04-03 Thread Jens Bauer via Digitalmars-d-learn

On Friday, 3 April 2015 at 13:37:50 UTC, Rikki Cattermole wrote:

On 4/04/2015 2:12 a.m., Jens Bauer wrote:
Is there a way of setting the target section for a variable or 
an array ?


Supposedly gdc supports it.

[0] http://wiki.dlang.org/GDC/Using_GDC Extensions-Attributes
[1] 
https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes


I tried to use some of the examples, but I got the following 
error:


src/start.d:7:10: error: module attribute is in file 
'gcc/attribute.d' which cannot be read

   import gcc.attribute;
  ^

-Am I missing some files somewhere ?


Re: linking C library with D, creating a OpenVG D port

2015-04-03 Thread ddos via Digitalmars-d-learn

thanks Rikki!

also if anyone is interested in OpenVG i have now a running demo 
in D based on ShivaVG and derelict GLFW3, it's not beautiful but 
it works :D


http://imgur.com/ZH0kD0q

i'm pretty impressed how painless the compiling and interfacing 
to C was actually :) +1 for D