Back to index

salome-kernel  6.5.0
Defines | Functions
HDFascii.cc File Reference
#include "HDFOI.hxx"
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <string>

Go to the source code of this file.

Defines

#define dir_separator   '/'
#define MAX_STRING_SIZE   65535
#define MAX_ID_SIZE   20
#define NB_FLOAT_IN_ROW   3
#define NB_INTEGER_IN_ROW   9
#define ASCIIHDF_ID   "ASCIIHDF"
#define ATTRIBUTE_ID   "ATTRIBUTE"
#define DATASET_ID   "DATASET"
#define GROUP_ID   "GROUP"
#define ASCIIHDF_ID_END   "ASCIIHDF_END"
#define ATTRIBUTE_ID_END   "ATTRIBUTE_END"
#define DATASET_ID_END   "DATASET_END"
#define GROUP_ID_END   "GROUP_END"

Functions

void Move (const std::string &fName, const std::string &fNameDst)
bool Exists (const std::string thePath)
bool CreateAttributeFromASCII (HDFinternalObject *father, FILE *fp)
bool CreateDatasetFromASCII (HDFcontainerObject *father, FILE *fp)
bool CreateGroupFromASCII (HDFcontainerObject *father, FILE *fp)
void SaveAttributeInASCIIfile (HDFattribute *hdf_attribute, FILE *fp, int ident)
void SaveGroupInASCIIfile (HDFgroup *hdf_group, FILE *fp, int ident)
void SaveDatasetInASCIIfile (HDFdataset *hdf_dataset, FILE *fp, int ident)
std::string GetTmpDir ()
char * makeName (char *name)
char * restoreName (char *name)
void write_float64 (FILE *fp, hdf_float64 *value)
void read_float64 (FILE *fp, hdf_float64 *value)
void WriteSimpleData (FILE *fp, HDFdataset *hdf_dataset, hdf_type type, long size)

Define Documentation

#define ASCIIHDF_ID   "ASCIIHDF"

Definition at line 72 of file HDFascii.cc.

#define ASCIIHDF_ID_END   "ASCIIHDF_END"

Definition at line 77 of file HDFascii.cc.

#define ATTRIBUTE_ID   "ATTRIBUTE"

Definition at line 73 of file HDFascii.cc.

#define ATTRIBUTE_ID_END   "ATTRIBUTE_END"

Definition at line 78 of file HDFascii.cc.

#define DATASET_ID   "DATASET"

Definition at line 74 of file HDFascii.cc.

#define DATASET_ID_END   "DATASET_END"

Definition at line 79 of file HDFascii.cc.

#define dir_separator   '/'

Definition at line 46 of file HDFascii.cc.

#define GROUP_ID   "GROUP"

Definition at line 75 of file HDFascii.cc.

#define GROUP_ID_END   "GROUP_END"

Definition at line 80 of file HDFascii.cc.

#define MAX_ID_SIZE   20

Definition at line 68 of file HDFascii.cc.

#define MAX_STRING_SIZE   65535

Definition at line 67 of file HDFascii.cc.

#define NB_FLOAT_IN_ROW   3

Definition at line 69 of file HDFascii.cc.

#define NB_INTEGER_IN_ROW   9

Definition at line 70 of file HDFascii.cc.


Function Documentation

bool CreateAttributeFromASCII ( HDFinternalObject *  father,
FILE *  fp 
)

Definition at line 633 of file HDFascii.cc.

