gString.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 "gString.h"
00037 #include <xData.h>
00038 #include <string.h>
00039 
00040 #if defined __cplusplus
00041 namespace GIDI {
00042 using namespace GIDI;
00043 #endif
00044 
00045 /*
00046 ***************************************************
00047 */
00048 int gString_initialize( statusMessageReporting *smr, gString *gStr, int size, int increment ) {
00049 
00050     if( size > 0 ) {
00051         //if( ( gStr->gStr = xData_malloc2( smr, size + 1, 0, "gStr->gStr" ) ) == NULL ) return( 1 );
00052         if( ( gStr->gStr = (char*) xData_malloc2( smr, size + 1, 0, "gStr->gStr" ) ) == NULL ) return( 1 );
00053         gStr->length = 1;
00054         gStr->gStr[0] = 0; }
00055     else {
00056         size = 0;
00057         gStr->length = 0;
00058         gStr->gStr = NULL;
00059     }
00060     gStr->allocated = size;
00061     //if( increment < gString_minIncrement ) increment = gString_minIncrement;
00062     if( increment < (int) gString_minIncrement ) increment = gString_minIncrement;
00063     gStr->increment = increment;
00064     return( 0 );
00065 }
00066 /*
00067 ***************************************************
00068 */
00069 int gString_release( statusMessageReporting *smr, gString *gStr ) {
00070 
00071     if( gStr->gStr != NULL ) free( gStr->gStr );
00072     gString_initialize( smr, gStr, 0, gStr->increment );
00073     return( 0 );
00074 }
00075 /*
00076 ***************************************************
00077 */
00078 //int gString_clear( statusMessageReporting *smr, gString *gStr ) {
00079 int gString_clear( statusMessageReporting *, gString *gStr ) {
00080 
00081     if( gStr->gStr != NULL ) {
00082         gStr->length = 1;
00083         gStr->gStr[0] = 0;
00084     }
00085     return( 0 );
00086 }
00087 /*
00088 ***************************************************
00089 */
00090 int gString_addTo( statusMessageReporting *smr, gString *gStr, char const *str ) {
00091 
00092     int n, size = strlen( str );
00093 
00094     if( gStr->gStr == NULL ) {
00095         if( gString_initialize( smr, gStr, size + 1, gStr->increment ) != 0 ) return( 1 ); }
00096     else if( ( gStr->length + size ) > gStr->allocated ) {
00097         n = gStr->increment;
00098         if( n < size ) n = size;
00099         //if( ( gStr->gStr = xData_realloc2( smr, gStr->gStr, gStr->allocated + n, "gStr->gStr" ) ) == NULL ) return( 1 );
00100         if( ( gStr->gStr = (char*) xData_realloc2( smr, gStr->gStr, gStr->allocated + n, "gStr->gStr" ) ) == NULL ) return( 1 );
00101         gStr->allocated += n;
00102     }
00103     strcpy( &(gStr->gStr[gStr->length - 1]), str );
00104     gStr->length = gStr->length + size;
00105     return( 0 );
00106 }
00107 /*
00108 ***************************************************
00109 */
00110 //char const *gString_string( statusMessageReporting *smr, gString *gStr ) {
00111 char const *gString_string( statusMessageReporting *, gString *gStr ) {
00112 
00113     return( gStr->gStr );
00114 }
00115 /*
00116 ***************************************************
00117 */
00118 //int gString_length( statusMessageReporting *smr, gString *gStr ) {
00119 int gString_length( statusMessageReporting *, gString *gStr ) {
00120 
00121     return( gStr->length );
00122 }
00123 /*
00124 ***************************************************
00125 */
00126 //int gString_allocated( statusMessageReporting *smr, gString *gStr ) {
00127 int gString_allocated( statusMessageReporting *, gString *gStr ) {
00128 
00129     return( gStr->allocated );
00130 }
00131 /*
00132 ***************************************************
00133 */
00134 //int gString_increment( statusMessageReporting *smr, gString *gStr ) {
00135 int gString_increment( statusMessageReporting *, gString *gStr ) {
00136 
00137     return( gStr->increment );
00138 }
00139 
00140 #if defined __cplusplus
00141 }
00142 #endif

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