Re: Converting a ubyte[] to a struct with respect to endianness?
On Fri, Jun 23, 2017 at 10:10:22PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 06/23/2017 09:26 PM, Felix wrote: > > That works, thanks! > > I've just tried this, which seems cleaner: > > import std.stdio; > import std.system; > import std.bitmanip; > > void ensureBigEndian(T)(ref T value) { > if (endian == Endian.littleEndian) { > value = *cast(T*)nativeToBigEndian(value).ptr; > } This is wrong, you should be detecting the endianness of the input and use {big,little}EndianToNative instead. For example, if the input is big endian, you should use bigEndianToNative. Internally, if native is already big endian, it will do nothing; otherwise it will swap the endianness. This way you don't have to check the current machine's endianness yourself; you can just recompile on a machine of different endianness and it will Just Work. T -- People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. -- D. Knuth
Re: Converting a ubyte[] to a struct with respect to endianness?
On 06/23/2017 09:26 PM, Felix wrote: That works, thanks! I've just tried this, which seems cleaner: import std.stdio; import std.system; import std.bitmanip; void ensureBigEndian(T)(ref T value) { if (endian == Endian.littleEndian) { value = *cast(T*)nativeToBigEndian(value).ptr; } } void main() { ubyte[] bytes = [ 0, 0, 0, 13 ]; uint u = *cast(uint*)bytes.ptr; writefln("Just read: %s", u); u.ensureBigEndian; writefln("Converted: %s", u); } Just read: 218103808 Converted: 13 Ali
Re: Converting a ubyte[] to a struct with respect to endianness?
That works, thanks!
Re: Converting a ubyte[] to a struct with respect to endianness?
On 06/23/2017 07:52 PM, Felix wrote: > So I'm guessing my ubytes are in the wrong order in the uint... how should I put them around the correct way so that my code won't break on another machine with different endianness? Yes, that would happen when your system is little-endian. (According to spec, the length field is big-endian.) You can detect what endianness the system has and swap the bytes of the length field: http://ddili.org/ders/d.en/union.html#ix_union.endian,%20std.system import std.system; import core.bitop; // ... if (endian == Endian.littleEndian) { address.value = bswap(address.value); } std.bitmanip may be useful as well: https://dlang.org/phobos/std_bitmanip.html Ali
Converting a ubyte[] to a struct with respect to endianness?
I'm trying to read in just the first part of a .png file to peek at it's width and height without loading in the whole file. I'm using FreeImage for reading the whole file but since it doesn't have a function to let me peek at the image size before loading it all in I'm rolling my own. I've gotten as a far as reading in the first 16 bytes which includes an 8 byte signature, then the length and type of the first chunk: struct Header { ubyte[8] signature; uint length; char[4] type; } union Un { Header h; ubyte[16] u; } auto f = File(path, "r"); foreach (ubyte[] buffer; f.byChunk(16)){ Un fff; fff.u = buffer[0..16]; writeln(fff.h); break; } It prints out: Header([137, 80, 78, 71, 13, 10, 26, 10], 218103808, "IHDR") The signature is correct, the type is correct, but the value I get for length should be 13, not 218103808. So I'm guessing my ubytes are in the wrong order in the uint... how should I put them around the correct way so that my code won't break on another machine with different endianness?
Re: implib.exe no output files
On Wednesday, 21 June 2017 at 11:30:56 UTC, Ivan Kazmenko wrote: [...] Step 5. I don't get any lib files. I got lib files on another computer, though.
Re: Read conditional function parameters during compile time using __traits
On Wednesday, 21 June 2017 at 20:48:52 UTC, ag0aep6g wrote: On 06/21/2017 09:39 PM, timvol wrote: size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 1 ) { return 10; // More complex calculated value } size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 2 ) { return 20; // More complex calculated value } size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 3 ) { return 30; // More complex calculated value } [...] But... how can I execute these functions? I mean, calling doCalcLength(1) function says "Variable ubFuncCode cannot be read at compile time". So my idea is to create an array during compile time using traits (e.g. __traits(allMembers)) and to check this later during runtime. For illustration purposes something like this: --> During compile time: void function()[ubyte] calcLengthArray; auto tr = __traits(allMembers, example); foreach ( string s; tr ) { calcLengthArray[__trait(get, s)] = s; } As far as I know, there's no way to get the ubFuncCode from the constraints. In order to figure out which values are valid, you have to try them all. Which is actually doable for a ubyte: size_t function()[ubyte] calcLengthArray; static this() { import std.meta: aliasSeqOf; import std.range: iota; foreach (ubFuncCode; aliasSeqOf!(iota(ubyte.max + 1))) { static if (is(typeof(!ubFuncCode))) { calcLengthArray[ubFuncCode] = !ubFuncCode; } } } Using a static constructor instead of direct initialization, because you can't initialize a static associative array directly. --> During runtime: size_t doCalcLength(ubyte ubFuncCode) { auto length = 0; if ( ubFuncCode in calcLengthArray ) { length = calcLengthArray[ubFuncCode]!(ubFuncCode)(); } return length; } If you can accept hard-coding the range of ubFuncCode values here (and if there are no holes), then you can generate a switch that calls the correct calcLength version: import std.meta: aliasSeqOf; import std.range: iota; enum min = 1; enum max = 3; sw: switch (ubFuncCode) { foreach (code; aliasSeqOf!(iota(min, max + 1))) { case code: length = calcLength!code(); break sw; } default: throw new Exception("unexpected ubFuncCode"); } Instead of hard-coding the range, you could also do the same here as above when filling calcLengthArray: loop over all ubyte values and figure out which ones are valid with a `static if`. I hope everyone knows what I want to do :). But... does anyone know how I can realize that? I don't want to use a switch/case structure because the calcLength() functions can be very complex and I've over 40 different function codes. So, I think the best approach is to use something similar to the one I described. I don't see how you're reducing the complexity here. You have the same code, just spread over 40 functions, plus the extra code to make it work. From what I see, I'd prefer the hand-written switch. Thanks in advance! I finally solved my problem by adjust my message structure. So I'm now sending the length of the message, followed by the function code and the data. I finally created different functions and annotated them with @FunctionCode(1), e.g.: @FunctionCode(1) void func1() { ... } @FunctionCode(2) void func2() { ... } But I now ran into the next problem. I'm creating an array containing these function using traits: #1: foreach ( cb; __traits(allMembers, test)) #2: { #3: static if ( __traits(isStaticFunction, __traits(getMember, test, cb)) ) #4: { #5: // do something #6: } #7: } But I'm getting the following error on line #3: error: undefined identifier '_D11TypeInfo_ya6__initZ' I figured out that some other users are also had this error. Unfortunately, the error wasn't solved perfectly as far as I know. So, how can I resolve the eror?
Re: How to add class in DIET template
On 6/23/17 12:06 PM, Martin Tschierschke wrote: You can use the html syntax for the class parameter, the funny/nice thing is, that mystr can be used directly without "#{}" around. code(class=mystr) #{line} works. Yes, I just found out recently -- when you don't use quotes for an attribute value, it's treated as a D expression (and then interpolated). If that D expression is of type bool, then the attribute is added or not based on the value of that bool. Good for things like checkboxes and 'checked'. -Steve
Re: How to add class in DIET template
On Friday, 23 June 2017 at 06:59:22 UTC, Suliman wrote: I need to get external variable and make class by it's value - string mystr = "lng-" ~ language; - foreach(i, line; arrayOfLines ) li code.mystr #{line} I need to get HTML code like this: some D code But class name become "mystr" and I am getting: some D code How to fix it? You can use the html syntax for the class parameter, the funny/nice thing is, that mystr can be used directly without "#{}" around. code(class=mystr) #{line} works.
Re: Cannot implicitly convert expression (struct this)
On Thursday, 22 June 2017 at 09:57:44 UTC, Andre Pany wrote: This line raises the error: TestStruct s2 = TestStruct(Reason.FU); Error: cannot implicitly convert expression ("Fu") of type Reason to InitialEnum!(Reason) While this line is working fine: TestStruct s1 = {reason: Reason.FU}; I think these should be equivalent, report a bug.
Re: Immutable
On 06/23/2017 04:29 PM, Adam D. Ruppe wrote: try `new immutable AppendChatCommand` instead of just `new`. If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error. With a `pure` constructor, just `new` should work, too.
Re: Dealing with the interior pointers bug
On Thu, 2017-06-22 at 18:38 +, Boris-Barboris via Digitalmars-d- learn wrote: > […] > > Casts are part of the type system. Yes, D type system allows > invalid operations. It's not the compiler's fault, it's type > system's fault. > […] Well maybe casts should be allowed as they effectively break the type system. Sadly D2 has casts, so the type system is weak, so problems with GC algorithms allowed. Maybe it is time for D3, which is D2 and no casts. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Immutable
On Friday, 23 June 2017 at 14:29:41 UTC, harakim wrote: heh. I've been working on this for an hour or so. Right after I posted, I tried casting it, which worked. Thank you for your time. cast works, but `new immutable` with a pure ctor should work better. (casts are easy to get wrong so best to avoid them if there's another way)
Re: Immutable
On Friday, 23 June 2017 at 14:29:40 UTC, Adam D. Ruppe wrote: On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote: immutable(AppendChatCommand) command = new AppendChatCommand(type, text); try `new immutable AppendChatCommand` instead of just `new`. If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error. Thanks for showing me the right way to do it. My casting strategy just lead to a runtime error.
Re: Immutable
On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote: I am building a system where one thread generates commands and sends them to another thread. The commands will never change once they are created. I have marked the values immutable, but I've been struggling to understand the requirements for sharing a variable across threads. [...] heh. I've been working on this for an hour or so. Right after I posted, I tried casting it, which worked. Thank you for your time.
Re: Immutable
On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote: immutable(AppendChatCommand) command = new AppendChatCommand(type, text); try `new immutable AppendChatCommand` instead of just `new`. If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error.
Immutable
I am building a system where one thread generates commands and sends them to another thread. The commands will never change once they are created. I have marked the values immutable, but I've been struggling to understand the requirements for sharing a variable across threads. cannot implicitly convert expression (new AppendChatCommand(type, text)) of type data.messages.AppendChatCommand to immutable(AppendChatCommand) I'm trying to do this: immutable(AppendChatCommand) command = new AppendChatCommand(type, text); send(childTid, command); //Compiler does not like command if it's not immutable or shared (and I won't be modifying it or using it in this thread) The commands are generated and send as messages to the child. I will never even use them again in the parent thread. What is the easiest way to do this? Do I need to mark the class somehow? I can provide a contrived example if necessary.
Re: GDC generate wrong .exe ("not a valid win32 application")
On Wednesday, 21 June 2017 at 15:55:27 UTC, David Nadlinger wrote: On Monday, 19 June 2017 at 14:08:56 UTC, Patric Dexheimer wrote: Fresh install of GDC. (tried with 32x ad 32_64x) Where did you get the GDC executable from? The GDC project doesn't currently offer any official builds that target Windows; the 6.3.0 builds from https://gdcproject.org/downloads in fact generate Linux binaries. — David Oh, ops. I looked at the title "x86_64-w64-mingw32" not at the (target) x86_64-linux-gnu. Thanks :)
Re: Dub command line knowledge sought
On Fri, 2017-06-23 at 09:24 +, Laeeth Isharc via Digitalmars-d- learn wrote: > […] > > Check out the Kaleidic fork maintained by John Colvin - currently > in his personal repository on github. We submitted back our > changes but ended up being quite a lot so not all have been > accepted yet. Allows you to change location of dub repos - > useful to avoid work and personal interacting without having to > use containers or VMs, but it may be easy enough to change path > on an adhoc basis as you wish to do. Don't recall right now, but > take a look. Thanks for the tip. Hopefully the fork will go away quickly because everything is merged in to the mainline. Using a fork is fine for me personally, but I cannot rely on it for SCons: a SCons tool either has to carry the entirety (not really feasible I suspect, but I will check) or assume the platform supplied one. I guess a subsidiary issue is that Dub is not available where SCons is, so special measures are already needed. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Dub command line knowledge sought
On Friday, 23 June 2017 at 08:26:21 UTC, Russel Winder wrote: On Fri, 2017-06-23 at 08:11 +, Nicholas Wilson via Digitalmars-d- learn wrote: On Friday, 23 June 2017 at 07:51:51 UTC, Russel Winder wrote: > I am likely just staring at and missing the data needed: > > How does one invoke dub to fetch and build, and put into a > place other that ~/.dub/… a package from Dub? dub fetch foo --version=1.0.0 mv ~/.dub/packages/foo-1.0.0 /desired/path dub add-path /desired/path dunno about how to do that in one step. This is what I feared. The real awkwardness here is that Dub stores all the compilation products nicely separated by platform, compiler, and configuration, and puts the last build in a nice named place on the assumption there will only ever be one. So generally the ~/.dub/packages/foo-1.0.0 has to be moved as above. Using unit-threaded as an example: ~/.dub/packages/unit-threaded-0.7.24/unit-threaded/.dub/build/library-debug-linux.posix-x86_64-ldc_2073-1AF38A4B25224ABFB5BB5ED68A0E4633 Is the location of the last debug build on linux using ldc2, but what is that hash code, how to make that deducible so that it isn't necessary to move, just build. Check out the Kaleidic fork maintained by John Colvin - currently in his personal repository on github. We submitted back our changes but ended up being quite a lot so not all have been accepted yet. Allows you to change location of dub repos - useful to avoid work and personal interacting without having to use containers or VMs, but it may be easy enough to change path on an adhoc basis as you wish to do. Don't recall right now, but take a look.
Re: Dub command line knowledge sought
On Fri, 2017-06-23 at 08:11 +, Nicholas Wilson via Digitalmars-d- learn wrote: > On Friday, 23 June 2017 at 07:51:51 UTC, Russel Winder wrote: > > I am likely just staring at and missing the data needed: > > > > How does one invoke dub to fetch and build, and put into a > > place other that ~/.dub/… a package from Dub? > > dub fetch foo --version=1.0.0 > mv ~/.dub/packages/foo-1.0.0 /desired/path > dub add-path /desired/path > > dunno about how to do that in one step. This is what I feared. The real awkwardness here is that Dub stores all the compilation products nicely separated by platform, compiler, and configuration, and puts the last build in a nice named place on the assumption there will only ever be one. So generally the ~/.dub/packages/foo-1.0.0 has to be moved as above. Using unit-threaded as an example: ~/.dub/packages/unit-threaded-0.7.24/unit-threaded/.dub/build/library-debug-linux.posix-x86_64-ldc_2073-1AF38A4B25224ABFB5BB5ED68A0E4633 Is the location of the last debug build on linux using ldc2, but what is that hash code, how to make that deducible so that it isn't necessary to move, just build. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Dub command line knowledge sought
On Friday, 23 June 2017 at 07:51:51 UTC, Russel Winder wrote: I am likely just staring at and missing the data needed: How does one invoke dub to fetch and build, and put into a place other that ~/.dub/… a package from Dub? dub fetch foo --version=1.0.0 mv ~/.dub/packages/foo-1.0.0 /desired/path dub add-path /desired/path dunno about how to do that in one step.
Dub command line knowledge sought
I am likely just staring at and missing the data needed: How does one invoke dub to fetch and build, and put into a place other that ~/.dub/… a package from Dub? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
How to add class in DIET template
I need to get external variable and make class by it's value - string mystr = "lng-" ~ language; - foreach(i, line; arrayOfLines ) li code.mystr #{line} I need to get HTML code like this: some D code But class name become "mystr" and I am getting: some D code How to fix it?