{
  char name[HDF_NAME_MAX_LEN+1];

  hdf_type type;
  int size;
  fscanf(fp, "%s %i %i\n", name, &type, &size);
  char* new_name = restoreName(name);
  HDFattribute* hdf_attribute = new HDFattribute(new_name, father, type, size);

  hdf_attribute->CreateOnDisk();

  delete [] new_name;
  
  if (type == HDF_STRING) { 
    char tmp;
    fscanf(fp, "%c", &tmp);
    char *val = new char[size+1];
    val[size] = (char)0;
    fread(val, 1, size, fp);
    hdf_attribute->WriteOnDisk(val);
    delete [] val;
  } else if (type == HDF_FLOAT64) {
    hdf_float64 val;
    read_float64(fp, &val);
    hdf_attribute->WriteOnDisk(&val);
  } else if(type == HDF_INT64) {
    hdf_int64 val;
    fscanf(fp, "%li", &val);
    hdf_attribute->WriteOnDisk(&val);
  } else if(type == HDF_INT32) {
    hdf_int32 val;
    fscanf(fp, "%i", &val);
    hdf_attribute->WriteOnDisk(&val);
  }
  
  hdf_attribute->CloseOnDisk();
  hdf_attribute = 0; //will be deleted by father destructor


  char id_of_end[MAX_ID_SIZE];
  fscanf(fp, "%s\n", id_of_end);
  if(strcmp(id_of_end, ATTRIBUTE_ID_END) != 0) {
    std::cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << std::endl;
    return false;
  }

  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool CreateDatasetFromASCII ( HDFcontainerObject *  father,
FILE *  fp 
)

Definition at line 496 of file HDFascii.cc.

{
  char name[HDF_NAME_MAX_LEN+1];
  hdf_type type;
  hdf_byte_order order;
  int nbDim, nbAttr;
  long i, size;

  fscanf(fp, "%s %i %i\n", name, &type, &nbAttr);
  char* new_name = restoreName(name);

  fscanf(fp, "%i\n", &nbDim);

  hdf_size* sizeArray = new hdf_size[nbDim];
  int dim = 0;
  for(i = 0; i<nbDim; i++) {
    fscanf(fp, "%i\n", &dim);
    sizeArray[i] = dim;
  }
 
   // order (2-d member) was not written in earlier versions
  char tmp;
  int nbRead = fscanf(fp, "%li %i%c", &size, &order, &tmp);
  if ( nbRead < 2 ) { // fscanf stops before ":"
    fscanf(fp, "%c", &tmp);
    order = H5T_ORDER_NONE;
  }
  if ( type != HDF_FLOAT64 )  // use order only for FLOAT64
    order = H5T_ORDER_NONE;


  HDFarray* anArray = 0;
  if( type == HDF_ARRAY ){
    //Get array information
    hdf_type arr_data_type;
    int arr_ndim;
    fscanf(fp, "%c", &tmp);
    fscanf(fp, " %i\n", &arr_data_type ); //Get array data type
    fscanf(fp, " %i\n", &arr_ndim ); //Get array nDim
    hdf_size *arr_dim = new hdf_size[arr_ndim];

    int tdim = 0;
    for( int i = 0;i < arr_ndim; i++ ) {
      fscanf(fp, " %i", &tdim);
      arr_dim[i] = tdim;
    }
    anArray = new HDFarray(0, arr_data_type, arr_ndim, arr_dim);
    anArray->CreateOnDisk();

    type = arr_data_type;
    delete [] arr_dim;
  }

  HDFdataset* hdf_dataset = new HDFdataset(new_name, father, anArray ? HDF_ARRAY : type, sizeArray, nbDim, order);
  
  if(anArray)
    hdf_dataset->SetArrayId(anArray->GetId());

  delete [] new_name;
  delete [] sizeArray;

  hdf_dataset->CreateOnDisk();

  if (type == HDF_STRING) { 
    char *val = new char[size+1];
    fread(val, 1, size, fp);
    hdf_dataset->WriteOnDisk(val);
    delete [] val;
  } else if (type == HDF_FLOAT64) {
    hdf_float64* val = new hdf_float64[size];
    for(i=0; i<size; i++) {
      read_float64(fp, &(val[i]));
    }
    hdf_dataset->WriteOnDisk(val);
    delete [] val;
  } else if(type == HDF_INT64) {
    hdf_int64* val = new hdf_int64[size];
    for(i=0; i<size; i++) {
      fscanf(fp, " %li", &(val[i]));
    }
    hdf_dataset->WriteOnDisk(val);
    delete [] val;
  } else if(type == HDF_INT32) {
    hdf_int32* val = new hdf_int32[size];
    for(i=0; i<size; i++) {
      fscanf(fp, " %i", &(val[i]));
    }
    hdf_dataset->WriteOnDisk(val);
    delete [] val;
  } else if(type == HDF_CHAR) {
    hdf_char* val = new hdf_char[size];
    for(i=0; i<size; i++) {
      fscanf(fp, " %i", &(val[i]));
    }
    hdf_dataset->WriteOnDisk(val);
    delete [] val;
  }

  char token[MAX_ID_SIZE];

  for(i = 0; i < nbAttr; i++) {
    fscanf(fp, "%s\n", token);
    
    if(strcmp(token, ATTRIBUTE_ID) == 0) {
      if(!CreateAttributeFromASCII(hdf_dataset, fp)) {
       std::cout << "Can not create attribute " << i << " for dataset " << name << std::endl;
       return false;
      }
    }
    else {
      std::cout << "CreateGroupFromASCII : Unrecognized type " << token << std::endl; 
      return false;
    }
  }
  
  fscanf(fp, "%s\n", token);
  if(strcmp(token, DATASET_ID_END) != 0) {
    std::cout << "CreateDatasetFromASCII : Invalid end token : " << token << std::endl;
    return false;
  }

  hdf_dataset->CloseOnDisk();
  hdf_dataset = 0; //will be deleted by father destructor

  if(anArray) {
    anArray->CloseOnDisk();
    anArray = 0; //will be deleted by father destructor
  }

  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool CreateGroupFromASCII ( HDFcontainerObject *  father,
FILE *  fp 
)

Definition at line 438 of file HDFascii.cc.

{
  char name[HDF_NAME_MAX_LEN+1];
  int nbsons, i;
  fscanf(fp, "%s %i\n", name, &nbsons);  

  char* new_name = restoreName(name);

  HDFgroup* hdf_group = new HDFgroup(new_name, father);

  delete [] new_name;

  hdf_group->CreateOnDisk();

  for(i = 0; i < nbsons; i++) {
    char id_of_begin[MAX_ID_SIZE];
    fscanf(fp, "%s\n", id_of_begin);
    
    if(strcmp(id_of_begin, GROUP_ID) == 0) {
      if(!CreateGroupFromASCII(hdf_group, fp)) {
       std::cout << "Can not create subgroup " << i << " for group " << name << std::endl;
       return false;
      }
    }
    else if(strcmp(id_of_begin, DATASET_ID) == 0) {
      if(!CreateDatasetFromASCII(hdf_group, fp)) {
       std::cout << "Can not create dataset " << i << " for group " << name << std::endl;
       return false;
      }
    }
    else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
      if(!CreateAttributeFromASCII(hdf_group, fp)) {
       std::cout << "Can not create attribute " << i << " for group " << name << std::endl;
       return false;
      }
    }
    else 
      std::cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << std::endl; 
  }
  
  hdf_group->CloseOnDisk();
  hdf_group = 0; //will be deleted by father destructor

  char id_of_end[MAX_ID_SIZE];
  fscanf(fp, "%s\n", id_of_end);
  if(strcmp(id_of_end, GROUP_ID_END) != 0) {
    std::cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << std::endl;
    return false;
  }

  return true;
}

Here is the call graph for this function:

bool Exists ( const std::string  thePath)
std::string GetTmpDir ( )

Definition at line 688 of file HDFascii.cc.

{
 //Find a temporary directory to store a file
  std::string aTmpDir;
  char *Tmp_dir = getenv("SALOME_TMP_DIR");
  if(Tmp_dir != NULL) {
    aTmpDir = std::string(Tmp_dir);
    if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;
  }
  else {
#ifdef WIN32
    aTmpDir = std::string("C:\\");
#else
    aTmpDir = std::string("/tmp/");
#endif
  }

  srand((unsigned int)time(NULL));
  int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
  char buffer[127];
  sprintf(buffer, "%d", aRND);
 std:: string aSubDir(buffer);
  if(aSubDir.size() <= 1) aSubDir = std::string("123409876");

  aTmpDir += aSubDir; //Get RND sub directory

  if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;

  std::string aDir = aTmpDir;

  for(aRND = 0; Exists(aDir); aRND++) {
    sprintf(buffer, "%d", aRND);
    aDir = aTmpDir+buffer;  //Build a unique directory name
  }

#ifdef WIN32
  //fuction CreateDirectory create only final directory, but not intermediate
  CreateDirectory(aTmpDir.c_str(), NULL);
  CreateDirectory(aDir.c_str(), NULL);
#else
  mkdir(aDir.c_str(), 0x1ff); 
#endif

  return aDir + dir_separator;
}

Here is the call graph for this function:

char * makeName ( char *  name)

Definition at line 734 of file HDFascii.cc.

{
  std::string aName(name), aNewName;
  int i, length = aName.size();
  char replace = (char)19;

  for(i=0; i<length; i++) {
    if(aName[i] == ' ') aNewName+=replace;
    else aNewName += aName[i];
  }

  length = strlen(aNewName.c_str());
  char *new_str = new char[ 1+length ];
  strcpy(new_str , aNewName.c_str()) ;
  return new_str;
}

Here is the caller graph for this function:

void Move ( const std::string &  fName,
const std::string &  fNameDst 
)

Definition at line 802 of file HDFascii.cc.

{ 
#ifdef WIN32
  MoveFileEx (fName.c_str(), fNameDst.c_str(),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
#else
  rename(fName.c_str(), fNameDst.c_str());
#endif
}
void read_float64 ( FILE *  fp,
hdf_float64 value 
)

Definition at line 777 of file HDFascii.cc.

{
  unsigned char* array = (unsigned char*)value;
  for(int i = 0; i < sizeof(hdf_float64); i++) {
    unsigned tmp;
    fscanf(fp, " %x", &tmp); 
    array[i] = (unsigned char)tmp;
  }
}

Here is the caller graph for this function:

char * restoreName ( char *  name)

Definition at line 751 of file HDFascii.cc.

{
  std::string aName(name), aNewName;
  int i, length = aName.size();
  char replace = (char)19;

  for(i=0; i<length; i++) {
    if(aName[i] == replace) aNewName+=' ';
    else aNewName += aName[i];
  }

  length = strlen(aNewName.c_str());
  char *new_str = new char[ 1+length ];
  strcpy(new_str , aNewName.c_str()) ;
  return new_str;
}

Here is the caller graph for this function:

void SaveAttributeInASCIIfile ( HDFattribute *  hdf_attribute,
FILE *  fp,
int  ident 
)

Definition at line 304 of file HDFascii.cc.

{
  hdf_attribute->OpenOnDisk();

  hdf_type type = hdf_attribute->GetType();

  char* name = makeName(hdf_attribute->GetName());
  int size = hdf_attribute->GetSize();

  fprintf(fp, "%s\n", ATTRIBUTE_ID);
  fprintf(fp, "%s %i %i\n", name, type, size);

  delete [] name;

  if (type == HDF_STRING) {    
    char* val = new char[size+1];
    hdf_attribute->ReadFromDisk(val);
    fprintf(fp, ":");
    fwrite(val, 1, size, fp);
    fprintf(fp, "\n");
    delete [] val;
  } else if (type == HDF_FLOAT64) {
    hdf_float64 val;
    hdf_attribute->ReadFromDisk(&val);
    write_float64(fp, &val);
    fprintf(fp, "\n");
  } else if(type == HDF_INT64) {
    hdf_int64 val;
    hdf_attribute->ReadFromDisk(&val);
    fprintf(fp, "%li \n", val);
  } else if(type == HDF_INT32) {
    hdf_int32 val;
    hdf_attribute->ReadFromDisk(&val);
    fprintf(fp, "%i \n", val);
  }

  fprintf(fp, "%s\n", ATTRIBUTE_ID_END);

  hdf_attribute->CloseOnDisk();  
}

Here is the call graph for this function:

Here is the caller graph for this function:

void SaveDatasetInASCIIfile ( HDFdataset *  hdf_dataset,
FILE *  fp,
int  ident 
)

Definition at line 233 of file HDFascii.cc.

{
  hdf_dataset->OpenOnDisk();

  long size =  (long) hdf_dataset->GetSize();
  long ndim = hdf_dataset->nDim(); //Get number of dimesions
  hdf_size *dim = new hdf_size[ndim];
  hdf_type type = hdf_dataset->GetType();
  hdf_byte_order order = hdf_dataset->GetOrder();
  int nbAttr = hdf_dataset->nAttributes(); 

  char* name = makeName(hdf_dataset->GetName());

  fprintf(fp, "%s\n", DATASET_ID);
  fprintf(fp, "%s %i %i\n", name, type, nbAttr);
  delete [] name;

  hdf_dataset->GetDim(dim);
  fprintf(fp, " %i\n", ndim);

  for(int i = 0;i < ndim;i++) {
    fprintf(fp, " %i", dim[i]);
  }

  fprintf(fp, "\n");
  delete [] dim;

  fprintf(fp, "%li %i:", size, order);
  if( type == HDF_ARRAY ) {
    HDFarray *array = new HDFarray(hdf_dataset);
    hdf_type data_type = array->GetDataType();
    fprintf(fp, "\n" );
    fprintf(fp, " %i\n", data_type ); //Write array data type

    //Write nDim of the array
    int arr_ndim = array->nDim();
    fprintf(fp, " %i\n", arr_ndim);
    hdf_size *arr_dim = new hdf_size[arr_ndim];
    array->GetDim(arr_dim);

    for( int i = 0;i < arr_ndim; i++ ) {
      fprintf(fp, " %i", arr_dim[i]);
    }
        
    //And write the data array
    WriteSimpleData( fp, hdf_dataset, data_type, size);
  } else {
    WriteSimpleData( fp, hdf_dataset, type, size);
  }
  
  fprintf(fp, "\n");

  for ( int j=0; j<nbAttr; j++ )
  {
    name = hdf_dataset->GetAttributeName(j);
    HDFattribute *hdf_attribute = new HDFattribute(name, hdf_dataset);
    delete [] name;
    SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
    hdf_attribute = 0;
  }

  fprintf(fp, "%s\n", DATASET_ID_END);

  hdf_dataset->CloseOnDisk(); 
}

Here is the call graph for this function:

Here is the caller graph for this function:

void SaveGroupInASCIIfile ( HDFgroup *  hdf_group,
FILE *  fp,
int  ident 
)

Definition at line 183 of file HDFascii.cc.

{
  hdf_group->OpenOnDisk();

  int nbsons = hdf_group->nInternalObjects(), nbAttr = hdf_group->nAttributes(); 

  fprintf(fp, "%s\n", GROUP_ID);

  char* name = makeName(hdf_group->GetName());

  fprintf(fp, "%s %i\n", name, nbsons+nbAttr);
  delete [] name;

  for(int j=0; j<nbAttr; j++) {
    name = hdf_group->GetAttributeName(j);
    HDFattribute *hdf_attribute = new HDFattribute(name, hdf_group);
    delete [] name;
    SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
    hdf_attribute = 0;
  }

  char objName[HDF_NAME_MAX_LEN+1];
 
  for (int i=0; i<nbsons; i++) {
    hdf_group->InternalObjectIndentify(i, objName);

    if (strncmp(objName, "INTERNAL_COMPLEX",16) == 0)  continue;

    hdf_object_type type = hdf_group->InternalObjectType(objName);

    if  (type == HDF_DATASET) {
      HDFdataset* hdf_dataset = new HDFdataset(objName, hdf_group);
      SaveDatasetInASCIIfile(hdf_dataset, fp, ident+1);
      hdf_dataset = 0;
    } else if (type == HDF_GROUP)   {      
      HDFgroup *hdf_subgroup = new HDFgroup(objName, hdf_group);
      SaveGroupInASCIIfile(hdf_subgroup, fp, ident+1);
      hdf_subgroup = 0;
    } 
  }

  fprintf(fp, "%s\n", GROUP_ID_END);

  hdf_group->CloseOnDisk();  
}

Here is the call graph for this function:

void write_float64 ( FILE *  fp,
hdf_float64 value 
)

Definition at line 768 of file HDFascii.cc.

{
  unsigned char* array = (unsigned char*)value;
  for(int i = 0; i < sizeof(hdf_float64); i++) {
    unsigned tmp = (unsigned short)array[i];
    fprintf(fp, " %2x", tmp);
  }
}

Here is the caller graph for this function:

void WriteSimpleData ( FILE *  fp,
HDFdataset *  hdf_dataset,
hdf_type  type,
long  size 
)

Definition at line 811 of file HDFascii.cc.

                                                                                    {
  if (type == HDF_STRING) { 
    char* val = new char[size];
    hdf_dataset->ReadFromDisk(val);
    fwrite(val, 1, size, fp);
    delete [] val;
  } else if (type == HDF_FLOAT64) {
    hdf_float64* val = new hdf_float64[size];
    hdf_dataset->ReadFromDisk(val);
    fprintf(fp, "\n");
    for (int i = 0, j = 0; i < size; i++) {
      write_float64(fp, &val[i]);
      if(++j == NB_FLOAT_IN_ROW) {
       fprintf(fp, "\n");
       j = 0;
      }
      else fprintf(fp,"  ");
    }
    delete [] val;
  } else if(type == HDF_INT64) {
    hdf_int64* val = new hdf_int64[size];
    hdf_dataset->ReadFromDisk(val);
    fprintf(fp, "\n");
    for (int i = 0, j = 0; i < size; i++) {
      fprintf(fp, " %li", val[i]);
      if(++j == NB_INTEGER_IN_ROW) {
       fprintf(fp, "\n");
       j = 0;
      }
    }
    delete [] val;
  } else if(type == HDF_INT32) {
    hdf_int32* val = new hdf_int32[size];
    hdf_dataset->ReadFromDisk(val);
    fprintf(fp, "\n");
    for (int i = 0, j = 0; i < size; i++) {
      fprintf(fp, " %i", val[i]);
      if(++j == NB_INTEGER_IN_ROW) {
       fprintf(fp, "\n");
       j = 0;
      }
    }
    delete [] val;
  }else if(type == HDF_CHAR) {
    hdf_char* val = new hdf_char[size];
    hdf_dataset->ReadFromDisk(val);
    fprintf(fp, "\n");
    for (int i = 0, j = 0; i < size; i++) {
      fprintf(fp, " %i", val[i]);
      if(++j == NB_INTEGER_IN_ROW) {
       fprintf(fp, "\n");
       j = 0;
      }
    }
    delete [] val;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function: