I need to translate a piece of C code into Delphi/Pascal code, however I’m having trouble understanding a couple of lines.
C Code:
static char* get_boundary(apr_pool_t* p, const char* ctype) {
char* ret = NULL ;
if ( ctype ) {
char* lctype = lccopy(p, ctype) ;
char* bdy = strstr(lctype, "boundary") ;
if ( bdy ) {
char* ptr = strchr(bdy, '=') ;
if ( ptr ) {
// what is the following line supposed to do?
bdy = (char*) ctype + ( ptr - lctype ) + 1;
// and this? I understand it's a loop, but *ptr and ++ptr is ugly!
for ( ptr = bdy; *ptr; ++ptr )
if ( *ptr == ';' || isspace(*ptr) )
*ptr = 0 ;
ret = apr_pstrdup(p, bdy) ;
}
}
}
return ret ;
}
My current translation:
function get_boundary(p: Papr_pool_t; const ctype: PChar): PChar;
var
LCType: PChar;
LBody: PChar;
begin
Result := NIL;
LCType := lccopy(p, ctype);
LBody := strpos(LCType, 'boundary');
if LBody <> NIL then begin
// now what? (:
end; // if LBody <> NIL then begin
end;
lccopy is creating a copy of the ctype parameter and make it lowercase.
Some details regarding translation are highly appreciated, like ‘bdy = (char*) ctype + ( ptr – lctype ) + 1;’ and the for loop.
FYI I’m translating mod_upload.c.
So…
( ptr - lctype )is pointer arithmetic to find how far intolctypeptrpoints. It’s the difference between the addresses held in the pointers, divided by the size of the data type they point at (in this casechar, so that size is just1).So
bdy = (char*) ctype + ( ptr - lctype ) + 1;pointsbdyat the character following the'='found previously, but in the original stringctypeinstead of the lowercase copylctype.This is not a terribly strange way to iterate through a string in C.
ptrpoints to each character while iterating through, and*ptrgives the character; so*ptrwill test asFALSEwhen the terminating null byte is reached to end the loop.++ptris more pointer arithmetic to move to the pointer to the next character. Even if this seems messy, it’s a pretty natural way to do it in C.So the loop moves through each character of the string pointed to by
bdy, and during each iteration*ptraccesses the current character.It appears the purpose of the loop was to terminate the string (by placing an earlier null terminator) at the next semicolon or whitespace character found.
There’s enough difference between Delphi and C when it comes to string manipulation that you might be better off just figuring out what the function is doing, and then writing a Delphi equivalent from scratch rather than trying to translate it directly like this.
It looks like the function looks for “boundary” (case insensitively) in
ctype, then skips past the next “=” found, and returns a copy of everything from there up to the next semicolon or whitespace character. You could do the same thing in Delphi easily with Delphi strings and functions, using very different code if you’re willing to convert the strings first…Also, if it matters, it looks like the original C code ignores anything between “boundary” and “=” — so it would accept, say, “
boundary asdf jidlsah;lkdsf =Value” as well as “boundary=Value“