digitalmars.D.learn - help with c translation
- robby (84/84) Jul 02 2009 can anybody help me translate this c function to d, im having a problem ...
- Lars T. Kyllingstad (12/120) Jul 02 2009 I'm no C expert, but I'll give it a shot.
- robby (1/1) Jul 02 2009 thanks for help, i did the conversion as you mentioned and i get various...
- Lars T. Kyllingstad (4/5) Jul 02 2009 Perhaps you could paste the error messages here? And again, are you
- robby (3/3) Jul 02 2009 i'm using D1/Tango. sorry, im not sure to how to explain the error messa...
- Lars T. Kyllingstad (9/14) Jul 02 2009 Several places in that code, I notice things like this:
- robby (3/22) Jul 02 2009 it does work, though the code kinda a bit messy with cast(*ubyte) everyw...
- Ary Borenszweig (5/20) Jul 02 2009 Well, it should work! const means, once a value is assigned to that
- Lars T. Kyllingstad (11/32) Jul 02 2009 I suppose it's linked to D's automatic initialization of variables. If I...
- BCS (4/9) Jul 02 2009 In D1, const is truly const, as in never changes, ever, not even from on...
- Lars T. Kyllingstad (5/18) Jul 02 2009 In D2 you use enum for that, I think. The contents of const and
- Bill Baxter (3/12) Jul 02 2009 That's what D2 uses "enum" for.
can anybody help me translate this c function to d, im having a problem with the data types. thanks. //------------------------- unsigned int BLZCC blz_pack(const void *source, void *destination, unsigned int length, void *workmem) { BLZPACKDATA ud; const unsigned char **lookup = workmem; const unsigned char *backptr = source; /* check for length == 0 */ if (length == 0) return 0; /* init lookup[] */ { int i; for (i = 0; i < BLZ_WORKMEM_SIZE/4; ++i) lookup[i] = 0; } ud.source = source; ud.destination = destination; /* first byte verbatim */ *ud.destination++ = *ud.source++; /* check for length == 1 */ if (--length == 0) return 1; /* init first tag */ ud.tagpos = ud.destination; ud.destination += 2; ud.tag = 0; ud.bitcount = 16; /* main compression loop */ while (length > 4) { const unsigned char *ppos; unsigned int len = 0; /* update lookup[] up to current position */ while (backptr < ud.source) { lookup[blz_hash4(backptr)] = backptr; backptr++; } /* look up current position */ ppos = lookup[blz_hash4(ud.source)]; /* check match */ if (ppos) { while ((len < length) && (*(ppos + len) == *(ud.source + len))) ++len; } /* output match or literal */ if (len > 3) { unsigned int pos = ud.source - ppos - 1; /* output match tag */ blz_putbit(&ud, 1); /* output length */ blz_putgamma(&ud, len - 2); /* output position */ blz_putgamma(&ud, (pos >> 8) + 2); *ud.destination++ = pos & 0x00ff; ud.source += len; length -= len; } else { /* output literal tag */ blz_putbit(&ud, 0); /* copy literal */ *ud.destination++ = *ud.source++; length--; } } /* output any remaining literals */ while (length > 0) { /* output literal tag */ blz_putbit(&ud, 0); /* copy literal */ *ud.destination++ = *ud.source++; length--; } /* shift last tag into position and store */ ud.tag <<= ud.bitcount; ud.tagpos[0] = ud.tag & 0x00ff; ud.tagpos[1] = (ud.tag >> 8) & 0x00ff; /* return compressed length */ return ud.destination - (unsigned char *)destination; }
Jul 02 2009
robby wrote:can anybody help me translate this c function to d, im having a problem with the data types. thanks. //------------------------- unsigned int BLZCC blz_pack(const void *source, void *destination, unsigned int length, void *workmem) { BLZPACKDATA ud; const unsigned char **lookup = workmem; const unsigned char *backptr = source; /* check for length == 0 */ if (length == 0) return 0; /* init lookup[] */ { int i; for (i = 0; i < BLZ_WORKMEM_SIZE/4; ++i) lookup[i] = 0; } ud.source = source; ud.destination = destination; /* first byte verbatim */ *ud.destination++ = *ud.source++; /* check for length == 1 */ if (--length == 0) return 1; /* init first tag */ ud.tagpos = ud.destination; ud.destination += 2; ud.tag = 0; ud.bitcount = 16; /* main compression loop */ while (length > 4) { const unsigned char *ppos; unsigned int len = 0; /* update lookup[] up to current position */ while (backptr < ud.source) { lookup[blz_hash4(backptr)] = backptr; backptr++; } /* look up current position */ ppos = lookup[blz_hash4(ud.source)]; /* check match */ if (ppos) { while ((len < length) && (*(ppos + len) == *(ud.source + len))) ++len; } /* output match or literal */ if (len > 3) { unsigned int pos = ud.source - ppos - 1; /* output match tag */ blz_putbit(&ud, 1); /* output length */ blz_putgamma(&ud, len - 2); /* output position */ blz_putgamma(&ud, (pos >> 8) + 2); *ud.destination++ = pos & 0x00ff; ud.source += len; length -= len; } else { /* output literal tag */ blz_putbit(&ud, 0); /* copy literal */ *ud.destination++ = *ud.source++; length--; } } /* output any remaining literals */ while (length > 0) { /* output literal tag */ blz_putbit(&ud, 0); /* copy literal */ *ud.destination++ = *ud.source++; length--; } /* shift last tag into position and store */ ud.tag <<= ud.bitcount; ud.tagpos[0] = ud.tag & 0x00ff; ud.tagpos[1] = (ud.tag >> 8) & 0x00ff; /* return compressed length */ return ud.destination - (unsigned char *)destination; }I'm no C expert, but I'll give it a shot. unsigned int -> uint (or size_t, if it is the length of an array) unsigned char -> ubyte For future reference, a handy table of C-to-D type correspondences can be found here: http://www.digitalmars.com/d/2.0/htomodule.html You didn't say whether you use D1 or D2. If it is D1 I believe you should also replace "const void *" by just "void*" in the function declaration. Hope this helps, -Lars
Jul 02 2009
thanks for help, i did the conversion as you mentioned and i get various errors involving constant, lvalue, etc., just fyi, its part of code from brieflz (LZ77 variant) which ive been trying to port to D for use in my program.
Jul 02 2009
robby wrote:thanks for help, i did the conversion as you mentioned and i get various errors involving constant, lvalue, etc., just fyi, its part of code from brieflz (LZ77 variant) which ive been trying to port to D for use in my program.Perhaps you could paste the error messages here? And again, are you using D1 or D2? -Lars
Jul 02 2009
i'm using D1/Tango. sorry, im not sure to how to explain the error messages, but if you need to look a t full code, here is the link http://www.ibsensoftware.com/download.html thanks again.
Jul 02 2009
robby wrote:i'm using D1/Tango. sorry, im not sure to how to explain the error messages, but if you need to look a t full code, here is the link http://www.ibsensoftware.com/download.html thanks again.Several places in that code, I notice things like this: const type foo; ... foo = bar; I have no idea why this works in C, but in D it certainly doesn't. Try removing "const" everywhere in your translated code, including the BLZPACKDATA struct. It's brutal, I know, but it may work. -Lars
Jul 02 2009
Lars T. Kyllingstad Wrote:robby wrote:it does work, though the code kinda a bit messy with cast(*ubyte) everywhere, :P thanks for your time.i'm using D1/Tango. sorry, im not sure to how to explain the error messages, but if you need to look a t full code, here is the link http://www.ibsensoftware.com/download.html thanks again.Several places in that code, I notice things like this: const type foo; ... foo = bar; I have no idea why this works in C, but in D it certainly doesn't. Try removing "const" everywhere in your translated code, including the BLZPACKDATA struct. It's brutal, I know, but it may work. -Lars
Jul 02 2009
Lars T. Kyllingstad escribió:robby wrote:Well, it should work! const means, once a value is assigned to that variable, it never changes again. The compiler can do static analysis to verify this. And that's why it works. And that's why D should also work this way, IMHO.i'm using D1/Tango. sorry, im not sure to how to explain the error messages, but if you need to look a t full code, here is the link http://www.ibsensoftware.com/download.html thanks again.Several places in that code, I notice things like this: const type foo; ... foo = bar; I have no idea why this works in C, but in D it certainly doesn't.
Jul 02 2009
Ary Borenszweig wrote:Lars T. Kyllingstad escribió:I suppose it's linked to D's automatic initialization of variables. If I understand it correctly, just typing const int foo; is the same as const int foo = 0; With your suggestion, const variables could not be automatically initialized. In that case: int* foo; // foo is null const int* bar; // bar could point anywhere! -Larsrobby wrote:Well, it should work! const means, once a value is assigned to that variable, it never changes again. The compiler can do static analysis to verify this. And that's why it works. And that's why D should also work this way, IMHO.i'm using D1/Tango. sorry, im not sure to how to explain the error messages, but if you need to look a t full code, here is the link http://www.ibsensoftware.com/download.html thanks again.Several places in that code, I notice things like this: const type foo; ... foo = bar; I have no idea why this works in C, but in D it certainly doesn't.
Jul 02 2009
Hello Ary,Well, it should work! const means, once a value is assigned to that variable, it never changes again. The compiler can do static analysis to verify this. And that's why it works. And that's why D should also work this way, IMHO.In D1, const is truly const, as in never changes, ever, not even from one run of the program to another. D2 keeps this idea but IIRC calls it something else.
Jul 02 2009
BCS wrote:Hello Ary,In D2 you use enum for that, I think. The contents of const and invariant variables can both be set at run time, whereas enums must be known at compile time. -LarsWell, it should work! const means, once a value is assigned to that variable, it never changes again. The compiler can do static analysis to verify this. And that's why it works. And that's why D should also work this way, IMHO.In D1, const is truly const, as in never changes, ever, not even from one run of the program to another. D2 keeps this idea but IIRC calls it something else.
Jul 02 2009
On Thu, Jul 2, 2009 at 9:07 AM, BCS<none anon.com> wrote:Hello Ary,That's what D2 uses "enum" for. --bbWell, it should work! const means, once a value is assigned to that variable, it never changes again. The compiler can do static analysis to verify this. And that's why it works. And that's why D should also work this way, IMHO.In D1, const is truly const, as in never changes, ever, not even from one run of the program to another. D2 keeps this idea but IIRC calls it something else.
Jul 02 2009