Salome HOME
Merge from V6_main 13/12/2012
[modules/hexablock.git] / src / HEXABLOCK / Hex.cxx
1
2 // C++ : La clase principale de Hexa
3
4 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
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.
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");
37    glob = Globale::getInstance ();
38 }
39 // ======================================================== Destructeur
40 Hex::~Hex ()
41 {
42 #ifndef NO_CASCADE
43    int nbre = liste_documents.size();
44    for (int nd=0 ; nd<nbre ; nd++) 
45        delete liste_documents [nd];
46 #endif
47 }
48 // ======================================================== countDocument
49 int Hex::countDocument ()
50 {
51    return liste_documents.size();
52 }
53 // ======================================================== getDocument
54 Document* Hex::getDocument (int nro)
55 {
56    if (nro<0 || nro>=(int)liste_documents.size())
57       return NULL;
58    
59    return liste_documents [nro];
60 }
61 // ======================================================== removeDocument
62 void Hex::removeDocument (Document* doc)
63 {
64    bool actif = glob->dump.start ("hexablock", "removeDocument");
65    if (actif)
66        glob->dump << doc;
67
68    int nbre = liste_documents.size();
69    for (int nd=0 ; nd<nbre ; nd++) 
70        {
71        if (doc == liste_documents [nd])
72           {
73           liste_documents.erase (liste_documents.begin()+nd);
74           delete doc;
75           glob->dump.close (actif);
76           return;
77           }
78        }
79                       // Pas trouve dans la liste. On detruit quand meme
80     glob->dump.close (actif);
81     delete doc;
82 }
83 // ======================================================== addDocument
84 Document* Hex::addDocument (cpchar nomdoc)
85 {
86    bool actif = glob->dump.start ("hexablock", "addDocument");
87    if (actif)
88        glob->dump << nomdoc;
89
90    string name;
91    makeName (nomdoc, name);
92    Document* doc = new Document (name.c_str(), this);
93
94    liste_documents.push_back (doc);
95
96    glob->dump.close (actif, doc);
97    return doc;
98 }
99 // ======================================================== loadDocument
100 Document* Hex::loadDocument (cpchar filename)
101 {
102    bool actif = glob->dump.start ("hexablock", "loadDocument");
103    if (actif)
104        glob->dump << filename;
105
106    Document* doc = addDocument ("default");
107    doc->loadXml (filename);
108
109    glob->dump.close (actif, doc);
110    return doc;
111 }
112 // ======================================================== loadAllDocs
113 int Hex::loadAllDocs (cpchar stream)
114 {
115    int posit  = 0;
116    int car    = ' '; 
117    int nbdocs = 0;
118
119    while ( (car=stream[posit++]) != '>')
120          if (car>='0' && car <='9')
121             nbdocs = 10*nbdocs + car - '0';
122             
123    for (int nro = 0; nro<nbdocs ; nro++)
124        {
125        Document* doc = addDocument ("xxxx");
126        doc->setXml (stream, posit);
127        }
128
129    PutData (posit);
130    return HOK;
131 }
132 // ======================================================== saveAllDocs
133 int Hex::saveAllDocs (cpchar filename)
134 {
135    FILE* fic = fopen (filename, "w");
136    if (fic==NULL)
137       return HERR;
138
139    int nbdocs = countDocument ();
140    fprintf (fic, "<%d>", nbdocs);
141
142    for (int nro = 0; nro<nbdocs ; nro++)
143        {
144        Document* doc = getDocument (nro);
145        if (doc!=NULL)
146            doc->appendXml (fic);
147        }
148
149    fclose  (fic);
150    return HOK;
151 }
152 // ======================================================== findDocument
153 Document* Hex::findDocument (cpchar name)
154 {
155    int nbdocs   = liste_documents.size();
156    for (int nro = 0; nro<nbdocs ; nro++)
157        {
158        Document*  doc = getDocument (nro);
159        if (doc!=NULL && Cestegal (name, doc->getName ()))
160           return doc;
161        }
162    return NULL;
163 }
164 // ======================================================== makeName
165 void Hex::makeName (cpchar radical, string& name)
166 {
167    char cnum [8];
168    int  numero = 0;
169    while (true)
170        {
171        name = radical;
172        if (numero>0)
173           {
174           sprintf (cnum, "_%d", numero);
175           name += cnum;
176           }
177        numero ++;
178        if (findDocument (name)==NULL)
179           return;
180        }
181 }
182 // ======================================================== lockDump
183 void Hex::lockDump ()
184 {
185    glob->dump.lock ();
186 }
187 // ======================================================== restoreDump
188 void Hex::restoreDump ()
189 {
190    glob->dump.restore (DumpActif);
191 }
192 // ====================================================== getInstance
193 Hex* Hex::getInstance  ()
194 {
195    if (first_instance==NULL) 
196        first_instance = new Hex ();
197
198    return first_instance;
199 }
200 END_NAMESPACE_HEXA