Help me decide D or C

2019-07-31 Thread Alexandre via Digitalmars-d-learn

Hi everyone,

I would like an honest opinion.
I have a beginner level (able to do very small programs) in a few 
languages  such as python, go, C, guile(scheme) and common lisp. 
I want to pick a language and go deep with it and focus on only 
one for at least the next 2 years or so.


Should I go for C and then when I become a better programmer 
change to D?

Should I start with D right now?

The reason I am considering starting with C: since I am a 
beginner, obvious I will need lots of books, tutorials, videos 
etc. And I believe C would have more resources and maybe a low 
level to help with programming in general. And, when I need a 
more powerful language, I would than learn D. Since you know the 
good and the ugly of the D programming language I wonder, what 
you would think would be the best to do right now?


Thank you for your help!



Re: Help me decide D or C

2019-07-31 Thread Alexandre via Digitalmars-d-learn

On Wednesday, 31 July 2019 at 22:16:42 UTC, bachmeier wrote:

On Wednesday, 31 July 2019 at 18:38:02 UTC, Alexandre wrote:

[...]


What is your goal? In my opinion, learning C is a waste of time 
in 2019 unless you have something specific in mind related to a 
job. C is mostly "fun with segmentation faults". Most of your 
time is not spent solving problems. If you want to be 
productive, choose D, Go, Rust, C++, or just about anything but 
C.



My goals:

1) Improve as a programmer
2) Have fun doing programs

Thats it basically. I am planning to study all "free" time I 
have. I am doing basically this since last year.


Re: Help me decide D or C

2019-07-31 Thread Alexandre via Digitalmars-d-learn

On Wednesday, 31 July 2019 at 20:04:39 UTC, Ali Çehreli wrote:

n 07/31/2019 12:05 PM, Paul Backus wrote:

> I would not recommend D as a beginning language, both because
there are
> fewer beginner-oriented resources available for it than for C
and Python
> (the only one I know of is Ali Çehreli's book [1]), and
because it's a
> bigger, more complicated language.
>
> [1] http://www.ddili.org/ders/d.en/index.html

Ali here... :) Thanks for the link and I agree that D is much 
larger than C. At least for that reason, learning C first or on 
the side would still be good for the OP.


Regarding "Programming in D", although it covers most[1] of the 
language, it specifically targets beginners; so, it may not be 
too difficult for the OP. Just give it a try... :)


Ali
[1] Unfortunately, copy constructors and some of the other 
recent features are still missing.


I am considering reading your book + Andrei's book + 
documentation on the site. That would be my plan to learn D. Good 
job with your book btw, I enjoyed a lot the parts I've read.





Re: Help me decide D or C

2019-08-01 Thread Alexandre via Digitalmars-d-learn

On Thursday, 1 August 2019 at 15:42:08 UTC, a11e99z wrote:

On Thursday, 1 August 2019 at 15:17:11 UTC, a11e99z wrote:

[...]


imo better choice is (with criteria to find best job)
- Qt:
C++ with any library that u need in one style
- C#:
web, graphics, mobiles, command tools with nice language.
- Java/Kotlin:
same as C# but in top-3. C# is top-5 with more comfort 
language than Java. Kotlin same comfort as C#, but JVM (Virtual 
Machine of Java and Kotlin) still does not support value types, 
that is sucks.

- JavaScript/TypeScript:
web-browser language with node.js that allows to program 
server side too.

- You can try Python too.
another dynamic language (as JavaScript). I don't like 
langs that based on space/tabs so I can not say anything about 
it.


[...]


Right now, job is not a good criteria for me. I work in a not 
related field and I doubt I would get any job working with CS. 
That would be great, but I doubt it anyway, so it is more a hobby 
thing.


Re: Help me decide D or C

2019-08-02 Thread Alexandre via Digitalmars-d-learn

On Friday, 2 August 2019 at 12:30:44 UTC, berni wrote:

On Wednesday, 31 July 2019 at 18:38:02 UTC, Alexandre wrote:

[...]


