"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/wifipcap/TimeVal.h" (19 Feb 2021, 4736 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TimeVal.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.4_vs_1.4.5.

    1 ////////////////////////////////////////////////////////////////////////////////
    2 // Mercury and Colyseus Software Distribution 
    3 // 
    4 // Copyright (C) 2004-2005 Ashwin Bharambe (ashu@cs.cmu.edu)
    5 //               2004-2005 Jeffrey Pang    (jeffpang@cs.cmu.edu)
    6 //                    2004 Mukesh Agrawal  (mukesh@cs.cmu.edu)
    7 //                    2013 Simson L. Garfinkel (simsong@acm.org)
    8 // 
    9 // This program is free software; you can redistribute it and/or
   10 // modify it under the terms of the GNU General Public License as
   11 // published by the Free Software Foundation; either version 2, or (at
   12 // your option) any later version.
   13 // 
   14 // This program is distributed in the hope that it will be useful, but
   15 // WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   17 // General Public License for more details.
   18 // 
   19 // You should have received a copy of the GNU General Public License
   20 // along with this program; if not, write to the Free Software
   21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   22 // USA
   23 ////////////////////////////////////////////////////////////////////////////////
   24 /* -*- Mode:c++; c-basic-offset:4; tab-width:4; indent-tabs-mode:t -*- */
   25 
   26 /**************************************************************************
   27   TimeVal.h
   28 
   29 begin           : Oct 16, 2003
   30 version         : $Id: TimeVal.h,v 1.1.1.1 2006/12/14 01:22:11 jpang Exp $
   31 copyright       : (C) 2003      Jeff Pang        ( jeffpang@cs.cmu.edu )
   32                   (C) 2003      Justin Weisz     ( jweisz@cs.cmu.edu   )
   33                   (C) 2013      Simson Garfinkel ( simsong@acm.org )
   34 
   35  ***************************************************************************/
   36 
   37 #ifndef __TIME_VAL_H__
   38 #define __TIME_VAL_H__
   39 
   40 #include <iostream>
   41 #include <iomanip>
   42 
   43 #ifndef _WIN32
   44 #include <sys/time.h>
   45 #include <time.h>
   46 #else
   47 #include <winsock2.h>
   48 #endif
   49 
   50 #include "types.h"
   51 
   52 typedef struct timeval TimeVal;
   53 
   54 #define MSEC_IN_SEC 1000
   55 #define USEC_IN_SEC 1000000
   56 #define USEC_IN_MSEC 1000
   57 
   58 inline bool operator<(struct timeval a, struct timeval b) {
   59     return (a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_usec < b.tv_usec));
   60 }
   61 
   62 inline bool operator>(struct timeval a, struct timeval b) {
   63     return (a.tv_sec > b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_usec > b.tv_usec));
   64 }
   65 
   66 inline bool operator==(struct timeval a, struct timeval b) {
   67     return (a.tv_sec == b.tv_sec) && (a.tv_usec == b.tv_usec);
   68 }
   69 
   70 inline bool operator<=(struct timeval a, struct timeval b) {
   71     return a < b || a == b;
   72 }
   73 
   74 inline bool operator>=(struct timeval a, struct timeval b) {
   75     return a > b || a == b;
   76 }
   77 
   78 inline bool operator!=(struct timeval a, struct timeval b) {
   79     return !(a == b);
   80 }
   81 
   82 inline struct timeval operator+(struct timeval a, double add_msec) {
   83     struct timeval ret;
   84 
   85     // convert into sec/usec parts
   86     sint32 sec_part  = (sint32)(add_msec/MSEC_IN_SEC);
   87     sint32 usec_part = (sint32)((add_msec - sec_part * MSEC_IN_SEC)*USEC_IN_MSEC);
   88 
   89     // do the initial addition
   90     ret.tv_sec  = a.tv_sec + sec_part;
   91     ret.tv_usec = a.tv_usec + usec_part;
   92 
   93     // perform a carry if necessary
   94     if (ret.tv_usec > USEC_IN_SEC) {
   95     ret.tv_sec++;
   96     ret.tv_usec = ret.tv_usec % USEC_IN_SEC;
   97     } else if (ret.tv_usec < 0) {
   98     ret.tv_sec--;
   99     ret.tv_usec = USEC_IN_SEC + ret.tv_usec;
  100     }
  101 
  102     return ret;
  103 }
  104 
  105 inline int64_t operator-(struct timeval a, struct timeval b) {
  106     return ((sint64)a.tv_sec - (sint64)b.tv_sec)*USEC_IN_SEC + 
  107     ((sint64)a.tv_usec - (sint64)b.tv_usec);
  108 }
  109 
  110 inline float timeval_to_float (struct timeval a)
  111 {
  112     return (float) a.tv_sec + ((float) a.tv_usec / USEC_IN_SEC);
  113 }
  114 
  115 inline std::ostream& operator<<(std::ostream& os, const TimeVal& t) 
  116 {
  117     return os << &t;
  118 }
  119 
  120 #ifndef HAVE_TIMEVAL_OUT
  121 #define HAVE_TIMEVAL_OUT
  122 inline std::ostream& operator<<(std::ostream& os, const TimeVal* t)
  123 {
  124     return os << t->tv_sec << "." << std::setw(6) << std::setfill('0') << t->tv_usec;
  125     
  126 }
  127 #endif
  128 
  129 //bool operator<(struct timeval a, struct timeval b);
  130 //bool operator<=(struct timeval a, struct timeval b);
  131 //bool operator>(struct timeval a, struct timeval b);
  132 //bool operator>=(struct timeval a, struct timeval b);
  133 //bool operator==(struct timeval a, struct timeval b);
  134 //bool operator!=(struct timeval a, struct timeval b);
  135 //struct timeval operator+(struct timeval a, double add_msec);
  136 //sint64 operator-(struct timeval a, struct timeval b); /* usec result */
  137 //float timeval_to_float (struct timeval a);
  138 
  139 extern TimeVal TIME_NONE;
  140 
  141 //std::ostream& operator<<(std::ostream& os, const TimeVal &t);
  142 //std::ostream& operator<<(std::ostream& os, const TimeVal *t);
  143 //////////////////////////////////////////////////////////////////////////////
  144 
  145 #endif