Salome HOME
bf52d49d28cf01496fda61618adcd53a95d73c8c
[modules/hexablock.git] / src / HEXABLOCK / Hex.cxx
1
2 // C++ : La clase principale de Hexa
3
4 // Copyright (C) 2009-2023  CEA, EDF
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "Hex.hxx"
23
24 #include "HexEltBase.hxx"
25 #include "HexDocument.hxx"
26 #include "HexGlobale.hxx"
27 #include <clocale>
28
29 BEGIN_NAMESPACE_HEXA
30
31 Hex* Hex::first_instance = NULL;
32
33 // ======================================================== Constructeur
34 Hex::Hex ()
35 {
36    //setlocale (LC_NUMERIC, "C"); // VSR 2020-03-11: commented out - initial locale has to be restored somewhere...
37    glob = Globale::getInstance ();
38 }
39 // ======================================================== Destructeur
40 Hex::~Hex ()
41 {
42    int nbre = liste_documents.size();
43    for (int nd=0 ; nd<nbre ; nd++) 
44        delete liste_documents [nd];
45 }
46 // ======================================================== countDocument
47 int Hex::countDocument ()
48 {
49    return liste_documents.size();
50 }
51 // ======================================================== getDocument
52 Document* Hex::getDocument (int nro)
53 {
54    if (nro<0 || nro>=(int)liste_documents.size())
55       return NULL;
56    
57    return liste_documents [nro];
58 }
59 // ======================================================== removeDocument
60 void Hex::removeDocument (Document* doc)
61 {
62    bool actif = glob->dump.start ("hexablock", "removeDocument");
63    if (actif)
64        glob->dump << doc;
65
66    int nbre = liste_documents.size();
67    for (int nd=0 ; nd<nbre ; nd++) 
68        {
69        if (doc == liste_documents [nd])
70           {
71           liste_documents.erase (liste_documents.begin()+nd);
72           delete doc;
73           glob->dump.close (actif);
74           return;
75           }
76        }
77                       // Pas trouve dans la liste. On detruit quand meme
78     glob->dump.close (actif);
79     delete doc;
80 }
81 // ======================================================== addDocument
82 Document* Hex::addDocument (cpchar nomdoc)
83 {
84    bool actif = glob->dump.start ("hexablock", "addDocument");
85    if (actif)
86        glob->dump << nomdoc;
87
88    std::string name;
89    makeName (nomdoc, name);
90    Document* doc = new Document (name.c_str(), this);
91
92    liste_documents.push_back (doc);
93
94    glob->dump.close (actif, doc);
95    return doc;
96 }
97 // ======================================================== loadDocument
98 Document* Hex::loadDocument (cpchar filename)
99 {
100    bool actif = glob->dump.start ("hexablock", "loadDocument");
101    if (actif)
102        glob->dump << filename;
103
104    Document* doc = addDocument ("default");
105    doc->loadXml (filename);
106
107    glob->dump.close (actif, doc);
108    return doc;
109 }
110 // ======================================================== loadAllDocs
111 int Hex::loadAllDocs (cpchar stream)
112 {
113    int posit  = 0;
114    int car    = ' '; 
115    int nbdocs = 0;
116
117    while ( (car=stream[posit++]) != '>')
118          if (car>='0' && car <='9')
119             nbdocs = 10*nbdocs + car - '0';
120             
121    for (int nro = 0; nro<nbdocs ; nro++)
122        {
123        Document* doc = addDocument ("xxxx");
124        doc->setXml (stream, posit);
125        }
126
127    PutData (posit);
128    return HOK;
129 }
130 // ======================================================== saveAllDocs
131 int Hex::saveAllDocs (cpchar filename)
132 {
133    FILE* fic = fopen (filename, "w");
134    if (fic==NULL)
135       return HERR;
136
137    int nbdocs = countDocument ();
138    fprintf (fic, "<%d>", nbdocs);
139
140    for (int nro = 0; nro<nbdocs ; nro++)
141        {
142        Document* doc = getDocument (nro);
143        if (doc!=NULL)
144            doc->appendXml (fic);
145        }
146
147    fclose  (fic);
148    return HOK;
149 }
150 // ======================================================== findDocument
151 Document* Hex::findDocument (cpchar name)
152 {
153    int nbdocs   = liste_documents.size();
154    for (int nro = 0; nro<nbdocs ; nro++)
155        {
156        Document*  doc = getDocument (nro);
157        if (doc!=NULL && Cestegal (name, doc->getName ()))
158           return doc;
159        }
160    return NULL;
161 }
162 // ======================================================== makeName
163 void Hex::makeName (cpchar radical, std::string& name)
164 {
165    char cnum [8];
166    int  numero = 0;
167    while (true)
168        {
169        name = radical;
170        if (numero>0)
171           {
172           sprintf (cnum, "_%d", numero);
173           name += cnum;
174           }
175        numero ++;
176        if (findDocument (name)==NULL)
177           return;
178        }
179 }
180 // ======================================================== lockDump
181 void Hex::lockDump ()
182 {
183    glob->dump.lock ();
184 }
185 // ======================================================== restoreDump
186 void Hex::restoreDump ()
187 {
188    glob->dump.restore (DumpActif);
189 }
190 // ====================================================== getInstance
191 Hex* Hex::getInstance  ()
192 {
193    if (first_instance==NULL) 
194        first_instance = new Hex ();
195
196    return first_instance;
197 }
198 // ======================================================== what (?)
199 void Hex::what ()
200 {
201    bool actif = glob->dump.start ("hexablock", "what", false);
202
203    Globale* glob = Globale::getInstance  ();
204    glob->mess.printMessage();
205
206    glob->dump.close (actif);
207 }
208 // ======================================================== sizeofMessage
209 int Hex::sizeofMessage ()
210 {
211    return glob->mess.sizeofMessage (); 
212 }
213 // ======================================================== getMessageLine
214 cpchar Hex::getMessageLine (int nlig)
215
216    return glob->mess.getLine (nlig);
217 }
218 // ======================================================== hex_instance
219 Hex* hex_instance ()
220 {
221    Hex*   hexa = Hex::getInstance ();
222    return hexa;
223 }
224 // ======================================================== what (fonction)
225 void what ()
226 {
227    Hex* hexa = Hex::getInstance ();
228    hexa->what ();
229 }
230
231 END_NAMESPACE_HEXA