excel_writer_18.0.0_ae0dcf49/extras/csv2tex.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
------------------------------------------------------------------------------
--  File:            CSV2TeX.adb
--  Description:     Converts CSV (text with Comma Separated Values) input
--                   into LaTeX array output. NB: the special characters
--                   like '%', '\', '&', '{',... should be translated before!
--                   CSV is "the" ASCII format for Lotus 1-2-3 and MS Excel
--  Created:         22-Apr-2003
--  Author:          Gautier de Montmollin
------------------------------------------------------------------------------

with Ada.Text_IO, Ada.Strings.Fixed;
with CSV; -- replaces CSV_Parser

procedure CSV2TeX is
  use Ada.Text_IO, Ada.Strings;
  first : Boolean := True;
  separator : constant Character := ';';
  --  ';', ',' or ASCII.HT
begin
  while not End_Of_File loop
    declare
      csv_line : constant String := Get_Line;
      bds : constant CSV.Fields_Bounds := CSV.Get_Bounds (csv_line, separator);
    begin
      if first then
        Put_Line ("% Array translated by CSV2TeX");
        Put_Line ("% Check http://excel-writer.sourceforge.net/ ,");
        Put_Line ("% in the ./extras directory");
        Put ("\begin{array}{");
        for i in bds'Range loop
          Put ('c');
          if i < bds'Last then
            Put ('|');
          end if;
        end loop;
        Put_Line ("} % array description");
        first := False;
      end if;
      for i in bds'Range loop
        Put (Ada.Strings.Fixed.Trim (CSV.Extract (csv_line, bds, i), Both));
        if i < bds'Last then
          Put ("&");
        end if;
      end loop;
    end;
    Put_Line ("\\");
  end loop;
  Put_Line ("\end{array}");
end CSV2TeX;