Forum > General
Translating from C
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.
geraldholdsworth:
Couldn't find any information about whether ZStream would do it. I've got it doing GZip and Zip.
geraldholdsworth:
I actually use both Zipper and ZStream in other parts of the project for compressing and decompressing ZIP data and GZip data, respectively.
For the size of the LZW decompression code, it will hardly make much of a difference.
del:
Good luck with pointers in Pascal. The documentation is pretty silly. The "tutorials" typically have a pointer, and a fixed array, and then they'll have the pointer point to the fixed array. >:(
And there's a weird wrinkle with Move(srcPtr, dstPtr, numBytes). You have to "deference" the pointers to make this work: Move(srcPtr^, dstPtr^, numBytes). I do a lot of chained floating point image processing and the available image containers don't support floating point. So I have to roll my own. At any rate, if you're fluent in C you might want to try a pure pointer (no dynamic arrays) solution in Pascal.
Navigation
[0] Message Index
[#] Next page