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