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 <sys/time.h>
00025
00026 #include <FS.H>
00027
00028 #include <stdio.h>
00029
00030 #define USECS_PER_SEC 1000000
00031
00032 struct Delta_Time
00033 {
00034 unsigned long secs;
00035 unsigned long usecs;
00036
00037
00038 void read(FILE *input) throw(FS::EndOfFile)
00039 {
00040 unsigned short file_secs;
00041 size_t n_read =
00042 fread((void *) &file_secs, sizeof(file_secs), 1, input);
00043 if(n_read < 1)
00044 {
00045 throw FS::EndOfFile();
00046 }
00047 assert(n_read == 1);
00048 secs = file_secs;
00049 unsigned int file_usecs;
00050 n_read = fread((void *) &file_usecs, sizeof(file_usecs), 1, input);
00051 if(n_read < 1)
00052 {
00053 throw FS::EndOfFile();
00054 }
00055 assert(n_read == 1);
00056 usecs = file_usecs;
00057 }
00058
00059 Delta_Time(FILE *input) throw(FS::EndOfFile)
00060 {
00061 read(input);
00062 }
00063
00064 Delta_Time()
00065 : secs(0), usecs(0)
00066 {
00067 }
00068
00069
00070 void usecs_overflow()
00071 {
00072 if(usecs > USECS_PER_SEC)
00073 {
00074 secs += usecs/USECS_PER_SEC;
00075 usecs %= USECS_PER_SEC;
00076 }
00077 }
00078
00079 bool operator>(const Delta_Time &other) const
00080 {
00081 return ((secs > other.secs) ||
00082 ((secs == other.secs) &&
00083 (usecs > other.usecs)));
00084 }
00085
00086 bool operator>=(const Delta_Time &other) const
00087 {
00088 return ((secs > other.secs) ||
00089 ((secs == other.secs) &&
00090 (usecs >= other.usecs)));
00091 }
00092
00093 Delta_Time operator-(const Delta_Time &other) const
00094 {
00095 Delta_Time result;
00096
00097 if(secs == other.secs)
00098 {
00099 result.secs = 0;
00100 assert(usecs >= other.usecs);
00101 result.usecs = usecs - other.usecs;
00102 }
00103 else
00104 {
00105 assert(secs > other.secs);
00106 result.secs = (secs - other.secs) - 1;
00107 result.usecs = (usecs + USECS_PER_SEC) - other.usecs;
00108 result.usecs_overflow();
00109 }
00110
00111 return result;
00112 }
00113
00114 Delta_Time operator+(const Delta_Time &other) const
00115 {
00116 Delta_Time result;
00117
00118 result.secs = secs + other.secs;
00119 result.usecs = usecs + other.usecs;
00120 result.usecs_overflow();
00121
00122 return result;
00123 }
00124
00125 Delta_Time &operator+=(const Delta_Time &other)
00126 {
00127 secs += other.secs;
00128 usecs += other.usecs;
00129 usecs_overflow();
00130
00131 return *this;
00132 }
00133
00134 Delta_Time &operator=(const Delta_Time &other)
00135 {
00136 secs = other.secs;
00137 usecs = other.usecs;
00138
00139 return *this;
00140 }
00141
00142 double msecs() const
00143 {
00144 double result = ((double) usecs) / 1000;
00145 result += secs*1000;
00146 return result;
00147 }
00148
00149 bool operator==(const Delta_Time &other) const throw()
00150 {
00151 return ((secs == other.secs) && (usecs == other.usecs));
00152 }
00153
00154 bool operator!=(const Delta_Time &other) const throw()
00155 {
00156 return ((secs != other.secs) && (usecs != other.usecs));
00157 }
00158 };
00159
00160 inline struct timeval operator+(const struct timeval &start,
00161 const Delta_Time &delta)
00162 {
00163 struct timeval result = start;
00164 result.tv_sec += delta.secs;
00165 result.tv_usec += delta.usecs;
00166 if(result.tv_usec > USECS_PER_SEC)
00167 {
00168 result.tv_sec += result.tv_usec/USECS_PER_SEC;
00169 result.tv_usec %= USECS_PER_SEC;
00170 }
00171 return result;
00172 }
00173
00174 Delta_Time operator-(const struct timeval &end,
00175 const struct timeval &start)
00176 {
00177 Delta_Time result;
00178
00179 if(start.tv_sec == end.tv_sec)
00180 {
00181 result.secs = 0;
00182 assert(end.tv_usec >= start.tv_usec);
00183 result.usecs = end.tv_usec - start.tv_usec;
00184 }
00185 else
00186 {
00187 assert(end.tv_sec > start.tv_sec);
00188 result.secs = (end.tv_sec - start.tv_sec) - 1;
00189 result.usecs = (end.tv_usec + USECS_PER_SEC) - start.tv_usec;
00190 result.usecs_overflow();
00191 }
00192
00193 return result;
00194 }
00195
00196 inline bool operator==(const struct timeval &lhs,
00197 const struct timeval &rhs)
00198 {
00199 return ((lhs.tv_sec == rhs.tv_sec) &&
00200 (lhs.tv_usec == rhs.tv_usec));
00201 }
00202
00203 inline bool operator<=(const struct timeval &lhs,
00204 const struct timeval &rhs)
00205 {
00206 return ((lhs.tv_sec < rhs.tv_sec) ||
00207 ((lhs.tv_sec == rhs.tv_sec) &&
00208 (lhs.tv_usec <= rhs.tv_usec)));
00209 }
00210
00211 inline bool operator<(const struct timeval &lhs,
00212 const struct timeval &rhs)
00213 {
00214 return ((lhs.tv_sec < rhs.tv_sec) ||
00215 ((lhs.tv_sec == rhs.tv_sec) &&
00216 (lhs.tv_usec < rhs.tv_usec)));
00217 }