1 // Copyright (C) 2007 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : Salome_file_i.cxx
22 // Author : André RIBES, EDF
26 #include "Salome_file_i.hxx"
27 #include "utilities.h"
32 # define _getcwd getcwd
41 //=============================================================================
43 * Default constructor,
45 //=============================================================================
47 Salome_file_i::Salome_file_i()
51 _path_max = 1 + pathconf("/", _PC_PATH_MAX);
55 //Note The C Runtime supports path lengths up to 32768 characters in length, but it is up to the operating system, specifically the file system, to support these longer paths. The sum of the fields should not exceed _MAX_PATH for full backwards compatibility with Windows 98 FAT32 file systems. Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, and Windows Server 2003 NTFS file system supports paths up to 32768 characters in length, but only when using the Unicode APIs. When using long path names, prefix the path with the characters \\?\ and use the Unicode versions of the C Runtime functions.
56 //currently #define _MAX_PATH 260
58 _state.name = CORBA::string_dup("");
59 _state.hdf5_file_name = CORBA::string_dup("");
60 _state.number_of_files = 0;
61 _state.files_ok = true;
62 _container = Engines::Container::_nil();
63 _default_source_Salome_file = Engines::Salome_file::_nil();
66 //=============================================================================
70 //=============================================================================
72 Salome_file_i::~Salome_file_i()
76 //=============================================================================
79 * \see Engines::Salome_file::load
81 //=============================================================================
83 Salome_file_i::load(const char* hdf5_file) {
84 _state.hdf5_file_name = CORBA::string_dup(hdf5_file);
89 HDFdataset *hdf_dataset;
95 hdf_file = new HDFfile((char*) hdf5_file);
96 hdf_file->OpenOnDisk(HDF_RDONLY);
98 hdf_group = new HDFgroup("CONFIG",hdf_file);
99 hdf_group->OpenOnDisk();
100 hdf_dataset = new HDFdataset("MODE",hdf_group);
101 hdf_dataset->OpenOnDisk();
102 size = hdf_dataset->GetSize();
103 value = new char[size];
104 hdf_dataset->ReadFromDisk(value);
105 hdf_dataset->CloseOnDisk();
106 std::string mode(value);
109 hdf_group = new HDFgroup("GROUP_FILES",hdf_file);
110 hdf_group->OpenOnDisk();
111 hdf_dataset = new HDFdataset("LIST_OF_FILES",hdf_group);
112 hdf_dataset->OpenOnDisk();
113 size = hdf_dataset->GetSize();
114 value = new char[size];
115 hdf_dataset->ReadFromDisk(value);
116 hdf_dataset->CloseOnDisk();
117 std::string list_of_files(value);
120 std::istringstream iss(list_of_files);
121 std::string file_name;
122 while (std::getline(iss, file_name, ' '))
124 std::string dataset_group_name("DATASET");
125 dataset_group_name += file_name;
127 hdf_group = new HDFgroup(dataset_group_name.c_str(), hdf_file);
128 hdf_group->OpenOnDisk();
130 hdf_dataset = new HDFdataset("NAME",hdf_group);
131 hdf_dataset->OpenOnDisk();
132 size = hdf_dataset->GetSize();
133 value = new char[size];
134 hdf_dataset->ReadFromDisk(value);
135 hdf_dataset->CloseOnDisk();
136 std::string name(value);
138 hdf_dataset = new HDFdataset("PATH",hdf_group);
139 hdf_dataset->OpenOnDisk();
140 size = hdf_dataset->GetSize();
141 value = new char[size];
142 hdf_dataset->ReadFromDisk(value);
143 hdf_dataset->CloseOnDisk();
144 std::string path(value);
146 hdf_dataset = new HDFdataset("TYPE",hdf_group);
147 hdf_dataset->OpenOnDisk();
148 size = hdf_dataset->GetSize();
149 value = new char[size];
150 hdf_dataset->ReadFromDisk(value);
151 hdf_dataset->CloseOnDisk();
152 std::string type(value);
154 hdf_dataset = new HDFdataset("SOURCE_FILE_NAME",hdf_group);
155 hdf_dataset->OpenOnDisk();
156 size = hdf_dataset->GetSize();
157 value = new char[size];
158 hdf_dataset->ReadFromDisk(value);
159 hdf_dataset->CloseOnDisk();
160 std::string source_file_name(value);
162 hdf_dataset = new HDFdataset("STATUS",hdf_group);
163 hdf_dataset->OpenOnDisk();
164 size = hdf_dataset->GetSize();
165 value = new char[size];
166 hdf_dataset->ReadFromDisk(value);
167 hdf_dataset->CloseOnDisk();
168 std::string status(value);
172 // Changing path, is now current directory
173 path = getcwd(NULL, _path_max);
175 std::string group_name("GROUP");
176 group_name += file_name;
177 hdf_group = new HDFgroup(group_name.c_str(),hdf_file);
178 hdf_group->OpenOnDisk();
179 hdf_dataset = new HDFdataset("FILE DATASET",hdf_group);
180 hdf_dataset->OpenOnDisk();
181 size = hdf_dataset->GetSize();
182 buffer = new char[size];
184 if ( (fd = ::open(file_name.c_str(),O_RDWR|O_CREAT,00666)) <0) {
185 SALOME::ExceptionStruct es;
186 es.type = SALOME::INTERNAL_ERROR;
187 std::string text = "open failed";
188 es.text = CORBA::string_dup(text.c_str());
189 throw SALOME::SALOME_Exception(es);
191 hdf_dataset->ReadFromDisk(buffer);
192 if ( write(fd,buffer,size) <0) {
193 SALOME::ExceptionStruct es;
194 es.type = SALOME::INTERNAL_ERROR;
195 std::string text = "write failed";
196 es.text = CORBA::string_dup(text.c_str());
197 throw SALOME::SALOME_Exception(es);
199 // Close the target file
203 infos.file_name = CORBA::string_dup(file_name.c_str());
204 infos.path = CORBA::string_dup(path.c_str());
205 infos.type = CORBA::string_dup(type.c_str());
206 infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
207 infos.status = CORBA::string_dup(status.c_str());
209 _fileManaged[file_name] = infos;
211 // Update Salome_file state
212 _state.number_of_files++;
213 _state.files_ok = true;
217 infos.file_name = CORBA::string_dup(file_name.c_str());
218 infos.path = CORBA::string_dup(path.c_str());
219 infos.type = CORBA::string_dup(type.c_str());
220 infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
221 infos.status = CORBA::string_dup(status.c_str());
222 // Infos for parallel extensions...
224 infos.container = Engines::Container::_duplicate(_container);
226 _fileManaged[file_name] = infos;
228 // Update Salome_file state
229 _state.number_of_files++;
231 _state.files_ok = false;
237 SALOME::ExceptionStruct es;
238 es.type = SALOME::INTERNAL_ERROR;
239 std::string text = "!!!! HDFexception";
240 es.text = CORBA::string_dup(text.c_str());
241 throw SALOME::SALOME_Exception(es);
245 //=============================================================================
248 * \see Engines::Salome_file::save
250 //=============================================================================
252 Salome_file_i::save(const char* hdf5_file) {
253 _state.hdf5_file_name = CORBA::string_dup(hdf5_file);
258 HDFdataset *hdf_dataset;
260 _t_fileManaged::iterator begin = _fileManaged.begin();
261 _t_fileManaged::iterator end = _fileManaged.end();
263 hdf_file = new HDFfile((char*) _state.hdf5_file_name.in());
264 hdf_file->CreateOnDisk();
266 // Save mode information
267 hdf_group = new HDFgroup("CONFIG", hdf_file);
268 hdf_group->CreateOnDisk();
269 std::string mode("infos");
270 size[0] = strlen(mode.c_str()) + 1;
271 hdf_dataset = new HDFdataset("MODE", hdf_group, HDF_STRING, size, 1);
272 hdf_dataset->CreateOnDisk();
273 hdf_dataset->WriteOnDisk((void *) mode.c_str());
274 hdf_dataset->CloseOnDisk();
275 hdf_group->CloseOnDisk();
277 // List of files that are managed
278 std::string list_of_files;
279 for(;begin!=end;begin++)
281 Engines::file file_infos = begin->second;
282 std::string file_name(file_infos.file_name.in());
284 list_of_files = list_of_files + file_name + std::string(" ");
286 hdf_group = new HDFgroup("GROUP_FILES", hdf_file);
287 hdf_group->CreateOnDisk();
288 size[0] = strlen(list_of_files.c_str()) + 1;
289 hdf_dataset = new HDFdataset("LIST_OF_FILES", hdf_group, HDF_STRING, size, 1);
290 hdf_dataset->CreateOnDisk();
291 hdf_dataset->WriteOnDisk((void *) list_of_files.c_str());
292 hdf_dataset->CloseOnDisk();
293 hdf_group->CloseOnDisk();
295 // Insert Files into the hdf5_file
296 begin = _fileManaged.begin();
297 for(;begin!=end;begin++)
299 Engines::file file_infos = begin->second;
300 std::string file_name(file_infos.file_name.in());
301 std::string comp_file_name(_fileManaged[file_name].path.in());
302 comp_file_name.append(_fileManaged[file_name].file_name.in());
303 std::string dataset_group_name("DATASET");
304 dataset_group_name += std::string(_fileManaged[file_name].file_name.in());
306 hdf_group = new HDFgroup((char *) dataset_group_name.c_str(), hdf_file);
307 hdf_group->CreateOnDisk();
308 size[0] = strlen(file_infos.file_name.in()) + 1;
309 hdf_dataset = new HDFdataset("NAME", hdf_group, HDF_STRING, size, 1);
310 hdf_dataset->CreateOnDisk();
311 hdf_dataset->WriteOnDisk((void *) file_infos.file_name.in());
312 hdf_dataset->CloseOnDisk();
313 size[0] = strlen(file_infos.path.in()) + 1;
314 hdf_dataset = new HDFdataset("PATH", hdf_group, HDF_STRING, size, 1);
315 hdf_dataset->CreateOnDisk();
316 hdf_dataset->WriteOnDisk((void *) file_infos.path.in());
317 hdf_dataset->CloseOnDisk();
318 size[0] = strlen(file_infos.type.in()) + 1;
319 hdf_dataset = new HDFdataset("TYPE", hdf_group, HDF_STRING, size, 1);
320 hdf_dataset->CreateOnDisk();
321 hdf_dataset->WriteOnDisk((void *) file_infos.type.in());
322 hdf_dataset->CloseOnDisk();
323 size[0] = strlen(file_infos.source_file_name.in()) + 1;
324 hdf_dataset = new HDFdataset("SOURCE_FILE_NAME", hdf_group, HDF_STRING, size, 1);
325 hdf_dataset->CreateOnDisk();
326 hdf_dataset->WriteOnDisk((void *) file_infos.source_file_name.in());
327 hdf_dataset->CloseOnDisk();
328 size[0] = strlen(file_infos.status.in()) + 1;
329 hdf_dataset = new HDFdataset("STATUS", hdf_group, HDF_STRING, size, 1);
330 hdf_dataset->CreateOnDisk();
331 hdf_dataset->WriteOnDisk((void *) file_infos.status.in());
332 hdf_dataset->CloseOnDisk();
333 hdf_group->CloseOnDisk();
336 hdf_file->CloseOnDisk();
338 // delete hdf_dataset;
339 // delete hdf_group; ----> SEGFAULT !!!
340 // delete hdf_file; ----> SEGFAULT !!!
344 SALOME::ExceptionStruct es;
345 es.type = SALOME::INTERNAL_ERROR;
346 std::string text = "!!!! HDFexception";
347 es.text = CORBA::string_dup(text.c_str());
348 throw SALOME::SALOME_Exception(es);
352 //=============================================================================
355 * \see Engines::Salome_file::save_all
357 //=============================================================================
359 Salome_file_i::save_all(const char* hdf5_file) {
361 _state.hdf5_file_name = CORBA::string_dup(hdf5_file);
362 // Test Salome_file status
363 if (_state.files_ok == false) {
364 SALOME::ExceptionStruct es;
365 es.type = SALOME::INTERNAL_ERROR;
366 std::string text = "File Not Ok !";
367 es.text = CORBA::string_dup(text.c_str());
368 throw SALOME::SALOME_Exception(es);
371 // For each file we create two groups
372 // First group contains file's informations
373 // Second group contains the file
374 // At the end we create a group and a dataset containing the names
380 HDFdataset *hdf_dataset;
382 _t_fileManaged::iterator begin = _fileManaged.begin();
383 _t_fileManaged::iterator end = _fileManaged.end();
385 hdf_file = new HDFfile((char*) _state.hdf5_file_name.in());
386 hdf_file->CreateOnDisk();
388 // Save mode information
389 hdf_group = new HDFgroup("CONFIG", hdf_file);
390 hdf_group->CreateOnDisk();
391 std::string mode("all");
392 size[0] = strlen(mode.c_str()) + 1;
393 hdf_dataset = new HDFdataset("MODE", hdf_group, HDF_STRING, size, 1);
394 hdf_dataset->CreateOnDisk();
395 hdf_dataset->WriteOnDisk((void *) mode.c_str());
396 hdf_dataset->CloseOnDisk();
397 hdf_group->CloseOnDisk();
400 // List of files that will be inserted
401 std::string list_of_files;
402 for(;begin!=end;begin++)
404 Engines::file file_infos = begin->second;
405 std::string file_name(file_infos.file_name.in());
407 list_of_files = list_of_files + file_name + std::string(" ");
409 hdf_group = new HDFgroup("GROUP_FILES", hdf_file);
410 hdf_group->CreateOnDisk();
411 size[0] = strlen(list_of_files.c_str()) + 1;
412 hdf_dataset = new HDFdataset("LIST_OF_FILES", hdf_group, HDF_STRING, size, 1);
413 hdf_dataset->CreateOnDisk();
414 hdf_dataset->WriteOnDisk((void *) list_of_files.c_str());
415 hdf_dataset->CloseOnDisk();
416 hdf_group->CloseOnDisk();
418 // Insert Files into the hdf5_file
419 begin = _fileManaged.begin();
420 for(;begin!=end;begin++)
422 Engines::file file_infos = begin->second;
423 std::string file_name(file_infos.file_name.in());
424 std::string comp_file_name(_fileManaged[file_name].path.in());
425 comp_file_name.append(_fileManaged[file_name].file_name.in());
426 std::string group_name("GROUP");
427 group_name += std::string(_fileManaged[file_name].file_name.in());
428 std::string dataset_group_name("DATASET");
429 dataset_group_name += std::string(_fileManaged[file_name].file_name.in());
431 hdf_group = new HDFgroup((char *) group_name.c_str(), hdf_file);
432 hdf_group->CreateOnDisk();
433 HDFConvert::FromAscii(comp_file_name.c_str(), *hdf_group, "FILE DATASET");
434 hdf_group->CloseOnDisk();
436 hdf_group = new HDFgroup((char *) dataset_group_name.c_str(), hdf_file);
437 hdf_group->CreateOnDisk();
438 size[0] = strlen(file_infos.file_name.in()) + 1;
439 hdf_dataset = new HDFdataset("NAME", hdf_group, HDF_STRING, size, 1);
440 hdf_dataset->CreateOnDisk();
441 hdf_dataset->WriteOnDisk((void *) file_infos.file_name.in());
442 hdf_dataset->CloseOnDisk();
443 size[0] = strlen(file_infos.path.in()) + 1;
444 hdf_dataset = new HDFdataset("PATH", hdf_group, HDF_STRING, size, 1);
445 hdf_dataset->CreateOnDisk();
446 hdf_dataset->WriteOnDisk((void *) file_infos.path.in());
447 hdf_dataset->CloseOnDisk();
448 size[0] = strlen(file_infos.type.in()) + 1;
449 hdf_dataset = new HDFdataset("TYPE", hdf_group, HDF_STRING, size, 1);
450 hdf_dataset->CreateOnDisk();
451 hdf_dataset->WriteOnDisk((void *) file_infos.type.in());
452 hdf_dataset->CloseOnDisk();
453 size[0] = strlen(file_infos.source_file_name.in()) + 1;
454 hdf_dataset = new HDFdataset("SOURCE_FILE_NAME", hdf_group, HDF_STRING, size, 1);
455 hdf_dataset->CreateOnDisk();
456 hdf_dataset->WriteOnDisk((void *) file_infos.source_file_name.in());
457 hdf_dataset->CloseOnDisk();
458 size[0] = strlen(file_infos.status.in()) + 1;
459 hdf_dataset = new HDFdataset("STATUS", hdf_group, HDF_STRING, size, 1);
460 hdf_dataset->CreateOnDisk();
461 hdf_dataset->WriteOnDisk((void *) file_infos.status.in());
462 hdf_dataset->CloseOnDisk();
463 hdf_group->CloseOnDisk();
467 hdf_file->CloseOnDisk();
469 // delete hdf_dataset;
470 // delete hdf_group; ----> SEGFAULT !!!
471 // delete hdf_file; ----> SEGFAULT !!!
475 SALOME::ExceptionStruct es;
476 es.type = SALOME::INTERNAL_ERROR;
477 std::string text = "!!!! HDFexception";
478 es.text = CORBA::string_dup(text.c_str());
479 throw SALOME::SALOME_Exception(es);
483 //=============================================================================
486 * \see Engines::Salome_file::setLocalFile
488 //=============================================================================
490 Salome_file_i::setLocalFile(const char* comp_file_name)
492 std::string file_name("");
493 std::string path("");
494 std::string type("local");
495 std::string source_file_name("");
496 std::string status("not_ok");
498 std::string cp_file_name(comp_file_name);
499 std::size_t index = cp_file_name.rfind("/");
502 file_name = cp_file_name.substr(index+1);
503 path = cp_file_name.substr(0,index+1);
507 file_name = comp_file_name;
508 path = getcwd(NULL, _path_max);;
511 // Test if this file is already added
512 _t_fileManaged::iterator it = _fileManaged.find(file_name);
513 if (it != _fileManaged.end())
515 SALOME::ExceptionStruct es;
516 es.type = SALOME::INTERNAL_ERROR;
517 std::string text = "file already added";
518 es.text = CORBA::string_dup(text.c_str());
519 throw SALOME::SALOME_Exception(es);
522 // Test if the file is ok
523 if(fopen(comp_file_name,"rb") != NULL)
526 // Adding file with is informations
528 infos.file_name = CORBA::string_dup(file_name.c_str());
529 infos.path = CORBA::string_dup(path.c_str());
530 infos.type = CORBA::string_dup(type.c_str());
531 infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
532 infos.status = CORBA::string_dup(status.c_str());
533 // Infos for parallel extensions...
535 infos.container = Engines::Container::_duplicate(_container);
537 _fileManaged[file_name] = infos;
539 // Update Salome_file state
540 _state.number_of_files++;
542 _state.files_ok = false;
545 //=============================================================================
548 * \see Engines::Salome_file::setDistributedFile
550 //=============================================================================
552 Salome_file_i::setDistributedFile(const char* comp_file_name)
554 std::string file_name("");
555 std::string path("");
556 std::string type("distributed");
557 std::string source_file_name("");
558 std::string status("not_ok");
560 std::string cp_file_name(comp_file_name);
561 std::size_t index = cp_file_name.rfind("/");
564 file_name = cp_file_name.substr(index+1);
565 path = cp_file_name.substr(0,index+1);
569 file_name = comp_file_name;
570 path = getcwd(NULL, _path_max);;
573 // Test if this file is already added
574 _t_fileManaged::iterator it = _fileManaged.find(file_name);
575 if (it != _fileManaged.end())
577 SALOME::ExceptionStruct es;
578 es.type = SALOME::INTERNAL_ERROR;
579 std::string text = "file already added";
580 es.text = CORBA::string_dup(text.c_str());
581 throw SALOME::SALOME_Exception(es);
584 // Adding file with his informations
586 infos.file_name = CORBA::string_dup(file_name.c_str());
587 infos.path = CORBA::string_dup(path.c_str());
588 infos.type = CORBA::string_dup(type.c_str());
589 infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
590 infos.status = CORBA::string_dup(status.c_str());
591 // Infos for parallel extensions...
593 infos.container = Engines::Container::_duplicate(_container);
595 _fileManaged[file_name] = infos;
597 if(!CORBA::is_nil(_default_source_Salome_file))
599 _fileDistributedSource[file_name] =
600 Engines::Salome_file::_duplicate(_default_source_Salome_file);
603 // Update Salome_file state
604 _state.number_of_files++;
605 _state.files_ok = false;
608 //=============================================================================
611 * \see Engines::Salome_file::connect
613 //=============================================================================
615 Salome_file_i::connect(Engines::Salome_file_ptr source_Salome_file)
617 if(CORBA::is_nil(_default_source_Salome_file))
619 _default_source_Salome_file = Engines::Salome_file::_duplicate(source_Salome_file);
620 _t_fileManaged::iterator begin = _fileManaged.begin();
621 _t_fileManaged::iterator end = _fileManaged.end();
622 for(;begin!=end;begin++) {
623 // Get the name of the file
624 std::string file_name = begin->first;
625 _t_fileDistributedSource::iterator it = _fileDistributedSource.find(file_name);
626 if (it == _fileDistributedSource.end())
628 _fileDistributedSource[file_name] = Engines::Salome_file::_duplicate(source_Salome_file);
634 SALOME::ExceptionStruct es;
635 es.type = SALOME::INTERNAL_ERROR;
636 std::string text = "already connected to a default Salome_file";
637 es.text = CORBA::string_dup(text.c_str());
638 throw SALOME::SALOME_Exception(es);
640 // We can connect this Salome_file if there is only one file managed
641 // by the Salome_file
643 //if (_fileManaged.size() == 1)
645 // only one file managed
646 // _t_fileManaged::iterator it = _fileManaged.begin();
647 // fname = it->first;
648 // _fileDistributedSource[fname] = Engines::Salome_file::_duplicate(source_Salome_file);
652 // SALOME::ExceptionStruct es;
653 // es.type = SALOME::INTERNAL_ERROR;
654 // std::string text = "cannot connect";
655 // es.text = CORBA::string_dup(text.c_str());
656 // throw SALOME::SALOME_Exception(es);
660 //=============================================================================
663 * \see Engines::Salome_file::connectDistributedFile
665 //=============================================================================
667 Salome_file_i::connectDistributedFile(const char * file_name,
668 Engines::Salome_file_ptr source_Salome_file)
670 // Test if this file is added
671 _t_fileManaged::iterator it = _fileManaged.find(file_name);
672 if (it == _fileManaged.end())
674 SALOME::ExceptionStruct es;
675 es.type = SALOME::INTERNAL_ERROR;
676 std::string text = "file is not added";
677 es.text = CORBA::string_dup(text.c_str());
678 throw SALOME::SALOME_Exception(es);
682 _fileDistributedSource[file_name] = Engines::Salome_file::_duplicate(source_Salome_file);
686 //=============================================================================
689 * \see Engines::Salome_file::setDistributedSourceFile
691 //=============================================================================
693 Salome_file_i::setDistributedSourceFile(const char* file_name,
694 const char * source_file_name)
696 std::string fname(file_name);
698 // Test if this file is added
699 _t_fileManaged::iterator it = _fileManaged.find(fname);
700 if (it == _fileManaged.end())
702 SALOME::ExceptionStruct es;
703 es.type = SALOME::INTERNAL_ERROR;
704 std::string text = "file is not added";
705 es.text = CORBA::string_dup(text.c_str());
706 throw SALOME::SALOME_Exception(es);
710 _fileManaged[fname].source_file_name = CORBA::string_dup(source_file_name);
714 //=============================================================================
717 * \see Engines::Salome_file::recvFiles
719 //=============================================================================
721 Salome_file_i::recvFiles() {
723 std::string files_not_ok("");
725 _t_fileManaged::iterator begin = _fileManaged.begin();
726 _t_fileManaged::iterator end = _fileManaged.end();
727 for(;begin!=end;begin++)
730 Engines::file file_infos = begin->second;
731 // Test if the file is local or distributed
732 if (std::string(file_infos.type.in()) == "local")
734 if (std::string(file_infos.status.in()) == "not_ok")
735 result = checkLocalFile(file_infos.file_name.in());
739 if (std::string(file_infos.status.in()) == "not_ok")
740 result = getDistributedFile(file_infos.file_name.in());
742 // if the result is false
743 // we add this file to files_not_ok
746 files_not_ok.append(" ");
747 files_not_ok.append(file_infos.file_name.in());
751 if (files_not_ok != "")
753 SALOME::ExceptionStruct es;
754 es.type = SALOME::INTERNAL_ERROR;
755 std::string text = "files not ready : " + files_not_ok;
756 es.text = CORBA::string_dup(text.c_str());
757 throw SALOME::SALOME_Exception(es);
761 // We change the state of the Salome_file
762 _state.files_ok = true;
766 //=============================================================================
768 * local C++ method : This method is used by revFiles to check if a local
769 * managed file is ok.
770 * \param fileName name of the file
772 //=============================================================================
774 Salome_file_i::checkLocalFile(std::string file_name)
778 std::string comp_file_name(_fileManaged[file_name].path.in());
779 comp_file_name.append("/");
780 comp_file_name.append(_fileManaged[file_name].file_name.in());
781 if(fopen(comp_file_name.c_str(),"rb") == NULL)
783 INFOS("file " << comp_file_name << " cannot be open for reading");
784 _fileManaged[file_name].status = CORBA::string_dup("not_ok");
790 _fileManaged[file_name].status = CORBA::string_dup("ok");
795 //=============================================================================
797 * local C++ method : this method is used by recvFiles to get a
798 * distributed file from is distributed source.
799 * If there is no source_file_name for the file, it tries to get
800 * the file from the source. In this case, the source distributed file has to managed
801 * only one file to be able to the send the file.
803 * \param fileName name of the file
805 //=============================================================================
807 Salome_file_i::getDistributedFile(std::string file_name)
810 const char * source_file_name = _fileManaged[file_name].source_file_name.in();
813 std::string comp_file_name(_fileManaged[file_name].path.in());
814 comp_file_name.append("/");
815 comp_file_name.append(_fileManaged[file_name].file_name.in());
817 // Test if the process can write on disk
818 if ((fp = fopen(comp_file_name.c_str(),"wb")) == NULL)
820 INFOS("file " << comp_file_name << " cannot be open for writing");
821 _fileManaged[file_name].status = CORBA::string_dup("not_ok");
828 fileId = _fileDistributedSource[file_name]->open(source_file_name);
832 _fileManaged[file_name].status = CORBA::string_dup("not_ok");
840 Engines::fileBlock* aBlock;
843 MESSAGE("begin of transfer of " << comp_file_name);
847 aBlock = _fileDistributedSource[file_name]->getBlock(fileId);
848 toFollow = aBlock->length();
849 CORBA::Octet *buf = aBlock->get_buffer();
850 #if defined(_DEBUG_) || defined(_DEBUG)
851 int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
852 ASSERT(nbWri == toFollow);
854 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
859 MESSAGE("end of transfer of " << comp_file_name);
860 _fileDistributedSource[file_name]->close(fileId);
864 INFOS("open reference file for copy impossible");
867 _fileManaged[file_name].status = CORBA::string_dup("not_ok");
871 _fileManaged[file_name].status = CORBA::string_dup("ok");
875 //=============================================================================
878 * \see Engines::Salome_file::removeFile
880 //=============================================================================
882 Salome_file_i::removeFile(const char* file_name)
884 MESSAGE("Salome_file_i::removeFile : NOT YET IMPLEMENTED");
887 //=============================================================================
890 * \see Engines::Salome_file::removeFiles
892 //=============================================================================
894 Salome_file_i::removeFiles() {
895 MESSAGE("Salome_file_i::removeFiles : NOT YET IMPLEMENTED");
898 //=============================================================================
901 * \see Engines::Salome_file::getFilesInfos
903 //=============================================================================
905 Salome_file_i::getFilesInfos() {
907 Engines::files * infos = new Engines::files();
908 infos->length(_fileManaged.size());
910 _t_fileManaged::iterator begin = _fileManaged.begin();
911 _t_fileManaged::iterator end = _fileManaged.end();
913 for(;begin!=end;begin++) {
914 (*infos)[i] = *(new Engines::file(begin->second));
920 //=============================================================================
923 * \see Engines::Salome_file::getFileInfos
925 //=============================================================================
927 Salome_file_i::getFileInfos(const char* file_name) {
929 std::string fname(file_name);
931 // Test if this file is managed
932 _t_fileManaged::iterator it = _fileManaged.find(fname);
933 if (it == _fileManaged.end())
935 SALOME::ExceptionStruct es;
936 es.type = SALOME::INTERNAL_ERROR;
937 es.text = "file is not managed";
938 throw SALOME::SALOME_Exception(es);
941 Engines::file * infos = new Engines::file(_fileManaged[fname]);
945 //=============================================================================
948 * \see Engines::Salome_file::getSalome_fileState
950 //=============================================================================
952 Salome_file_i::getSalome_fileState()
954 return new Engines::SfState(_state);
957 //=============================================================================
959 * CORBA method: try to open the file given. If the file is readable, return
960 * a positive integer else return 0;
961 * \param fileName file name to be transfered
962 * \return fileId = positive integer > 0 if open OK.
964 //=============================================================================
967 Salome_file_i::open(const char* file_name)
971 std::string fname(file_name);
973 // We enter in the simple case where the user
974 // has not used setDistributedSourceFile.
975 // In this case we try to see if the Salome_file
976 if (_fileManaged.size() == 1)
978 // only one file managed
979 _t_fileManaged::iterator it = _fileManaged.begin();
984 // we can't choose the file so :
989 _t_fileManaged::iterator it = _fileManaged.find(fname);
990 if (it == _fileManaged.end())
995 std::string comp_file_name(_fileManaged[fname].path.in());
996 comp_file_name.append("/");
997 comp_file_name.append(fname);
998 MESSAGE("Salome_file_i::open " << comp_file_name);
1000 if ((fp = fopen(comp_file_name.c_str(),"rb")) == NULL)
1002 INFOS("file " << comp_file_name << " is not readable");
1007 _fileAccess[aKey] = fp;
1011 //=============================================================================
1013 * CORBA method: close the file associated to the fileId given at open.
1014 * \param fileId got in return from open method
1016 //=============================================================================
1019 Salome_file_i::close(CORBA::Long fileId)
1021 MESSAGE("Salome_file_i::close");
1023 if (!(fp = _fileAccess[fileId]) )
1025 INFOS(" no FILE structure associated to fileId " << fileId);
1030 //=============================================================================
1032 * CORBA method: get a block of data from the file associated to the fileId
1034 * \param fileId got in return from open method
1035 * \return an octet sequence. Last one is empty.
1037 //=============================================================================
1039 #define FILEBLOCK_SIZE 256*1024
1042 Salome_file_i::getBlock(CORBA::Long fileId)
1044 Engines::fileBlock* aBlock = new Engines::fileBlock;
1047 if (! (fp = _fileAccess[fileId]) )
1049 INFOS(" no FILE structure associated to fileId " <<fileId);
1053 // use replace member function for sequence to avoid copy
1054 // see Advanced CORBA Programming with C++ pp 187-194
1056 buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
1057 int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
1058 aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
1063 Salome_file_i::setContainer(Engines::Container_ptr container)
1065 _container = Engines::Container::_duplicate(container);
1067 // Update All the files
1068 _t_fileManaged::iterator begin = _fileManaged.begin();
1069 _t_fileManaged::iterator end = _fileManaged.end();
1070 for(;begin!=end;begin++) {
1071 begin->second.container = Engines::Container::_duplicate(container);