【发布时间】:2012-02-06 06:24:19
【问题描述】:
我需要将一段 C 代码翻译成 Delphi/Pascal 代码,但是我无法理解几行代码。
C 代码:
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 ;
}
我目前的翻译:
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 正在创建 ctype 参数的副本并将其设为小写。
非常感谢有关翻译的一些细节,例如 'bdy = (char*) ctype + ( ptr - lctype ) + 1;'和 for 循环。
仅供参考,我正在翻译 mod_upload.c。
【问题讨论】:
-
也许你可以写一些胶水代码来从 Pascal 调用你的 C 代码!
-
@BasileStarynkevitch 我不喜欢到处使用鸭带,此外,这将有助于我更好地理解 C。
-
1.使用
string而不是PChar。 C 没有字符串对象。不要让 C 的能力驱动你的 Delphi 设计。 2.strchr和strstr与Delphi的Pos作用相同。 -
@DavidHeffernan 谢谢,不知道!
-
Dorin,我喜欢你的“duck tape”拼写;-)
标签: c delphi pascal freepascal