Geant4  10.00.p02
xData_1d_x.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_oned_x_ID = "1d.x";
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_1d_x( statusMessageReporting *smr, xData_element *element ) {
55 
56  xDataType *xDT = &(element->xDataTypeInfo);
57 
58  xDT->status = xData_xDataType_Ok;
59  xDT->typeString = xData_oned_x_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_1d_x( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
71 
72  return( xData_is_xDataType( smr, xDT, xData_oned_x_ID, setMsg ) );
73 }
74 /*
75 ************************************************************
76 */
77 int xData_isElement_1d_x( statusMessageReporting *smr, xData_element *element, int setMsg ) {
78 
79  return( xData_is_1d_x( smr, &(element->xDataTypeInfo), setMsg ) );
80 }
81 /*
82 ************************************************************
83 */
84 int xData_1d_x_copyData( statusMessageReporting *smr, xData_element *element, xData_Int nAllocatedBytes, double *d ) {
85 
86  xData_Int i, n;
87  xDataType *xDT = &(element->xDataTypeInfo);
88  double *p;
89 
90  if( !xData_isElement_1d_x( smr, element, 1 ) ) return( 1 );
91  n = xDT->end - xDT->start;
92  //if( n * sizeof( double ) > nAllocatedBytes ) {
93  if( n * sizeof( double ) > (size_t) nAllocatedBytes ) {
94  void *smrUser = xData_get_smrUserInterfaceFromElement( element );
95  smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "allocated memory = %lld to small, need %lld", nAllocatedBytes, n );
96  return( 1 );
97  }
98  p = (double *) xDT->data;
99  for( i = 0; i < n; i++, d++, p++ ) *d = *p;
100  return( 0 );
101 }
102 /*
103 ************************************************************
104 */
105 double *xData_1d_x_allocateCopyData( statusMessageReporting *smr, xData_element *element ) {
106 
107  xData_Int i, n;
108  xDataType *xDT = &(element->xDataTypeInfo);
109  double *p, *data;
110 
111  if( !xData_isElement_1d_x( smr, element, 1 ) ) return( NULL );
112  n = xDT->end - xDT->start;
113  p = (double *) xDT->data;
114  //if( ( data = xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( NULL );
115  if( ( data = (double*) xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( NULL );
116  for( i = 0; i < n; i++, p++ ) data[i] = *p;
117  return( data );
118 }
119 /*
120 ************************************************************
121 */
122 int xData_1d_x_free_copyData( statusMessageReporting *smr, void *data ) {
123 
124  xData_free( smr, data );
125  return( 0 );
126 }
127 /*
128 ************************************************************
129 */
130 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
131 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
132 
133  xData_Int i, n, status = 0;
134  char *e;
135  const char *s;
136  double *p;
137  void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
138 
139  if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
140  release( smr, xDT );
141 
142  n = xDT->end - xDT->start;
143  if( ( xDT->data = xData_malloc2( smr, n * sizeof( double ), 0, "1d.x-toData" ) ) == NULL ) return( 1 );
144  for( i = 0, s = text, p = (double *) xDT->data; i < n; i++, p++, s = e ) {
145  if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
146  }
147  if( status == 0 ) {
148  while( isspace( *e ) ) e++;
149  if( *e != 0 ) {
150  smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "xData.1d.x contains extra data = %s", e );
151  status = 1;
152  }
153  }
154  if( status != 0 ) release( smr, xDT );
155  return( status );
156 }
157 /*
158 ************************************************************
159 */
160 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
161 static char *toString( statusMessageReporting *, xDataType *xDT ) {
162 
163  xData_Int i, n;
164  char *str, *p;
165  double *data = (double *) xDT->data;
166 
167  n = xDT->end - xDT->start;
168  if( n < 0 ) n = 0;
169  if( ( str = (char *) malloc( ( n + 1 ) * 17 ) ) == NULL ) return( NULL );
170  for( i = 0, p = str; i < n; i++, p += 17, data++ ) {
171  sprintf( p, " %15.7e\n", *data );
172  }
173  *p = 0;
174  return( str );
175 }
176 /*
177 ************************************************************
178 */
179 static int release( statusMessageReporting *smr, xDataType *xDT ) {
180 
181  if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
182  return( xDT->status = xData_xDataType_Ok );
183 }
184 
185 #if defined __cplusplus
186 }
187 #endif
static int release(statusMessageReporting *smr, xDataType *xDT)
Definition: xData_1d_x.cc:179
int xData_xDataTypeConvertAttributes(statusMessageReporting *smr, xData_element *element)
Definition: xData.cc:668
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
double * xData_1d_x_allocateCopyData(statusMessageReporting *smr, xData_element *element)
Definition: xData_1d_x.cc:105
int xData_isElement_1d_x(statusMessageReporting *smr, xData_element *element, int setMsg)
Definition: xData_1d_x.cc:77
static char * toString(statusMessageReporting *smr, xDataType *xDT)
Definition: xData_1d_x.cc:161
static const double s
Definition: G4SIunits.hh:150
int xData_1d_x_free_copyData(statusMessageReporting *smr, void *data)
Definition: xData_1d_x.cc:122
const G4int n
int xData_is_1d_x(statusMessageReporting *smr, xDataType *xDT, int setMsg)
Definition: xData_1d_x.cc:70
int xData_1d_x_copyData(statusMessageReporting *smr, xData_element *element, xData_Int nAllocatedBytes, double *d)
Definition: xData_1d_x.cc:84
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_init_1d_x(statusMessageReporting *smr, xData_element *element)
Definition: xData_1d_x.cc:54
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
static int toData(statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text)
Definition: xData_1d_x.cc:131