In my oppinion C should have been deprecated about 50 years ago 
and it's not worth while to learn it if you are not interested 
in the history of programming or you have to learn it, because 
you need to maintain software which is allready written in C. 
But that's my oppinion; others may have a different sight.


[...]


Could you elaborate more about C being a burden? I have read so 
many people saying C gives a great foundation and should be 
everyone's first language. Now I am confused.


Re: Help me decide D or C

2019-08-02 Thread Alexandre via Digitalmars-d-learn

On Friday, 2 August 2019 at 15:51:25 UTC, Russel Winder wrote:
On Fri, 2019-08-02 at 13:45 +, Alexandre via 
Digitalmars-d-learn wrote:



[…]
Could you elaborate more about C being a burden? I have read 
so many people saying C gives a great foundation and should be 
everyone's first language. Now I am confused.


C is a programming language created in the early 1970s to make 
writing UNIX easier. Early versions of UNIX (and Multics before 
it) were written in assembly language. Dennis Ritchie et al. 
wanted to use a programming language that had a higher level of 
abstraction than assembly language so as to make writing UNIX 
easier. BCPL gave many of the ideas for B which led to C, 
effectively a portable assembly language but with special eyes 
on the PDP-8, PDP-11, and later VAX-11 machine codes. C was 
hugely successful for writing operating systems because it was 
"close to the metal" and yet with better abstractions than 
assembly language. I spent many happy (and many unhappy) hours 
in the early 1980s writing device drivers for UNIX 6, UNIX 7, 
and BSD 4.0. C was the right tool for the job at hand at that 
time.


Many tools associated with UNIX were written in C, including 
the C compiler, since the only other option at the time in the 
UNIX context was assembly language. Already though there was 
the question: was C the right tool for the job of writing 
applications – as opposed to hardware controlling software. One 
could argue that "buffer overruns" was  clear evidence that C 
was the wrong tool for the job.


Unfortunately the obsession with C, even if it was not the 
right tool for the job at hand, had taken hold: if you didn't 
write your application in C you were somehow a second or third 
rate human being, let alone programmer.


Then came C++ (or then C with Classes) and the beginning of the 
rift between the C camp and the "we need a programming language 
with higher levels of abstraction" camp. I am sure many can 
write lots on the 1990s and 2000s and the various language 
wars, but here we are in 2010s entering the 2020s and we have 
D, Rust, Go, Java, Kotlin, Python, Ruby, C++, Lisp, Prolog, 
Erlang, etc. all of which have their problems, but all of which 
have their "sweet spots" for being the right tool for the job 
at hand. C is no longer the de facto standard language for 
writing all software. People are increasingly recognising that 
it is as if C were specifically created for writing software 
that controls hardware.


C still has a role in the world of programming, and it 
definitely has a status as one of the most important 
programming languages ever.


Moral of this story is that, for me, in 2019, if you are 
writing applications

software or software tools, C is not the right tool for the job.


Do you thing D would be the right tool for the job at this point 
for me? Assuming I have 2 goals in mind: 1) become a better 
programmer and 2) want to make fun writing software for myself 
and if possible show something I might be proud of.
I thought C would be a better choice for the 1), because everyone 
says it's great to see whats behind the hood and things like 
that. My experience with C btw is CS50 course, plus around 
200/300 pages of some books, still reading and a few toy 
projects. So, basically 0 knowledge. haha. But after reading your 
opinion, I guess C might not be the right tool for me, since I 
wont be doing any kind of thing related to hardware (I think).




I have receive so many good opinions so far. I realize there is 
no consensus what so ever. As I was suggested Haskell, Python, D, 
C etc. It's a good thing, but hard to make a decision.


Working with images

2017-11-01 Thread Alexandre via Digitalmars-d-learn
I have a project written in C++, that I'm thinking to migrating 
to D, but, what is preventing me from migrating to D, is the part 
of the system that works with images, where the system generates 
the image of a payment receipt, currently in the system written 
in C ++, there is an array with image base RGB where I can open 
that RGB by lib. that I use to generate the image in C ++ (the 
lib I use is the CImg), in this system I need to insert texts, 
images and a bar code in that base image..


