]> SALOME platform Git repositories - modules/kernel.git/blob - src/HDFPersist/HDFascii.cc
Salome HOME
Ajout des properties
[modules/kernel.git] / src / HDFPersist / HDFascii.cc
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File      : SALOMEDS_Tool.cxx
21 //  Created   : Mon Oct 21 16:24:34 2002
22 //  Author    : Sergey RUIN
23
24 //  Project   : SALOME
25 //  Module    : SALOMEDS
26
27 #include "HDFOI.hxx"
28
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> 
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39
40 #ifdef WNT
41 #include <io.h>
42 #include <time.h>
43 #endif
44
45 using namespace std;
46
47 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp);
48 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp);
49 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp);
50
51 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident);
52 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident);
53 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident);
54
55 char* GetTmpDir();
56 char* makeName(char* name);
57 char* restoreName(char* name);
58 void write_float64(FILE* fp, hdf_float64* value);
59 void read_float64(FILE* fp, hdf_float64* value);
60
61 #define MAX_STRING_SIZE   65535
62 #define MAX_ID_SIZE       20
63 #define NB_FLOAT_IN_ROW   3
64 #define NB_INTEGER_IN_ROW 9
65
66 #define ASCIIHDF_ID  "ASCIIHDF"
67 #define ATTRIBUTE_ID "ATTRIBUTE"
68 #define DATASET_ID   "DATASET"
69 #define GROUP_ID     "GROUP"
70
71 #define ASCIIHDF_ID_END  "ASCIIHDF_END"
72 #define ATTRIBUTE_ID_END "ATTRIBUTE_END"
73 #define DATASET_ID_END   "DATASET_END"
74 #define GROUP_ID_END     "GROUP_END"
75
76
77 //============================================================================
78 // function : isASCII
79 // purpose  : Returns True if the file is a converted to ASCII HDF file
80 //============================================================================
81 bool HDFascii::isASCII(const char* thePath) {
82   int fd;
83   if(!(fd = open(thePath, O_RDONLY))) return false;
84   char* aBuffer = new char[9];
85   aBuffer[8] = (char)0;
86   read(fd, aBuffer, 8); 
87   close(fd);
88
89   if(strcmp(aBuffer, ASCIIHDF_ID) == 0) return true;
90
91   return false;
92 }
93
94 //############################## HDF => ASCII ################################
95
96 //============================================================================
97 // function : ConvertFromHDFToASCII
98 // purpose  : Converts a file pointed by thePath to ASCII format
99 //            If isReplace is true the newly created file will replace the existent one.
100 //            If isReplace is false theExtension will be added to a created file name 
101 //            Returns the file name of ASCII file
102 //============================================================================
103 char* HDFascii::ConvertFromHDFToASCII(const char* thePath,
104                                       bool isReplace,
105                                       const char* theExtension)
106 {
107   TCollection_AsciiString aPath((char*)thePath);
108   if(!isReplace) { 
109     if(theExtension == NULL) aPath += ".asc";    
110     else aPath += (char*)theExtension;
111   }
112
113   TCollection_AsciiString aFileName(aPath);
114   if(isReplace) aFileName=aPath+".ascii_tmp";
115  
116   HDFfile *hdf_file = new HDFfile((char*)thePath); 
117   hdf_file->OpenOnDisk(HDF_RDONLY);
118
119   char name[HDF_NAME_MAX_LEN+1];
120   int nbsons = hdf_file->nInternalObjects(), nbAttr = hdf_file->nAttributes(); 
121
122   FILE* fp = fopen(aFileName.ToCString(), "w");
123   fprintf(fp, "%s\n", ASCIIHDF_ID);
124   fprintf(fp, "%i\n", nbsons+nbAttr);
125
126   for(unsigned j=0; j<nbAttr; j++) {
127     char* attr_name = hdf_file->GetAttributeName(j);
128     HDFattribute *hdf_attribute = new HDFattribute(attr_name, hdf_file);
129     delete attr_name;
130     SaveAttributeInASCIIfile(hdf_attribute, fp, 0);
131     hdf_attribute = 0;
132   }
133
134   for (Standard_Integer i=0; i<nbsons; i++) {
135     hdf_file->InternalObjectIndentify(i,name);
136     if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
137
138     hdf_object_type type = hdf_file->InternalObjectType(name);
139
140     if(type == HDF_DATASET) { 
141       HDFdataset* hdf_dataset = new HDFdataset(name, hdf_file);
142       SaveDatasetInASCIIfile(hdf_dataset, fp, 0);
143       hdf_dataset = 0; 
144     } else if(type == HDF_GROUP) {
145       HDFgroup *hdf_group = new HDFgroup(name, hdf_file); 
146       SaveGroupInASCIIfile(hdf_group, fp, 0);
147       hdf_group = 0;
148     }
149   }
150
151   fprintf(fp, "%s", ASCIIHDF_ID_END);
152
153   fclose(fp);
154
155   hdf_file->CloseOnDisk();
156   delete hdf_file;
157
158   if(isReplace) {
159     OSD_Path anOSDPath(aFileName);
160     OSD_File anOSDFile(anOSDPath);
161     if(anOSDFile.Exists())
162       anOSDFile.Move(aPath);
163     else 
164       return NULL;
165   }
166
167   int length = strlen(aPath.ToCString());
168   char *new_str = new char[ 1+length ];
169   strcpy(new_str , aPath.ToCString()) ;
170
171   return new_str;
172 }
173
174
175 //============================================================================
176 // function : SaveGroupInASCIIfile
177 // purpose  : 
178 //============================================================================
179 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident)
180 {
181   hdf_group->OpenOnDisk();
182
183   TCollection_AsciiString anIdent(ident, '\t');
184   int nbsons = hdf_group->nInternalObjects(), nbAttr = hdf_group->nAttributes(); 
185
186   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), GROUP_ID);*/
187   fprintf(fp, "%s\n", GROUP_ID);
188
189   char* name = makeName(hdf_group->GetName());
190
191   /*fprintf(fp, "%s%s %i\n", anIdent.ToCString(), name, nbsons+nbAttr);*/
192   fprintf(fp, "%s %i\n", name, nbsons+nbAttr);
193   delete name;
194
195   for(unsigned j=0; j<nbAttr; j++) {
196     name = hdf_group->GetAttributeName(j);
197     HDFattribute *hdf_attribute = new HDFattribute(name, hdf_group);
198     delete name;
199     SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
200     hdf_attribute = 0;
201   }
202
203   char objName[HDF_NAME_MAX_LEN+1];
204  
205   for (int i=0; i<nbsons; i++) {
206     hdf_group->InternalObjectIndentify(i, objName);
207
208     if (strncmp(objName, "INTERNAL_COMPLEX",16) == 0)  continue;
209
210     hdf_object_type type = hdf_group->InternalObjectType(objName);
211
212     if  (type == HDF_DATASET) {
213       HDFdataset* hdf_dataset = new HDFdataset(objName, hdf_group);
214       SaveDatasetInASCIIfile(hdf_dataset, fp, ident+1);
215       hdf_dataset = 0;
216     } else if (type == HDF_GROUP)   {      
217       HDFgroup *hdf_subgroup = new HDFgroup(objName, hdf_group);
218       SaveGroupInASCIIfile(hdf_subgroup, fp, ident+1);
219       hdf_subgroup = 0;
220     } 
221   }
222
223   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), GROUP_ID_END);*/
224   fprintf(fp, "%s\n", GROUP_ID_END);
225
226   hdf_group->CloseOnDisk();  
227 }
228
229 //============================================================================
230 // function : SaveDatasetInASCIIfile
231 // purpose  : 
232 //============================================================================
233 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
234 {
235   hdf_dataset->OpenOnDisk();
236
237   long size =  hdf_dataset->GetSize();
238   long ndim = hdf_dataset->nDim(); //Get number of dimesions
239   hdf_size *dim = new hdf_size[ndim];
240   hdf_type type = hdf_dataset->GetType();
241   hdf_byte_order order = hdf_dataset->GetOrder();
242   int nbAttr = hdf_dataset->nAttributes(); 
243
244   TCollection_AsciiString anIdent(ident, '\t');
245   TCollection_AsciiString anIdentChild(ident+1, '\t');
246
247   char* name = makeName(hdf_dataset->GetName());
248
249   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), DATASET_ID);*/
250   fprintf(fp, "%s\n", DATASET_ID);
251   /*fprintf(fp, "%s%s %i %i\n", anIdent.ToCString(), name, type, nbAttr);*/
252   fprintf(fp, "%s %i %i\n", name, type, nbAttr);
253   delete name;
254
255   hdf_dataset->GetDim(dim);
256   /*fprintf(fp, "%s %i\n", anIdentChild.ToCString(), ndim);*/
257   fprintf(fp, " %i\n", ndim);
258
259   for(int i = 0;i < ndim;i++) {
260     /*fprintf(fp, "%s%i",  anIdentChild.ToCString(), dim[i]);*/
261     fprintf(fp, " %i", dim[i]);
262   }
263
264   /*fprintf(fp, "%s\n", anIdentChild.ToCString());*/
265   fprintf(fp, "\n");
266   delete dim;
267
268   /*fprintf(fp, "%s%li:", anIdentChild.ToCString(), size);*/
269 //   fprintf(fp, "%li:", size);
270   fprintf(fp, "%li %i:", size, order);
271
272   if (type == HDF_STRING) {     
273     char* val = new char[size];
274     hdf_dataset->ReadFromDisk(val);
275     fwrite(val, 1, size, fp);
276     delete val;
277   } else if (type == HDF_FLOAT64) {
278     hdf_float64* val = new hdf_float64[size];
279     hdf_dataset->ReadFromDisk(val);
280     fprintf(fp, "\n");
281     for (int i = 0, j = 0; i < size; i++) {
282       write_float64(fp, &val[i]);
283       if(++j == NB_FLOAT_IN_ROW) {
284         fprintf(fp, "\n");
285         j = 0;
286       }
287       else fprintf(fp,"  ");
288     }
289     delete val;
290   } else if(type == HDF_INT64) {
291     hdf_int64* val = new hdf_int64[size];
292     hdf_dataset->ReadFromDisk(val);
293     fprintf(fp, "\n");
294     for (int i = 0, j = 0; i < size; i++) {
295       fprintf(fp, " %li", val[i]);
296       if(++j == NB_INTEGER_IN_ROW) {
297         fprintf(fp, "\n");
298         j = 0;
299       }
300     }
301     delete val;
302   } else if(type == HDF_INT32) {
303     hdf_int32* val = new hdf_int32[size];
304     hdf_dataset->ReadFromDisk(val);
305     fprintf(fp, "\n");
306     for (int i = 0, j = 0; i < size; i++) {
307       fprintf(fp, " %i", val[i]);
308       if(++j == NB_INTEGER_IN_ROW) {
309         fprintf(fp, "\n");
310         j = 0;
311       }
312     }
313     delete val;
314   }
315   
316   fprintf(fp, "\n");
317
318 #ifndef WNT
319   for(unsigned j=0; j<nbAttr; j++) {
320 #else
321   for(j=0; j<nbAttr; j++) {
322 #endif
323     name = hdf_dataset->GetAttributeName(j);
324     HDFattribute *hdf_attribute = new HDFattribute(name, hdf_dataset);
325     delete name;
326     SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
327     hdf_attribute = 0;
328   }
329
330   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), DATASET_ID_END); */
331   fprintf(fp, "%s\n", DATASET_ID_END);
332
333   hdf_dataset->CloseOnDisk(); 
334 }
335
336
337 //============================================================================
338 // function : SaveAttributeInASCIIfile
339 // purpose  : 
340 //============================================================================
341 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident)
342 {
343   hdf_attribute->OpenOnDisk();
344
345   hdf_type type = hdf_attribute->GetType();
346
347   TCollection_AsciiString anIdent(ident, '\t');
348   TCollection_AsciiString anIdentChild(ident+1, '\t');
349
350   char* name = makeName(hdf_attribute->GetName());
351   int size = hdf_attribute->GetSize();
352
353   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), ATTRIBUTE_ID);*/
354   fprintf(fp, "%s\n", ATTRIBUTE_ID);
355   /*fprintf(fp, "%s%s %i %i\n", anIdent.ToCString(), name, type, size);*/
356   fprintf(fp, "%s %i %i\n", name, type, size);
357
358   delete name;
359
360   if (type == HDF_STRING) {    
361     char* val = new char[size+1];
362     hdf_attribute->ReadFromDisk(val);
363     /*fprintf(fp, "%s:", anIdentChild.ToCString());*/
364     fprintf(fp, ":");
365     fwrite(val, 1, size, fp);
366     fprintf(fp, "\n");
367     delete val;
368   } else if (type == HDF_FLOAT64) {
369     hdf_float64 val;
370     hdf_attribute->ReadFromDisk(&val);
371     /*fprintf(fp, "%s",  anIdentChild.ToCString());*/
372     write_float64(fp, &val);
373     fprintf(fp, "\n");
374   } else if(type == HDF_INT64) {
375     hdf_int64 val;
376     hdf_attribute->ReadFromDisk(&val);
377     /*fprintf(fp, "%s%li \n", anIdentChild.ToCString(), val);*/
378     fprintf(fp, "%li \n", val);
379   } else if(type == HDF_INT32) {
380     hdf_int32 val;
381     hdf_attribute->ReadFromDisk(&val);
382     /*fprintf(fp, "%s%i \n", anIdentChild.ToCString(), val);*/
383     fprintf(fp, "%i \n", val);
384   }
385
386   /*fprintf(fp, "%s%s\n", anIdent.ToCString(), ATTRIBUTE_ID_END);*/
387   fprintf(fp, "%s\n", ATTRIBUTE_ID_END);
388
389   hdf_attribute->CloseOnDisk();  
390 }
391
392 //############################## ASCII => HDF ################################
393
394 //============================================================================
395 // function : ConvertFromASCIIToHDF
396 // purpose  : Converts a file pointed by thePath to HDF format
397 //            Returns a name of directory where a created HDF file is placed
398 //            The created file is named "hdf_from_ascii.hdf"
399 //============================================================================
400 char* HDFascii::ConvertFromASCIIToHDF(const char* thePath)
401 {
402   // Get a temporary directory to store a file
403   TCollection_AsciiString aTmpDir = GetTmpDir(), aFileName("hdf_from_ascii.hdf");
404   // Build a full file name of temporary file
405   TCollection_AsciiString aFullName = aTmpDir + aFileName;
406
407   HDFfile *hdf_file = new HDFfile(aFullName.ToCString()); 
408   hdf_file->CreateOnDisk();
409   
410   FILE *fp = fopen(thePath, "r");
411   if(!fp) return NULL;
412
413   char type[9];
414   int nbsons, i;
415   fscanf(fp, "%s", type);
416   fscanf(fp, "%i",&nbsons);
417
418   if(strcmp(type, ASCIIHDF_ID) != 0) return NULL;
419
420   for(i = 0; i < nbsons; i++) {
421     char id_of_begin[MAX_ID_SIZE];
422     fscanf(fp, "%s\n", id_of_begin);
423
424     if(strcmp(id_of_begin, GROUP_ID) == 0) {
425       if(!CreateGroupFromASCII(hdf_file, fp)) {
426         cout << "ConvertFromASCIIToHDF : Can not create group number " << i << endl;
427         return NULL;
428       }
429     }
430     else if(strcmp(id_of_begin, DATASET_ID) == 0) {
431       if(!CreateDatasetFromASCII(hdf_file, fp)) {
432         cout << "ConvertFromASCIIToHDF :Can not create dataset number " << i << endl;
433         return NULL;
434       }
435     }
436     else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
437       if(!CreateAttributeFromASCII(hdf_file, fp)) {
438         cout << "ConvertFromASCIIToHDF :Can not create attribute number " << i << endl;
439         return NULL;
440       }
441     }
442     else 
443       cout << "ConvertFromASCIIToHDF : Unrecognized type " << id_of_begin << endl; 
444   }
445
446   char id_of_end[MAX_ID_SIZE];
447   fscanf(fp, "%s", id_of_end);
448   if(strcmp(id_of_end, ASCIIHDF_ID_END) != 0) {
449     cout << "ConvertFromASCIIToHDF : Can not find the end ASCII token " << endl;
450     return false;  
451   }
452
453   hdf_file->CloseOnDisk();
454   delete hdf_file;
455
456   int length = strlen(aTmpDir.ToCString());
457   char *new_str = new char[ 1+length ];
458   strcpy(new_str , aTmpDir.ToCString()) ;
459
460   return new_str;
461 }
462
463
464 //============================================================================
465 // function : CreateGroupFromASCII
466 // purpose  : Creates a HDF group from a set attributes situated under theLabel
467 //============================================================================
468 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp)
469 {
470   char name[HDF_NAME_MAX_LEN+1];
471   int nbsons, i;
472   fscanf(fp, "%s %i\n", name, &nbsons);  
473
474   char* new_name = restoreName(name);
475
476   HDFgroup* hdf_group = new HDFgroup(new_name, father);
477
478   delete new_name;
479
480   hdf_group->CreateOnDisk();
481
482   for(i = 0; i < nbsons; i++) {
483     char id_of_begin[MAX_ID_SIZE];
484     fscanf(fp, "%s\n", id_of_begin);
485     
486     if(strcmp(id_of_begin, GROUP_ID) == 0) {
487       if(!CreateGroupFromASCII(hdf_group, fp)) {
488         cout << "Can not create subgroup " << i << " for group " << name << endl;
489         return false;
490       }
491     }
492     else if(strcmp(id_of_begin, DATASET_ID) == 0) {
493       if(!CreateDatasetFromASCII(hdf_group, fp)) {
494         cout << "Can not create dataset " << i << " for group " << name << endl;
495         return false;
496       }
497     }
498     else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
499       if(!CreateAttributeFromASCII(hdf_group, fp)) {
500         cout << "Can not create attribute " << i << " for group " << name << endl;
501         return false;
502       }
503     }
504     else 
505       cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << endl; 
506   }
507   
508   hdf_group->CloseOnDisk();
509   hdf_group = 0; //will be deleted by father destructor
510
511   char id_of_end[MAX_ID_SIZE];
512   fscanf(fp, "%s\n", id_of_end);
513   if(strcmp(id_of_end, GROUP_ID_END) != 0) {
514     cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << endl;
515     return false;
516   }
517
518   return true;
519 }
520
521
522 //============================================================================
523 // function : CreateDatasetFromASCII
524 // purpose  : Creates a HDF dataset from a set attributes situated under theLabel
525 //============================================================================
526 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
527 {
528   char name[HDF_NAME_MAX_LEN+1];
529   hdf_type type;
530   hdf_byte_order order;
531   int nbDim, nbAttr;
532   long i, size;
533
534   fscanf(fp, "%s %i %i\n", name, &type, &nbAttr);
535   char* new_name = restoreName(name);
536
537   fscanf(fp, "%i\n", &nbDim);
538
539   hdf_size* sizeArray = new hdf_size[nbDim];
540   int dim = 0;
541   for(i = 0; i<nbDim; i++) {
542     fscanf(fp, "%i\n", &dim);
543     sizeArray[i] = dim;
544   }
545  
546   // order (2-d member) was not written in earlier versions
547   char tmp;
548   int nbRead = fscanf(fp, "%li %i%c", &size, &order, &tmp);
549   if ( nbRead < 2 ) { // fscanf stops before ":"
550     fscanf(fp, "%c", &tmp);
551     order = H5T_ORDER_NONE;
552   }
553   if ( type != HDF_FLOAT64 )  // use order only for FLOAT64
554     order = H5T_ORDER_NONE;
555
556   HDFdataset* hdf_dataset = new HDFdataset(new_name, father,type, sizeArray, nbDim, order);
557   delete new_name;
558   delete sizeArray;
559
560   hdf_dataset->CreateOnDisk();
561
562   if (type == HDF_STRING) {     
563     char *val = new char[size+1];
564     fread(val, 1, size, fp);
565     hdf_dataset->WriteOnDisk(val);
566     delete val;
567   } else if (type == HDF_FLOAT64) {
568     hdf_float64* val = new hdf_float64[size];
569     for(i=0; i<size; i++) {
570       read_float64(fp, &(val[i]));
571     }
572     hdf_dataset->WriteOnDisk(val);
573     delete val;
574   } else if(type == HDF_INT64) {
575     hdf_int64* val = new hdf_int64[size];
576     for(i=0; i<size; i++) {
577       fscanf(fp, " %li", &(val[i]));
578     }
579     hdf_dataset->WriteOnDisk(val);
580     delete val;
581   } else if(type == HDF_INT32) {
582     hdf_int32* val = new hdf_int32[size];
583     for(i=0; i<size; i++) {
584       fscanf(fp, " %i", &(val[i]));
585     }
586     hdf_dataset->WriteOnDisk(val);
587     delete val;
588   }
589
590   char token[MAX_ID_SIZE];
591
592   for(i = 0; i < nbAttr; i++) {
593     fscanf(fp, "%s\n", token);
594     
595     if(strcmp(token, ATTRIBUTE_ID) == 0) {
596       if(!CreateAttributeFromASCII(hdf_dataset, fp)) {
597         cout << "Can not create attribute " << i << " for dataset " << name << endl;
598         return false;
599       }
600     }
601     else {
602       cout << "CreateGroupFromASCII : Unrecognized type " << token << endl; 
603       return false;
604     }
605   }
606   
607   fscanf(fp, "%s\n", token);
608   if(strcmp(token, DATASET_ID_END) != 0) {
609     cout << "CreateDatasetFromASCII : Invalid end token : " << token << endl;
610     return false;
611   }
612
613   hdf_dataset->CloseOnDisk();
614   hdf_dataset = 0; //will be deleted by father destructor
615
616   return true;
617 }
618
619
620 //============================================================================
621 // function : CreateAttributeFromASCII
622 // purpose  : Creates a HDF attribute from a set attributes situated under theLabel
623 //============================================================================
624 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp)
625 {
626   char name[HDF_NAME_MAX_LEN+1];
627
628   hdf_type type;
629   int size;
630   fscanf(fp, "%s %i %i\n", name, &type, &size);
631   char* new_name = restoreName(name);
632   HDFattribute* hdf_attribute = new HDFattribute(new_name, father, type, size);
633
634   hdf_attribute->CreateOnDisk();
635
636   delete new_name;
637   
638   if (type == HDF_STRING) {     
639     char tmp;
640     fscanf(fp, "%c", &tmp);
641     char *val = new char[size+1];
642     val[size] = (char)0;
643     fread(val, 1, size, fp);
644     hdf_attribute->WriteOnDisk(val);
645     delete val;
646   } else if (type == HDF_FLOAT64) {
647     hdf_float64 val;
648     read_float64(fp, &val);
649     hdf_attribute->WriteOnDisk(&val);
650   } else if(type == HDF_INT64) {
651     hdf_int64 val;
652     fscanf(fp, "%li", &val);
653     hdf_attribute->WriteOnDisk(&val);
654   } else if(type == HDF_INT32) {
655     hdf_int32 val;
656     fscanf(fp, "%i", &val);
657     hdf_attribute->WriteOnDisk(&val);
658   }
659   
660   hdf_attribute->CloseOnDisk();
661   hdf_attribute = 0; //will be deleted by father destructor
662
663
664   char id_of_end[MAX_ID_SIZE];
665   fscanf(fp, "%s\n", id_of_end);
666   if(strcmp(id_of_end, ATTRIBUTE_ID_END) != 0) {
667     cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << endl;
668     return false;
669   }
670
671   return true;
672 }
673
674
675 //============================================================================
676 // function : GetTempDir
677 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
678 //============================================================================ 
679 char* GetTmpDir()
680 {
681   //Find a temporary directory to store a file
682
683   TCollection_AsciiString aTmpDir;
684
685 #ifdef WNT
686   char *aTmp;
687   aTmp = getenv("TMP");
688   if(aTmp != NULL)
689         aTmpDir = TCollection_AsciiString(aTmp);
690   else
691         aTmpDir = TCollection_AsciiString("C:\\");
692 #else
693   aTmpDir = TCollection_AsciiString("/tmp/");
694 #endif
695
696   srand((unsigned int)time(NULL));
697
698   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
699   TCollection_AsciiString aSubDir(aRND);
700   if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
701
702   aTmpDir += aSubDir; //Get RND sub directory
703
704 #ifdef WIN32
705   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
706 #else
707   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
708 #endif
709
710   OSD_Path aPath(aTmpDir);
711   OSD_Directory aDir(aPath);
712
713   for(aRND = 0; aDir.Exists(); aRND++) {
714     aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND));  //Build a unique directory name
715     aPath = OSD_Path(aTmpDir);
716     aDir = OSD_Directory(aPath);
717   }
718
719   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
720   aDir.Build(aProtection);
721
722   int length = strlen(aTmpDir.ToCString());
723   char *new_str = new char[ 1+length ];
724   strcpy(new_str , aTmpDir.ToCString());
725
726   return new_str;
727 }
728
729 char* makeName(char* name)
730 {
731   TCollection_AsciiString aName(name), aNewName;
732   Standard_Integer i, length = aName.Length();
733   char replace = (char)19;
734
735   for(i=1; i<=length; i++) {
736     if(aName.Value(i) == ' ') aNewName+=replace;
737     else aNewName += aName.Value(i);
738   }
739
740   length = strlen(aNewName.ToCString());
741   char *new_str = new char[ 1+length ];
742   strcpy(new_str , aNewName.ToCString()) ;
743   return new_str;
744 }
745
746 char* restoreName(char* name)
747 {
748   TCollection_AsciiString aName(name), aNewName;
749   Standard_Integer i, length = aName.Length();
750   char replace = (char)19;
751
752   for(i=1; i<=length; i++) {
753     if(aName.Value(i) == replace) aNewName+=' ';
754     else aNewName += aName.Value(i);
755   }
756
757   length = strlen(aNewName.ToCString());
758   char *new_str = new char[ 1+length ];
759   strcpy(new_str , aNewName.ToCString()) ;
760   return new_str;
761 }
762
763 void write_float64(FILE* fp, hdf_float64* value)
764 {
765   unsigned char* array = (unsigned char*)value;
766   for(int i = 0; i < sizeof(hdf_float64); i++) {
767     unsigned tmp = (unsigned short)array[i];
768     fprintf(fp, " %2x", tmp);
769   }
770 }
771
772 void read_float64(FILE* fp, hdf_float64* value)
773 {
774   unsigned char* array = (unsigned char*)value;
775   for(int i = 0; i < sizeof(hdf_float64); i++) {
776     unsigned tmp;
777     fscanf(fp, " %x", &tmp); 
778     array[i] = (unsigned char)tmp;
779   }
780 }