Delphi中的类型转换指针添加

杰克·埃文斯(Jake Evans)

我试图了解如何在Delphi中进行类型转换,类似于C / C ++。

我要转换的C ++代码是这样的;

typedef struct tagEXT2_INODE
{
    uint16_t    i_mode;     /* File mode */
    uint16_t    i_uid;      /* Low 16 bits of Owner Uid */
    uint32_t    i_size;     /* Size in bytes */
    uint32_t    i_atime;        /* Access time */
    uint32_t    i_ctime;        /* Creation time */
    uint32_t    i_mtime;        /* Modification time */
    uint32_t    i_dtime;        /* Deletion Time */
    uint16_t    i_gid;      /* Low 16 bits of Group Id */
    uint16_t    i_links_count;  /* Links count */
    uint32_t    i_blocks;       /* Blocks count */
    uint32_t    i_flags;        /* File flags */
    union {
        struct {
            uint32_t  l_i_reserved1;
        } linux1;
        struct {
            uint32_t  h_i_translator;
        } hurd1;
        struct {
            uint32_t  m_i_reserved1;
        } masix1;
    } osd1;             /* OS dependent 1 */
    uint32_t    i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
    uint32_t    i_generation;   /* File version (for NFS) */
    uint32_t    i_file_acl;     /* File ACL */
//    uint32_t  i_dir_acl;      /* Directory ACL */
    uint32_t    i_size_high;            /* This is used store the high 32 bit of file size in large files */
    uint32_t    i_faddr;        /* Fragment address */
    union {
        struct {
            uint8_t l_i_frag;   /* Fragment number */
            uint8_t l_i_fsize;  /* Fragment size */
            uint16_t    i_pad1;
            uint16_t    l_i_uid_high;   /* these 2 fields    */
            uint16_t    l_i_gid_high;   /* were reserved2[0] */
            uint32_t    l_i_reserved2;
        } linux2;
        struct {
            uint8_t h_i_frag;   /* Fragment number */
            uint8_t h_i_fsize;  /* Fragment size */
            uint16_t    h_i_mode_high;
            uint16_t    h_i_uid_high;
            uint16_t    h_i_gid_high;
            uint16_t    h_i_author;
        } hurd2;
        struct {
            uint8_t m_i_frag;   /* Fragment number */
            uint8_t m_i_fsize;  /* Fragment size */
            uint16_t    m_pad1;
            uint32_t    m_i_reserved2[2];
        } masix2;
    } osd2;                 /* OS dependent 2 */
} __attribute__ ((__packed__)) EXT2_INODE;


EXT2_INODE *src;
char *inode_buffer;
int inode_index, ret = 0;

inode_buffer = (char *)malloc(4096);


src = (EXT2_INODE *)(inode_buffer + inode_index);

有人可以解释一下如何这样src转换吗?我将如何在Delphi中执行相同的操作?

谢谢

编辑:这是我的Ext2 iNode记录;类型

  TExt2iNode = Record
    i_mode : Word;
    i_uid : Word;
    i_size : Cardinal;
    i_atime : Cardinal;
    i_ctime : Cardinal;
    i_mtime : Cardinal;
    i_dtimes : Cardinal;
    i_gid : Word;
    i_links_count : Word;
    i_blocks: Cardinal;
    i_flags : Cardinal;
    osd1 : Record
      linux1 : Record
        l_i_reserved1 : Cardinal;
      end;
      hurd1 : Record
        h_i_translator: Cardinal;
      End;
      masix1 : Record
        m_i_reserved1 : Cardinal;
      End;
    End;
    i_block: array [0..EXT2_N_BLOCKS-1] of Cardinal;
    i_generation : Cardinal;
    i_file_acl : Cardinal;
    i_size_high : Cardinal;
    i_faddr : Cardinal;

    osd2 : Record
      Linux2 : Record
        l_i_frag : Byte;
        l_i_fsize : Byte;
        i_pad1 : Word;
        l_i_uid_high : Word;
        l_i_gid_high : Word;
        l_i_reserved2 : Cardinal
      end;
      hurd2 : Record
        h_i_frag : Byte;
        h_i_fsize : Byte;
        h_i_mode_high : Word;
        h_i_uid_high : Word;
        h_i_gid_high : Word;
        h_i_author : Word;
      end;
      masix2 : Record
        m_i_frag:Byte;
        m_i_fsize : Byte;
        m_pad1 : Word;
        m_i_reserved : array[0..1] of Cardinal;
      end;
    end;
  End;