There is some lib. in D ( multiplataform ), that I could be this, 
insert texts, imagens and a barcode into a existing image, that I 
could be open from memory ?


Re: Any book recommendation for writing a compiler?

2017-11-01 Thread Alexandre via Digitalmars-d-learn

On Wednesday, 1 November 2017 at 20:56:22 UTC, Dr. Assembly wrote:
On Wednesday, 1 November 2017 at 20:53:44 UTC, Dr. Assembly 
wrote:
Hey guys, if I were to get into dmd's source code to play a 
little bit (just for fun, no commercial use at all), which 
books/resources do you recommend to start out?


I'd like something on back-end too, for example, code 
generation, convert the AST to actual assembly/machine code. On 
my research I found mostly front-end related stuff, like 
recursive descent parser.


Look at this book: https://www.t3x.org/reload/index.html

Some cool open source languages:
* Elena programming language: 
https://github.com/ELENA-LANG/elena-lang

* DotNetPeLib: https://github.com/LADSoft/DotNetPELib
* OrangeC/C++ compiler: https://github.com/LADSoft/OrangeC


I have some cool projects:
* A simple C compiler that generates a EXE/DLL: 
https://gist.github.com/bencz/79d4738afe962b5479d1
* CoffLib ( a lib to generate x86 EXE/DLL in .Net ) : 
https://github.com/bencz/CoffLib

* Zinnia-lang: https://github.com/bencz/Zinnia-lang
* dncpp: https://github.com/bencz/dncpp
* Pythonish-Compiler: https://github.com/bencz/Pythonish-Compiler


Sum informations in file....

2014-07-10 Thread Alexandre via Digitalmars-d-learn
I have one file with a lot of numeric data... and I need to sum 
all that data...


That is my actual code:

module main;

import std.stdio;
import std.file;
import std.conv : to;

int main(string[] args)
{
auto f = File("oi.txt");
auto r = f.byLine();

auto tot = 0;
foreach(line;r)
{
if(line[0] == '1')
tot += to!int(line[253..266]);
}

writeln(tot);
return 0;
}

I want to know if have a more better way to make this... maybe 
using lambda or tamplates


Re: Sum informations in file....

2014-07-10 Thread Alexandre via Digitalmars-d-learn

O, real intresting the mode functional style!!!
Like linq!
hahah

Btw, it's work very well, thansk!!!
But, how I can insert an ',' comma to separe the decimal place ? 
( the last 2 digits )

I can't find a "insert" instruction in std.string or std.array

On Thursday, 10 July 2014 at 15:01:52 UTC, bearophile wrote:

Alexandre:

I want to know if have a more better way to make this... maybe 
using lambda or tamplates


Your code is not bad. This is a bit better (untested):


void main() {
import std.stdio;
import std.conv: to;

auto lines = "oi.txt".File.byLine;

int tot = 0;
foreach (const line; lines) {
if (line[0] == '1')
tot += line[253 .. 266].to!int;
}

tot.writeln;
}


If you want to write in a mode functional style (untested) 
(probably it requires the 2.066beta):


void main() {
import std.stdio, std.algorithm, std.range, std.conv;

"oi.txt"
.File
.byLine
.filter!(line => line[0] == '1')
.map!(line => line[253 .. 266].to!int)
.sum
.writeln;
}


Bye,
bearophile




Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn

I have a string X and I need to insert a char in that string...

auto X = "100";

And I need to inser a ',' in position 3 of this string..., I try 
to use the array.insertInPlace, but, not work...


I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');


Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn

Sorry..
I mean:

auto X = "100";
auto N = X.insertInPlace(3,',');

On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:

I have a string X and I need to insert a char in that string...

auto X = "100";

And I need to inser a ',' in position 3 of this string..., I 
try to use the array.insertInPlace, but, not work...


I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');




Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn

I used that solution:

string InsertComma(string val)
{
return val[0 .. $-2] ~ "," ~ val[$-2 .. $];
}

