www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - help with c translation

reply robby <handsomerobby fakemail.com> writes:
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
next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
prev sibling parent reply robby <handsomerobby fakemail.com> writes:
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
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
parent reply robby <handsomerobby fakemail.com> writes:
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
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
next sibling parent robby <handsomerobby fakemail.com> writes:
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.
Jul 02 2009
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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.
Jul 02 2009
next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Jul 02 2009
prev sibling parent reply BCS <none anon.com> writes:
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
next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
BCS wrote:
 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.
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. -Lars
Jul 02 2009
prev sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Thu, Jul 2, 2009 at 9:07 AM, BCS<none anon.com> wrote:
 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.
That's what D2 uses "enum" for. --bb
Jul 02 2009