Geant4  10.00.p01
tpia_frame.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory
5 # Written by Bret R. Beck, beck6@llnl.gov.
6 # CODE-461393
7 # All rights reserved.
8 #
9 # This file is part of GIDI. For details, see nuclear.llnl.gov.
10 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # 1) Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the disclaimer below.
17 # 2) Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the disclaimer (as noted below) in the
19 # documentation and/or other materials provided with the distribution.
20 # 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21 # used to endorse or promote products derived from this software without
22 # specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # <<END-copyright>>
35 */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <tpia_target.h>
40 #include <tpia_misc.h>
41 #include "G4Types.hh"
42 
43 #if defined __cplusplus
44 namespace GIDI {
45 using namespace GIDI;
46 #endif
47 
48 static G4ThreadLocal int nlab_Str = 0, nCOM_Str = 0;
49 static const char lab_Str[] = "lab", COM_Str[] = "centerOfMass";
50 /*
51 ************************************************************
52 */
53 //int tpia_frame_clear( statusMessageReporting *smr, tpia_data_frame *frame ) {
54 int tpia_frame_clear( statusMessageReporting *, tpia_data_frame *frame ) {
55 
56  frame->frames = 0;
57  return( 0 );
58 }
59 /*
60 ************************************************************
61 */
62 int tpia_frame_setFromElement( statusMessageReporting *smr, xData_element *element, int dimension, tpia_data_frame *frame ) {
63 
64  char const *value = xData_getAttributesValueInElement( element, "frame" );
65 
66  if( value == NULL ) {
67  tpia_misc_setMessageError_Element( smr, NULL, element, __FILE__, __LINE__, 1, "element is missing frame attribute" );
68  return( 1 );
69  }
70  return( tpia_frame_setFromString( smr, element->fullName, value, dimension, frame ) );
71 }
72 /*
73 ************************************************************
74 */
75 int tpia_frame_setFromString( statusMessageReporting *smr, const char *forItem, const char *value, int dimension, tpia_data_frame *frame ) {
76 
77  char const *e;
78  int status = 1, i;
79 
80  for( i = 0; i < tpia_maxNumberOfFrames; i++ ) tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_None );
81  if( dimension > tpia_maxNumberOfFrames ) {
82  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "dimension argument = %d is greater than tpia_maxNumberOfFrames = %d", dimension,
83  tpia_maxNumberOfFrames );
84  return( status );
85  }
86  for( i = 0, e = value; ( i < dimension ) && ( *e != 0 ); i++ ) {
87  if( strstr( e, lab_Str ) == e ) {
88  tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_lab );
89  e += 3; }
90  else if( strstr( e, COM_Str ) == e ) {
91  tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_COM );
92  e += 12; }
93  else {
94  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame '%s' for %s", value, forItem );
95  break;
96  }
97  if( *e != 0 ) {
98  if( *e != ',' ) {
99  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad separater for frame '%s' for %s", value, forItem );
100  break;
101  }
102  e++;
103  }
104  }
105  if( smr_isOk( smr ) ) {
106  if( i == dimension ) {
107  if( *e == 0 ) {
108  status = 0; }
109  else {
110  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "extra values for frame '%s' for %s", value, forItem );
111  } }
112  else {
113  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "missing values for frame '%s' for %s", value, forItem );
114  }
115  }
116  return( status );
117 }
118 /*
119 ************************************************************
120 */
121 //int tpia_frame_getDimensions( statusMessageReporting *smr, tpia_data_frame *frame ) {
122 int tpia_frame_getDimensions( statusMessageReporting *, tpia_data_frame *frame ) {
123 
124  int i, dimension = 0;
125  unsigned value = frame->frames;
126 
127  for( i = 0; i < tpia_maxNumberOfFrames; i++ ) {
128  if( ( value & 3 ) == 0 ) break;
129  dimension++;
130  value = value >> 2;
131  }
132  return( dimension );
133 }
134 /*
135 ************************************************************
136 */
137 //char *tpia_frame_toString( statusMessageReporting *smr, const char *fromItem, tpia_data_frame *frame ) {
138 char *tpia_frame_toString( statusMessageReporting *smr, const char *, tpia_data_frame *frame ) {
139 
140  int i, n = tpia_frame_getDimensions( smr, frame ), value, nStr = 0;
141  char *str = NULL, *p;
142 
143  if( nlab_Str == 0 ) {
144  nlab_Str = strlen( lab_Str );
145  nCOM_Str = strlen( COM_Str );
146  }
147  for( i = 0; i < n; i++ ) {
148  value = tpia_frame_getColumn( smr, frame, i );
149  if( value == tpia_referenceFrame_COM ) {
150  nStr += nCOM_Str + 1; }
151  else if( value == tpia_referenceFrame_lab ) {
152  nStr += nlab_Str + 1; }
153  else {
154  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, i );
155  return( NULL );
156  }
157  }
158  if( nStr == 0 ) nStr = 1;
159  //if( ( str = xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
160  if( ( str = (char*) xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
161  for( i = 0, p = str - 1; i < n; i++ ) {
162  p++;
163  value = tpia_frame_getColumn( smr, frame, i );
164  if( value == tpia_referenceFrame_COM ) {
165  strcpy( p, COM_Str );
166  p += nCOM_Str; }
167  else {
168  strcpy( p, lab_Str );
169  p += nlab_Str;
170  }
171  *p = ',';
172  }
173  *p = 0;
174  return( str );
175 }
176 /*
177 ************************************************************
178 */
179 int tpia_frame_setColumns( statusMessageReporting *smr, tpia_data_frame *frame, int nColumns, int *values ) {
180 
181  int i;
182 
183  tpia_frame_clear( smr, frame );
184  for( i = 0; i < nColumns; i++ ) if( tpia_frame_setColumn( smr, frame, i, values[i] ) != 0 ) return( 1 );
185 
186  return( 0 );
187 }
188 /*
189 ************************************************************
190 */
191 int tpia_frame_setColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column, int value ) {
192 
193  int i;
194 
195  if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
196  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for setting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
197  return( 1 ); }
198  else {
199  if( ( value < tpia_referenceFrame_None ) || ( value > tpia_referenceFrame_Max ) ) {
200  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, column );
201  return( 1 ); }
202  else {
203  i = 3 << ( 2 * column );
204  frame->frames = frame->frames & (~i);
205  value = value << ( 2 * column );
206  frame->frames = frame->frames | value;
207  }
208  }
209  return( 0 );
210 }
211 /*
212 ************************************************************
213 */
214 int tpia_frame_getColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column ) {
215 
216  unsigned int value;
217 
218  if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
219  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for getting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
220  return( -1 ); }
221  else {
222  value = frame->frames >> ( 2 * column );
223  }
224  return( (int) ( value & 3 ) );
225 }
226 
227 #if defined __cplusplus
228 }
229 #endif
int tpia_frame_setColumns(statusMessageReporting *smr, tpia_data_frame *frame, int nColumns, int *values)
Definition: tpia_frame.cc:179
int tpia_frame_clear(statusMessageReporting *, tpia_data_frame *frame)
Definition: tpia_frame.cc:54
static G4ThreadLocal int nlab_Str
Definition: tpia_frame.cc:48
static const char COM_Str[]
Definition: tpia_frame.cc:49
int tpia_misc_setMessageError_Element(statusMessageReporting *smr, void *userInterface, xData_element *element, const char *file, int line, int code, const char *fmt,...)
Definition: tpia_misc.cc:183
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
const char * xData_getAttributesValueInElement(xData_element *element, const char *name)
Definition: xData.cc:546
int tpia_frame_setFromString(statusMessageReporting *smr, const char *forItem, const char *value, int dimension, tpia_data_frame *frame)
Definition: tpia_frame.cc:75
#define G4ThreadLocal
Definition: tls.hh:52
int smr_isOk(statusMessageReporting *smr)
int tpia_frame_getColumn(statusMessageReporting *smr, tpia_data_frame *frame, int column)
Definition: tpia_frame.cc:214
static G4ThreadLocal int nCOM_Str
Definition: tpia_frame.cc:48
const G4int n
int tpia_frame_setColumn(statusMessageReporting *smr, tpia_data_frame *frame, int column, int value)
Definition: tpia_frame.cc:191
int tpia_frame_getDimensions(statusMessageReporting *, tpia_data_frame *frame)
Definition: tpia_frame.cc:122
int tpia_frame_setFromElement(statusMessageReporting *smr, xData_element *element, int dimension, tpia_data_frame *frame)
Definition: tpia_frame.cc:62
static const char lab_Str[]
Definition: tpia_frame.cc:49
char * tpia_frame_toString(statusMessageReporting *smr, const char *, tpia_data_frame *frame)
Definition: tpia_frame.cc:138