On Thursday, 10 July 2014 at 16:23:44 UTC, John Colvin wrote:

On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:

I have a string X and I need to insert a char in that string...

auto X = "100";

And I need to inser a ',' in position 3 of this string..., I 
try to use the array.insertInPlace, but, not work...


I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');


insertInPlace works like this:

auto X = "100";
auto X1 = X;
X.insertInPlace(3, ',');
assert(X == "100,");
assert(X1 == "100");


You can also do this:

auto X = "100";
auto N = X[0 .. 3] ~ ',' ~ X[3 .. $];
assert(X == "100");
assert(N == "100,");




Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn

Oh, I used that letters in upper case, just for a simple sample...

On Thursday, 10 July 2014 at 16:32:53 UTC, Marc Schütz wrote:

On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote:

Sorry..
I mean:

auto X = "100";
auto N = X.insertInPlace(3,',');

On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
I have a string X and I need to insert a char in that 
string...


auto X = "100";

And I need to inser a ',' in position 3 of this string..., I 
try to use the array.insertInPlace, but, not work...


I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');


`std.array.insertInPlace` doesn't return anything. "In place" 
here means "in situ", i.e. it will not create a new string, but 
insert the new elements into the existing one. This operation 
may still reallocate, in which case the array slice you're 
passing in will be updated to point to the new memory.


Either use this instead:

auto x = "100";
auto n = x.dup;
n.insertInPlace(3, ',');
// or: insertInPlace(n, 3, ',');

... or use slicing and concatenating to construct a new string:

auto g = x[0 .. 3] ~ ',' ~ x[3 .. $];

(Side note about style: It's common practice to use lower-case 
names for variables, upper-case first letters are used to 
denote types. But of course, that's a matter of taste.)




Re: Sum a lot of numbers...

2014-07-10 Thread Alexandre via Digitalmars-d-learn

PS: that is my code:

import std.stdio, std.algorithm, std.range, std.conv;

string InsertComma(string val)
{
return val[0 .. $-2] ~ "," ~ val[$-2 .. $];
}

int main(string[] argv)
{
auto x = "oi.txt"
.File
.byLine
.filter!(line => line[0] == '1')
.map!(line => line[127 .. 140].to!real)
.sum;

auto valor = to!string(x);

write(InsertComma(valor));

readln();
 return 0;
}


On Thursday, 10 July 2014 at 17:16:01 UTC, Alexandre wrote:

Hi :)

I need to sum a list of numbers... but, when I calculate the 
sum of this numbers, I got a simplify representation of sum:


2.97506e+,12

How I can make to get the correctly representation of this 
number ?


Sum a lot of numbers...

2014-07-10 Thread Alexandre via Digitalmars-d-learn

Hi :)

I need to sum a list of numbers... but, when I calculate the sum 
of this numbers, I got a simplify representation of sum:


2.97506e+,12

How I can make to get the correctly representation of this number 
?


Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn

basically format
I read a cobol struct file...

From pos X to Y I have a money value... but, this value don't 
have any format..


0041415

The 15 is the cents... bascally I need to put the ( comma ), we 
use comma to separate the cents, here in Brazil...


On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:

On 07/10/2014 06:05 PM, Alexandre wrote:

I have a string X and I need to insert a char in that string...

auto X = "100";

And I need to inser a ',' in position 3 of this string..., I 
try to use

the array.insertInPlace, but, not work...

I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');


Do you really want to insert a comma in the string, or do you 
want to

format a number as "100,000,000,000.00"?




SImple C++ code to D

2014-07-13 Thread Alexandre via Digitalmars-d-learn

I have this code in C++

//...
char image[0x800];
//...
auto dosh = reinterpret_cast(&image[0]);
dosh->e_magic = *(WORD*)"MZ";
dosh->e_cblp = 0x90;
dosh->e_cp = 3;
dosh->e_cparhdr = 4;
dosh->e_maxalloc = 0x;
dosh->e_sp = 0xb8;
dosh->e_lfarlc = 0x40;
dosh->e_lfanew = 0x80;
BYTE stub[] = { 0xb8, 0x01, 0x4c, 0xcd, 0x21 };
memcpy(&image[0x40], stub, sizeof(stub));


