Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 02, 2009 help with c translation | ||||
---|---|---|---|---|
| ||||
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; } |
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to robby | 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 |
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
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. |
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to robby | 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
|
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | 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. |
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to robby | 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
|
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | Lars T. Kyllingstad Wrote:
> 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
it does work, though the code kinda a bit messy with cast(*ubyte) everywhere, :P
thanks for your time.
|
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | Lars T. Kyllingstad escribió:
> 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.
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.
|
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig wrote:
> Lars T. Kyllingstad escribió:
>> 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.
>
> 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 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!
-Lars
|
July 02, 2009 Re: help with c translation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | 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.
|
Copyright © 1999-2021 by the D Language Foundation