xData_1d_x.cc

Go to the documentation of this file.
00001 /*
00002 # <<BEGIN-copyright>>
00003 # Copyright (c) 2010, Lawrence Livermore National Security, LLC. 
00004 # Produced at the Lawrence Livermore National Laboratory 
00005 # Written by Bret R. Beck, beck6@llnl.gov. 
00006 # CODE-461393
00007 # All rights reserved. 
00008 #  
00009 # This file is part of GIDI. For details, see nuclear.llnl.gov. 
00010 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov. 
00011 # 
00012 # Redistribution and use in source and binary forms, with or without modification, 
00013 # are permitted provided that the following conditions are met: 
00014 #
00015 #      1) Redistributions of source code must retain the above copyright notice, 
00016 #         this list of conditions and the disclaimer below.
00017 #      2) Redistributions in binary form must reproduce the above copyright notice, 
00018 #         this list of conditions and the disclaimer (as noted below) in the 
00019 #          documentation and/or other materials provided with the distribution.
00020 #      3) Neither the name of the LLNS/LLNL nor the names of its contributors may be 
00021 #         used to endorse or promote products derived from this software without 
00022 #         specific prior written permission. 
00023 #
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
00025 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00026 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT 
00027 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR 
00028 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00030 # OR SERVICES;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
00031 # AND ON  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00034 # <<END-copyright>>
00035 */
00036 #include <stdlib.h>
00037 #include <limits.h>
00038 #include <ctype.h>
00039 #include "xData.h"
00040 
00041 #if defined __cplusplus
00042 namespace GIDI {
00043 using namespace GIDI;
00044 #endif
00045 
00046 //char const * const xData_oned_x_ID = "1d.x";
00047 
00048 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
00049 static char *toString( statusMessageReporting *smr, xDataType *xDT );
00050 static int release( statusMessageReporting *smr, xDataType *xDT );
00051 /*
00052 ************************************************************
00053 */
00054 int xData_init_1d_x( statusMessageReporting *smr, xData_element *element ) {
00055 
00056     xDataType *xDT = &(element->xDataTypeInfo);
00057 
00058     xDT->status = xData_xDataType_Ok;
00059     xDT->typeString = xData_oned_x_ID;
00060     xDT->element = element;
00061     xDT->toData = toData;
00062     xDT->toString = toString;
00063     xDT->release = release;
00064     xDT->data = NULL;
00065     return( xData_xDataTypeConvertAttributes( smr, element ) );
00066 }
00067 /*
00068 ************************************************************
00069 */
00070 int xData_is_1d_x( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
00071 
00072     return( xData_is_xDataType( smr, xDT, xData_oned_x_ID, setMsg ) );
00073 }
00074 /*
00075 ************************************************************
00076 */
00077 int xData_isElement_1d_x( statusMessageReporting *smr, xData_element *element, int setMsg ) {
00078 
00079     return( xData_is_1d_x( smr, &(element->xDataTypeInfo), setMsg ) );
00080 }
00081 /*
00082 ************************************************************
00083 */
00084 int xData_1d_x_copyData( statusMessageReporting *smr, xData_element *element, xData_Int nAllocatedBytes, double *d ) {
00085 
00086     xData_Int i, n;
00087     xDataType *xDT = &(element->xDataTypeInfo);
00088     double *p;
00089 
00090     if( !xData_isElement_1d_x( smr, element, 1 ) ) return( 1 );
00091     n = xDT->end - xDT->start;
00092     //if( n * sizeof( double ) > nAllocatedBytes ) {
00093     if( n * sizeof( double ) > (size_t) nAllocatedBytes ) {
00094         void *smrUser = xData_get_smrUserInterfaceFromElement( element );
00095         smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "allocated memory = %lld to small, need %lld", nAllocatedBytes, n );
00096         return( 1 );
00097     }
00098     p = (double *) xDT->data;
00099     for( i = 0; i < n; i++, d++, p++ ) *d = *p;
00100     return( 0 );
00101 }
00102 /*
00103 ************************************************************
00104 */
00105 double *xData_1d_x_allocateCopyData( statusMessageReporting *smr, xData_element *element ) {
00106 
00107     xData_Int i, n;
00108     xDataType *xDT = &(element->xDataTypeInfo);
00109     double *p, *data;
00110 
00111     if( !xData_isElement_1d_x( smr, element, 1 ) ) return( NULL );
00112     n = xDT->end - xDT->start;
00113     p = (double *) xDT->data;
00114     //if( ( data = xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( NULL );
00115     if( ( data = (double*) xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( NULL );
00116     for( i = 0; i < n; i++, p++ ) data[i] = *p;
00117     return( data );
00118 }
00119 /*
00120 ************************************************************
00121 */
00122 int xData_1d_x_free_copyData( statusMessageReporting *smr, void *data ) {
00123 
00124     xData_free( smr, data );
00125     return( 0 );
00126 }
00127 /*
00128 ************************************************************
00129 */
00130 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
00131 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
00132 
00133     xData_Int i, n, status = 0;
00134     char *e;
00135     const char *s;
00136     double *p;
00137     void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
00138 
00139     if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
00140     release( smr, xDT );
00141 
00142     n = xDT->end - xDT->start;
00143     if( ( xDT->data = xData_malloc2( smr, n * sizeof( double ), 0, "1d.x-toData" ) ) == NULL ) return( 1 );
00144     for( i = 0, s = text, p = (double *) xDT->data; i < n; i++, p++, s = e ) {
00145         if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
00146     }
00147     if( status == 0 ) {
00148         while( isspace( *e ) ) e++;
00149         if( *e != 0 ) {
00150             smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "xData.1d.x contains extra data = %s", e );
00151             status = 1;
00152         }
00153     }
00154     if( status != 0 ) release( smr, xDT );
00155     return( status );
00156 }
00157 /*
00158 ************************************************************
00159 */
00160 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
00161 static char *toString( statusMessageReporting *, xDataType *xDT ) {
00162 
00163     xData_Int i, n;
00164     char *str, *p;
00165     double *data = (double *) xDT->data;
00166 
00167     n = xDT->end - xDT->start;
00168     if( n < 0 ) n = 0;
00169     if( ( str = (char *) malloc( ( n + 1 ) * 17 ) ) == NULL ) return( NULL );
00170     for( i = 0, p = str; i < n; i++, p += 17, data++ ) {
00171         sprintf( p, " %15.7e\n", *data );
00172     }
00173     *p = 0;
00174     return( str );
00175 }
00176 /*
00177 ************************************************************
00178 */
00179 static int release( statusMessageReporting *smr, xDataType *xDT ) {
00180 
00181     if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
00182     return( xDT->status = xData_xDataType_Ok );
00183 }
00184 
00185 #if defined __cplusplus
00186 }
00187 #endif

Generated on Mon May 27 17:50:36 2013 for Geant4 by  doxygen 1.4.7