cgma
FacetAttrib.cpp
Go to the documentation of this file.
00001 #include "FacetAttrib.hpp"
00002 #include "CubitString.hpp"
00003 #include "CubitSimpleAttrib.hpp"
00004 #include "CubitFileIOWrapper.hpp"
00005 #include "CubitMessage.hpp"
00006 #include <algorithm>
00007 
00008   // Constructor - copy from CubitSimpleAttrib
00009 FacetAttrib::FacetAttrib( const CubitSimpleAttrib& csa ) : listNext(0)
00010 {
00011   int i;
00012 
00013     // save counts
00014   numStrings = csa.string_data_list().size();
00015   numDoubles = csa.double_data_list().size();
00016   numIntegers = csa.int_data_list().size();
00017 
00018     // allocate arrays, but don't try to allocate zero-length arrays
00019   stringArray = numStrings ? new CubitString[numStrings] : NULL;
00020   doubleArray = numDoubles ? new double[numDoubles] : NULL;
00021   integerArray = numIntegers ? new int[numIntegers] : NULL;
00022 
00023     // copy data into arrays
00024   for( i = 0; i < numStrings; i++ )
00025     stringArray[i] = csa.string_data_list()[i];
00026   for( i = 0; i < numIntegers; i++ )
00027     integerArray[i] = csa.int_data_list()[i];
00028   for( i = 0; i < numDoubles; i++ )
00029       doubleArray[i] = csa.double_data_list()[i];
00030 }
00031 
00032   // Private constructor for use by restore(FILE*)
00033 FacetAttrib::FacetAttrib( int string_count, CubitString strings[],
00034                           int double_count, double doubles[],
00035                           int int_count, int integers[] )
00036 : stringArray(strings), doubleArray(doubles), integerArray(integers),
00037   numStrings(string_count),  numDoubles(double_count), numIntegers(int_count),
00038   listNext(0)
00039 {}
00040 
00041 
00042   // Destructor -- free arrays
00043 FacetAttrib::~FacetAttrib()
00044 {
00045     // "delete"ing NULL pointers is okay.
00046   delete [] integerArray;
00047   delete [] doubleArray;
00048   delete [] stringArray;
00049 }
00050 
00051   // Copy this into a new CubitSimpleAttrib
00052 CubitSimpleAttrib FacetAttrib::get_CSA() const
00053 {
00054     // Set initial list size
00055   std::vector<CubitString> string_list(numStrings);
00056   std::vector<int> int_list(numIntegers);
00057   std::vector<double> double_list(numDoubles);
00058 
00059     // Don't need to 'new' objects in DLIList because
00060     // CSA will make copies.  Just put addresses in list.
00061   int i;
00062   for( i = 0; i < numStrings; i++ )
00063     string_list[i] = stringArray[i];
00064   for( i = 0; i < numIntegers; i++ )
00065     int_list[i] = integerArray[i];
00066   for( i = 0; i < numDoubles; i++ )
00067     double_list[i] = doubleArray[i];
00068 
00069   return CubitSimpleAttrib( &string_list, &double_list, &int_list );
00070 }
00071 
00072   // compare to a CubitSimpleAttrib
00073 bool FacetAttrib::equals( const CubitSimpleAttrib& csa ) const
00074 {
00075   // compare counts
00076   if( csa.int_data_list().size() != (size_t)numIntegers ||
00077       csa.double_data_list().size() != (size_t)numDoubles ||
00078       csa.string_data_list().size() != (size_t)numStrings )
00079     return false;
00080 
00081     // compare strings first because most likely the
00082     // first string (the name) will differ.
00083   if(!std::equal(stringArray, stringArray+numStrings, csa.string_data_list().begin()))
00084     return false;
00085   if(!std::equal(doubleArray, doubleArray+numDoubles, csa.double_data_list().begin()))
00086     return false;
00087   if(!std::equal(integerArray, integerArray+numIntegers, csa.int_data_list().begin()))
00088     return false;
00089 
00090   return true;
00091 }
00092 
00093   // write to a file at the current file offset
00094 CubitStatus FacetAttrib::save(FILE *save_file) const
00095 {
00096   if( save_file == NULL)
00097   {
00098     PRINT_ERROR("Problem saving MBG attributes: null FILE ptr\n");
00099     return CUBIT_FAILURE;
00100   }
00101 
00102   NCubitFile::CIOWrapper wrapper(save_file);
00103 
00104   // write a version number for the attribute data
00105   unsigned int Attrib_Version = 1;
00106   wrapper.Write(&Attrib_Version, 1);
00107 
00108   // write the number of strings, number of doubles, and number of integers
00109   int counts[3] = { numStrings, numDoubles, numIntegers };
00110   wrapper.Write(reinterpret_cast<unsigned int*>(counts), 3);
00111 
00112   // write the string data
00113   int i;
00114   for( i = 0; i < numStrings; i++ )
00115     wrapper.Write(stringArray[i].c_str());
00116 
00117   // write the doubles
00118   wrapper.Write(doubleArray, numDoubles);
00119 
00120   // write the integers
00121   wrapper.Write(reinterpret_cast<unsigned int*>(integerArray), numIntegers);
00122   
00123   return CUBIT_SUCCESS;
00124 }
00125 
00126 
00127   // read from file starting at current file offset
00128 FacetAttrib* FacetAttrib::restore(FILE *restore_file, unsigned int endian)
00129 {
00130   if( restore_file == NULL )
00131     return NULL;
00132 
00133   NCubitFile::CIOWrapper wrapper(endian, restore_file );
00134 
00135   // write a version number for the attribute data
00136   unsigned int version;
00137   wrapper.Read(&version, 1);
00138 
00139   // haven't handled any version changes yet
00140   if( version != 1 )
00141   {
00142     PRINT_ERROR("Wrong FacetAttrib version : %u\n", version );
00143     return NULL;
00144   }
00145 
00146   // read the number of strings, number of doubles, and number of integers
00147   int counts[3];
00148   wrapper.Read(reinterpret_cast<unsigned int*>(counts), 3);
00149   int n_strings = counts[0];
00150   int n_doubles = counts[1];
00151   int n_ints = counts[2];
00152   
00153     // allocate arrays, but don't try to allocate zero-length array
00154   CubitString* strings = n_strings ? new CubitString[n_strings] : NULL;
00155   double *doubles = n_doubles ? new double[n_doubles] : NULL;
00156   int *ints = n_ints ? new int[n_ints] : NULL;
00157   
00158   // read the string data
00159   int i;
00160   for( i = 0; i < n_strings; i++ )
00161   {
00162     char *string = wrapper.Read();
00163     strings[i] = CubitString(string);
00164     delete [] string;
00165   }
00166 
00167   // write the doubles
00168   wrapper.Read(doubles, n_doubles);
00169 
00170   // write the integers
00171   wrapper.Read(reinterpret_cast<unsigned int*>(ints), n_ints);
00172 
00173   return new FacetAttrib(n_strings, strings, n_doubles, doubles, n_ints, ints);
00174 }
00175 
00176 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines