(* :Title: Convert from symbol to "enum" *)

(* :Context: QuantLib`Common`QuantLibCommon` *)

(* :Author: Niels Elken Sønderby *)

(* :Mathematica Version: 4.0 *)

(* :Copyright: Copyright (C) 2003 Niels Elken Sønderby *)

(* :License:

   This file is part of QuantLib for Mathematica, a Mathematica extension for
   QuantLib, a free-software/open-source financial C++ library
   - http://www.nielses.dk/quantlib/mma/
   - http://quantlib.org/

   QuantLib for Mathematica is free software: you can redistribute it and/or 
   modify it under the terms of the QuantLib license.  You should have 
   received a copy of the license along with this program; if not, please 
   email ferdinando@ametrano.net The license is also available online at 
   http://quantlib.org/html/license.html

   This program is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
   or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
*)

BeginPackage["QuantLib`Common`QuantLibCommon`",
	     "QuantLib`Common`CppLink`"]

SymbolToNumber

NumberToSymbol

ExportTeX::usage = 
"ExportTeX[filename, table] exports a table with TableHeadings 
to a file in TeX-format."

ExportTeXString::usage = 
"ExportTeXString[table] gives the string for a table with TableHeadings 
in TeX-format."

Begin["`Private`"]

SymbolToNumber[number_, mapping_List] := (number /. (#1 -> #2 & @@@ mapping))

NumberToSymbol[symb_, mapping_List] := (symb /. (#2 -> #1 & @@@ mapping))

ExportTeXString[tbl_] := (
    tbl2 = If[Head[tbl] === TableForm, tbl, TableForm[tbl]];
    tblwhead = 
      If[Length[tbl2] > 1 && 
          tbl2[[2, 1]] === TableHeadings, {tbl2[[2, 2, 2]]}~Join~tbl2[[1]] // 
          TableForm, tbl2];
    align = "{" <> StringJoin @@ Table["l", {Length[tbl2[[1, 1]]]}] <> "}";
    tblstring = 
      StringReplace[
        ToString[tblwhead // TeXForm], {"\\cr  }" -> "\\end{tabular}", 
          "\\cr" -> "\\\\", "\\matrix{" -> "\\begin{tabular}" <> align}])

ExportTeX[filename_, tbl_] := Export[filename, ExportTeXString[tbl], "Text"]

End[]

EndPackage[]