I maded this in D:

module main;

import std.stdio;
import std.c.windows.windows;

struct _IMAGE_DOS_HEADER
{
WORD e_magic;
WORD e_cblp;
WORD e_cp;
WORD e_crlc;
WORD e_cparhdr;
WORD e_minalloc;
WORD e_maxalloc;
WORD e_ss;
WORD e_sp;
WORD e_csum;
WORD e_ip;
WORD e_cs;
WORD e_lfarlc;
WORD e_ovno;
WORD e_res[4];
WORD e_oemid;
WORD e_oeminfo;
WORD e_res2[10];
LONG e_lfanew;
}
alias IMAGE_DOS_HEADER = _IMAGE_DOS_HEADER;
alias PIMAGE_DOS_HEADER = _IMAGE_DOS_HEADER*;

char image[0x800];

void main(string[] args)
{
auto dosh = cast(IMAGE_DOS_HEADER*)&image[0];
//dosh.e_magic = 0x5A4D;
dosh.e_magic = cast(WORD*)("MZ");

auto stub = [0xb8, 0x01, 0x4c, 0xcd, 0x21];
dmemcpy(&image[0x40], stub, stub.sizeof);
}

void * dmemcpy ( void * destination, const void * source, size_t 
num ) pure nothrow

{
(cast(ubyte*)destination)[0 ..
num][]=(cast(const(ubyte)*)source)[0 .. num];
return destination;
}

But I got errors in this:
dmemcpy(&image[0x40], stub, stub.sizeof);

and in the cast of "MZ" to Word*
How is the best way to solve this ?


Re: SImple C++ code to D

2014-07-13 Thread Alexandre via Digitalmars-d-learn

Ok, thanks thanks!!!
Have a lot of thinks I need to learn

When I generate the exe file, something is wrong with this:
dosh.e_magic = cast(WORD)*"MZ".ptr;

When the PE file is generate in EXE have just the "M" of "MZ"...


Re: SImple C++ code to D

2014-07-13 Thread Alexandre via Digitalmars-d-learn

Oh, thanks a lot!!

With a little change all work!

look:

struct IMAGE_DOS_HEADER {
WORD e_magic,
 e_cblp,

//...

void main() {
import std.stdio;
import std.file;
import core.stdc.string;

auto dosh = cast(PIMAGE_DOS_HEADER)image.ptr;
dosh.e_magic = ('Z' << 8) + 'M';

immutable stub = x"b8 01 4c cd 21";
memcpy(&image[IMAGE_DOS_HEADER.sizeof],stub.ptr, stub.length);

std.file.write("a.exe", image);
}


Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

Look at line 114 of my code: http://dpaste.com/3B5WYGV

Have a more better way to make this conversion ?
*(DWORD*)"PE\0\0" ?


Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn
And some other strange thing is, the generate struct in PE file 
is wrong.. look at imagens...


As it should be...: http://i.imgur.com/4z1T3jF.png

And how is being generated...: http://i.imgur.com/Oysokuh.png

My actual source is that: http://dpaste.com/2TZKWF5

On Monday, 14 July 2014 at 11:55:18 UTC, Alexandre wrote:

Look at line 114 of my code: http://dpaste.com/3B5WYGV

Have a more better way to make this conversion ?
*(DWORD*)"PE\0\0" ?




Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

I don't see much need for such aliases.

Ok, how I can reduce the number of aliaSs ?

I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof],


Btw, I need to start that part of code in x80


Look, that is my C++ code base: http://dpaste.com/1MMZK4R
I get a lot of problens, to convert 'strings' to UCHAR... :/


Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;


that is a Real-Mode Stub Program

On Monday, 14 July 2014 at 12:32:38 UTC, Andrea Fontana wrote:

Is there any counter-indication with this:

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;

?

Is it a compile time value?


On Monday, 14 July 2014 at 12:18:20 UTC, bearophile wrote:

Alexandre:


Look at line 114 of my code: http://dpaste.com/3B5WYGV


