Geant4  10.00.p02
xData_2d_xy.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 <stdlib.h>
37 #include <limits.h>
38 #include <ctype.h>
39 #include "xData.h"
40 
41 #if defined __cplusplus
42 namespace GIDI {
43 using namespace GIDI;
44 #endif
45 
46 //char const * const xData_twod_xy_ID = "2d.xy";
47 
48 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
49 static char *toString( statusMessageReporting *smr, xDataType *xDT );
50 static int release( statusMessageReporting *smr, xDataType *xDT );
51 /*
52 ************************************************************
53 */
54 int xData_init_2d_xy( statusMessageReporting *smr, xData_element *element ) {
55 
56  xDataType *xDT = &(element->xDataTypeInfo);
57 
58  xDT->status = xData_xDataType_Ok;
59  xDT->typeString = xData_twod_xy_ID;
60  xDT->element = element;
61  xDT->toData = toData;
62  xDT->toString = toString;
63  xDT->release = release;
64  xDT->data = NULL;
65  return( xData_xDataTypeConvertAttributes( smr, element ) );
66 }
67 /*
68 ************************************************************
69 */
70 int xData_is_2d_xy( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
71 
72  return( xData_is_xDataType( smr, xDT, xData_twod_xy_ID, setMsg ) );
73 }
74 /*
75 ************************************************************
76 */
77 int xData_isElement_2d_xy( statusMessageReporting *smr, xData_element *element, int setMsg ) {
78 
79  return( xData_is_2d_xy( smr, &(element->xDataTypeInfo), setMsg ) );
80 }
81 /*
82 ************************************************************
83 */
84 double *xData_2d_xy_allocateCopyData( statusMessageReporting *smr, xData_element *element, xData_Int *length ) {
85 
86  xData_Int i;
87  xDataType *xDT = &(element->xDataTypeInfo);
88  double *data;
89 
90  if( !xData_isElement_2d_xy( smr, element, 1 ) ) return( NULL );
91  *length = xDT->length;
92  //if( ( data = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) ) {
93  if( ( data = (double*)xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) ) {
94  for( i = 0; i < 2 * xDT->length; i++ ) data[i] = ((double *) xDT->data)[i];
95  }
96  return( data );
97 }
98 /*
99 ************************************************************
100 */
101 int xData_2d_xy_free_copyData( statusMessageReporting *smr, void *data ) {
102 
103  xData_free( smr, data );
104  return( 0 );
105 }
106 /*
107 ************************************************************
108 */
109 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
110 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
111 
112  xData_Int i, status = 0;
113  char *e;
114  const char *s;
115  double *p;
116  void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
117 
118  if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__,
119  1, "bad xDataType instance" ) );
120  release( smr, xDT );
121  if( ( xDT->data = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) == NULL ) return( 1 );
122  for( i = 0, s = text, p = (double *) xDT->data; i < 2 * xDT->length; i++, p++, s = e ) {
123  if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
124  }
125  if( status == 0 ) {
126  while( isspace( *e ) ) e++;
127  if( *e != 0 ) {
128  smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "text contains extra data = %s", e );
129  status = 1;
130  }
131  }
132  if( status != 0 ) release( smr, xDT );
133  return( status );
134 }
135 /*
136 ************************************************************
137 */
138 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
139 static char *toString( statusMessageReporting *, xDataType *xDT ) {
140 
141  xData_Int i, n = xDT->length;
142  char *str, *p;
143  double *data = (double *) xDT->data;
144 
145  if( n < 0 ) n = 0;
146  if( ( str = (char *) malloc( 2 * ( n + 1 ) * 17 ) ) == NULL ) return( NULL );
147  for( i = 0, p = str; i < n; i++, p += 2 * 16 + 1, data += 2 ) {
148  sprintf( p, " %15.7e %15.7e\n", *data, data[1] );
149  }
150  *p = 0;
151  return( str );
152 }
153 /*
154 ************************************************************
155 */
156 static int release( statusMessageReporting *smr, xDataType *xDT ) {
157 
158  if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
159  return( xDT->status = xData_xDataType_Ok );
160 }
161 
162 #if defined __cplusplus
163 }
164 #endif
int xData_init_2d_xy(statusMessageReporting *smr, xData_element *element)
Definition: xData_2d_xy.cc:54
int xData_xDataTypeConvertAttributes(statusMessageReporting *smr, xData_element *element)
Definition: xData.cc:668
int xData_is_2d_xy(statusMessageReporting *smr, xDataType *xDT, int setMsg)
Definition: xData_2d_xy.cc:70
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
double * xData_2d_xy_allocateCopyData(statusMessageReporting *smr, xData_element *element, xData_Int *length)
Definition: xData_2d_xy.cc:84
static const double s
Definition: G4SIunits.hh:150
int xData_isElement_2d_xy(statusMessageReporting *smr, xData_element *element, int setMsg)
Definition: xData_2d_xy.cc:77
static int toData(statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text)
Definition: xData_2d_xy.cc:110
int xData_2d_xy_free_copyData(statusMessageReporting *smr, void *data)
Definition: xData_2d_xy.cc:101
const G4int n
static char * toString(statusMessageReporting *smr, xDataType *xDT)
Definition: xData_2d_xy.cc:139
static int release(statusMessageReporting *smr, xDataType *xDT)
Definition: xData_2d_xy.cc:156
int xData_is_xDataType(statusMessageReporting *smr, xDataType *xDT, char const *const type, int setMsg)
Definition: xData.cc:900
void * xData_free(statusMessageReporting *, void *p)
Definition: xDataMisc.cc:89
int xData_stringTo_double(statusMessageReporting *smr, void *smrUserInterface, char const *c, double *value, char const *endings, char **e)
Definition: xData.cc:1044
void * xData_get_smrUserInterfaceFromElement(xData_element *element)
Definition: xData.cc:952
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
int xData_setMessageError_ReturnInt(int value, statusMessageReporting *smr, void *userInterface, const char *packageName, int lineNumber, int code, const char *fmt,...)
Definition: xDataMisc.cc:160