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.
string
rather thanPChar
. C doesn't have a string object. Don't let C's capabilities drive your Delphi design. 2.strchr
andstrstr
serve the same purpose as Delphi'sPos
. – David Heffernan