Non-blocking keyboard input

2023-12-26 Thread Joe--- via Digitalmars-d-learn
??? Surely there there is a 
one liner library solution  for this?


I have a program that spawns a thread for debugging information 
and uses the keyboard input which allows me to display the 
information.


If I use getchar or readline then it blocks the thread. This is 
generally fine because that is all the thread does. The problem 
is that it also blocks the program termination as the main thread 
will not exit while the thread is running which is is because 
it's waiting on keyboard input stuck on getchar or fgetc or 
whatever.


If I terminate the threads using thread_term then it terminates 
the program but the program then does not return the return code 
that it was successfully finished(because it was prematurely 
terminated by thread_term.


Surely there is some type of peek for keyboards in D? I can't 
seem to get kbhit or because the library is not included(well, I 
tried to use one from dmc but it seemed to be invalid. snn.lib 
IIRC).



This shouldn't be hard... yet it is.





curl :sending mail via SMTP

2023-12-26 Thread phcute via Digitalmars-d-learn

Greetings!

I was trying to translate an example code snippets from curl 
which demonstrates how to sending an email via smtp using curl 
api.The orignal c code works fine,but my D code failed at the 
beginnig when it is running.


Appreicate for the help to figure me out what's wrong with the 
code.



dmd v2.106.0 , VS community 2019,Win10 64bit
'''
call dmd -m64 curl.lib sendmail.d && sendmail

* No URL set!
curl_easy_perform() failed:URL using bad/illegal format or 
missing URL


'''

D source:

'''
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import etc.c.curl;
//import std.conv;
//import std.string;

pragma(lib,"curl.lib");


const PASSWORD="real_pass_word";
const FROM_ADDR="sender_em...@add.ress";
const TO_ADDR="receiver_em...@add.ress";
const CC_ADDR="cc_copy_em...@add.ress";

const FROM_MAIL="sender_em...@add.ress";
const TO_MAIL="receiver_em...@add.ress";
const CC_MAIL="cc_copy_em...@add.ress";

const SMTP_VALUE="smtp://smtp-mail.outlook.com:25";//M$ 
outlook.com mail SMTP...



//static const char* payload_text=cast(const char*)(

enum contents=(
"To: " ~ TO_MAIL ~ "\r\n" ~
"From: " ~ FROM_MAIL ~ "\r\n" ~
"Cc: " ~ CC_MAIL  ~ "\r\n" ~
"\r\n" ~
"Subject: SMTP example message" ~ "\r\n" ~
"\r\n" ~
"The body of the message starts here." ~ "\r\n" ~
"\r\n" ~
"It could be a lot of lines, could be MIME encoded, 
whatever.\r\n\r\nGood Luck!\r\nGood Luck!" ~ "\r\n");



static const char* payload_text= cast(const char*)contents.ptr;

struct upload_status
{
size_t bytes_read;
}

static size_t payload_source(char* ptr,size_t size,size_t 
nmemb,void* userp)

{
upload_status* upload_ctx=cast(upload_status*)userp;
//char* data;
size_t room=size*nmemb;

if((size==0)||(nmemb==0)||((size*nmemb)<1))
{
return 0;
}

const char* data=&payload_text[upload_ctx.bytes_read];
//const char* data=null;

	//memcpy(cast(void*)data,cast(void*)payload_text,upload_ctx.bytes_read);

if(data)
{
size_t len=strlen(data);
if(room betterC ? no
void main()
{

CURL* curl;
CURLcode res=CurlError.ok;
curl_slist* recipents=null;
upload_status upload_ctx={0};

curl=curl_easy_init();

if(curl)
{
curl_easy_setopt(curl,CurlProto.smtp,SMTP_VALUE.ptr);

curl_easy_setopt(curl,CurlOption.username,FROM_MAIL.ptr);
curl_easy_setopt(curl,CurlOption.mail_from,FROM_ADDR.ptr);
curl_easy_setopt(curl,CurlOption.password,PASSWORD.ptr);

recipents=curl_slist_append(recipents,TO_ADDR.ptr);
recipents=curl_slist_append(recipents,CC_ADDR.ptr);
curl_easy_setopt(curl,CurlOption.mail_rcpt,recipents);


		curl_easy_setopt(curl,CurlOption.readfunction,&payload_source);

curl_easy_setopt(curl,CurlOption.readdata,&upload_ctx);
curl_easy_setopt(curl,CurlOption.verbose,1);
curl_easy_setopt(curl,CurlOption.upload,1);
curl_easy_setopt(curl,CurlOption.use_ssl,CurlUseSSL.all);

curl_easy_setopt(curl,CurlOption.ssl_verifypeer,0);

res=curl_easy_perform(curl);

if(res!=CurlError.ok)
{
			fprintf(stderr,"curl_easy_perform() 
failed:%s\n",curl_easy_strerror(res));

}

curl_slist_free_all(recipents);


curl_easy_cleanup(curl);
}

getchar();

//return 0;

}

'''





Re: FileException when calling getTimes on a DirEntry

2023-12-26 Thread kdevel via Digitalmars-d-learn

On Tuesday, 26 December 2023 at 21:09:05 UTC, Renato wrote:

On Sunday, 24 December 2023 at 21:18:44 UTC, kdevel wrote:
[...]
I would have expected you asking questions like "Which program 
might have generated that symlink?", "How do I get the 
meta-data from the
symlink and not from the file it points to ?" or "How do I 
handle that exception?".


Who do you think you are to tell me how I should ask questions?


In this thread I didn't tell you how you should ask questions. 
However, if there are any further questions regarding the topic 
of this thread don't hesitate to ask.


Re: FileException when calling getTimes on a DirEntry

2023-12-26 Thread Renato via Digitalmars-d-learn

On Sunday, 24 December 2023 at 21:18:44 UTC, kdevel wrote:

On Sunday, 24 December 2023 at 20:00:15 UTC, Renato wrote:
I asked what could be causing an Exception in my code to 
happen as that was quite unexpected.


I would have expected you asking questions like "Which program 
might have generated that symlink?", "How do I get the 
meta-data from the
symlink and not from the file it points to ?" or "How do I 
handle that exception?".


Who do you think you are to tell me how I should ask questions?

How pretentious can people get on these forums? If you don't like 
how someone asks a question, it's much nicer to just refrain from 
interacting. Does it make you feel better to shit on people's 
questions? It certainly didn't make me feel good at all to have 
to deal with your unwarranted, condescending tone.


Re: dlang.org/Learn "hello_world".sort.chain ...

2023-12-26 Thread Sergey via Digitalmars-d-learn

On Tuesday, 26 December 2023 at 13:58:54 UTC, tony wrote:

On Tuesday, 26 December 2023 at 11:19:29 UTC, Sergey wrote:


Use typeid, instead of typeof


Thanks!

Got quite a type but I will worry about that later: 
std.range.SortedRange!(Result, "a < b").SortedRange


Yes, because sort is returning special type, compatible with 
Ranges interface.
You can add 'sort(...).array' and the type will become 'int[]' 
(or which types arrays were before).


Ranges are specific things for D (one of the core feature of the 
lang and std), which can evaluated lazily. You can check some 
description how they are working in the documentation 
https://dlang.org/phobos/std_range.html


Re: dlang.org/Learn "hello_world".sort.chain ...

2023-12-26 Thread tony via Digitalmars-d-learn

On Tuesday, 26 December 2023 at 11:19:29 UTC, Sergey wrote:


Use typeid, instead of typeof


Thanks!

Got quite a type but I will worry about that later: 
std.range.SortedRange!(Result, "a < b").SortedRange





Re: dlang.org/Learn "hello_world".sort.chain ...

2023-12-26 Thread Sergey via Digitalmars-d-learn

On Tuesday, 26 December 2023 at 10:53:10 UTC, Tony wrote:
I just typed in the program that is on the first page of Learn. 
It has this line:


sort(chain(arr1, arr2, arr3));

I assigned that to a variable:

arr4 = sort(chain(arr1, arr2, arr3));

then printed it out

writefln("%s",arr4);   // works

and then tried to print out the type of arr4:

writefln("%s",typeof(arr4));

and got this error:

// HelloWorld.d:25:19: error: cannot pass type 
SortedRange!(Result, "a < b") as a function argument


What exactly is that type? Or maybe, what would it take to 
understand what that type is?


Use typeid, instead of typeof