| 1 | //------------------------------------------------------------------------- |
|---|
| 2 | // Filename : FacetAttribSet.cpp |
|---|
| 3 | // |
|---|
| 4 | // Purpose : Common attribute functionality for MBG |
|---|
| 5 | // |
|---|
| 6 | // Special Notes : |
|---|
| 7 | // |
|---|
| 8 | // Creator : Jason Kraftcheck |
|---|
| 9 | // |
|---|
| 10 | // Creation Date : 03/01/03 |
|---|
| 11 | //------------------------------------------------------------------------- |
|---|
| 12 | |
|---|
| 13 | #include "FacetAttribSet.hpp" |
|---|
| 14 | #include "FacetAttrib.hpp" |
|---|
| 15 | #include "CubitSimpleAttrib.hpp" |
|---|
| 16 | #include "CubitFileIOWrapper.hpp" |
|---|
| 17 | |
|---|
| 18 | void FacetAttribSet::append_attribute( CubitSimpleAttrib* csa ) |
|---|
| 19 | { |
|---|
| 20 | FacetAttrib* new_attrib = new FacetAttrib(csa); |
|---|
| 21 | new_attrib->listNext = listHead; |
|---|
| 22 | listHead = new_attrib; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | void FacetAttribSet::remove_attribute( CubitSimpleAttrib* csa ) |
|---|
| 26 | { |
|---|
| 27 | if( !listHead ) |
|---|
| 28 | return; |
|---|
| 29 | |
|---|
| 30 | FacetAttrib* attrib = 0; |
|---|
| 31 | if ( listHead->equals(csa) ) |
|---|
| 32 | { |
|---|
| 33 | attrib = listHead; |
|---|
| 34 | listHead = listHead->listNext; |
|---|
| 35 | delete attrib; |
|---|
| 36 | return; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | for ( FacetAttrib* prev = listHead; prev->listNext; prev = prev->listNext ) |
|---|
| 40 | { |
|---|
| 41 | if( prev->listNext->equals(csa) ) |
|---|
| 42 | { |
|---|
| 43 | attrib = prev->listNext; |
|---|
| 44 | prev->listNext = attrib->listNext; |
|---|
| 45 | delete attrib; |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void FacetAttribSet::remove_all_attributes() |
|---|
| 52 | { |
|---|
| 53 | while( listHead ) |
|---|
| 54 | { |
|---|
| 55 | FacetAttrib* dead = listHead; |
|---|
| 56 | listHead = dead->listNext; |
|---|
| 57 | delete dead; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | CubitStatus FacetAttribSet::get_attributes( DLIList<CubitSimpleAttrib*>& list ) const |
|---|
| 62 | { |
|---|
| 63 | for( FacetAttrib* attrib = listHead; attrib; attrib = attrib->listNext ) |
|---|
| 64 | list.append( attrib->get_CSA() ); |
|---|
| 65 | return CUBIT_SUCCESS; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | CubitStatus FacetAttribSet::get_attributes( const CubitString& name, |
|---|
| 69 | DLIList<CubitSimpleAttrib*>& list ) const |
|---|
| 70 | { |
|---|
| 71 | for( FacetAttrib* attrib = listHead; attrib; attrib = attrib->listNext ) |
|---|
| 72 | if( attrib->name() == name ) |
|---|
| 73 | list.append( attrib->get_CSA() ); |
|---|
| 74 | return CUBIT_SUCCESS; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | CubitStatus FacetAttribSet::save_attributes( FILE* file_ptr ) const |
|---|
| 78 | { |
|---|
| 79 | FacetAttrib *curr_attrib; |
|---|
| 80 | CubitStatus status = CUBIT_SUCCESS; |
|---|
| 81 | |
|---|
| 82 | //save # attribs |
|---|
| 83 | unsigned int size = attribute_count(); |
|---|
| 84 | NCubitFile::CIOWrapper wrapper( file_ptr ); |
|---|
| 85 | wrapper.Write( &size, 1 ); |
|---|
| 86 | |
|---|
| 87 | //save each attrib |
|---|
| 88 | for( curr_attrib = listHead; curr_attrib; curr_attrib = curr_attrib->listNext ) |
|---|
| 89 | if( !curr_attrib->save(file_ptr) ) |
|---|
| 90 | status = CUBIT_FAILURE; |
|---|
| 91 | |
|---|
| 92 | return status; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | CubitStatus FacetAttribSet::restore_attributes( FILE* file_ptr, unsigned endian ) |
|---|
| 96 | { |
|---|
| 97 | FacetAttrib *curr_attrib; |
|---|
| 98 | |
|---|
| 99 | //Read # attribs |
|---|
| 100 | unsigned int size; |
|---|
| 101 | NCubitFile::CIOWrapper wrapper( endian, file_ptr ); |
|---|
| 102 | wrapper.Read( &size, 1 ); |
|---|
| 103 | |
|---|
| 104 | for (unsigned i = 0; i < size; i++) |
|---|
| 105 | { |
|---|
| 106 | curr_attrib = FacetAttrib::restore( file_ptr, endian); |
|---|
| 107 | if (!curr_attrib) |
|---|
| 108 | { |
|---|
| 109 | // file corrupt? don't try to read any more |
|---|
| 110 | return CUBIT_FAILURE; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | curr_attrib->listNext = listHead; |
|---|
| 114 | listHead = curr_attrib; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | return CUBIT_SUCCESS; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | int FacetAttribSet::attribute_count() const |
|---|
| 122 | { |
|---|
| 123 | int count = 0; |
|---|
| 124 | for( FacetAttrib* attrib = listHead; attrib; attrib = attrib->listNext ) |
|---|
| 125 | count++; |
|---|
| 126 | return count; |
|---|
| 127 | } |
|---|