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 #include <stdarg.h>
00026 #include <ctype.h>
00027 #include <stdio.h>
00028 #include <time.h>
00029 #include <string.h>
00030
00031 #include "common.h"
00032
00033
00034 void ExitFunction()
00035 {
00036 PrintDebug("exitfunction: nothing to do (yet)\n");
00037 }
00038
00039 void __printlog(char * file_name, const char * funcname,int N,const char *format, ...)
00040 {
00041 va_list ap;
00042 if (VERBOSE>=0)
00043 {
00044 va_start(ap,format);
00045 if (VERBOSE>1)
00046 {
00047 if (funcname!=NULL)
00048 {
00049 fprintf(stderr,"%s [%s,%s,%d]: ",ARGV0,file_name,funcname,N);
00050 }
00051 else
00052 {
00053 fprintf(stderr,"%s [%s,%d]: ",ARGV0,file_name,N);
00054 }
00055 } else {
00056 fprintf(stderr,"%s: ",ARGV0);
00057 }
00058 vfprintf(stderr,format,ap);
00059 va_end(ap);
00060 }
00061 }
00062
00063 void __printwarning(char * file_name, const char * funcname,int N,const char *format, ...)
00064 {
00065 va_list ap;
00066 va_start(ap,format);
00067 if (VERBOSE>0) {
00068 if (funcname!=NULL)
00069 {
00070 fprintf(stderr,"%s [%s,%s,%d] warning: ",ARGV0,file_name,funcname,N);
00071 }
00072 else
00073 {
00074 fprintf(stderr,"%s [%s,%d] warning: ",ARGV0,file_name,N);
00075 }
00076 } else {
00077 fprintf(stderr,"%s warning: ",ARGV0);
00078 }
00079 vfprintf(stderr,format,ap);
00080 va_end(ap);
00081 }
00082
00083 void __printerror(char * file_name, const char * funcname,int N,const char *format, ...)
00084 {
00085 va_list ap;
00086 va_start(ap,format);
00087 if (VERBOSE>0) {
00088 if (funcname!=NULL)
00089 {
00090 fprintf(stderr,"%s [%s,%s,%d] error: ",ARGV0,file_name,funcname,N);
00091 }
00092 else
00093 {
00094 fprintf(stderr,"%s [%s,%d] error: ",ARGV0,file_name,N);
00095 }
00096 } else {
00097 fprintf(stderr,"%s error: ",ARGV0);
00098 }
00099 vfprintf(stderr,format,ap);
00100 va_end(ap);
00101 }
00102
00103 void badarg(const char *s)
00104 {
00105 fprintf(stderr,"%s error: bad argument '%s'\n",ARGV0,s);
00106 }
00107
00108 void badargnum()
00109 {
00110 fprintf(stderr,"%s error: bad argument number\n",ARGV0);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 int str2time(char *s,double *time)
00139 {
00140 int i,ok=0,l;
00141 struct tm tt;
00142 double sec=0.;
00143 time_t lt;
00144 char *fmt1="%d%*c%d%*c%d%*c%d%*c%d%*c%lf";
00145 char *fmt2="%04d%02d%02d%02d%02d%lf";
00146 char *fmt=NULL;
00147 if ((0==strcasecmp(s,"now"))||(0==strcasecmp(s,"present")))
00148 {
00149 *time=2145916800.0;
00150 return(6);
00151 }
00152 memset(&tt,0,sizeof(struct tm));
00153 for (i=strlen(s)-1; i; i--)
00154 if ((s[i]) && (s[i]==' ')) s[i]=0; else break;
00155 fmt=fmt2;
00156 l=strlen(s);
00157 for (i=0; i<l; i++)
00158 if (!isdigit(s[i]) && s[i]!='.') { fmt=fmt1; break; }
00159 l=strlen(s);
00160 for (i=0; i<l; i++)
00161 if (s[i]=='.') ok++;
00162 if (ok>1) fmt=fmt1;
00163 ok=sscanf(s,fmt,
00164 &tt.tm_year,
00165 &tt.tm_mon,
00166 &tt.tm_mday,
00167 &tt.tm_hour,
00168 &tt.tm_min,
00169 &sec);
00170 switch (ok) { case 1: tt.tm_mon=1;
00171 case 2: tt.tm_mday=1;
00172 case 3: tt.tm_hour=0;
00173 case 4: tt.tm_min=0;
00174 case 5: sec=0.0; tt.tm_sec=0;
00175 case 6: tt.tm_sec=(int)sec;
00176 sec-=(double)tt.tm_sec;
00177 }
00178 if (!tt.tm_mon) tt.tm_mon=1;
00179 if (!tt.tm_mday) tt.tm_mday=1;
00180 *time=sec;
00181 tt.tm_mon--;
00182 if (tt.tm_year>100) tt.tm_year-=1900;
00183 lt=mktime(&tt);
00184 if (lt<0) { PrintError("mktime error (%s)\n",s); return(-1); }
00185 *time+=(double)lt;
00186 return(ok);
00187 }
00188
00189 int strJ2time(char *s,double *time)
00190 {
00191 int i,ok=0,l;
00192 struct tm tt;
00193 double sec=0.;
00194 time_t lt;
00195 char *fmt1="%d%*c%d%*c%d%*c%d%*c%lf";
00196 char *fmt2="%04d%03d%02d%02d%lf";
00197 char *fmt=NULL;
00198 memset(&tt,0,sizeof(struct tm));
00199 for (i=strlen(s)-1; i; i--)
00200 if ((s[i]) && (s[i]==' ')) s[i]=0; else break;
00201 fmt=fmt2;
00202 l=strlen(s);
00203 for (i=0; i<l; i++)
00204 if (!isdigit(s[i]) && s[i]!='.') { fmt=fmt1; break; }
00205 l=strlen(s);
00206 for (i=0; i<l; i++)
00207 if (s[i]=='.') ok++;
00208 if (ok>1) fmt=fmt1;
00209 ok=sscanf(s,fmt,
00210 &tt.tm_year,
00211 &tt.tm_mday,
00212 &tt.tm_hour,
00213 &tt.tm_min,
00214 &sec);
00215 switch (ok) { case 1: tt.tm_mday=1;
00216 case 2: tt.tm_hour=0;
00217 case 3: tt.tm_min=0;
00218 case 4: sec=0.0; tt.tm_sec=0;
00219 case 5: tt.tm_sec=(int)sec;
00220 sec-=(double)tt.tm_sec;
00221 }
00222 if (!tt.tm_mon) tt.tm_mon=1;
00223 if (!tt.tm_mday) tt.tm_mday=1;
00224 *time=sec;
00225 tt.tm_mon=0;
00226 if (tt.tm_year>100) tt.tm_year-=1900;
00227 lt=mktime(&tt);
00228 if (lt<0) { PrintError("mktime error\n"); return(-1); }
00229 *time+=(double)lt;
00230 return(ok);
00231 }
00232
00233
00234 char *time2str(double temps)
00235 {
00236 static char ligne[40];
00237 time_t T0;
00238 struct tm tm0;
00239 T0=(time_t)temps;
00240 memcpy(&tm0,gmtime(&T0),sizeof(struct tm));
00241 memset(ligne,0,40);
00242 strftime(ligne,40,"%Y.%m.%d-%H:%M:%S.",&tm0);
00243 sprintf(ligne+strlen(ligne),"%06d",(int)(1000000.*(temps-(double)T0)));
00244 return(ligne);
00245 }
00246
00247 char * time2name(double temps,const char *pref, const char *suf)
00248 {
00249 time_t T0;
00250 static char ligne[255];
00251 struct tm tm0;
00252 T0=(time_t)temps;
00253 memcpy(&tm0,gmtime(&T0),sizeof(struct tm));
00254 memset(ligne,0,255);
00255 strcpy(ligne,(pref)?pref:"");
00256 strftime(ligne+strlen(ligne),40,"%Y.%m.%d-%H.%M.%S",&tm0);
00257 if (suf) strcat(ligne,suf);
00258 return(ligne);
00259 }
00260