Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.hxx
1 // Copyright (C) 2009-2024  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Class : Ecriture d'un fichier XML
21 //
22 #ifndef __XML_WRITER_H
23 #define __XML_WRITER_H
24
25 #include "hexa_base.hxx"
26 #include <stack>
27 #include <vector>
28
29 BEGIN_NAMESPACE_HEXA
30
31 class HexaExport XmlWriter 
32 {
33 public :
34    XmlWriter ();
35   ~XmlWriter ()    { closeXml () ; }
36
37    int    setFileName (cpchar filename);  // Un seul xml par fichier
38    int    setFile     (pfile  afile);     // Plusieurs xml par fichier
39    int    setStream   ();                 // Sauvegarde par flux
40    int    startXml ();
41    void   closeXml ();
42    cpchar getXml   ()                { return xml_buffer.c_str();    }
43
44    void openMark  (cpchar balise);                 // <Balise .. 
45    void addMark   (cpchar balise, bool jump=true); // <Balise> + eol
46    void closeMark (bool jump=false);               // </Balise> ou  />
47    void endMark ();       //         >
48
49    void addAttribute (cpchar attrib, cpchar  valeur); // attrib="valeur"
50    void addAttribute (cpchar attrib, int     valeur);
51    void addAttribute (cpchar attrib, double  valeur);
52    void addAttribute (cpchar attrib, std::string& valeur);
53
54 private :
55     void jumpLine ();
56     void alaLigne (bool force=false);
57     void ecrire   (cpchar  mot);
58     void ecrire   (std::string& mot) { ecrire (mot.c_str()) ; }
59     void addMot   (cpchar  mot);
60
61 private :
62     enum  {InaFile, InaStudy, InaStream} xml_mode;
63     enum  {xml_decal=3, xml_size=80};
64
65     pfile  xml_file;
66     int    xml_level;
67     int    xml_pos;
68     bool   on_file;
69     std::string  xml_buffer;
70     std::stack <std::string, std::vector <std::string> >  pile_mark; 
71     std::stack <int,         std::vector <int> >          pile_etat; 
72 };
73 // ====================================================== addAttribute (I)
74 inline void XmlWriter::addAttribute (cpchar attrib, int valeur)
75 {
76    char buffer [20]; 
77    sprintf (buffer, "%d", valeur);
78    addAttribute (attrib, buffer);
79 }
80 // ====================================================== addAttribute (R)
81 inline void XmlWriter::addAttribute (cpchar attrib, double valeur)
82 {
83    char buffer [20]; 
84    sprintf (buffer, "%g", valeur);
85    addAttribute (attrib, buffer);
86 }
87 // ====================================================== addAttribute (stl)
88 inline void XmlWriter::addAttribute (cpchar attrib, std::string& valeur)
89 {
90    addAttribute (attrib, valeur.c_str());
91 }
92 // ====================================================== addMot
93 inline void XmlWriter::addMot (cpchar mot)
94 {
95    if (on_file) 
96       fprintf (xml_file, mot);
97    else 
98       xml_buffer += mot;
99 }
100
101 END_NAMESPACE_HEXA
102 #endif