1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDS_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
29 //#include <OSD_Path.hxx>
30 //#include <OSD_File.hxx>
31 //#include <OSD_Protection.hxx>
32 //#include <OSD_Directory.hxx>
33 //#include <TCollection_AsciiString.hxx>
51 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp);
52 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp);
53 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp);
55 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident);
56 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident);
57 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident);
60 char* makeName(char* name);
61 char* restoreName(char* name);
62 void write_float64(FILE* fp, hdf_float64* value);
63 void read_float64(FILE* fp, hdf_float64* value);
64 bool directoryExists(const char* dir);
65 bool createDirectory(const char* dir);
66 bool is_readable( const std::string & file );
68 #define MAX_STRING_SIZE 65535
69 #define MAX_ID_SIZE 20
70 #define NB_FLOAT_IN_ROW 3
71 #define NB_INTEGER_IN_ROW 9
73 #define ASCIIHDF_ID "ASCIIHDF"
74 #define ATTRIBUTE_ID "ATTRIBUTE"
75 #define DATASET_ID "DATASET"
76 #define GROUP_ID "GROUP"
78 #define ASCIIHDF_ID_END "ASCIIHDF_END"
79 #define ATTRIBUTE_ID_END "ATTRIBUTE_END"
80 #define DATASET_ID_END "DATASET_END"
81 #define GROUP_ID_END "GROUP_END"
84 //============================================================================
86 // purpose : Returns True if the file is a converted to ASCII HDF file
87 //============================================================================
88 bool HDFascii::isASCII(const char* thePath) {
90 if(!(fd = open(thePath, O_RDONLY))) return false;
91 char* aBuffer = new char[9];
96 if(strcmp(aBuffer, ASCIIHDF_ID) == 0) return true;
101 //############################## HDF => ASCII ################################
103 //============================================================================
104 // function : ConvertFromHDFToASCII
105 // purpose : Converts a file pointed by thePath to ASCII format
106 // If isReplace is true the newly created file will replace the existent one.
107 // If isReplace is false theExtension will be added to a created file name
108 // Returns the file name of ASCII file
109 //============================================================================
110 char* HDFascii::ConvertFromHDFToASCII(const char* thePath,
112 const char* theExtension)
114 //TCollection_AsciiString aPath((char*)thePath);
115 std::string aPath((char*)thePath);
117 if(theExtension == NULL) aPath += ".asc";
118 else aPath += (char*)theExtension;
121 //TCollection_AsciiString aFileName(aPath);
122 std::string aFileName(aPath);
123 if(isReplace) aFileName=aPath+".ascii_tmp";
125 HDFfile *hdf_file = new HDFfile((char*)thePath);
126 hdf_file->OpenOnDisk(HDF_RDONLY);
128 char name[HDF_NAME_MAX_LEN+1];
129 int nbsons = hdf_file->nInternalObjects(), nbAttr = hdf_file->nAttributes();
131 //FILE* fp = fopen(aFileName.ToCString(), "w");
132 FILE* fp = fopen(aFileName.c_str(), "w");
133 fprintf(fp, "%s\n", ASCIIHDF_ID);
134 fprintf(fp, "%i\n", nbsons+nbAttr);
136 for(unsigned j=0; j<nbAttr; j++) {
137 char* attr_name = hdf_file->GetAttributeName(j);
138 HDFattribute *hdf_attribute = new HDFattribute(attr_name, hdf_file);
140 SaveAttributeInASCIIfile(hdf_attribute, fp, 0);
144 //for (Standard_Integer i=0; i<nbsons; i++) {
145 for (int i=0; i<nbsons; i++) {
146 hdf_file->InternalObjectIndentify(i,name);
147 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
149 hdf_object_type type = hdf_file->InternalObjectType(name);
151 if(type == HDF_DATASET) {
152 HDFdataset* hdf_dataset = new HDFdataset(name, hdf_file);
153 SaveDatasetInASCIIfile(hdf_dataset, fp, 0);
155 } else if(type == HDF_GROUP) {
156 HDFgroup *hdf_group = new HDFgroup(name, hdf_file);
157 SaveGroupInASCIIfile(hdf_group, fp, 0);
162 fprintf(fp, "%s", ASCIIHDF_ID_END);
166 hdf_file->CloseOnDisk();
170 // OSD_Path anOSDPath(aFileName);
171 // OSD_File anOSDFile(anOSDPath);
172 // if(anOSDFile.Exists())
173 // anOSDFile.Move(aPath);
176 if(access (aFileName.c_str() , F_OK ) == 0) {
177 rename(aFileName.c_str(), aPath.c_str());
183 //int length = strlen(aPath.ToCString());
184 int length = strlen(aPath.c_str());
185 char *new_str = new char[ 1+length ];
186 //strcpy(new_str , aPath.ToCString()) ;
187 strcpy(new_str , aPath.c_str()) ;
193 //============================================================================
194 // function : SaveGroupInASCIIfile
196 //============================================================================
197 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident)
199 hdf_group->OpenOnDisk();
201 //TCollection_AsciiString anIdent(ident, '\t');
202 std::ostringstream oss;
204 std::string anIdent(oss.str());
205 int nbsons = hdf_group->nInternalObjects(), nbAttr = hdf_group->nAttributes();
207 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), GROUP_ID);*/
208 fprintf(fp, "%s\n", GROUP_ID);
210 char* name = makeName(hdf_group->GetName());
212 /*fprintf(fp, "%s%s %i\n", anIdent.ToCString(), name, nbsons+nbAttr);*/
213 fprintf(fp, "%s %i\n", name, nbsons+nbAttr);
216 for(unsigned j=0; j<nbAttr; j++) {
217 name = hdf_group->GetAttributeName(j);
218 HDFattribute *hdf_attribute = new HDFattribute(name, hdf_group);
220 SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
224 char objName[HDF_NAME_MAX_LEN+1];
226 for (int i=0; i<nbsons; i++) {
227 hdf_group->InternalObjectIndentify(i, objName);
229 if (strncmp(objName, "INTERNAL_COMPLEX",16) == 0) continue;
231 hdf_object_type type = hdf_group->InternalObjectType(objName);
233 if (type == HDF_DATASET) {
234 HDFdataset* hdf_dataset = new HDFdataset(objName, hdf_group);
235 SaveDatasetInASCIIfile(hdf_dataset, fp, ident+1);
237 } else if (type == HDF_GROUP) {
238 HDFgroup *hdf_subgroup = new HDFgroup(objName, hdf_group);
239 SaveGroupInASCIIfile(hdf_subgroup, fp, ident+1);
244 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), GROUP_ID_END);*/
245 fprintf(fp, "%s\n", GROUP_ID_END);
247 hdf_group->CloseOnDisk();
250 //============================================================================
251 // function : SaveDatasetInASCIIfile
253 //============================================================================
254 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
256 hdf_dataset->OpenOnDisk();
258 long size = hdf_dataset->GetSize();
259 long ndim = hdf_dataset->nDim(); //Get number of dimesions
260 hdf_size *dim = new hdf_size[ndim];
261 hdf_type type = hdf_dataset->GetType();
262 hdf_byte_order order = hdf_dataset->GetOrder();
263 int nbAttr = hdf_dataset->nAttributes();
265 // TCollection_AsciiString anIdent(ident, '\t');
266 // TCollection_AsciiString anIdentChild(ident+1, '\t');
267 std::ostringstream oss;
268 std::ostringstream oss2;
271 std::string anIdent(oss.str());
272 std::string anIdentChild(oss2.str());
274 char* name = makeName(hdf_dataset->GetName());
276 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), DATASET_ID);*/
277 fprintf(fp, "%s\n", DATASET_ID);
278 /*fprintf(fp, "%s%s %i %i\n", anIdent.ToCString(), name, type, nbAttr);*/
279 fprintf(fp, "%s %i %i\n", name, type, nbAttr);
282 hdf_dataset->GetDim(dim);
283 /*fprintf(fp, "%s %i\n", anIdentChild.ToCString(), ndim);*/
284 fprintf(fp, " %i\n", ndim);
286 for(int i = 0;i < ndim;i++) {
287 /*fprintf(fp, "%s%i", anIdentChild.ToCString(), dim[i]);*/
288 fprintf(fp, " %i", dim[i]);
291 /*fprintf(fp, "%s\n", anIdentChild.ToCString());*/
295 /*fprintf(fp, "%s%li:", anIdentChild.ToCString(), size);*/
296 // fprintf(fp, "%li:", size);
297 fprintf(fp, "%li %i:", size, order);
299 if (type == HDF_STRING) {
300 char* val = new char[size];
301 hdf_dataset->ReadFromDisk(val);
302 fwrite(val, 1, size, fp);
304 } else if (type == HDF_FLOAT64) {
305 hdf_float64* val = new hdf_float64[size];
306 hdf_dataset->ReadFromDisk(val);
308 for (int i = 0, j = 0; i < size; i++) {
309 write_float64(fp, &val[i]);
310 if(++j == NB_FLOAT_IN_ROW) {
314 else fprintf(fp," ");
317 } else if(type == HDF_INT64) {
318 hdf_int64* val = new hdf_int64[size];
319 hdf_dataset->ReadFromDisk(val);
321 for (int i = 0, j = 0; i < size; i++) {
322 fprintf(fp, " %li", val[i]);
323 if(++j == NB_INTEGER_IN_ROW) {
329 } else if(type == HDF_INT32) {
330 hdf_int32* val = new hdf_int32[size];
331 hdf_dataset->ReadFromDisk(val);
333 for (int i = 0, j = 0; i < size; i++) {
334 fprintf(fp, " %i", val[i]);
335 if(++j == NB_INTEGER_IN_ROW) {
345 for ( unsigned j=0; j<nbAttr; j++ )
347 name = hdf_dataset->GetAttributeName(j);
348 HDFattribute *hdf_attribute = new HDFattribute(name, hdf_dataset);
350 SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
354 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), DATASET_ID_END); */
355 fprintf(fp, "%s\n", DATASET_ID_END);
357 hdf_dataset->CloseOnDisk();
361 //============================================================================
362 // function : SaveAttributeInASCIIfile
364 //============================================================================
365 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident)
367 hdf_attribute->OpenOnDisk();
369 hdf_type type = hdf_attribute->GetType();
371 //TCollection_AsciiString anIdent(ident, '\t');
372 //TCollection_AsciiString anIdentChild(ident+1, '\t');
373 std::ostringstream oss;
374 std::ostringstream oss2;
377 std::string anIdent(oss.str());
378 std::string anIdentChild(oss2.str());
380 char* name = makeName(hdf_attribute->GetName());
381 int size = hdf_attribute->GetSize();
383 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), ATTRIBUTE_ID);*/
384 fprintf(fp, "%s\n", ATTRIBUTE_ID);
385 /*fprintf(fp, "%s%s %i %i\n", anIdent.ToCString(), name, type, size);*/
386 fprintf(fp, "%s %i %i\n", name, type, size);
390 if (type == HDF_STRING) {
391 char* val = new char[size+1];
392 hdf_attribute->ReadFromDisk(val);
393 /*fprintf(fp, "%s:", anIdentChild.ToCString());*/
395 fwrite(val, 1, size, fp);
398 } else if (type == HDF_FLOAT64) {
400 hdf_attribute->ReadFromDisk(&val);
401 /*fprintf(fp, "%s", anIdentChild.ToCString());*/
402 write_float64(fp, &val);
404 } else if(type == HDF_INT64) {
406 hdf_attribute->ReadFromDisk(&val);
407 /*fprintf(fp, "%s%li \n", anIdentChild.ToCString(), val);*/
408 fprintf(fp, "%li \n", val);
409 } else if(type == HDF_INT32) {
411 hdf_attribute->ReadFromDisk(&val);
412 /*fprintf(fp, "%s%i \n", anIdentChild.ToCString(), val);*/
413 fprintf(fp, "%i \n", val);
416 /*fprintf(fp, "%s%s\n", anIdent.ToCString(), ATTRIBUTE_ID_END);*/
417 fprintf(fp, "%s\n", ATTRIBUTE_ID_END);
419 hdf_attribute->CloseOnDisk();
422 //############################## ASCII => HDF ################################
424 //============================================================================
425 // function : ConvertFromASCIIToHDF
426 // purpose : Converts a file pointed by thePath to HDF format
427 // Returns a name of directory where a created HDF file is placed
428 // The created file is named "hdf_from_ascii.hdf"
429 //============================================================================
430 char* HDFascii::ConvertFromASCIIToHDF(const char* thePath)
432 // Get a temporary directory to store a file
433 //TCollection_AsciiString aTmpDir = GetTmpDir(), aFileName("hdf_from_ascii.hdf");
434 std::string aTmpDir = GetTmpDir();
435 std::string aFileName("hdf_from_ascii.hdf");
436 // Build a full file name of temporary file
437 //TCollection_AsciiString aFullName = aTmpDir + aFileName;
438 std::string aFullName = aTmpDir + aFileName;
440 //HDFfile *hdf_file = new HDFfile(aFullName.ToCString());
441 HDFfile *hdf_file = new HDFfile((char *) aFullName.c_str());
442 hdf_file->CreateOnDisk();
444 FILE *fp = fopen(thePath, "r");
449 fscanf(fp, "%s", type);
450 fscanf(fp, "%i",&nbsons);
452 if(strcmp(type, ASCIIHDF_ID) != 0) return NULL;
454 for(i = 0; i < nbsons; i++) {
455 char id_of_begin[MAX_ID_SIZE];
456 fscanf(fp, "%s\n", id_of_begin);
458 if(strcmp(id_of_begin, GROUP_ID) == 0) {
459 if(!CreateGroupFromASCII(hdf_file, fp)) {
460 cout << "ConvertFromASCIIToHDF : Can not create group number " << i << endl;
464 else if(strcmp(id_of_begin, DATASET_ID) == 0) {
465 if(!CreateDatasetFromASCII(hdf_file, fp)) {
466 cout << "ConvertFromASCIIToHDF :Can not create dataset number " << i << endl;
470 else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
471 if(!CreateAttributeFromASCII(hdf_file, fp)) {
472 cout << "ConvertFromASCIIToHDF :Can not create attribute number " << i << endl;
477 cout << "ConvertFromASCIIToHDF : Unrecognized type " << id_of_begin << endl;
480 char id_of_end[MAX_ID_SIZE];
481 fscanf(fp, "%s", id_of_end);
482 if(strcmp(id_of_end, ASCIIHDF_ID_END) != 0) {
483 cout << "ConvertFromASCIIToHDF : Can not find the end ASCII token " << endl;
487 hdf_file->CloseOnDisk();
490 //int length = strlen(aTmpDir.ToCString());
491 int length = strlen(aTmpDir.c_str());
492 cerr << "length : " << length << endl;
493 char *new_str = new char[ 1+length ];
494 //strcpy(new_str , aTmpDir.ToCString()) ;
495 strcpy(new_str , aTmpDir.c_str()) ;
497 cerr << "test ::: " << aTmpDir << endl;
498 cerr << "test ::: " << new_str << endl;
504 //============================================================================
505 // function : CreateGroupFromASCII
506 // purpose : Creates a HDF group from a set attributes situated under theLabel
507 //============================================================================
508 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp)
510 char name[HDF_NAME_MAX_LEN+1];
512 fscanf(fp, "%s %i\n", name, &nbsons);
514 char* new_name = restoreName(name);
516 HDFgroup* hdf_group = new HDFgroup(new_name, father);
520 hdf_group->CreateOnDisk();
522 for(i = 0; i < nbsons; i++) {
523 char id_of_begin[MAX_ID_SIZE];
524 fscanf(fp, "%s\n", id_of_begin);
526 if(strcmp(id_of_begin, GROUP_ID) == 0) {
527 if(!CreateGroupFromASCII(hdf_group, fp)) {
528 cout << "Can not create subgroup " << i << " for group " << name << endl;
532 else if(strcmp(id_of_begin, DATASET_ID) == 0) {
533 if(!CreateDatasetFromASCII(hdf_group, fp)) {
534 cout << "Can not create dataset " << i << " for group " << name << endl;
538 else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
539 if(!CreateAttributeFromASCII(hdf_group, fp)) {
540 cout << "Can not create attribute " << i << " for group " << name << endl;
545 cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << endl;
548 hdf_group->CloseOnDisk();
549 hdf_group = 0; //will be deleted by father destructor
551 char id_of_end[MAX_ID_SIZE];
552 fscanf(fp, "%s\n", id_of_end);
553 if(strcmp(id_of_end, GROUP_ID_END) != 0) {
554 cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << endl;
562 //============================================================================
563 // function : CreateDatasetFromASCII
564 // purpose : Creates a HDF dataset from a set attributes situated under theLabel
565 //============================================================================
566 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
568 char name[HDF_NAME_MAX_LEN+1];
570 hdf_byte_order order;
574 fscanf(fp, "%s %i %i\n", name, &type, &nbAttr);
575 char* new_name = restoreName(name);
577 fscanf(fp, "%i\n", &nbDim);
579 hdf_size* sizeArray = new hdf_size[nbDim];
581 for(i = 0; i<nbDim; i++) {
582 fscanf(fp, "%i\n", &dim);
586 // order (2-d member) was not written in earlier versions
588 int nbRead = fscanf(fp, "%li %i%c", &size, &order, &tmp);
589 if ( nbRead < 2 ) { // fscanf stops before ":"
590 fscanf(fp, "%c", &tmp);
591 order = H5T_ORDER_NONE;
593 if ( type != HDF_FLOAT64 ) // use order only for FLOAT64
594 order = H5T_ORDER_NONE;
596 HDFdataset* hdf_dataset = new HDFdataset(new_name, father,type, sizeArray, nbDim, order);
600 hdf_dataset->CreateOnDisk();
602 if (type == HDF_STRING) {
603 char *val = new char[size+1];
604 fread(val, 1, size, fp);
605 hdf_dataset->WriteOnDisk(val);
607 } else if (type == HDF_FLOAT64) {
608 hdf_float64* val = new hdf_float64[size];
609 for(i=0; i<size; i++) {
610 read_float64(fp, &(val[i]));
612 hdf_dataset->WriteOnDisk(val);
614 } else if(type == HDF_INT64) {
615 hdf_int64* val = new hdf_int64[size];
616 for(i=0; i<size; i++) {
617 fscanf(fp, " %li", &(val[i]));
619 hdf_dataset->WriteOnDisk(val);
621 } else if(type == HDF_INT32) {
622 hdf_int32* val = new hdf_int32[size];
623 for(i=0; i<size; i++) {
624 fscanf(fp, " %i", &(val[i]));
626 hdf_dataset->WriteOnDisk(val);
630 char token[MAX_ID_SIZE];
632 for(i = 0; i < nbAttr; i++) {
633 fscanf(fp, "%s\n", token);
635 if(strcmp(token, ATTRIBUTE_ID) == 0) {
636 if(!CreateAttributeFromASCII(hdf_dataset, fp)) {
637 cout << "Can not create attribute " << i << " for dataset " << name << endl;
642 cout << "CreateGroupFromASCII : Unrecognized type " << token << endl;
647 fscanf(fp, "%s\n", token);
648 if(strcmp(token, DATASET_ID_END) != 0) {
649 cout << "CreateDatasetFromASCII : Invalid end token : " << token << endl;
653 hdf_dataset->CloseOnDisk();
654 hdf_dataset = 0; //will be deleted by father destructor
660 //============================================================================
661 // function : CreateAttributeFromASCII
662 // purpose : Creates a HDF attribute from a set attributes situated under theLabel
663 //============================================================================
664 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp)
666 char name[HDF_NAME_MAX_LEN+1];
670 fscanf(fp, "%s %i %i\n", name, &type, &size);
671 char* new_name = restoreName(name);
672 HDFattribute* hdf_attribute = new HDFattribute(new_name, father, type, size);
674 hdf_attribute->CreateOnDisk();
678 if (type == HDF_STRING) {
680 fscanf(fp, "%c", &tmp);
681 char *val = new char[size+1];
683 fread(val, 1, size, fp);
684 hdf_attribute->WriteOnDisk(val);
686 } else if (type == HDF_FLOAT64) {
688 read_float64(fp, &val);
689 hdf_attribute->WriteOnDisk(&val);
690 } else if(type == HDF_INT64) {
692 fscanf(fp, "%li", &val);
693 hdf_attribute->WriteOnDisk(&val);
694 } else if(type == HDF_INT32) {
696 fscanf(fp, "%i", &val);
697 hdf_attribute->WriteOnDisk(&val);
700 hdf_attribute->CloseOnDisk();
701 hdf_attribute = 0; //will be deleted by father destructor
704 char id_of_end[MAX_ID_SIZE];
705 fscanf(fp, "%s\n", id_of_end);
706 if(strcmp(id_of_end, ATTRIBUTE_ID_END) != 0) {
707 cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << endl;
715 //============================================================================
716 // function : GetTempDir
717 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
718 //============================================================================
721 //Find a temporary directory to store a file
723 //TCollection_AsciiString aTmpDir;
728 aTmp = getenv("TMP");
730 // aTmpDir = TCollection_AsciiString(aTmp);
731 aTmpDir = std::string(aTmp);
734 // aTmpDir = TCollection_AsciiString("C:\\");
735 aTmpDir = std::string("C:\\");
738 //aTmpDir = TCollection_AsciiString("/tmp/");
739 aTmpDir = std::string("/tmp/");
742 srand((unsigned int)time(NULL));
744 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
745 // TCollection_AsciiString aSubDir(aRND);
746 // if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
747 std::ostringstream oss;
749 std::string aSubDir(oss.str());
750 if(aSubDir.length() <= 1) aSubDir = std::string("123409876");
752 aTmpDir += aSubDir; //Get RND sub directory
755 // if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
756 if(aTmpDir[aTmpDir.length()-1] != '\\') aTmpDir+='\\';
758 // if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
759 if(aTmpDir[aTmpDir.length()-1] != '/') aTmpDir+='/';
762 // OSD_Path aPath(aTmpDir);
763 // OSD_Directory aDir(aPath);
764 std::string aDir(aTmpDir);
766 //std::cerr << "temp dir .... : " << aTmpDir << std::endl;
768 for(aRND = 0; directoryExists(aDir.c_str()); aRND++) {
769 //aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
770 aTmpDir.insert((aTmpDir.length() - 1), oss.str()); //Build a unique directory name
771 std::cerr << "Tempory directory exist, changing name ..." << std::endl;
772 std::cerr << "temp dir .... : " << aTmpDir << std::endl;
773 aDir = std::string(aTmpDir);
774 // aPath = OSD_Path(aTmpDir);
775 // aDir = OSD_Directory(aPath);
778 //OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
779 //aDir.Build(aProtection);
780 createDirectory(aDir.c_str());
781 //int length = strlen(aTmpDir.ToCString());
782 int length = strlen(aTmpDir.c_str());
783 char *new_str = new char[ 1+length ];
784 //strcpy(new_str , aTmpDir.ToCString());
785 strcpy(new_str , aTmpDir.c_str());
789 char* makeName(char* name)
791 //TCollection_AsciiString aName(name), aNewName;
792 std::string aName(name), aNewName;
793 //Standard_Integer i, length = aName.Length();
794 int i, length = aName.length();
795 char replace = (char)19;
797 for(i=1; i<=length; i++) {
798 //if(aName.Value(i) == ' ') aNewName+=replace;
799 //else aNewName += aName.Value(i);
800 if(aName[i] == ' ') aNewName+=replace;
801 else aNewName += aName[i];
804 //length = strlen(aNewName.ToCString());
805 length = strlen(aNewName.c_str());
806 char *new_str = new char[ 1+length ];
807 //strcpy(new_str , aNewName.ToCString()) ;
808 strcpy(new_str , aNewName.c_str()) ;
812 char* restoreName(char* name)
814 //TCollection_AsciiString aName(name), aNewName;
815 std::string aName(name), aNewName;
816 //Standard_Integer i, length = aName.Length();
817 int i, length = aName.length();
818 char replace = (char)19;
820 for(i=1; i<=length; i++) {
821 //if(aName.Value(i) == replace) aNewName+=' ';
822 //else aNewName += aName.Value(i);
823 if(aName[i] == replace) aNewName+=' ';
824 else aNewName += aName[i];
827 //length = strlen(aNewName.ToCString());
828 length = strlen(aNewName.c_str());
829 char *new_str = new char[ 1+length ];
830 //strcpy(new_str , aNewName.ToCString()) ;
831 strcpy(new_str , aNewName.c_str()) ;
835 void write_float64(FILE* fp, hdf_float64* value)
837 unsigned char* array = (unsigned char*)value;
838 for(int i = 0; i < sizeof(hdf_float64); i++) {
839 unsigned tmp = (unsigned short)array[i];
840 fprintf(fp, " %2x", tmp);
844 void read_float64(FILE* fp, hdf_float64* value)
846 unsigned char* array = (unsigned char*)value;
847 for(int i = 0; i < sizeof(hdf_float64); i++) {
849 fscanf(fp, " %x", &tmp);
850 array[i] = (unsigned char)tmp;
854 bool directoryExists(const char* dir)
856 DIR* dhandle = opendir(dir);
869 bool createDirectory(const char* dir)
871 return mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0;
874 bool is_readable( const std::string & file )
876 std::ifstream fichier( file.c_str() );
877 return !fichier.fail();