00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "adio.h"
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 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 int ADIOI_Strncpy( char *dest, const char *src, size_t n )
00051 {
00052     char * restrict d_ptr = dest;
00053     const char * restrict s_ptr = src;
00054     register int i;
00055 
00056     i = (int)n;
00057     while (*s_ptr && i-- > 0) {
00058     *d_ptr++ = *s_ptr++;
00059     }
00060     
00061     if (i > 0) { 
00062     *d_ptr = 0;
00063     return 0;
00064     }
00065     else
00066     
00067 
00068     return 1;
00069 }
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 int ADIOI_Strnapp( char *dest, const char *src, size_t n )
00092 {
00093     char * restrict d_ptr = dest;
00094     const char * restrict s_ptr = src;
00095     register int i;
00096 
00097     
00098     i = (int)n;
00099     while (i-- > 0 && *d_ptr) d_ptr++;
00100     if (i <= 0) return 1;
00101 
00102     
00103     while (*s_ptr && i-- > 0) {
00104     *d_ptr++ = *s_ptr++;
00105     }
00106 
00107     
00108 
00109     if (i >= 0) { 
00110     *d_ptr = 0;
00111     return 0;
00112     }
00113     else {
00114     
00115     *--d_ptr = 0;
00116     
00117     
00118 
00119     return 1;
00120     }
00121 }
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 char *ADIOI_Strdup( const char *str )
00146 {
00147     char *p = ADIOI_Malloc( strlen(str) + 1 );
00148     char *in_p = (char *)str;
00149     char *save_p;
00150 
00151     save_p = p;
00152     if (p) {
00153     while (*in_p) {
00154         *p++ = *in_p++;
00155     }
00156     *p = '\0';
00157     }
00158     return save_p;
00159 }
00160 
00161 
00162 
00163 
00164 
00165 #ifndef HAVE_SNPRINTF
00166 #include <ctype.h>
00167 
00168 #include <stdarg.h>
00169 
00170 
00171 
00172 
00173 int ADIOI_Snprintf( char *str, size_t size, const char *format, ... )
00174 {
00175     int n;
00176     const char *p;
00177     char *out_str = str;
00178     va_list list;
00179 
00180     va_start(list, format);
00181 
00182     p = format;
00183     while (*p && size > 0) {
00184     char *nf;
00185 
00186     nf = strchr(p, '%');
00187     if (!nf) {
00188         
00189         while (size-- > 0 && *p) {
00190         *out_str++ = *p++;
00191         }
00192     }
00193     else {
00194         int nc;
00195         int width = -1;
00196 
00197         
00198         while (p < nf && size-- > 0) {
00199         *out_str++ = *p++;
00200         }
00201         
00202         
00203         nc = nf[1];
00204         if (isdigit(nc)) {
00205         
00206         
00207         width = nc - '0';
00208         p = nf + 2;
00209         while (*p && isdigit(*p)) {
00210             width = 10 * width + (*p++ - '0');
00211         }
00212         
00213 
00214         nc = *p++;
00215         }
00216         else {
00217         
00218         p += 2;
00219         }
00220 
00221         switch (nc) {
00222         case '%':
00223         *out_str++ = '%';
00224         size--;
00225         break;
00226 
00227         case 'd':
00228         {
00229         int val;
00230         char tmp[20];
00231         char *t = tmp;
00232         
00233         val = va_arg( list, int );
00234         sprintf( tmp, "%d", val );
00235         if (width > 0) {
00236             int tmplen = strlen(tmp);
00237             
00238 
00239             while (size-- > 0 && width-- > tmplen) 
00240             *out_str++ = ' ';
00241         }
00242         while (size-- > 0 && *t) {
00243             *out_str++ = *t++;
00244         }
00245         }
00246         break;
00247 
00248         case 'x':
00249         {
00250         int val;
00251         char tmp[20];
00252         char *t = tmp;
00253         
00254         val = va_arg( list, int );
00255         sprintf( tmp, "%x", val );
00256         if (width > 0) {
00257             int tmplen = strlen(tmp);
00258             
00259 
00260             while (size-- > 0 && width-- > tmplen) 
00261             *out_str++ = ' ';
00262         }
00263         while (size-- > 0 && *t) {
00264             *out_str++ = *t++;
00265         }
00266         }
00267         break;
00268 
00269         case 'p':
00270         {
00271         void *val;
00272         char tmp[20];
00273         char *t = tmp;
00274         val = va_arg( list, void * );
00275         sprintf( tmp, "%p", val );
00276         if (width > 0) {
00277             int tmplen = strlen(tmp);
00278             
00279 
00280             while (size-- > 0 && width-- > tmplen) 
00281             *out_str++ = ' ';
00282         }
00283         while (size-- > 0 && *t) {
00284             *out_str++ = *t++;
00285         }
00286         }
00287         break;
00288 
00289         case 's':
00290         {
00291         char *s_arg;
00292         
00293         s_arg = va_arg( list, char * );
00294         while (size-- > 0 && s_arg && *s_arg) {
00295             *out_str++ = *s_arg++;
00296         }
00297         }
00298         break;
00299 
00300         default:
00301         
00302         return -1;
00303         break;
00304         }
00305     }
00306     }
00307 
00308     va_end(list);
00309 
00310     if (size-- > 0) *out_str++ = '\0';
00311 
00312     n = (int)(out_str - str);
00313     return n;
00314 }
00315 #endif