Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xDataMisc.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 <string.h>
38 #include <ctype.h>
39 #if !defined(WIN32) || defined(__GNUC__)
40  #include <unistd.h>
41 #else
42  #include <direct.h>
43  #define getcwd _getcwd
44 #endif
45 #include "xData.h"
46 
47 #if defined __cplusplus
48 namespace GIDI {
49 using namespace GIDI;
50 #endif
51 
52 
53 /*
54 ************************************************************
55 */
56 void *xData_malloc( statusMessageReporting *smr, size_t size, int zero, const char *forItem, const char *file, int line ) {
57 
58  void *p = xData_realloc( smr, NULL, size, forItem, file, line );
59  int i;
60  char *c;
61  long long *l;
62 
63  if( ( p != NULL ) && zero ) {
64  //for( i = 0, l = (long long *) p; i < size / sizeof( long long ); i++, l++ ) *l = 0;
65  for( i = 0, l = (long long *) p; i < (int)( size / sizeof( long long ) ); i++, l++ ) *l = 0;
66  //for( i = sizeof( long long ) * i, c = (char *) l; i < size; i++, c++ ) *c = 0;
67  for( i = sizeof( long long ) * i, c = (char *) l; i < (int) size; i++, c++ ) *c = 0;
68  }
69 
70  return( p );
71 }
72 /*
73 ************************************************************
74 */
75 void *xData_realloc( statusMessageReporting *smr, void *pOld, size_t size, const char *forItem, const char *file, int line ) {
76 
77  void *p = realloc( pOld, size );
78 
79  if( ( p == NULL ) && ( smr != NULL ) ) {
80  smr_setMessageError( smr, NULL, file, line, -1, " xData_realloc: failed to realloc size = %llu for variable %s\n",
81  (unsigned long long) size, forItem );
82  }
83  return( p );
84 }
85 /*
86 ************************************************************
87 */
88 //void *xData_free( statusMessageReporting *smr, void *p ) {
89 void *xData_free( statusMessageReporting *, void *p ) {
90 
91  if( p != NULL ) free( p );
92  return( NULL );
93 }
94 /*
95 ************************************************************
96 */
97 char *xDataMisc_allocateCopyString( statusMessageReporting *smr, const char *s, const char *forItem, const char *file, int line ) {
98 /*
99 * User must free returned string.
100 */
101  char *c;
102 
103  //if( ( c = xData_malloc( smr, strlen( s ) + 1, 0, forItem, file, line ) ) != NULL ) {
104  if( ( c = (char*) xData_malloc( smr, strlen( s ) + 1, 0, forItem, file, line ) ) != NULL ) {
105  strcpy( c, s );
106  }
107  return( c );
108 }
109 /*
110 ************************************************************
111 */
112 char *xDataMisc_getAbsPath( statusMessageReporting *smr, const char *fileName ) {
113 /*
114 * User must free returned string.
115 */
116  int n = strlen( fileName ) + 1, nCwd = 0;
117  //char *absPath, cwd[4 * 1024] = "", *p, *needle;
118  char *absPath, cwd[4 * 1024 + 1] = "", *p, *needle;
119 
120  if( fileName[0] != '/' ) {
121  //if( getcwd( cwd, sizeof( cwd ) + 1 ) == NULL ) {
122  if( getcwd( cwd, sizeof( cwd ) ) == NULL ) {
123  smr_setMessageError( smr, NULL, __FILE__, __LINE__, -1, "hardwired cwd too small" );
124  return( NULL );
125  }
126  nCwd = strlen( cwd );
127  n += nCwd + 1; /* cwd + '/'. */
128  }
129  //if( ( absPath = xData_malloc2( smr, n, 0, "absPath" ) ) == NULL ) return( NULL );
130  if( ( absPath = (char*) xData_malloc2( smr, n, 0, "absPath" ) ) == NULL ) return( NULL );
131  if( fileName[0] != '/' ) {
132  strcpy( absPath, cwd );
133  strcat( absPath, "/" );
134  strcat( absPath, fileName ); }
135  else {
136  strcpy( absPath, fileName );
137  }
138 
139  while( 1 ) { /* Remove all ./ from path. */
140  if( ( needle = strstr( absPath, "/./" ) ) == NULL ) break;
141  p = needle;
142  for( needle += 2; *needle; p++, needle++ ) *p = *needle;
143  *p = 0;
144  }
145 
146  while( 1 ) { /* Remove all ../ from path. */
147  if( ( needle = strstr( absPath, "/../" ) ) == NULL ) break;
148  p = needle - 1;
149  while( ( p > absPath ) && ( *p != '/' ) ) p--;
150  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. */
151  if( p == absPath ) break; /* Ditto. */
152  for( needle += 3; *needle; p++, needle++ ) *p = *needle;
153  *p = 0;
154  }
155  return( absPath );
156 }
157 /*
158 ************************************************************
159 */
160 int xData_setMessageError_ReturnInt( int value, statusMessageReporting *smr, void *userInterface, const char *packageName, int lineNumber, int code,
161  const char *fmt, ... ) {
162 
163  va_list args;
164 
165  va_start( args, fmt );
166  smr_setMessageError( smr, userInterface, packageName, lineNumber, code, fmt, args );
167  va_end( args );
168  return( value );
169 }
170 
171 #if defined __cplusplus
172 }
173 #endif
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
const XML_Char * s
void free(void *__ptr)
Definition: hjmalloc.cc:140
const char * p
Definition: xmltok.h:285
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313
char * xDataMisc_allocateCopyString(statusMessageReporting *smr, const char *s, const char *forItem, const char *file, int line)
Definition: xDataMisc.cc:97
char * xDataMisc_getAbsPath(statusMessageReporting *smr, const char *fileName)
Definition: xDataMisc.cc:112
const G4int n
void * xData_malloc(statusMessageReporting *smr, size_t size, int zero, const char *forItem, const char *file, int line)
Definition: xDataMisc.cc:56
Definition: inftrees.h:24
void * xData_free(statusMessageReporting *, void *p)
Definition: xDataMisc.cc:89
const XML_Char int const XML_Char * value
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
int xData_setMessageError_ReturnInt(int value, statusMessageReporting *smr, void *userInterface, const char *packageName, int lineNumber, int code, const char *fmt,...)
Definition: xDataMisc.cc:160
void * xData_realloc(statusMessageReporting *smr, void *pOld, size_t size, const char *forItem, const char *file, int line)
Definition: xDataMisc.cc:75