我以前在C / C ++中使用过管道,但是我正在设置一些传统的Ada代码中的管道......但是我仍然处于Ada的“学习”阶段,而且还有很多我仍然没有但还不知道。
话虽这么说,我一直在试图弄清楚Ada中的管道是如何设置的。如何使用它们。到目前为止我只发现了这些文章:
不要误会他们对他们有很多好的知识,但是1面向Ada95(我有能力编译到Ada05),2只列出功能,& 3提供的解释很少。
有没有人知道Ada管道的教程?或者更好的是,有人能给出一个如何在Ada中实现简单管道的快速示例吗?
我意识到这不是最理想的问题,但我已经没有“Google Combinations”......
答案 0 :(得分:3)
您可能对Streams感兴趣 - http://en.wikibooks.org/wiki/Ada_Programming/Input_Output/Stream_Tutorial
特别是因为,如果你创建一个将流绑定到管道的包,你可以使用Type_Name'Read(stream,object)& Type_Name'Write(stream,object)。
答案 1 :(得分:2)
使用GNAT GPL 2015在Windows 10上编译
with Ada.Text_IO;
with System;
with Interfaces.C;
procedure Main is
package Pipes is
type Pipe is private;
type Get_Result is private;
function Open_Read (Command : String) return Pipe;
procedure Close (Stream : Pipe);
function Get (Stream : Pipe) return Get_Result;
function End_Of_File (Item : Get_Result) return Boolean;
function To_Ada (Item : Get_Result) return Character;
private
use System;
use Interfaces.C;
type Pipe is new Address;
type Get_Result is new int;
end;
package body Pipes is
function popen (command : char_array; mode : char_array) return Address with Import, Convention => C, External_Name => "popen";
function pclose (stream : Address) return int with Import, Convention => C, External_Name => "pclose";
function fgetc (stream : Address) return int with Import, Convention => C, External_Name => "fgetc";
function Open_Read (Command : String) return Pipe is
Mode : constant char_array := "r" & nul;
Result : Address;
begin
Result := popen (To_C (Command), Mode);
if Result = Null_Address then
raise Program_Error with "popen error";
end if;
return Pipe (Result);
end;
procedure Close (Stream : Pipe) is
Result : int;
begin
Result := pclose (Address (Stream));
if Result = -1 then
raise Program_Error with "pclose error";
end if;
end;
function Get (Stream : Pipe) return Get_Result is
begin
return Get_Result (fgetc (Address (Stream)));
end;
function End_Of_File (Item : Get_Result) return Boolean is (Item = -1);
function To_Ada (Item : Get_Result) return Character is (Character'Val (Get_Result'Pos (Item)));
end;
procedure Test is
use Ada.Text_IO;
use Pipes;
P : Pipe;
C : Get_Result;
begin
P := Open_Read ("netstat");
loop
C := Get (P);
exit when End_Of_File (C);
Put (To_Ada (C));
end loop;
Close (P);
end;
begin
Test;
end;
输出因用户而异。
Active Connections
Proto Local Address Foreign Address State
TCP 192.168.1.140:49698 stackoverflow:https ESTABLISHED
TCP 192.168.1.140:49874 stackoverflow:https ESTABLISHED
TCP 192.168.1.140:49915 stackoverflow:https TIME_WAIT
TCP 192.168.1.140:49916 stackoverflow:https TIME_WAIT
答案 2 :(得分:1)
阿达语言对管道问题无话可说;它们不构成标准库的一部分。我怀疑C ++和Boost库也是如此(不是我是用户);管道是一个操作系统设施。
你的article 1包含编译和使用Ada 2005和Ada 2012以及(我希望)Ada 95的源代码 - 无论如何在Unix系统上。那不是吗?
您的第2条 - 而Util
包found by poking around a bit - 表示它适用于Windows和Unix。
第3条提供的软件具有维护的巨大优势!
至于教程 - 由于管道不在标准中,您必须使用编写您选择的特定库的人员所提供的内容。第1条和第3条都包含示范程序(我没有检查第2条)。我怀疑就是这样!
答案 3 :(得分:1)
这可能会过度工程,但如果允许使用gnat库,则会gnat.sockets。
毕竟管道是一个简单版本的套接字(或套接字是管道的扩展版本),它们都允许您在任务/进程之间传输数据。
答案 4 :(得分:0)
如果要在Ada应用程序中使用Unix管道,则应考虑POSIX Ada API,因为它是正式标准。其中一个实现是“FLORIST”库。
Crimeville language server是一个示例,说明如何使用管道来处理Ada应用程序与具有管道友好界面的旧应用程序之间的通信。