xDataMisc.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 <string.h>
00038 #include <ctype.h>
00039 #if !defined(WIN32) || defined(__GNUC__)
00040    #include <unistd.h>
00041 #else
00042    #include <direct.h>
00043    #define getcwd _getcwd
00044 #endif
00045 #include "xData.h"
00046 
00047 #if defined __cplusplus
00048 namespace GIDI {
00049 using namespace GIDI;
00050 #endif
00051 
00052 
00053 /*
00054 ************************************************************
00055 */
00056 void *xData_malloc( statusMessageReporting *smr, size_t size, int zero, const char *forItem, const char *file, int line ) {
00057 
00058     void *p = xData_realloc( smr, NULL, size, forItem, file, line );
00059     int i;
00060     char *c;
00061     long long *l;
00062 
00063     if( ( p != NULL ) && zero ) {
00064         //for( i = 0, l = (long long *) p; i < size / sizeof( long long ); i++, l++ ) *l = 0;
00065         for( i = 0, l = (long long *) p; i < (int)( size / sizeof( long long ) ); i++, l++ ) *l = 0;
00066         //for( i = sizeof( long long ) * i, c = (char *) l; i < size; i++, c++ ) *c = 0;
00067         for( i = sizeof( long long ) * i, c = (char *) l; i < (int) size; i++, c++ ) *c = 0;
00068     }
00069 
00070     return( p );
00071 }
00072 /*
00073 ************************************************************
00074 */
00075 void *xData_realloc( statusMessageReporting *smr, void *pOld, size_t size, const char *forItem, const char *file, int line ) {
00076 
00077     void *p = realloc( pOld, size );
00078 
00079     if( ( p == NULL ) && ( smr != NULL ) ) {
00080         smr_setMessageError( smr, NULL, file, line, -1, " xData_realloc: failed to realloc size = %llu for variable %s\n", 
00081             (unsigned long long) size, forItem );
00082     }
00083     return( p );
00084 }
00085 /*
00086 ************************************************************
00087 */
00088 //void *xData_free( statusMessageReporting *smr, void *p ) {
00089 void *xData_free( statusMessageReporting *, void *p ) {
00090 
00091     if( p != NULL ) free( p );
00092     return( NULL );
00093 }
00094 /*
00095 ************************************************************
00096 */
00097 char *xDataMisc_allocateCopyString( statusMessageReporting *smr, const char *s, const char *forItem, const char *file, int line ) {
00098 /*
00099 *   User must free returned string.
00100 */
00101     char *c;
00102 
00103     //if( ( c = xData_malloc( smr, strlen( s ) + 1, 0, forItem, file, line ) ) != NULL ) {
00104     if( ( c = (char*) xData_malloc( smr, strlen( s ) + 1, 0, forItem, file, line ) ) != NULL ) {
00105         strcpy( c, s );
00106     }
00107     return( c );
00108 }
00109 /*
00110 ************************************************************
00111 */
00112 char *xDataMisc_getAbsPath( statusMessageReporting *smr, const char *fileName ) {
00113 /*
00114 *   User must free returned string.
00115 */
00116     int n = strlen( fileName ) + 1, nCwd = 0;
00117     //char *absPath, cwd[4 * 1024] = "", *p, *needle;
00118     char *absPath, cwd[4 * 1024 + 1] = "", *p, *needle;
00119 
00120     if( fileName[0] != '/' ) {
00121         //if( getcwd( cwd, sizeof( cwd ) + 1 ) == NULL ) {
00122         if( getcwd( cwd, sizeof( cwd ) ) == NULL ) {
00123             smr_setMessageError( smr, NULL, __FILE__, __LINE__, -1, "hardwired cwd too small" );
00124             return( NULL );
00125         }
00126         nCwd = strlen( cwd );
00127         n += nCwd + 1;                                  /* cwd + '/'. */
00128     }
00129     //if( ( absPath = xData_malloc2( smr, n, 0, "absPath" ) ) == NULL ) return( NULL );
00130     if( ( absPath = (char*) xData_malloc2( smr, n, 0, "absPath" ) ) == NULL ) return( NULL );
00131     if( fileName[0] != '/' ) {
00132         strcpy( absPath, cwd );
00133         strcat( absPath, "/" );
00134         strcat( absPath, fileName ); }
00135     else {
00136         strcpy( absPath, fileName );
00137     }
00138 
00139     while( 1 ) {                                        /* Remove all ./ from path. */
00140         if( ( needle = strstr( absPath, "/./" ) ) == NULL ) break;
00141         p = needle;
00142         for( needle += 2; *needle; p++, needle++ ) *p = *needle;
00143         *p = 0;
00144     }
00145 
00146     while( 1 ) {                                        /* Remove all ../ from path. */
00147         if( ( needle = strstr( absPath, "/../" ) ) == NULL ) break;
00148         p = needle - 1;
00149         while( ( p > absPath ) && ( *p != '/' ) ) p--;
00150         if( *p != '/' ) break;                           /* This should not happen if path is legit, I think, and I do not know what to do so will leave it. */
00151         if( p == absPath ) break;                       /* Ditto. */
00152         for( needle += 3; *needle; p++, needle++ ) *p = *needle;
00153         *p = 0;
00154     }
00155     return( absPath );
00156 }
00157 /*
00158 ************************************************************
00159 */
00160 int xData_setMessageError_ReturnInt( int value, statusMessageReporting *smr, void *userInterface, const char *packageName, int lineNumber, int code, 
00161     const char *fmt, ... ) {
00162 
00163     va_list args;
00164 
00165     va_start( args, fmt );
00166     smr_setMessageError( smr, userInterface, packageName, lineNumber, code, fmt, args );
00167     va_end( args );
00168     return( value );
00169 }
00170 
00171 #if defined __cplusplus
00172 }
00173 #endif

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