Salome HOME
Porting HEXABLOCK module on WIN32 platform.
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.hxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
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.
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 "Hex_defines.hxx"
26 #include "hexa_base.hxx"
27 #include <stack>
28 #include <vector>
29
30 BEGIN_NAMESPACE_HEXA
31
32 class HEXABLOCKENGINE_EXPORT XmlWriter 
33 {
34 public :
35    XmlWriter ();
36   ~XmlWriter ()    { closeXml () ; }
37
38    int    setFileName (cpchar filename);  // Un seul xml par fichier
39    int    setFile     (pfile  afile);     // Plusieurs xml par fichier
40    int    setStream   ();                 // Sauvegarde par flux
41    int    startXml ();
42    void   closeXml ();
43    cpchar getXml   ()                { return xml_buffer.c_str();    }
44
45    void openMark  (cpchar balise);                 // <Balise .. 
46    void addMark   (cpchar balise, bool jump=true); // <Balise> + eol
47    void closeMark (bool jump=false);               // </Balise> ou  />
48    void endMark ();       //         >
49
50    void addAttribute (cpchar attrib, cpchar  valeur); // attrib="valeur"
51    void addAttribute (cpchar attrib, int     valeur);
52    void addAttribute (cpchar attrib, double  valeur);
53    void addAttribute (cpchar attrib, string& valeur);
54
55 private :
56     void jumpLine ();
57     void alaLigne (bool force=false);
58     void ecrire   (cpchar  mot);
59     void ecrire   (string& mot) { ecrire (mot.c_str()) ; }
60     void addMot   (cpchar  mot);
61
62 private :
63     enum  {InaFile, InaStudy, InaStream} xml_mode;
64     enum  {xml_decal=3, xml_size=80};
65
66     pfile  xml_file;
67     int    xml_level;
68     int    xml_pos;
69     bool   on_file;
70     std::string  xml_buffer;
71     std::stack <std::string, std::vector <std::string> >  pile_mark; 
72     std::stack <int,         std::vector <int> >          pile_etat; 
73 };
74 // ====================================================== addAttribute (I)
75 inline void XmlWriter::addAttribute (cpchar attrib, int valeur)
76 {
77    char buffer [20]; 
78    sprintf (buffer, "%d", valeur);
79    addAttribute (attrib, buffer);
80 }
81 // ====================================================== addAttribute (R)
82 inline void XmlWriter::addAttribute (cpchar attrib, double valeur)
83 {
84    char buffer [20]; 
85    sprintf (buffer, "%g", valeur);
86    addAttribute (attrib, buffer);
87 }
88 // ====================================================== addAttribute (stl)
89 inline void XmlWriter::addAttribute (cpchar attrib, string& valeur)
90 {
91    addAttribute (attrib, valeur.c_str());
92 }
93 // ====================================================== addMot
94 inline void XmlWriter::addMot (cpchar mot)
95 {
96    if (on_file) 
97       fprintf (xml_file, mot);
98    else 
99       xml_buffer += mot;
100 }
101
102 END_NAMESPACE_HEXA
103 #endif