The indentations are messed up.



peh.Signature = ('\0' << 8) + ('\0' << 8) + ('E' << 8) + 'P';


You need shifts 8, 16, 24...



alias PIMAGE_DOS_HEADER = IMAGE_DOS_HEADER*;


I don't see much need for such aliases.



auto peh = cast(PIMAGE_NT_HEADERS32)&image[0x80];


I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof], 

Bye,
bearophile




Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

bearophile, Thanks for all help!

As I said, I'm coming from C # and C + +, I need to learn 
"tricks" of D language...

'm reading this book: http://ddili.org/ders/d.en/

I have a struct with union...
struct IMAGE_SECTION_HEADER
{
BYTE[8] Name;
union Misc
{
DWORD PhysicalAddress,
VirtualSize;
}
DWORD VirtualAddress,
SizeOfRawData,
PointerToRawData,
PointerToRelocations,
PointerToLinenumbers;
WORD NumberOfRelocations,
NumberOfLinenumbers;
DWORD Characteristics;
}

( the  identation is wrong here... )
Btw, my problem is, how to acess the union elements ?

I try this:
//...
scth[0].Misc.VirtualSize = 15;
//...

But, the compiler return that error:
main.d(151): Error: need 'this' for 'VirtualSize' of type 'uint'

On Monday, 14 July 2014 at 13:00:21 UTC, bearophile wrote:

Alexandre:


I get a lot of problens, to convert 'strings' to UCHAR... :/


I suggest you to take a look at the D docs and understand what 
D fixed-sized arrays are, dynamic arrays, and strings (that are 
dynamic arrays).


Bye,
bearophile




Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

Yes yes, I did it, I used the anonymous type

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

Soory, I not understand, why lose the optlink ?
Read the libs is simple, but, the linker do the brute force!

On Monday, 14 July 2014 at 15:17:06 UTC, Jason King wrote:

On Monday, 14 July 2014 at 14:50:36 UTC, Alexandre wrote:

Yes yes, I did it, I used the anonymous type

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


Now that you've done that, can you build us a linker that reads 
COFF libs so we can lose optlink:)




Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn

void InjectData(T)(ref T BaseSrc, string data)
{
memcpy(&BaseSrc, data.ptr, data.length);
}

It's possible to improve this function ?

On Monday, 14 July 2014 at 15:45:19 UTC, bearophile wrote:

Alexandre:

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


memcpy(&dosh.e_magic, "MZ".ptr, 2);
memcpy(&peh.Signature, "PE\0\0".ptr, 4);
memcpy(scth[1].Name.ptr, ".idata".ptr, 6);
memcpy(scth[2].Name.ptr, ".data".ptr, 5);
memcpy(&image[0x428], x"3820".ptr, 2);
memcpy(&image[0x430], x"3820".ptr, 2);
memcpy(&image[0x43a], "printf".ptr, 6);
memcpy(&image[0x448], "msvcrt.dll".ptr, 10);
memcpy(&image[0x201], x"00304000".ptr, 4);
memcpy(&image[0x207], x"30204000".ptr, 4);
memcpy(&image[0x600], "hello\n".ptr, 6);

Instead of this very bug-prone code, write yourself a little 
function to perform this more safely.


Bye,
bearophile




String to int exception

2014-07-15 Thread Alexandre via Digitalmars-d-learn

Hi :)

I made this function to inverse the bytes in intger or T 
(possible) type...


int reverseBytes(T)(T val)
{
int retVal = 0;
static if(is(T == string))
retVal = to!int(val);

return (retVal & 0x00FF) << 24 |
   (retVal & 0xFF00) << 8 |
   (retVal & 0x00FF) >> 8 |
   (retVal & 0xFF00) >> 24;
}

//...
writefln("%x", reverseBytes(x"00402030"));
//...

When I execute this program, I got this exception:
std.conv.ConvException@C:\D\dmd2\src\phobos\std\conv.d(1968): 
Unexpected '@' when converting from type string to type int


Re: String to int exception

