Random Access I/O

2016-03-25 Thread Chris Williams via Digitalmars-d-learn
I need to be able to perform random access I/O against a file, 
creating a new file if it doesn't exist, or opening as-is (no 
truncation) if it already exists.


None of the access modes for std.stdio.File seem to allow that. 
Any usage of the "w" mode causes my code to consider the file 
empty if it pre-exists (though, it doesn't always actually 
truncate the disk on file?)


If I was coding in C, I would use open() as it gives more options 
for access:


http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html

However, I don't see this exposed in phobos anywhere?


Static convertability testing?

2015-02-12 Thread Chris Williams via Digitalmars-d-learn
I have a template function that gets values out of a tree of 
variant types. My goal is to be able to write code like;


node.get!string(path, to, leaf);

Inside get(), I would like to use std.conv to dynamically convert 
(where able) to the target type (T) or, if that is not possible, 
to return T.init.


If I wanted to force the user to request the correct type as is 
stored in the structure, I could write code like:


switch (leafType) {
case STRING:
   static if (is(T : string)) {
  return leftValue;
   }
   else {
  return T.init
   }
break;
...

But since I want to allow all possiblities that std.conv 
supports, I want something like:


static if (isConvertible!(T, string)) {
   return leftValue.to!T;
}
else {
   return T.init;
}

Is there something like isConvertible() in the library somewhere?


Re: Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn
On Friday, 30 January 2015 at 22:22:27 UTC, Rikki Cattermole 
wrote:
On a slightly related note, I have code for UTC+0 to unix time 
stamp.

https://github.com/Devisualization/util/blob/b9ab5758e755c4e33832ac4aed0a5d7f2c728faf/source/core/devisualization/util/core/time.d


Unix timestamps can be negative, so you should probably be using 
longs instead of ulongs.


Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn
I'm attempting to print a human-readable version of a timestamp. 
The timestamp is coming from an external service, via JSON. An 
example is:


1421865781342

Which I know to be:

2015-01-21T18:43:01.342Z

The only method I see which takes an epoch-style timestamp, so 
that I can convert it to something printable, is the SysTime 
constructor:


pure nothrow @safe this(long stdTime, immutable TimeZone tz = 
null);


According to the reference, this seems to take the value in 
hnsecs. My expectation would be that this means multiplying my 
initial value by 1_000_000. But if I do that, I get a random date 
2500 years in the future.


I created this sample code:

void main() {
long time = 1421865781342L;
writefln(%s, SysTime(time));
writefln(%s, SysTime(time * 10L));
writefln(%s, SysTime(time * 100L));
writefln(%s, SysTime(time * 1_000L));
writefln(%s, SysTime(time * 10_000L));
writefln(%s, SysTime(time * 100_000L));
writefln(%s, SysTime(time * 1_000_000L));

writefln(%s, Clock.currTime.stdTime);
}

Outputs:

0001-Jan-02 07:36:48.5781342
0001-Jan-17 03:04:47.781342
0001-Jun-14 05:44:39.81342
0005-Jul-04 08:23:20.1342
0046-Jan-21 10:50:03.342
0451-Jul-28 11:17:15.42
4506-Sep-18 16:42:14.2
635582516

My expectation would be that the final line would be something 
beginning with 14 at least. Playing around with possible 
multipliers, there doesn't even seem to be any integer value that 
would allow conversion between the timestamp I received and 
whatever SysTime expects.


I'm using:

DMD64 D Compiler v2.066.1
Ubuntu from .deb package

Is this a bug, or am I doing something wrong?


Re: Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn

On Saturday, 31 January 2015 at 00:20:07 UTC, ketmar wrote:

On Sat, 31 Jan 2015 00:03:43 +, Chris Williams wrote:


since most database software probably
stores birthdates (many of which are pre-1970) in this format

O_O
a perfectly broken software.


And stdc:

http://h50146.www5.hp.com/products/software/oe/tru64unix/manual/v51a_ref/HTML/MAN/MAN3/3955.HTM

And UNIX:

http://www.lehman.cuny.edu/cgi-bin/man-cgi?mktime+3


Re: Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn

On Friday, 30 January 2015 at 22:38:21 UTC, Chris Williams wrote:
On Friday, 30 January 2015 at 22:22:27 UTC, Rikki Cattermole 
wrote:
On a slightly related note, I have code for UTC+0 to unix time 
stamp.

https://github.com/Devisualization/util/blob/b9ab5758e755c4e33832ac4aed0a5d7f2c728faf/source/core/devisualization/util/core/time.d


Unix timestamps can be negative, so you should probably be 
using longs instead of ulongs.


Yup, there was a world before January 1st, 1970.


Re: Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn

On Friday, 30 January 2015 at 23:50:53 UTC, ketmar wrote:

On Fri, 30 Jan 2015 23:42:04 +, Chris Williams wrote:

On Friday, 30 January 2015 at 22:38:21 UTC, Chris Williams 
wrote:
On Friday, 30 January 2015 at 22:22:27 UTC, Rikki Cattermole 
wrote:
On a slightly related note, I have code for UTC+0 to unix 
time stamp.

https://github.com/Devisualization/util/blob/

b9ab5758e755c4e33832ac4aed0a5d7f2c728faf/source/core/devisualization/util/
core/time.d


Unix timestamps can be negative, so you should probably be 
using longs

instead of ulongs.


Yup, there was a world before January 1st, 1970.


not for unix timestamps. please, stop that, unix timestamp was 
not
designed to present any dates before 1970. negative timestamp 
is a bug

in code.


Unless you know something I don't, everything I've ever read says 
that a negative unix timestamp is meant to refer to a time before 
1970. It may not have been intentional, but since most database 
software probably stores birthdates (many of which are pre-1970) 
in this format, having a library be unable to support them just 
makes the library useless for many situations.


Re: Time from timestamp?

2015-01-30 Thread Chris Williams via Digitalmars-d-learn
On Saturday, 31 January 2015 at 00:14:37 UTC, Steven 
Schveighoffer wrote:

On 1/30/15 5:18 PM, Chris Williams wrote:
I'm attempting to print a human-readable version of a 
timestamp. The
timestamp is coming from an external service, via JSON. An 
example is:


1421865781342

Which I know to be:

2015-01-21T18:43:01.342Z



http://dlang.org/phobos/std_datetime.html#.unixTimeToStdTime

It's kind of convoluted because there is no epoch, but you can 
make one:


import std.datetime;
import std.stdio;

void main(string[] args)
{
// can't make this enum because of time zone...
auto epoch = SysTime(unixTimeToStdTime(0), UTC());
writeln(epoch + 1_421_865_781_342.msecs);
}

output:
2015-Jan-21 18:43:01.342Z

Note the reason your code didn't work is because SysTime uses 
1/1/1 as the epoch.


-Steve


D'oh, I missed that in the description:

and convert it to hnsecs in UTC since midnight, January 1st, 1 
A.D. UTC


That does explain it. I also didn't spot the declaration of 
unixTimeToStdTime(), which assuredly helps.


Thank you!


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:

Sometime , I need to copy them:

thrift.Card tc;

db.Card dc;

dc.id = tc.id;
dc.pwd = tc.pwd;
...


It is boring coding, I want a solution to copy them 
automatically:

void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?


Assuming that the hibernated class isn't auto-generated and you 
can redefine its contents freely, the following style may be an 
alternative that works for you:


struct Foo {
public:
string a;
int b;
}

class FooClass {
public:
union {
struct {
string a;
int b;
};
Foo foo;
}

}

void main() {
Foo f = Foo(a, 10);
FooClass c = new FooClass();
c.foo = f;

writefln(%s %s, c.a, c.b);
}

Probably the anonymous struct will break the UDAs, but it should 
be worth testing.


Re: Using std.net.curl to stream data

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 14:18:38 UTC, Trollgeir wrote:
I'm having some trouble trying to stream data to my plot.ly 
graph:

https://plot.ly/62/~Trollgeir/

The API:  https://plot.ly/streaming/

I am able to post messages that get recorded into the stream 
live, although right after curl uploads it, it just seems to 
wait for a response it's not getting, and eventually timeouts. 
Does anyone have any advice?


auto client = HTTP(stream.plot.ly);
client.addRequestHeader(plotly-streamtoken,e8bg6omat6);
client.verbose = true;

string msg = { \x\: 500, \y\: 500 } \n;
client.postData(msg);
client.perform; 


You have to define a handler for HTTP.onReceive before calling 
HTTP.perform. It will receive ubyte arrays for each packet that 
comes in. For most purposes, you just copy that onto the end of a 
string in an external scope. But if you're streaming content, 
you'll need to do something more fancy.


Re: how convert the range to slice ?

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 22:43:36 UTC, bearophile wrote:

Nordlöw:

Is there any chance we could add logic to dmd+phobos that 
hints user about this?


It's such a fundamental part of D+Phobos that newbies are 
forced to learn this quickly. On the other hand an informative 
error message could be useful...


What error message do you suggest?

Bye,
bearophile


Range is not castable to array. See std.array.array to generate 
an array from a Range.


Re: Russian translation of the range term?

2014-11-12 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 12 November 2014 at 11:38:52 UTC, thedeemon wrote:
On Tuesday, 11 November 2014 at 11:50:18 UTC, Ivan Kazmenko 
wrote:

Hi!

I'm unsure what is the Russian equivalent for the term 
range, as in D range, the generalization of a pair of 
iterators.


I think последовательность (sequence) is the most 
appropriate, because the defining characteristic of an input 
range (most common one) is ability to provide data 
sequentially. Also, afaik, some languages like F# and Clojure 
use this term (often shortened to 'seq') for the same thing 
that D calls ranges.


While sequence makes more sense for how std.range thinks of 
ranges, I think the history of the term is closer to how we use 
slices. So another (English) alternative to try might be a 
view. It's a fairly common term in SQL databases, so presumably 
there's a translation for it in Russian.


Re: Why do some language-defined attributes have @ and some not?

2014-10-24 Thread Chris Williams via Digitalmars-d-learn

On Thursday, 23 October 2014 at 07:39:21 UTC, Gary Willoughby
wrote:
On Thursday, 23 October 2014 at 00:59:26 UTC, Shriramana Sharma 
via Digitalmars-d-
I submit that the syntax for attributes should be streamlined. 
Shall I

go and open a Bugzilla item?


No need: http://wiki.dlang.org/DIP64


Besides the @ symbols, isn't there also some inconsistency on
whether attributes go before or after the declaration?

@property public static void foo() const @safe pure nothrow

I've never bothered to ascertain how much of the positioning is
optional, but definitely none of it makes sense.


Re: Search Engine

2014-10-08 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 8 October 2014 at 18:15:08 UTC, ANtlord wrote:
It would be stable? I mean program, that will use C++ extern 
interface.


Trying to link to C++ code will cause some work to solve build 
issues, but there shouldn't be any stability impacts other than 
recognizing that C++ won't be using memory management.


Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling.

2014-06-25 Thread Chris Williams via Digitalmars-d-learn

On Monday, 23 June 2014 at 22:08:59 UTC, John Carter wrote:

On Monday, 23 June 2014 at 21:26:19 UTC, Chris Williams wrote:


More likely what you want are variants:


Hmm. Interesting.

Yes, Variant and VariantArray are much closer to the dynamic 
language semantics...


But the interesting thing is Tuple is much closer to What I 
Mean when I create these things.


I probably should used Rubies
   http://ruby-doc.org/core-2.0/Struct.html
but apart from naming the elements it gives me nothing beyond 
more keystrokes to enter.


Tuple is very similar, but also grants type safety.


D has structs. If you have any problem working with Tuples, you 
might consider moving over.


Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling.

2014-06-23 Thread Chris Williams via Digitalmars-d-learn

On Monday, 23 June 2014 at 21:18:39 UTC, John Carter wrote:
I guess between perl and Ruby and Scheme etc. I got used to 
creating hybrid containers


Want a pair of [string, fileList]? Just make an Array with two 
items, one a string, one and array of strings. Done.


D barfed... leaving me momentarily stunned... then Oh Yes, type 
safety, Tuple's are the answer where Tuples where Tuples...


Eventually found http://dlang.org/tuple.html and more 
specifically the somewhat unexpectedly named 
http://dlang.org/phobos/std_typecons.html and off I went...


I do have a personal design guideline of when you adding too 
much behaviour to a heterocontainer, refactor into a class.


But I guess I have never realised how often I do casually 
create heterogenous containers


Just rambling and musing.


More likely what you want are variants:

http://dlang.org/library/std/variant/variantArray.html
http://dlang.org/library/std/variant.html

Tuples can hold multiple types, but they're only available during 
compile time.


Separate allocation and construction?

2014-06-05 Thread Chris Williams via Digitalmars-d-learn
If I wanted to allocate memory for a class and then call its 
constructor as two separate steps, while still having the object 
be managed by the garbage collector, is there any way to do that?


I believe that I've figured out a way to accomplish the first 
step, but not the second.


import std.stdio;

class Foo {
this(string bar) {
writeln(bar);
}
}

void main() {
Foo f = (new Foo[1])[0];
f(Hello); // doesn't compile
}


Re: Separate allocation and construction?

2014-06-05 Thread Chris Williams via Digitalmars-d-learn

On Thursday, 5 June 2014 at 22:25:03 UTC, Adam D. Ruppe wrote:

On Thursday, 5 June 2014 at 22:22:16 UTC, Chris Williams wrote:
If I wanted to allocate memory for a class and then call its 
constructor as two separate steps, while still having the 
object be managed by the garbage collector, is there any way 
to do that?



Check out std.conv.emplace
http://dlang.org/phobos/std_conv.html#emplace

First, allocate the memory block for the class. The size is 
__traits(classInstanceSize, Yourclass). Then slice it:


enum size = __traits(classInstanceSize, YourClass);
auto memory = GC.malloc(size)[0 .. size];


Why slice? There seems to be a version of emplace that accepts a 
pointer, which is what GC.malloc() seems to return.