00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "eyedbconfig.h"
00025
00026 #ifdef HAVE_TIME_H
00027 #include <time.h>
00028 #endif
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032
00033 #include <eyedblib/machtypes.h>
00034 #include <eyedblib/butils.h>
00035 #include <eyedblib/thread.h>
00036
00037 #define IS_INT(c) (c == 'D' || c == 'I' || c == 'O' || c == 'X')
00038
00039 #define NBUFS 8
00040
00041 namespace eyedblib {
00042 static Mutex mt;
00043 }
00044
00045 namespace eyedblib {
00046 char *
00047 getFBuffer(const char *fmt, va_list ap)
00048 {
00049 static int argstr_alloc;
00050 static enum Type {INT, LONG, STR} *argstr;
00051 static int buffer_which;
00052 static int buffer_len[NBUFS];
00053 static char *buffer[NBUFS];
00054
00055 MutexLocker _(mt);
00056 int n;
00057 int argstr_cnt = 0;
00058 int argint_cnt = 0;
00059 int arg_cnt = 0;
00060 unsigned int len = 0;
00061
00062 unsigned int len_1 = strlen(fmt);
00063 for (; ;fmt++)
00064 {
00065 char c = *fmt;
00066 char num[32];
00067 char *pnum;
00068 int is_long;
00069
00070 if (!c)
00071 break;
00072
00073 if (c != '%')
00074 {
00075 len++;
00076 continue;
00077 }
00078
00079 is_long = 0;
00080 pnum = num;
00081 while (c = *++fmt)
00082 {
00083 if (!c)
00084 break;
00085
00086 if (c >= '0' && c <= '9')
00087 {
00088 *pnum++ = c;
00089 continue;
00090 }
00091
00092 if (c == 'l')
00093 {
00094 is_long++;
00095 continue;
00096 }
00097
00098 c = (c >= 'a' && c <= 'z' ? c + 'A'-'a' : c);
00099
00100 if (IS_INT(c) || c == 'U' || c == 'E' || c == 'G' || c == 'C')
00101 {
00102 if (c == 'U' && (IS_INT(*(fmt+1)) || *(fmt+1) == 'l'))
00103 continue;
00104
00105 if (arg_cnt >= argstr_alloc)
00106 {
00107 argstr_alloc = arg_cnt + 12;
00108 argstr = (enum Type *)
00109 realloc(argstr, sizeof(enum Type)*argstr_alloc);
00110 }
00111
00112 if (is_long > 1)
00113 {
00114 argstr[arg_cnt++] = LONG;
00115 argint_cnt++;
00116 }
00117 else
00118 argstr[arg_cnt++] = INT;
00119
00120 argint_cnt++;
00121 break;
00122 }
00123
00124 if (c == 'S')
00125 {
00126 if (arg_cnt >= argstr_alloc)
00127 {
00128 argstr_alloc = arg_cnt + 12;
00129 argstr = (enum Type *)
00130 realloc(argstr, sizeof(enum Type)*argstr_alloc);
00131 }
00132
00133 argstr[arg_cnt++] = STR;
00134 argstr_cnt++;
00135 break;
00136 }
00137
00138 if (c == '%')
00139 {
00140 len++;
00141 break;
00142 }
00143
00144
00145
00146
00147
00148 len += 2;
00149 break;
00150 }
00151
00152 if (pnum != num)
00153 {
00154 *pnum = 0;
00155 len += atoi(num);
00156 }
00157 }
00158
00159 len += argint_cnt * 20;
00160
00161 for (n = 0; n < arg_cnt; n++)
00162 {
00163 if (argstr[n] == STR)
00164 len += strlen(va_arg(ap, char *));
00165 else if (argstr[n] == LONG)
00166 (void)va_arg(ap, long long);
00167 else
00168 (void)va_arg(ap, int);
00169 }
00170
00171 if (buffer_which >= NBUFS)
00172 buffer_which = 0;
00173
00174 if (len+1 >= buffer_len[buffer_which])
00175 {
00176 free(buffer[buffer_which]);
00177 buffer_len[buffer_which] = len + 128 + 1024;
00178 buffer[buffer_which] = (char *)malloc(buffer_len[buffer_which]);
00179 }
00180
00181 return buffer[buffer_which++];
00182 }
00183
00184 void display_time(const char *fmt, ...)
00185 {
00186 va_list ap;
00187 struct timeval tp;
00188
00189 gettimeofday(&tp, NULL);
00190
00191 #ifdef HAVE_UNSIGNED_LONG_LONG
00192 unsigned long long ms = (unsigned long long)tp.tv_sec * 1000 + tp.tv_usec/1000;
00193 #endif
00194
00195 va_start(ap, fmt);
00196
00197 vfprintf(stdout, fmt, ap);
00198 fprintf(stdout, ": %lld ms\n", ms);
00199 va_end(ap);
00200 }
00201
00202 int
00203 is_number(const char *s)
00204 {
00205 char c;
00206 if (!*s)
00207 return 0;
00208
00209 while (c = *s++)
00210 if (!(c >= '0' && c <= '9'))
00211 return 0;
00212
00213 return 1;
00214 }
00215
00216 #define USEC_PER_SECOND 1000000
00217 #define USEC_PER_MS 1000
00218
00219 const char *setbuftime(eyedblib::int64 t)
00220 {
00221 #define NT 4
00222 static char buftim[NT][64];
00223 static int nt;
00224 char *ds;
00225
00226 time_t sec = t / USEC_PER_SECOND;
00227 eyedblib::int64 usec = t % USEC_PER_SECOND;
00228 const char *s = ctime(&sec);
00229
00230 if (nt == NT)
00231 nt = 0;
00232
00233 ds = buftim[nt++];
00234 strcpy(ds, s);
00235 ds[strlen(ds)-1] = 0;
00236
00237 char buf[64];
00238 sprintf(buf, " %03d.%03dms", (int)(usec / USEC_PER_MS),
00239 (int)(usec % USEC_PER_MS));
00240 strcat(ds, buf);
00241
00242 return ds;
00243 }
00244 }