Back to index

salome-kernel  6.5.0
HDFascii.cc
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File      : SALOMEDS_Tool.cxx
00024 //  Created   : Mon Oct 21 16:24:34 2002
00025 //  Author    : Sergey RUIN
00026 //  Project   : SALOME
00027 //  Module    : SALOMEDS
00028 //
00029 #include "HDFOI.hxx"
00030 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <fcntl.h>
00034 #include <stdio.h>
00035 #include <string>
00036 
00037 #ifdef WIN32
00038 #include <io.h>
00039 #include <time.h>
00040 #include <windows.h>
00041 #define open _open
00042 #define read _read
00043 #define close _close
00044 #define dir_separator '\\'
00045 #else
00046 #define dir_separator '/'
00047 #endif
00048 
00049 void Move(const std::string& fName, const std::string& fNameDst);
00050 bool Exists(const std::string thePath); 
00051 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp);
00052 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp);
00053 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp);
00054 
00055 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident);
00056 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident);
00057 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident);
00058 
00059 std::string GetTmpDir();
00060 char* makeName(char* name);
00061 char* restoreName(char* name);
00062 void write_float64(FILE* fp, hdf_float64* value);
00063 void read_float64(FILE* fp, hdf_float64* value);
00064 
00065 void WriteSimpleData( FILE* fp, HDFdataset *hdf_dataset, hdf_type type, long size );
00066 
00067 #define MAX_STRING_SIZE   65535
00068 #define MAX_ID_SIZE       20
00069 #define NB_FLOAT_IN_ROW   3
00070 #define NB_INTEGER_IN_ROW 9
00071 
00072 #define ASCIIHDF_ID  "ASCIIHDF"
00073 #define ATTRIBUTE_ID "ATTRIBUTE"
00074 #define DATASET_ID   "DATASET"
00075 #define GROUP_ID     "GROUP"
00076 
00077 #define ASCIIHDF_ID_END  "ASCIIHDF_END"
00078 #define ATTRIBUTE_ID_END "ATTRIBUTE_END"
00079 #define DATASET_ID_END   "DATASET_END"
00080 #define GROUP_ID_END     "GROUP_END"
00081 
00082 
00083 //============================================================================
00084 // function : isASCII
00085 // purpose  : Returns True if the file is a converted to ASCII HDF file
00086 //============================================================================
00087 bool HDFascii::isASCII(const char* thePath) {
00088   int fd;
00089   if(!(fd = open(thePath, O_RDONLY))) return false;
00090   char* aBuffer = new char[9];
00091   aBuffer[8] = (char)0;
00092   read(fd, aBuffer, 8); 
00093   close(fd);
00094 
00095   if(strcmp(aBuffer, ASCIIHDF_ID) == 0) return true;
00096 
00097   return false;
00098 }
00099 
00100 //############################## HDF => ASCII ################################
00101 
00102 //============================================================================
00103 // function : ConvertFromHDFToASCII
00104 // purpose  : Converts a file pointed by thePath to ASCII format
00105 //            If isReplace is true the newly created file will replace the existent one.
00106 //            If isReplace is false theExtension will be added to a created file name 
00107 //            Returns the file name of ASCII file
00108 //============================================================================
00109 char* HDFascii::ConvertFromHDFToASCII(const char* thePath,
00110                                   bool isReplace,
00111                                    const char* theExtension)
00112 {
00113   std::string aPath(thePath);
00114   if(!isReplace) { 
00115     if(theExtension == NULL) aPath += ".asc";    
00116     else aPath += (char*)theExtension;
00117   }
00118 
00119   std::string aFileName(aPath);
00120   if(isReplace) aFileName=aPath+".ascii_tmp";
00121  
00122   HDFfile *hdf_file = new HDFfile((char*)thePath); 
00123   hdf_file->OpenOnDisk(HDF_RDONLY);
00124 
00125   char name[HDF_NAME_MAX_LEN+1];
00126   int nbsons = hdf_file->nInternalObjects(), nbAttr = hdf_file->nAttributes(); 
00127 
00128   FILE* fp = fopen(aFileName.c_str(), "wb");
00129   fprintf(fp, "%s\n", ASCIIHDF_ID);
00130   fprintf(fp, "%i\n", nbsons+nbAttr);
00131 
00132   for(int j=0; j<nbAttr; j++) {
00133     char* attr_name = hdf_file->GetAttributeName(j);
00134     HDFattribute *hdf_attribute = new HDFattribute(attr_name, hdf_file);
00135     delete attr_name;
00136     SaveAttributeInASCIIfile(hdf_attribute, fp, 0);
00137     hdf_attribute = 0;
00138   }
00139 
00140   for (int i=0; i<nbsons; i++) {
00141     hdf_file->InternalObjectIndentify(i,name);
00142     if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
00143 
00144     hdf_object_type type = hdf_file->InternalObjectType(name);
00145 
00146     if(type == HDF_DATASET) { 
00147       HDFdataset* hdf_dataset = new HDFdataset(name, hdf_file);
00148       SaveDatasetInASCIIfile(hdf_dataset, fp, 0);
00149       hdf_dataset = 0; 
00150     } else if(type == HDF_GROUP) {
00151       HDFgroup *hdf_group = new HDFgroup(name, hdf_file); 
00152       SaveGroupInASCIIfile(hdf_group, fp, 0);
00153       hdf_group = 0;
00154     }
00155   }
00156 
00157   fprintf(fp, "%s", ASCIIHDF_ID_END);
00158 
00159   fclose(fp);
00160 
00161   hdf_file->CloseOnDisk();
00162   delete hdf_file;
00163 
00164   if(isReplace) {
00165     if(Exists(aFileName))
00166       Move(aFileName, aPath);
00167     else 
00168       return NULL;
00169   }
00170 
00171   int length = strlen(aPath.c_str());
00172   char *new_str = new char[ 1+length ];
00173   strcpy(new_str , aPath.c_str()) ;
00174 
00175   return new_str;
00176 }
00177 
00178 
00179 //============================================================================
00180 // function : SaveGroupInASCIIfile
00181 // purpose  : 
00182 //============================================================================
00183 void SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident)
00184 {
00185   hdf_group->OpenOnDisk();
00186 
00187   int nbsons = hdf_group->nInternalObjects(), nbAttr = hdf_group->nAttributes(); 
00188 
00189   fprintf(fp, "%s\n", GROUP_ID);
00190 
00191   char* name = makeName(hdf_group->GetName());
00192 
00193   fprintf(fp, "%s %i\n", name, nbsons+nbAttr);
00194   delete [] name;
00195 
00196   for(int j=0; j<nbAttr; j++) {
00197     name = hdf_group->GetAttributeName(j);
00198     HDFattribute *hdf_attribute = new HDFattribute(name, hdf_group);
00199     delete [] name;
00200     SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
00201     hdf_attribute = 0;
00202   }
00203 
00204   char objName[HDF_NAME_MAX_LEN+1];
00205  
00206   for (int i=0; i<nbsons; i++) {
00207     hdf_group->InternalObjectIndentify(i, objName);
00208 
00209     if (strncmp(objName, "INTERNAL_COMPLEX",16) == 0)  continue;
00210 
00211     hdf_object_type type = hdf_group->InternalObjectType(objName);
00212 
00213     if  (type == HDF_DATASET) {
00214       HDFdataset* hdf_dataset = new HDFdataset(objName, hdf_group);
00215       SaveDatasetInASCIIfile(hdf_dataset, fp, ident+1);
00216       hdf_dataset = 0;
00217     } else if (type == HDF_GROUP)   {      
00218       HDFgroup *hdf_subgroup = new HDFgroup(objName, hdf_group);
00219       SaveGroupInASCIIfile(hdf_subgroup, fp, ident+1);
00220       hdf_subgroup = 0;
00221     } 
00222   }
00223 
00224   fprintf(fp, "%s\n", GROUP_ID_END);
00225 
00226   hdf_group->CloseOnDisk();  
00227 }
00228 
00229 //============================================================================
00230 // function : SaveDatasetInASCIIfile
00231 // purpose  : 
00232 //============================================================================
00233 void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
00234 {
00235   hdf_dataset->OpenOnDisk();
00236 
00237   long size =  (long) hdf_dataset->GetSize();
00238   long ndim = hdf_dataset->nDim(); //Get number of dimesions
00239   hdf_size *dim = new hdf_size[ndim];
00240   hdf_type type = hdf_dataset->GetType();
00241   hdf_byte_order order = hdf_dataset->GetOrder();
00242   int nbAttr = hdf_dataset->nAttributes(); 
00243 
00244   char* name = makeName(hdf_dataset->GetName());
00245 
00246   fprintf(fp, "%s\n", DATASET_ID);
00247   fprintf(fp, "%s %i %i\n", name, type, nbAttr);
00248   delete [] name;
00249 
00250   hdf_dataset->GetDim(dim);
00251   fprintf(fp, " %i\n", ndim);
00252 
00253   for(int i = 0;i < ndim;i++) {
00254     fprintf(fp, " %i", dim[i]);
00255   }
00256 
00257   fprintf(fp, "\n");
00258   delete [] dim;
00259 
00260   fprintf(fp, "%li %i:", size, order);
00261   if( type == HDF_ARRAY ) {
00262     HDFarray *array = new HDFarray(hdf_dataset);
00263     hdf_type data_type = array->GetDataType();
00264     fprintf(fp, "\n" );
00265     fprintf(fp, " %i\n", data_type ); //Write array data type
00266 
00267     //Write nDim of the array
00268     int arr_ndim = array->nDim();
00269     fprintf(fp, " %i\n", arr_ndim);
00270     hdf_size *arr_dim = new hdf_size[arr_ndim];
00271     array->GetDim(arr_dim);
00272 
00273     for( int i = 0;i < arr_ndim; i++ ) {
00274       fprintf(fp, " %i", arr_dim[i]);
00275     }
00276         
00277     //And write the data array
00278     WriteSimpleData( fp, hdf_dataset, data_type, size);
00279   } else {
00280     WriteSimpleData( fp, hdf_dataset, type, size);
00281   }
00282   
00283   fprintf(fp, "\n");
00284 
00285   for ( int j=0; j<nbAttr; j++ )
00286   {
00287     name = hdf_dataset->GetAttributeName(j);
00288     HDFattribute *hdf_attribute = new HDFattribute(name, hdf_dataset);
00289     delete [] name;
00290     SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
00291     hdf_attribute = 0;
00292   }
00293 
00294   fprintf(fp, "%s\n", DATASET_ID_END);
00295 
00296   hdf_dataset->CloseOnDisk(); 
00297 }
00298 
00299 
00300 //============================================================================
00301 // function : SaveAttributeInASCIIfile
00302 // purpose  : 
00303 //============================================================================
00304 void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident)
00305 {
00306   hdf_attribute->OpenOnDisk();
00307 
00308   hdf_type type = hdf_attribute->GetType();
00309 
00310   char* name = makeName(hdf_attribute->GetName());
00311   int size = hdf_attribute->GetSize();
00312 
00313   fprintf(fp, "%s\n", ATTRIBUTE_ID);
00314   fprintf(fp, "%s %i %i\n", name, type, size);
00315 
00316   delete [] name;
00317 
00318   if (type == HDF_STRING) {    
00319     char* val = new char[size+1];
00320     hdf_attribute->ReadFromDisk(val);
00321     fprintf(fp, ":");
00322     fwrite(val, 1, size, fp);
00323     fprintf(fp, "\n");
00324     delete [] val;
00325   } else if (type == HDF_FLOAT64) {
00326     hdf_float64 val;
00327     hdf_attribute->ReadFromDisk(&val);
00328     write_float64(fp, &val);
00329     fprintf(fp, "\n");
00330   } else if(type == HDF_INT64) {
00331     hdf_int64 val;
00332     hdf_attribute->ReadFromDisk(&val);
00333     fprintf(fp, "%li \n", val);
00334   } else if(type == HDF_INT32) {
00335     hdf_int32 val;
00336     hdf_attribute->ReadFromDisk(&val);
00337     fprintf(fp, "%i \n", val);
00338   }
00339 
00340   fprintf(fp, "%s\n", ATTRIBUTE_ID_END);
00341 
00342   hdf_attribute->CloseOnDisk();  
00343 }
00344 
00345 //############################## ASCII => HDF ################################
00346 
00347 //============================================================================
00348 // function : ConvertFromASCIIToHDF
00349 // purpose  : Converts a file pointed by thePath to HDF format
00350 //            Returns a name of directory where a created HDF file is placed
00351 //            The created file is named "hdf_from_ascii.hdf"
00352 //============================================================================
00353 char* HDFascii::ConvertFromASCIIToHDF(const char* thePath,
00354                                   bool isReplace)
00355 {
00356   std::string aTmpDir, aFullName;
00357   if(!isReplace) {
00358     // Get a temporary directory to store a file
00359     aTmpDir = GetTmpDir();
00360     // Build a full file name of temporary file
00361     aFullName = aTmpDir + "hdf_from_ascii.hdf";
00362   }
00363   else {
00364     aTmpDir = thePath;
00365     aFullName = std::string(thePath)+".ascii_tmp";
00366   }
00367 
00368   FILE *fp = fopen(thePath, "rb");
00369   if(!fp) return NULL;
00370 
00371   HDFfile *hdf_file = new HDFfile((char*)aFullName.c_str()); 
00372   hdf_file->CreateOnDisk();
00373   
00374   char type[9];
00375   int nbsons, i;
00376   fscanf(fp, "%s", type);
00377   fscanf(fp, "%i",&nbsons);
00378 
00379   if(strcmp(type, ASCIIHDF_ID) != 0) return NULL;
00380 
00381   for(i = 0; i < nbsons; i++) {
00382     char id_of_begin[MAX_ID_SIZE];
00383     fscanf(fp, "%s\n", id_of_begin);
00384 
00385     if(strcmp(id_of_begin, GROUP_ID) == 0) {
00386       if(!CreateGroupFromASCII(hdf_file, fp)) {
00387        std::cout << "ConvertFromASCIIToHDF : Can not create group number " << i << std::endl;
00388        return NULL;
00389       }
00390     }
00391     else if(strcmp(id_of_begin, DATASET_ID) == 0) {
00392       if(!CreateDatasetFromASCII(hdf_file, fp)) {
00393        std::cout << "ConvertFromASCIIToHDF :Can not create dataset number " << i << std::endl;
00394        return NULL;
00395       }
00396     }
00397     else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
00398       if(!CreateAttributeFromASCII(hdf_file, fp)) {
00399        std::cout << "ConvertFromASCIIToHDF :Can not create attribute number " << i << std::endl;
00400        return NULL;
00401       }
00402     }
00403     else 
00404       std::cout << "ConvertFromASCIIToHDF : Unrecognized type " << id_of_begin << std::endl; 
00405   }
00406 
00407   char id_of_end[MAX_ID_SIZE];
00408   fscanf(fp, "%s", id_of_end);
00409   if(strcmp(id_of_end, ASCIIHDF_ID_END) != 0) {
00410     std::cout << "ConvertFromASCIIToHDF : Can not find the end ASCII token " << std::endl;
00411     return false;  
00412   }
00413 
00414   hdf_file->CloseOnDisk();
00415   delete hdf_file;
00416 
00417   if(isReplace) {
00418     if(Exists(aFullName))
00419       Move(aFullName, thePath);
00420     else 
00421       return NULL;
00422   }
00423 
00424   int length = strlen(aTmpDir.c_str());
00425   char *new_str = new char[ 1+length ];
00426   strcpy(new_str , aTmpDir.c_str()) ;
00427 
00428   fclose(fp);
00429 
00430   return new_str;
00431 }
00432 
00433 
00434 //============================================================================
00435 // function : CreateGroupFromASCII
00436 // purpose  : Creates a HDF group from a set attributes situated under theLabel
00437 //============================================================================
00438 bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp)
00439 {
00440   char name[HDF_NAME_MAX_LEN+1];
00441   int nbsons, i;
00442   fscanf(fp, "%s %i\n", name, &nbsons);  
00443 
00444   char* new_name = restoreName(name);
00445 
00446   HDFgroup* hdf_group = new HDFgroup(new_name, father);
00447 
00448   delete [] new_name;
00449 
00450   hdf_group->CreateOnDisk();
00451 
00452   for(i = 0; i < nbsons; i++) {
00453     char id_of_begin[MAX_ID_SIZE];
00454     fscanf(fp, "%s\n", id_of_begin);
00455     
00456     if(strcmp(id_of_begin, GROUP_ID) == 0) {
00457       if(!CreateGroupFromASCII(hdf_group, fp)) {
00458        std::cout << "Can not create subgroup " << i << " for group " << name << std::endl;
00459        return false;
00460       }
00461     }
00462     else if(strcmp(id_of_begin, DATASET_ID) == 0) {
00463       if(!CreateDatasetFromASCII(hdf_group, fp)) {
00464        std::cout << "Can not create dataset " << i << " for group " << name << std::endl;
00465        return false;
00466       }
00467     }
00468     else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
00469       if(!CreateAttributeFromASCII(hdf_group, fp)) {
00470        std::cout << "Can not create attribute " << i << " for group " << name << std::endl;
00471        return false;
00472       }
00473     }
00474     else 
00475       std::cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << std::endl; 
00476   }
00477   
00478   hdf_group->CloseOnDisk();
00479   hdf_group = 0; //will be deleted by father destructor
00480 
00481   char id_of_end[MAX_ID_SIZE];
00482   fscanf(fp, "%s\n", id_of_end);
00483   if(strcmp(id_of_end, GROUP_ID_END) != 0) {
00484     std::cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << std::endl;
00485     return false;
00486   }
00487 
00488   return true;
00489 }
00490 
00491 
00492 //============================================================================
00493 // function : CreateDatasetFromASCII
00494 // purpose  : Creates a HDF dataset from a set attributes situated under theLabel
00495 //============================================================================
00496 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
00497 {
00498   char name[HDF_NAME_MAX_LEN+1];
00499   hdf_type type;
00500   hdf_byte_order order;
00501   int nbDim, nbAttr;
00502   long i, size;
00503 
00504   fscanf(fp, "%s %i %i\n", name, &type, &nbAttr);
00505   char* new_name = restoreName(name);
00506 
00507   fscanf(fp, "%i\n", &nbDim);
00508 
00509   hdf_size* sizeArray = new hdf_size[nbDim];
00510   int dim = 0;
00511   for(i = 0; i<nbDim; i++) {
00512     fscanf(fp, "%i\n", &dim);
00513     sizeArray[i] = dim;
00514   }
00515  
00516    // order (2-d member) was not written in earlier versions
00517   char tmp;
00518   int nbRead = fscanf(fp, "%li %i%c", &size, &order, &tmp);
00519   if ( nbRead < 2 ) { // fscanf stops before ":"
00520     fscanf(fp, "%c", &tmp);
00521     order = H5T_ORDER_NONE;
00522   }
00523   if ( type != HDF_FLOAT64 )  // use order only for FLOAT64
00524     order = H5T_ORDER_NONE;
00525 
00526 
00527   HDFarray* anArray = 0;
00528   if( type == HDF_ARRAY ){
00529     //Get array information
00530     hdf_type arr_data_type;
00531     int arr_ndim;
00532     fscanf(fp, "%c", &tmp);
00533     fscanf(fp, " %i\n", &arr_data_type ); //Get array data type
00534     fscanf(fp, " %i\n", &arr_ndim ); //Get array nDim
00535     hdf_size *arr_dim = new hdf_size[arr_ndim];
00536 
00537     int tdim = 0;
00538     for( int i = 0;i < arr_ndim; i++ ) {
00539       fscanf(fp, " %i", &tdim);
00540       arr_dim[i] = tdim;
00541     }
00542     anArray = new HDFarray(0, arr_data_type, arr_ndim, arr_dim);
00543     anArray->CreateOnDisk();
00544 
00545     type = arr_data_type;
00546     delete [] arr_dim;
00547   }
00548 
00549   HDFdataset* hdf_dataset = new HDFdataset(new_name, father, anArray ? HDF_ARRAY : type, sizeArray, nbDim, order);
00550   
00551   if(anArray)
00552     hdf_dataset->SetArrayId(anArray->GetId());
00553 
00554   delete [] new_name;
00555   delete [] sizeArray;
00556 
00557   hdf_dataset->CreateOnDisk();
00558 
00559   if (type == HDF_STRING) { 
00560     char *val = new char[size+1];
00561     fread(val, 1, size, fp);
00562     hdf_dataset->WriteOnDisk(val);
00563     delete [] val;
00564   } else if (type == HDF_FLOAT64) {
00565     hdf_float64* val = new hdf_float64[size];
00566     for(i=0; i<size; i++) {
00567       read_float64(fp, &(val[i]));
00568     }
00569     hdf_dataset->WriteOnDisk(val);
00570     delete [] val;
00571   } else if(type == HDF_INT64) {
00572     hdf_int64* val = new hdf_int64[size];
00573     for(i=0; i<size; i++) {
00574       fscanf(fp, " %li", &(val[i]));
00575     }
00576     hdf_dataset->WriteOnDisk(val);
00577     delete [] val;
00578   } else if(type == HDF_INT32) {
00579     hdf_int32* val = new hdf_int32[size];
00580     for(i=0; i<size; i++) {
00581       fscanf(fp, " %i", &(val[i]));
00582     }
00583     hdf_dataset->WriteOnDisk(val);
00584     delete [] val;
00585   } else if(type == HDF_CHAR) {
00586     hdf_char* val = new hdf_char[size];
00587     for(i=0; i<size; i++) {
00588       fscanf(fp, " %i", &(val[i]));
00589     }
00590     hdf_dataset->WriteOnDisk(val);
00591     delete [] val;
00592   }
00593 
00594   char token[MAX_ID_SIZE];
00595 
00596   for(i = 0; i < nbAttr; i++) {
00597     fscanf(fp, "%s\n", token);
00598     
00599     if(strcmp(token, ATTRIBUTE_ID) == 0) {
00600       if(!CreateAttributeFromASCII(hdf_dataset, fp)) {
00601        std::cout << "Can not create attribute " << i << " for dataset " << name << std::endl;
00602        return false;
00603       }
00604     }
00605     else {
00606       std::cout << "CreateGroupFromASCII : Unrecognized type " << token << std::endl; 
00607       return false;
00608     }
00609   }
00610   
00611   fscanf(fp, "%s\n", token);
00612   if(strcmp(token, DATASET_ID_END) != 0) {
00613     std::cout << "CreateDatasetFromASCII : Invalid end token : " << token << std::endl;
00614     return false;
00615   }
00616 
00617   hdf_dataset->CloseOnDisk();
00618   hdf_dataset = 0; //will be deleted by father destructor
00619 
00620   if(anArray) {
00621     anArray->CloseOnDisk();
00622     anArray = 0; //will be deleted by father destructor
00623   }
00624 
00625   return true;
00626 }
00627 
00628 
00629 //============================================================================
00630 // function : CreateAttributeFromASCII
00631 // purpose  : Creates a HDF attribute from a set attributes situated under theLabel
00632 //============================================================================
00633 bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp)
00634 {
00635   char name[HDF_NAME_MAX_LEN+1];
00636 
00637   hdf_type type;
00638   int size;
00639   fscanf(fp, "%s %i %i\n", name, &type, &size);
00640   char* new_name = restoreName(name);
00641   HDFattribute* hdf_attribute = new HDFattribute(new_name, father, type, size);
00642 
00643   hdf_attribute->CreateOnDisk();
00644 
00645   delete [] new_name;
00646   
00647   if (type == HDF_STRING) { 
00648     char tmp;
00649     fscanf(fp, "%c", &tmp);
00650     char *val = new char[size+1];
00651     val[size] = (char)0;
00652     fread(val, 1, size, fp);
00653     hdf_attribute->WriteOnDisk(val);
00654     delete [] val;
00655   } else if (type == HDF_FLOAT64) {
00656     hdf_float64 val;
00657     read_float64(fp, &val);
00658     hdf_attribute->WriteOnDisk(&val);
00659   } else if(type == HDF_INT64) {
00660     hdf_int64 val;
00661     fscanf(fp, "%li", &val);
00662     hdf_attribute->WriteOnDisk(&val);
00663   } else if(type == HDF_INT32) {
00664     hdf_int32 val;
00665     fscanf(fp, "%i", &val);
00666     hdf_attribute->WriteOnDisk(&val);
00667   }
00668   
00669   hdf_attribute->CloseOnDisk();
00670   hdf_attribute = 0; //will be deleted by father destructor
00671 
00672 
00673   char id_of_end[MAX_ID_SIZE];
00674   fscanf(fp, "%s\n", id_of_end);
00675   if(strcmp(id_of_end, ATTRIBUTE_ID_END) != 0) {
00676     std::cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << std::endl;
00677     return false;
00678   }
00679 
00680   return true;
00681 }
00682 
00683 
00684 //============================================================================
00685 // function : GetTempDir
00686 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
00687 //============================================================================ 
00688 std::string GetTmpDir()
00689 {
00690  //Find a temporary directory to store a file
00691   std::string aTmpDir;
00692   char *Tmp_dir = getenv("SALOME_TMP_DIR");
00693   if(Tmp_dir != NULL) {
00694     aTmpDir = std::string(Tmp_dir);
00695     if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;
00696   }
00697   else {
00698 #ifdef WIN32
00699     aTmpDir = std::string("C:\\");
00700 #else
00701     aTmpDir = std::string("/tmp/");
00702 #endif
00703   }
00704 
00705   srand((unsigned int)time(NULL));
00706   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
00707   char buffer[127];
00708   sprintf(buffer, "%d", aRND);
00709  std:: string aSubDir(buffer);
00710   if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
00711 
00712   aTmpDir += aSubDir; //Get RND sub directory
00713 
00714   if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;
00715 
00716   std::string aDir = aTmpDir;
00717 
00718   for(aRND = 0; Exists(aDir); aRND++) {
00719     sprintf(buffer, "%d", aRND);
00720     aDir = aTmpDir+buffer;  //Build a unique directory name
00721   }
00722 
00723 #ifdef WIN32
00724   //fuction CreateDirectory create only final directory, but not intermediate
00725   CreateDirectory(aTmpDir.c_str(), NULL);
00726   CreateDirectory(aDir.c_str(), NULL);
00727 #else
00728   mkdir(aDir.c_str(), 0x1ff); 
00729 #endif
00730 
00731   return aDir + dir_separator;
00732 }
00733 
00734 char* makeName(char* name)
00735 {
00736   std::string aName(name), aNewName;
00737   int i, length = aName.size();
00738   char replace = (char)19;
00739 
00740   for(i=0; i<length; i++) {
00741     if(aName[i] == ' ') aNewName+=replace;
00742     else aNewName += aName[i];
00743   }
00744 
00745   length = strlen(aNewName.c_str());
00746   char *new_str = new char[ 1+length ];
00747   strcpy(new_str , aNewName.c_str()) ;
00748   return new_str;
00749 }
00750 
00751 char* restoreName(char* name)
00752 {
00753   std::string aName(name), aNewName;
00754   int i, length = aName.size();
00755   char replace = (char)19;
00756 
00757   for(i=0; i<length; i++) {
00758     if(aName[i] == replace) aNewName+=' ';
00759     else aNewName += aName[i];
00760   }
00761 
00762   length = strlen(aNewName.c_str());
00763   char *new_str = new char[ 1+length ];
00764   strcpy(new_str , aNewName.c_str()) ;
00765   return new_str;
00766 }
00767 
00768 void write_float64(FILE* fp, hdf_float64* value)
00769 {
00770   unsigned char* array = (unsigned char*)value;
00771   for(int i = 0; i < sizeof(hdf_float64); i++) {
00772     unsigned tmp = (unsigned short)array[i];
00773     fprintf(fp, " %2x", tmp);
00774   }
00775 }
00776 
00777 void read_float64(FILE* fp, hdf_float64* value)
00778 {
00779   unsigned char* array = (unsigned char*)value;
00780   for(int i = 0; i < sizeof(hdf_float64); i++) {
00781     unsigned tmp;
00782     fscanf(fp, " %x", &tmp); 
00783     array[i] = (unsigned char)tmp;
00784   }
00785 }
00786 
00787 bool Exists(const std::string thePath) 
00788 {
00789 #ifdef WIN32 
00790  if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
00791     DWORD errorId = GetLastError ();
00792     if ( errorId == ERROR_FILE_NOT_FOUND || errorId == ERROR_PATH_NOT_FOUND )
00793       return false;
00794   }
00795 #else 
00796   int status = access ( thePath.c_str() , F_OK ); 
00797   if (status != 0) return false;
00798 #endif
00799   return true;
00800 }
00801 
00802 void Move(const std::string& fName, const std::string& fNameDst)
00803 { 
00804 #ifdef WIN32
00805   MoveFileEx (fName.c_str(), fNameDst.c_str(),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
00806 #else
00807   rename(fName.c_str(), fNameDst.c_str());
00808 #endif
00809 }
00810 
00811 void WriteSimpleData( FILE* fp, HDFdataset *hdf_dataset, hdf_type type, long size ) {
00812   if (type == HDF_STRING) { 
00813     char* val = new char[size];
00814     hdf_dataset->ReadFromDisk(val);
00815     fwrite(val, 1, size, fp);
00816     delete [] val;
00817   } else if (type == HDF_FLOAT64) {
00818     hdf_float64* val = new hdf_float64[size];
00819     hdf_dataset->ReadFromDisk(val);
00820     fprintf(fp, "\n");
00821     for (int i = 0, j = 0; i < size; i++) {
00822       write_float64(fp, &val[i]);
00823       if(++j == NB_FLOAT_IN_ROW) {
00824        fprintf(fp, "\n");
00825        j = 0;
00826       }
00827       else fprintf(fp,"  ");
00828     }
00829     delete [] val;
00830   } else if(type == HDF_INT64) {
00831     hdf_int64* val = new hdf_int64[size];
00832     hdf_dataset->ReadFromDisk(val);
00833     fprintf(fp, "\n");
00834     for (int i = 0, j = 0; i < size; i++) {
00835       fprintf(fp, " %li", val[i]);
00836       if(++j == NB_INTEGER_IN_ROW) {
00837        fprintf(fp, "\n");
00838        j = 0;
00839       }
00840     }
00841     delete [] val;
00842   } else if(type == HDF_INT32) {
00843     hdf_int32* val = new hdf_int32[size];
00844     hdf_dataset->ReadFromDisk(val);
00845     fprintf(fp, "\n");
00846     for (int i = 0, j = 0; i < size; i++) {
00847       fprintf(fp, " %i", val[i]);
00848       if(++j == NB_INTEGER_IN_ROW) {
00849        fprintf(fp, "\n");
00850        j = 0;
00851       }
00852     }
00853     delete [] val;
00854   }else if(type == HDF_CHAR) {
00855     hdf_char* val = new hdf_char[size];
00856     hdf_dataset->ReadFromDisk(val);
00857     fprintf(fp, "\n");
00858     for (int i = 0, j = 0; i < size; i++) {
00859       fprintf(fp, " %i", val[i]);
00860       if(++j == NB_INTEGER_IN_ROW) {
00861        fprintf(fp, "\n");
00862        j = 0;
00863       }
00864     }
00865     delete [] val;
00866   }
00867 }