00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00065 for( i = 0, l = (long long *) p; i < (int)( size / sizeof( long long ) ); i++, l++ ) *l = 0;
00066
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
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
00100
00101 char *c;
00102
00103
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
00115
00116 int n = strlen( fileName ) + 1, nCwd = 0;
00117
00118 char *absPath, cwd[4 * 1024 + 1] = "", *p, *needle;
00119
00120 if( fileName[0] != '/' ) {
00121
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;
00128 }
00129
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 ) {
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 ) {
00147 if( ( needle = strstr( absPath, "/../" ) ) == NULL ) break;
00148 p = needle - 1;
00149 while( ( p > absPath ) && ( *p != '/' ) ) p--;
00150 if( *p != '/' ) break;
00151 if( p == absPath ) break;
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