Forum > General

Translating from C

(1/3) > >>

geraldholdsworth:
Hi all,

I'm not very proficient in C but I'm trying to translate a block of code to Pascal. I think it's getting unstuck because the original code is using pointers while I'm using arrays.

The original code is:

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---        unsigned long *prefix;        unsigned char *suffix;        unsigned char *stack;        unsigned char *stackptr;        prefix = (unsigned long*)malloc(sizeof(unsigned long) * (max + 1));        suffix = (unsigned char*)malloc(sizeof(unsigned char) * (max + 1));        stack  = (unsigned char*)malloc(sizeof(unsigned char) * (max + 1));        stackptr = &stack[0]; Then the stack is accessed thus (just a random line I picked out):

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---*stackptr++ = firstchar;prefix and suffix are accessed, however, via:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---*stackptr++ = suffix[code];code = prefix[code];So, onto Pascal and mine is:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---stackptr   : Cardinal;prefix     : array of Cardinal;suffix,stack      : array of Byte;and accessed thus:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---stack[stackptr]:=firstchar;inc(stackptr);Now the code doesn't do what it is supposed to, so I'm assuming that the problem may lie here. It could be else where, but I'm eliminating the possible to narrow it down.
Oh, I haven't really delved into using pointers in Pascal before, so this won't help!! :D

geraldholdsworth:
OK, found the problem...
In the original code we have:

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---/* Output what has been accumulated on the stack */while (stackptr > stack) {        stackptr--;        if (outptr < olen)        outbuf[outptr++] = *stackptr;} and I had written:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//Output what has been accumulated on the stackwhile stackptr>Length(stack) dobegin dec(stackptr); if outptr<Length(Result) then begin  Result[outptr]:=stack[stackptr];  inc(outptr); end;end;Where it the condition should have been (and now is):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while stackptr>0 doBTW, if anyone is interested, this is code to decompress an LZW block.

jamie:
The ZSTREAM unit won't do it for you?

geraldholdsworth:
Couldn't find any information about whether ZStream would do it. I've got it doing GZip and Zip.

jamie:
There is the "Zipper" unit which uses zstream unit which houses TZipper and TUnZipper .

Navigation

[0] Message Index

[#] Next page

Go to full version