00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <tpia_target.h>
00040 #include <tpia_misc.h>
00041
00042 #if defined __cplusplus
00043 namespace GIDI {
00044 using namespace GIDI;
00045 #endif
00046
00047 static int nlab_Str = 0, nCOM_Str = 0;
00048 static char lab_Str[] = "lab", COM_Str[] = "centerOfMass";
00049
00050
00051
00052
00053 int tpia_frame_clear( statusMessageReporting *, tpia_data_frame *frame ) {
00054
00055 frame->frames = 0;
00056 return( 0 );
00057 }
00058
00059
00060
00061 int tpia_frame_setFromElement( statusMessageReporting *smr, xData_element *element, int dimension, tpia_data_frame *frame ) {
00062
00063 char const *value = xData_getAttributesValueInElement( element, "frame" );
00064
00065 if( value == NULL ) {
00066 tpia_misc_setMessageError_Element( smr, NULL, element, __FILE__, __LINE__, 1, "element is missing frame attribute" );
00067 return( 1 );
00068 }
00069 return( tpia_frame_setFromString( smr, element->fullName, value, dimension, frame ) );
00070 }
00071
00072
00073
00074 int tpia_frame_setFromString( statusMessageReporting *smr, const char *forItem, const char *value, int dimension, tpia_data_frame *frame ) {
00075
00076 char const *e;
00077 int status = 1, i;
00078
00079 for( i = 0; i < tpia_maxNumberOfFrames; i++ ) tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_None );
00080 if( dimension > tpia_maxNumberOfFrames ) {
00081 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "dimension argument = %d is greater than tpia_maxNumberOfFrames = %d", dimension,
00082 tpia_maxNumberOfFrames );
00083 return( status );
00084 }
00085 for( i = 0, e = value; ( i < dimension ) && ( *e != 0 ); i++ ) {
00086 if( strstr( e, lab_Str ) == e ) {
00087 tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_lab );
00088 e += 3; }
00089 else if( strstr( e, COM_Str ) == e ) {
00090 tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_COM );
00091 e += 12; }
00092 else {
00093 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame '%s' for %s", value, forItem );
00094 break;
00095 }
00096 if( *e != 0 ) {
00097 if( *e != ',' ) {
00098 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad separater for frame '%s' for %s", value, forItem );
00099 break;
00100 }
00101 e++;
00102 }
00103 }
00104 if( smr_isOk( smr ) ) {
00105 if( i == dimension ) {
00106 if( *e == 0 ) {
00107 status = 0; }
00108 else {
00109 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "extra values for frame '%s' for %s", value, forItem );
00110 } }
00111 else {
00112 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "missing values for frame '%s' for %s", value, forItem );
00113 }
00114 }
00115 return( status );
00116 }
00117
00118
00119
00120
00121 int tpia_frame_getDimensions( statusMessageReporting *, tpia_data_frame *frame ) {
00122
00123 int i, dimension = 0;
00124 unsigned value = frame->frames;
00125
00126 for( i = 0; i < tpia_maxNumberOfFrames; i++ ) {
00127 if( ( value & 3 ) == 0 ) break;
00128 dimension++;
00129 value = value >> 2;
00130 }
00131 return( dimension );
00132 }
00133
00134
00135
00136
00137 char *tpia_frame_toString( statusMessageReporting *smr, const char *, tpia_data_frame *frame ) {
00138
00139 int i, n = tpia_frame_getDimensions( smr, frame ), value, nStr = 0;
00140 char *str = NULL, *p;
00141
00142 if( nlab_Str == 0 ) {
00143 nlab_Str = strlen( lab_Str );
00144 nCOM_Str = strlen( COM_Str );
00145 }
00146 for( i = 0; i < n; i++ ) {
00147 value = tpia_frame_getColumn( smr, frame, i );
00148 if( value == tpia_referenceFrame_COM ) {
00149 nStr += nCOM_Str + 1; }
00150 else if( value == tpia_referenceFrame_lab ) {
00151 nStr += nlab_Str + 1; }
00152 else {
00153 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, i );
00154 return( NULL );
00155 }
00156 }
00157 if( nStr == 0 ) nStr = 1;
00158
00159 if( ( str = (char*) xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
00160 for( i = 0, p = str - 1; i < n; i++ ) {
00161 p++;
00162 value = tpia_frame_getColumn( smr, frame, i );
00163 if( value == tpia_referenceFrame_COM ) {
00164 strcpy( p, COM_Str );
00165 p += nCOM_Str; }
00166 else {
00167 strcpy( p, lab_Str );
00168 p += nlab_Str;
00169 }
00170 *p = ',';
00171 }
00172 *p = 0;
00173 return( str );
00174 }
00175
00176
00177
00178 int tpia_frame_setColumns( statusMessageReporting *smr, tpia_data_frame *frame, int nColumns, int *values ) {
00179
00180 int i;
00181
00182 tpia_frame_clear( smr, frame );
00183 for( i = 0; i < nColumns; i++ ) if( tpia_frame_setColumn( smr, frame, i, values[i] ) != 0 ) return( 1 );
00184
00185 return( 0 );
00186 }
00187
00188
00189
00190 int tpia_frame_setColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column, int value ) {
00191
00192 int i;
00193
00194 if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
00195 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for setting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
00196 return( 1 ); }
00197 else {
00198 if( ( value < tpia_referenceFrame_None ) || ( value > tpia_referenceFrame_Max ) ) {
00199 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, column );
00200 return( 1 ); }
00201 else {
00202 i = 3 << ( 2 * column );
00203 frame->frames = frame->frames & (~i);
00204 value = value << ( 2 * column );
00205 frame->frames = frame->frames | value;
00206 }
00207 }
00208 return( 0 );
00209 }
00210
00211
00212
00213 int tpia_frame_getColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column ) {
00214
00215 unsigned int value;
00216
00217 if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
00218 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for getting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
00219 return( -1 ); }
00220 else {
00221 value = frame->frames >> ( 2 * column );
00222 }
00223 return( (int) ( value & 3 ) );
00224 }
00225
00226 #if defined __cplusplus
00227 }
00228 #endif