xData_2d_xy.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_twod_xy_ID = "2d.xy";
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_2d_xy( statusMessageReporting *smr, xData_element *element ) {
00055 
00056     xDataType *xDT = &(element->xDataTypeInfo);
00057 
00058     xDT->status = xData_xDataType_Ok;
00059     xDT->typeString = xData_twod_xy_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_2d_xy( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
00071 
00072     return( xData_is_xDataType( smr, xDT, xData_twod_xy_ID, setMsg ) );
00073 }
00074 /*
00075 ************************************************************
00076 */
00077 int xData_isElement_2d_xy( statusMessageReporting *smr, xData_element *element, int setMsg ) {
00078 
00079     return( xData_is_2d_xy( smr, &(element->xDataTypeInfo), setMsg ) );
00080 }
00081 /*
00082 ************************************************************
00083 */
00084 double *xData_2d_xy_allocateCopyData( statusMessageReporting *smr, xData_element *element, xData_Int *length ) {
00085 
00086     xData_Int i;
00087     xDataType *xDT = &(element->xDataTypeInfo);
00088     double *data;
00089 
00090     if( !xData_isElement_2d_xy( smr, element, 1 ) ) return( NULL );
00091     *length = xDT->length;
00092     //if( ( data = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) ) {
00093     if( ( data = (double*)xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) ) {
00094         for( i = 0; i < 2 * xDT->length; i++ ) data[i] = ((double *) xDT->data)[i];
00095     }
00096     return( data );
00097 }
00098 /*
00099 ************************************************************
00100 */
00101 int xData_2d_xy_free_copyData( statusMessageReporting *smr, void *data ) {
00102 
00103     xData_free( smr, data );
00104     return( 0 );
00105 }
00106 /*
00107 ************************************************************
00108 */
00109 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
00110 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
00111 
00112     xData_Int i, status = 0;
00113     char *e;
00114     const char *s;
00115     double *p;
00116     void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
00117 
00118     if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__,
00119         1, "bad xDataType instance" ) );
00120     release( smr, xDT );
00121     if( ( xDT->data = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "data" ) ) == NULL ) return( 1 );
00122     for( i = 0, s = text, p = (double *) xDT->data; i < 2 * xDT->length; i++, p++, s = e ) {
00123         if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
00124     }
00125     if( status == 0 ) {
00126         while( isspace( *e ) ) e++;
00127         if( *e != 0 ) {
00128             smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "text contains extra data = %s", e );
00129             status = 1;
00130         }
00131     }
00132     if( status != 0 ) release( smr, xDT );
00133     return( status );
00134 }
00135 /*
00136 ************************************************************
00137 */
00138 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
00139 static char *toString( statusMessageReporting *, xDataType *xDT ) {
00140 
00141     xData_Int i, n = xDT->length;
00142     char *str, *p;
00143     double *data = (double *) xDT->data;
00144 
00145     if( n < 0 ) n = 0;
00146     if( ( str = (char *) malloc( 2 * ( n + 1 ) * 17 ) ) == NULL ) return( NULL );
00147     for( i = 0, p = str; i < n; i++, p += 2 * 16 + 1, data += 2 ) {
00148         sprintf( p, " %15.7e %15.7e\n", *data, data[1] );
00149     }
00150     *p = 0;
00151     return( str );
00152 }
00153 /*
00154 ************************************************************
00155 */
00156 static int release( statusMessageReporting *smr, xDataType *xDT ) {
00157 
00158     if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
00159     return( xDT->status = xData_xDataType_Ok );
00160 }
00161 
00162 #if defined __cplusplus
00163 }
00164 #endif

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