2014-07-15 Thread Alexandre via Digitalmars-d-learn
Thanks, but, when I convert I recive a 'c' in the front of my 
number...


uint reverseBytes(uint val)
{
import core.bitop : bitswap;

return bitswap(val);
}

//...
writefln("%x", reverseBytes(0x00402030));
//...

// output: c040200

On Tuesday, 15 July 2014 at 12:16:26 UTC, bearophile wrote:


See also core.bitop.bswap.

Bye,
bearophile




Re: String to int exception

2014-07-15 Thread Alexandre via Digitalmars-d-learn

Something is wrong between our communication...

I am wanting to do something better to order the bytes, for this 
my code...

https://gist.github.com/bencz/3576dfc8a217a34c05a9

For example, in that case:
injectData(image[0x207], x"30204000");

It's more simple to use something like:
injectData(image[0x207], inverseBytes(0x00402030));


On Tuesday, 15 July 2014 at 13:15:34 UTC, bearophile wrote:

Alexandre:

Thanks, but, when I convert I recive a 'c' in the front of my 
number...


This shows it inverts all bits, not just the four byte 
positions. I don't understand:



import core.bitop: bitswap;

uint reverseBytes(in uint val) pure nothrow @safe @nogc {
return val.bitswap;
}

void main() {
import std.stdio;

immutable uint x = 0x_00_40_20_30U;
immutable uint y = x.reverseBytes;
writefln("%08x", x);
writefln("%032b", x);
writefln("%08x", y);
writefln("%032b", y);
}


Bye,
bearophile




Re: String to int exception

2014-07-15 Thread Alexandre via Digitalmars-d-learn

Strange..., why '@' ?

PS: Ali Çehreli, thanks for your book, your book is wonderful!!!

On Tuesday, 15 July 2014 at 13:49:51 UTC, Ali Çehreli wrote:

On 07/15/2014 04:56 AM, Alexandre wrote:

>  retVal = to!int(val);

> std.conv.ConvException@C:\D\dmd2\src\phobos\std\conv.d(1968):
Unexpected
> '@' when converting from type string to type int

That means to!int failed because 'val' contained a '@' 
character in it:


import std.conv;

void main()
{
auto s = "42@";// <-- What is that?
auto i = to!int(s);
}

However, there seems to be a bug in to(). It seems to 
incorrectly go one character ahead:


to!int("mn");

"Unexpected 'n' when converting from type string to type int"

to!int("m");

"Unexpected end of input when converting from type string to 
type int"


That is a bug, right?

So, in your case the unexpected '@' character may be after 
another unexpected one:


to!int(" @");

"Unexpected '@' when converting from type string to type int"

Ali




Re: SImple C++ code to D

2014-07-15 Thread Alexandre via Digitalmars-d-learn

Oh!
I used the RefCounted because this:
"The proposed C++ shared_ptr<>, which implements ref counting, 
suffers from all these faults. I haven't seen a heads up 
benchmark of shared_ptr<> vs mark/sweep, but I wouldn't be 
surprised if shared_ptr<> turned out to be a significant loser in 
terms of both performance and memory consumption.


That said, D may in the future optionally support some form of 
ref counting, as rc is better for managing scarce resources like 
file handles. Furthermore, if ref counting is a must, Phobos has 
the std.typecons.RefCounted type which implements it as a 
library, similar to C++'s shared_ptr<>."


I found that in this link: 
http://dlang.org/faq.html#reference-counting


On Tuesday, 15 July 2014 at 14:46:01 UTC, bearophile wrote:

Alexandre:


RefCounted!(DWORD) addr;


I think RefCounted is for advanced usages in D :-)



template Wrap(T)
{
struct Wrap
{
T val;
this(T val){val = val;}
}
}


Simpler:

struct Wrap(T) {
T val;
this(T val_) { this.val = val_; }
}


Or just:

struct Wrap(T) { T val; }

Bye,
bearophile




Re: SImple C++ code to D

2014-07-15 Thread Alexandre via Digitalmars-d-learn

I have this struct:

