-// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
//
-// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
+
// File : SALOMEDS_Tool.cxx
// Created : Mon Oct 21 16:24:34 2002
// Author : Sergey RUIN
#include <io.h>
#include <time.h>
#include <windows.h>
+#define open _open
+#define read _read
+#define close _close
#define dir_separator '\\'
#else
#define dir_separator '/'
#endif
-using namespace std;
-
-void Move(const string& fName, const string& fNameDst);
-bool Exists(const string thePath);
+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 SaveGroupInASCIIfile(HDFgroup *hdf_group, FILE* fp, int ident);
void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident);
-string GetTmpDir();
+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 MAX_STRING_SIZE 65535
#define MAX_ID_SIZE 20
#define NB_FLOAT_IN_ROW 3
bool isReplace,
const char* theExtension)
{
- string aPath(thePath);
+ std::string aPath(thePath);
if(!isReplace) {
if(theExtension == NULL) aPath += ".asc";
else aPath += (char*)theExtension;
}
- string aFileName(aPath);
+ std::string aFileName(aPath);
if(isReplace) aFileName=aPath+".ascii_tmp";
HDFfile *hdf_file = new HDFfile((char*)thePath);
char name[HDF_NAME_MAX_LEN+1];
int nbsons = hdf_file->nInternalObjects(), nbAttr = hdf_file->nAttributes();
- FILE* fp = fopen(aFileName.c_str(), "w");
+ FILE* fp = fopen(aFileName.c_str(), "wb");
fprintf(fp, "%s\n", ASCIIHDF_ID);
fprintf(fp, "%i\n", nbsons+nbAttr);
- for(unsigned j=0; j<nbAttr; j++) {
+ for(int j=0; j<nbAttr; j++) {
char* attr_name = hdf_file->GetAttributeName(j);
HDFattribute *hdf_attribute = new HDFattribute(attr_name, hdf_file);
delete attr_name;
char* name = makeName(hdf_group->GetName());
fprintf(fp, "%s %i\n", name, nbsons+nbAttr);
- delete name;
+ delete [] name;
- for(unsigned j=0; j<nbAttr; j++) {
+ for(int j=0; j<nbAttr; j++) {
name = hdf_group->GetAttributeName(j);
HDFattribute *hdf_attribute = new HDFattribute(name, hdf_group);
- delete name;
+ delete [] name;
SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
hdf_attribute = 0;
}
{
hdf_dataset->OpenOnDisk();
- long size = hdf_dataset->GetSize();
+ 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();
fprintf(fp, "%s\n", DATASET_ID);
fprintf(fp, "%s %i %i\n", name, type, nbAttr);
- delete name;
+ delete [] name;
hdf_dataset->GetDim(dim);
fprintf(fp, " %i\n", ndim);
}
fprintf(fp, "\n");
- delete dim;
+ delete [] dim;
fprintf(fp, "%li %i:", size, order);
-
- 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;
- }
+ 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]);
}
- delete val;
+
+ //And write the data array
+ WriteSimpleData( fp, hdf_dataset, data_type, size);
+ } else {
+ WriteSimpleData( fp, hdf_dataset, type, size);
}
fprintf(fp, "\n");
- for ( unsigned j=0; j<nbAttr; j++ )
+ for ( int j=0; j<nbAttr; j++ )
{
name = hdf_dataset->GetAttributeName(j);
HDFattribute *hdf_attribute = new HDFattribute(name, hdf_dataset);
- delete name;
+ delete [] name;
SaveAttributeInASCIIfile(hdf_attribute, fp, ident+1);
hdf_attribute = 0;
}
fprintf(fp, "%s\n", ATTRIBUTE_ID);
fprintf(fp, "%s %i %i\n", name, type, size);
- delete name;
+ delete [] name;
if (type == HDF_STRING) {
char* val = new char[size+1];
fprintf(fp, ":");
fwrite(val, 1, size, fp);
fprintf(fp, "\n");
- delete val;
+ delete [] val;
} else if (type == HDF_FLOAT64) {
hdf_float64 val;
hdf_attribute->ReadFromDisk(&val);
// Returns a name of directory where a created HDF file is placed
// The created file is named "hdf_from_ascii.hdf"
//============================================================================
-char* HDFascii::ConvertFromASCIIToHDF(const char* thePath)
+char* HDFascii::ConvertFromASCIIToHDF(const char* thePath,
+ bool isReplace)
{
- // Get a temporary directory to store a file
- string aTmpDir = GetTmpDir(), aFileName("hdf_from_ascii.hdf");
- // Build a full file name of temporary file
- string aFullName = aTmpDir + aFileName;
+ std::string aTmpDir, aFullName;
+ if(!isReplace) {
+ // Get a temporary directory to store a file
+ aTmpDir = GetTmpDir();
+ // Build a full file name of temporary file
+ aFullName = aTmpDir + "hdf_from_ascii.hdf";
+ }
+ else {
+ aTmpDir = thePath;
+ aFullName = std::string(thePath)+".ascii_tmp";
+ }
+
+ FILE *fp = fopen(thePath, "rb");
+ if(!fp) return NULL;
HDFfile *hdf_file = new HDFfile((char*)aFullName.c_str());
hdf_file->CreateOnDisk();
- FILE *fp = fopen(thePath, "r");
- if(!fp) return NULL;
-
char type[9];
int nbsons, i;
fscanf(fp, "%s", type);
if(strcmp(id_of_begin, GROUP_ID) == 0) {
if(!CreateGroupFromASCII(hdf_file, fp)) {
- cout << "ConvertFromASCIIToHDF : Can not create group number " << i << endl;
+ std::cout << "ConvertFromASCIIToHDF : Can not create group number " << i << std::endl;
return NULL;
}
}
else if(strcmp(id_of_begin, DATASET_ID) == 0) {
if(!CreateDatasetFromASCII(hdf_file, fp)) {
- cout << "ConvertFromASCIIToHDF :Can not create dataset number " << i << endl;
+ std::cout << "ConvertFromASCIIToHDF :Can not create dataset number " << i << std::endl;
return NULL;
}
}
else if(strcmp(id_of_begin, ATTRIBUTE_ID) == 0) {
if(!CreateAttributeFromASCII(hdf_file, fp)) {
- cout << "ConvertFromASCIIToHDF :Can not create attribute number " << i << endl;
+ std::cout << "ConvertFromASCIIToHDF :Can not create attribute number " << i << std::endl;
return NULL;
}
}
else
- cout << "ConvertFromASCIIToHDF : Unrecognized type " << id_of_begin << endl;
+ std::cout << "ConvertFromASCIIToHDF : Unrecognized type " << id_of_begin << std::endl;
}
char id_of_end[MAX_ID_SIZE];
fscanf(fp, "%s", id_of_end);
if(strcmp(id_of_end, ASCIIHDF_ID_END) != 0) {
- cout << "ConvertFromASCIIToHDF : Can not find the end ASCII token " << endl;
+ std::cout << "ConvertFromASCIIToHDF : Can not find the end ASCII token " << std::endl;
return false;
}
hdf_file->CloseOnDisk();
delete hdf_file;
+ if(isReplace) {
+ if(Exists(aFullName))
+ Move(aFullName, thePath);
+ else
+ return NULL;
+ }
+
int length = strlen(aTmpDir.c_str());
char *new_str = new char[ 1+length ];
strcpy(new_str , aTmpDir.c_str()) ;
+ fclose(fp);
+
return new_str;
}
HDFgroup* hdf_group = new HDFgroup(new_name, father);
- delete new_name;
+ delete [] new_name;
hdf_group->CreateOnDisk();
if(strcmp(id_of_begin, GROUP_ID) == 0) {
if(!CreateGroupFromASCII(hdf_group, fp)) {
- cout << "Can not create subgroup " << i << " for group " << name << endl;
+ 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)) {
- cout << "Can not create dataset " << i << " for group " << name << endl;
+ 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)) {
- cout << "Can not create attribute " << i << " for group " << name << endl;
+ std::cout << "Can not create attribute " << i << " for group " << name << std::endl;
return false;
}
}
else
- cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << endl;
+ std::cout << "CreateGroupFromASCII : Unrecognized type " << id_of_begin << std::endl;
}
hdf_group->CloseOnDisk();
char id_of_end[MAX_ID_SIZE];
fscanf(fp, "%s\n", id_of_end);
if(strcmp(id_of_end, GROUP_ID_END) != 0) {
- cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << endl;
+ std::cout << "CreateGroupFromASCII : Invalid end token : " << id_of_end << std::endl;
return false;
}
sizeArray[i] = dim;
}
- // order (2-d member) was not written in earlier versions
+ // 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 ":"
if ( type != HDF_FLOAT64 ) // use order only for FLOAT64
order = H5T_ORDER_NONE;
- HDFdataset* hdf_dataset = new HDFdataset(new_name, father,type, sizeArray, nbDim, order);
- delete new_name;
- delete sizeArray;
+
+ 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();
char *val = new char[size+1];
fread(val, 1, size, fp);
hdf_dataset->WriteOnDisk(val);
- delete 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;
+ 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;
+ 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;
+ 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];
if(strcmp(token, ATTRIBUTE_ID) == 0) {
if(!CreateAttributeFromASCII(hdf_dataset, fp)) {
- cout << "Can not create attribute " << i << " for dataset " << name << endl;
+ std::cout << "Can not create attribute " << i << " for dataset " << name << std::endl;
return false;
}
}
else {
- cout << "CreateGroupFromASCII : Unrecognized type " << token << endl;
+ std::cout << "CreateGroupFromASCII : Unrecognized type " << token << std::endl;
return false;
}
}
fscanf(fp, "%s\n", token);
if(strcmp(token, DATASET_ID_END) != 0) {
- cout << "CreateDatasetFromASCII : Invalid end token : " << token << endl;
+ 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;
}
hdf_attribute->CreateOnDisk();
- delete new_name;
+ delete [] new_name;
if (type == HDF_STRING) {
char tmp;
val[size] = (char)0;
fread(val, 1, size, fp);
hdf_attribute->WriteOnDisk(val);
- delete val;
+ delete [] val;
} else if (type == HDF_FLOAT64) {
hdf_float64 val;
read_float64(fp, &val);
char id_of_end[MAX_ID_SIZE];
fscanf(fp, "%s\n", id_of_end);
if(strcmp(id_of_end, ATTRIBUTE_ID_END) != 0) {
- cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << endl;
+ std::cout << "CreateAttributeFromASCII : Invalid end token : " << id_of_end << std::endl;
return false;
}
// function : GetTempDir
// purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
//============================================================================
-string GetTmpDir()
+std::string GetTmpDir()
{
-
//Find a temporary directory to store a file
-
- string aTmpDir;
-
+ std::string aTmpDir;
char *Tmp_dir = getenv("SALOME_TMP_DIR");
if(Tmp_dir != NULL) {
- aTmpDir = string(Tmp_dir);
+ aTmpDir = std::string(Tmp_dir);
if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;
-/*#ifdef WIN32
- if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
-#else
- if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
-#endif*/
}
else {
#ifdef WIN32
- aTmpDir = string("C:\\");
+ aTmpDir = std::string("C:\\");
#else
- aTmpDir = string("/tmp/");
+ aTmpDir = std::string("/tmp/");
#endif
}
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);
- string aSubDir(buffer);
- if(aSubDir.size() <= 1) aSubDir = string("123409876");
+ 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;
-/*
-#ifdef WIN32
- if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
-#else
- if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
-#endif
- */
- string aDir = aTmpDir;
-
+ std::string aDir = aTmpDir;
+
for(aRND = 0; Exists(aDir); aRND++) {
sprintf(buffer, "%d", aRND);
aDir = aTmpDir+buffer; //Build a unique directory name
char* makeName(char* name)
{
- string aName(name), aNewName;
+ std::string aName(name), aNewName;
int i, length = aName.size();
char replace = (char)19;
char* restoreName(char* name)
{
- string aName(name), aNewName;
+ std::string aName(name), aNewName;
int i, length = aName.size();
char replace = (char)19;
}
}
-bool Exists(const string thePath)
+bool Exists(const std::string thePath)
{
#ifdef WIN32
- if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
- if ( GetLastError () != ERROR_FILE_NOT_FOUND ) {
+ if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
+ DWORD errorId = GetLastError ();
+ if ( errorId == ERROR_FILE_NOT_FOUND || errorId == ERROR_PATH_NOT_FOUND )
return false;
- }
}
#else
int status = access ( thePath.c_str() , F_OK );
return true;
}
-void Move(const string& fName, const string& fNameDst)
+void Move(const std::string& fName, const std::string& fNameDst)
{
#ifdef WIN32
MoveFileEx (fName.c_str(), fNameDst.c_str(),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
#endif
}
+void WriteSimpleData( FILE* fp, HDFdataset *hdf_dataset, hdf_type type, long size ) {
+ 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;
+ }
+}