这是我的Ex2分区记录;

type 
  Ext2Partition = Class 
    private
      handle: THandle;
      sect_size: Integer;
      total_sectors: Int64;
      relative_sect: Int64;
      linux_name :AnsiString;

      inodes_per_group: integer;
      inode_size: integer;
      block_size: integer;
      totalGRoups: Integer;
      desc : TExt2_Group_Desc;

      last_block : Cardinal;
      inode_buffer : array of AnsiChar;
      root : Ext2File;
      buffercache : TList;
      lvol : LogicalVolume;
    public
      onview, is_valid: boolean;
    published
      Constructor Create(size, offset :int64; ssise: integer; PHandle: THandle);
      function read_inode(inum: Cardinal):Ext2File;
      function readblock(blocknum: cardinal; var buffer: array of AnsiChar):integer;
      function mount():integer;
  End;

这是我read_inode进行指针计算的函数;

function Ext2Partition.read_inode(inum: Cardinal):Ext2File;
var
  group, index, blknum: cardinal;
  inode_index : integer;
  ret: integer;
  fFile: EXt2File;
  src: TExt2iNode;
begin
  if inum = 0 then
    Result := NIL;

  SetLength(self.inode_buffer, self.block_size);

  group := (inum -1) DIV self.inodes_per_group;

  if group > self.totalGRoups then
  begin
    //ShowMessage('Error reading iNode');
    Result := -1;
  end;

  index := ((inum-1) MOD self.inodes_per_group) * self.inode_size;
  inode_index := (index MOD self.block_size);
  blknum := self.desc.bg_inode_table + (index div self.block_size);

  if blknum <> self.last_block then
    ret := readblock(blknum, self.inode_buffer);

  fFile := TExt2iNode.Create;

  //What goes here?

end;
罗伯·肯尼迪

该代码分配了一块内存。在其他地方,它确定了ext2 inode结构在该块内部某处的偏移量,由给出inode_index它将偏移量添加到块的开头,从而给出结构的地址。然后,代码对结果进行类型转换,以告知编译器所计算的char*地址确实是该结构类型的地址。

使用文字转换,我们将具有以下Delphi声明:

var
  inode_buffer: PAnsiChar;
  inode_index: Integer;
  src: PExt2_Inode;

因此,要分配src,您要进行类型转换并完全按照C ++代码中的步骤进行添加:

src := PExt2_Inode(inode_buffer + inode_index);

默认情况下,大多数Delphi指针类型都不支持这种指针算法,但是PAnsiChar很特殊。

到目前为止,使用翻译,在哪里inode_buffer有数组而不是指向内存块的指针,您将拥有以下内容:

src := PExt2_Inode(@inode_buffer[inode_index]);

该操作为数组建立索引,然后使用@运算符获取该数组元素的地址。(实际上,如果inode_buffer是我的原始PAnsiChar类型,也可以使用相同的语法。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C中的类型转换指针

来自分类Dev

Delphi指针转换

来自分类Dev

类型转换与指针转换

来自分类Dev

在C中类型转换双指针

来自分类Dev

指针类型转换,如python中的c

来自分类Dev

将静态数组转换为Delphi中的指针?

来自分类Dev

带指针的类型转换

来自分类Dev

类型转换整数以指向C中的整数指针

来自分类Dev

无法转换为C中的指针类型套接字编程

来自分类Dev

从 unsigned int 到 char 指针的类型转换中的分段错误

来自分类Dev

类型转换int指针到float指针

来自分类Dev

类型转换char指针到整数指针

来自分类Dev

功能指针类型转换

来自分类Dev

calloc(),类型转换和指针

来自分类Dev

cpp中指针的类型转换

来自分类Dev

无类型转换的空指针

来自分类Dev

指针操作和类型转换

来自分类Dev

c 中指针的类型转换

来自分类Dev

int 指针类型转换失败

来自分类Dev

从C ++到Delphi的类型转换

来自分类Dev

E2017指针类型需要在Delphi 7中使用移动功能

来自分类Dev

在Delphi 2010 RIDL编辑器中定义类型函数/过程指针的属性

来自分类Dev

c中的指针转换

来自分类Dev

C ++中指针的类型转换和类型转换

来自分类Dev

错误类型转换void指针到int

来自分类Dev

类型转换指针构造函数调用

来自分类Dev

将整数静态转换为指针类型

来自分类Dev

该指针类型转换表示法的含义

来自分类Dev

C ++中指针类型转换的说明