enum AddrType { Abs, RVA, Rel };
struct Address
{
shared_ptr addr;
AddrType type;

Address(): type(Abs) {}
Address(DWORD addr, AddrType type):
addr(shared_ptr(new DWORD(addr))), type(type) {}
Address(shared_ptr addr, AddrType type):
addr(addr), type(type) {}
Address(const Address &ad):
addr(ad.addr), type(ad.type) {}
};

My problem is with default alue init...
Address(): type(Abs) {}
Address(DWORD addr, AddrType type):
addr(shared_ptr(new DWORD(addr))), type(type) {}

How to make this in D ?
I do this:

struct Address
{
RefCounted!(DWORD) addr;
AddrType type = AddrType.Abs;
this(DWORD addr, AddrType type)
{
addr(RefCounted!(DWORD)(new DWORD(addr)));
this.type = type;
}
/*Address(shared_ptr addr, AddrType type) :
addr(addr), type(type) {}
Address(const Address &ad) :
addr(ad.addr), type(ad.type) {}*/
};

It's correct ?


In case for a template, when I have this:
template  struct Wrap {
T val;
Wrap(T val): val(val) {}
};

I maded this:
template Wrap(T)
{
struct Wrap
{
T val;
this(T val){val = val;}
}
}

PS: ( I know about identation... it's wrong here... )


Re: SImple C++ code to D

2014-07-15 Thread Alexandre via Digitalmars-d-learn

Yes yes, I will use the ref... is more safer!

I will try to re-create my logic...
btw, how is the best way to "reinterpret" this ??:

map syms;

and:
vector> values;
vector>> addrs;

On Tuesday, 15 July 2014 at 14:55:36 UTC, bearophile wrote:

Alexandre:

as rc is better for managing scarce resources like file 
handles.


File instances are ref counted in D. Is this useful for you?

Bye,
bearophile




Re: Binary IO

2014-07-18 Thread Alexandre via Digitalmars-d-learn

Maybe this can be usefull for u

https://gist.github.com/bencz/3576dfc8a217a34c05a9

On Thursday, 17 July 2014 at 23:04:06 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Thu, Jul 17, 2014 at 09:01:35PM +, seany via 
Digitalmars-d-learn wrote:

Data is a built in type? what includefile do I need?


It can be any type you want, it was just an example.


T




Re: How to know whether a file's encoding is ansi or utf8?

2014-07-22 Thread Alexandre via Digitalmars-d-learn

Read the BOM ?

module main;

import std.stdio;

enum Encoding
{
UTF7,
UTF8,
UTF32,
Unicode,
BigEndianUnicode,
ASCII
};

Encoding GetFileEncoding(string fileName)
{
import std.file;
auto bom = cast(ubyte[]) read(fileName, 4);

if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76)
return Encoding.UTF7;
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
return Encoding.UTF8;
if (bom[0] == 0xff && bom[1] == 0xfe)
return Encoding.Unicode; //UTF-16LE
if (bom[0] == 0xfe && bom[1] == 0xff)
return Encoding.BigEndianUnicode; //UTF-16BE
	if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 
0xff)

return Encoding.UTF32;

return Encoding.ASCII;
}

void main(string[] args)
{
if(GetFileEncoding("test.txt") == Encoding.UTF8)
writeln("The file is UTF8");
else
writeln("File is not UTF8 :(");
}



On Tuesday, 22 July 2014 at 09:50:00 UTC, Sam Hu wrote:

Greetings!

As subjected,how can I know whether a file is in UTF8 encoding 
or ansi?


Thanks for the help in advance.

Regards,
Sam




Re: How to know whether a file's encoding is ansi or utf8?

2014-07-22 Thread Alexandre via Digitalmars-d-learn

http://www.architectshack.com/TextFileEncodingDetector.ashx

On Tuesday, 22 July 2014 at 15:53:23 UTC, FreeSlave wrote:
Note that BOMs are optional and may be not presented in Unicode 
file. Also presence of leading bytes which look BOM does not 
necessarily mean that file is encoded in some kind of Unicode.



There are several difficulties in this case ...