Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
xDataMisc.cc File Reference
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "xData.h"

Go to the source code of this file.

Functions

void * xData_malloc (statusMessageReporting *smr, size_t size, int zero, const char *forItem, const char *file, int line)
 
void * xData_realloc (statusMessageReporting *smr, void *pOld, size_t size, const char *forItem, const char *file, int line)
 
void * xData_free (statusMessageReporting *, void *p)
 
char * xDataMisc_allocateCopyString (statusMessageReporting *smr, const char *s, const char *forItem, const char *file, int line)
 
char * xDataMisc_getAbsPath (statusMessageReporting *smr, const char *fileName)
 
int xData_setMessageError_ReturnInt (int value, statusMessageReporting *smr, void *userInterface, const char *packageName, int lineNumber, int code, const char *fmt,...)
 

Function Documentation

◆ xData_free()

◆ xData_malloc()

void * xData_malloc ( statusMessageReporting smr,
size_t  size,
int  zero,
const char *  forItem,
const char *  file,
int  line 
)

Definition at line 56 of file xDataMisc.cc.

56 {
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}
void * xData_realloc(statusMessageReporting *smr, void *pOld, size_t size, const char *forItem, const char *file, int line)
Definition: xDataMisc.cc:75

Referenced by xDataMisc_allocateCopyString().

◆ xData_realloc()

void * xData_realloc ( statusMessageReporting smr,
void *  pOld,
size_t  size,
const char *  forItem,
const char *  file,
int  line 
)

Definition at line 75 of file xDataMisc.cc.

75 {
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}
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)

Referenced by xData_malloc().

◆ xData_setMessageError_ReturnInt()

int xData_setMessageError_ReturnInt ( int  value,
statusMessageReporting smr,
void *  userInterface,
const char *  packageName,
int  lineNumber,
int  code,
const char *  fmt,
  ... 
)

Definition at line 160 of file xDataMisc.cc.

161 {
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}

◆ xDataMisc_allocateCopyString()

char * xDataMisc_allocateCopyString ( statusMessageReporting smr,
const char *  s,
const char *  forItem,
const char *  file,
int  line 
)

Definition at line 97 of file xDataMisc.cc.

97 {
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}
void * xData_malloc(statusMessageReporting *smr, size_t size, int zero, const char *forItem, const char *file, int line)
Definition: xDataMisc.cc:56

◆ xDataMisc_getAbsPath()

char * xDataMisc_getAbsPath ( statusMessageReporting smr,
const char *  fileName 
)

Definition at line 112 of file xDataMisc.cc.

112 {
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}
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313

Referenced by tpia_target_heated_read(), and tpia_target_read().