我编写了一个加密文件的 Ada 程序。它逐 block 读取它们以节省目标机器上的内存。不幸的是,Ada 的目录库读取 Long_Integer 中的文件大小,将读取限制为近 2GB 文件。尝试读取超过 2GB 的文件时,程序在运行时失败,出现堆栈溢出错误。
它的文档here是我上面理解的起源。如何将文件大小读入我自己定义的类型?我可以要求 25 个字节将上限增加到 100GB。
最佳答案
我刚刚发布了GCC bug 55119对此。
在您等待时(!),下面的代码可以在 Mac OS X Mountain Lion 上运行。在 Windows 上,它更复杂。见 adainclude/adaint.{c,h}
.
Ada 规范:
with Ada.Directories;
package Large_Files is
function Size (Name : String) return Ada.Directories.File_Size;
end Large_Files;
和正文(部分从
Ada.Directories
复制):with GNAT.OS_Lib;
with System;
package body Large_Files is
function Size (Name : String) return Ada.Directories.File_Size
is
C_Name : String (1 .. Name'Length + 1);
function C_Size (Name : System.Address) return Long_Long_Integer;
pragma Import (C, C_Size, "large_file_length");
begin
if not GNAT.OS_Lib.Is_Regular_File (Name) then
raise Ada.Directories.Name_Error
with "file """ & Name & """ does not exist";
else
C_Name (1 .. Name'Length) := Name;
C_Name (C_Name'Last) := ASCII.NUL;
return Ada.Directories.File_Size (C_Size (C_Name'Address));
end if;
end Size;
end Large_Files;
和C接口(interface):
/* large_files_interface.c */
#include <sys/stat.h>
long long large_file_length (const char *name)
{
struct stat statbuf;
if (stat(name, &statbuf) != 0) {
return 0;
} else {
return (long long) statbuf.st_size;
}
}
您可能需要使用
struct stat64
和 stat64()
在其他 Unix 系统上。正常编译C接口(interface),然后添加
-largs large_files_interface.o
到你的 gnatmake 命令行。编辑:在 Mac OS X(和 Debian)上,它们是 x86_64 机器,
sizeof(long)
是 8 个字节;所以 adaint.c
中的评论具有误导性,Ada.Directories.Size
最多可以返回 2**63-1。
关于ada - 如何在 Ada 中读取大文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112981/