从Ada中的管道读取输入 [英] Read input from a pipe in Ada

查看:90
本文介绍了从Ada中的管道读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码(见下文)从作为命令行参数给出的文件中读取数据。我想添加对能够从管道读取输入的支持。例如,当前版本将数据读取为 main< file_name> ,而应该也可以执行 cmd1 |主要。以下是从文件读取数据的来源:

I have a piece of code (see below) that reads data from a file given as a command line argument. I would like to add a support for being able to read the input from a pipe. For instance, the current version reads the data as main <file_name>, whereas it should also be possible to do something line cmd1 | main. Here is the source to read data from file:

procedure main is

    File : Ada.Text_IO.File_Type;

begin

   if Ada.Command_Line.Argument_Count /= 1 then

      return;

   else

      Ada.Text_IO.Open (
         File => File,
         Mode => In_File,
         Name => Ada.Command_Line.Argument (1));

      while (not Ada.Text_IO.End_Of_File (File)) loop
         -- Read line using Ada.Text_IO.Get_Line
         -- Process the line
      end loop;

      Ada.Text_IO.Close (File);

end main;

如果我理解正确,管道在Ada中只是非常规文件类型。但是,我该如何处理呢?

If I understood correctly, a pipe is just a non-regular file type in Ada. But how do I deal with it?

推荐答案

这些似乎都无法真正回答您的问题。管道仅使另一个程序的标准输出成为程序的标准输入,因此您通过读取Standard_Input来读取管道。

None of these seem to really answer your question. A pipe merely makes the standard output of another program be the standard input of your program, so you read a pipe by reading Standard_Input.

Current_Input函数返回File_Type。它最初返回Standard_Input,但是调用Set_Input会将其更改为返回您传递给Set_Input的任何内容。因此,如果没有给定文件,则如何从Standard_Input读取文件;如果给定文件,则如何从给定文件读取文件,大致概述如下:

The function Current_Input returns a File_Type. It initially returns Standard_Input, but calling Set_Input changes it to return whatever you passed to Set_Input. So a rough outline of how to read from Standard_Input if no file is given, and from the given file if one is, looks like:

File : File_Type;

if Argument_Count > 0 then
   Open (File => File, Name => Argument (1), Mode => In_File);
   Set_Input (File => File);
end if;

All_Lines : loop
   exit All_Lines when End_Of_File (Current_Input);

   Process (Line => Get_Line (Current_Input) );
end loop All_Lines;

这篇关于从Ada中的管道读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