Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

WKFUtils.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2009 John E. Stone
00004  *cr
00005  ***************************************************************************/
00006 /***************************************************************************
00007  * RCS INFORMATION:
00008  *
00009  *      $RCSfile: WKFUtils.C,v $
00010  *      $Author: johns $        $Locker:  $             $State: Exp $
00011  *      $Revision: 1.2 $       $Date: 2020/02/26 06:23:21 $
00012  *
00013  ***************************************************************************/
00027 /*
00028  * Copyright (c) 1994-2009 John E. Stone
00029  * All rights reserved.
00030  *
00031  * Redistribution and use in source and binary forms, with or without
00032  * modification, are permitted provided that the following conditions
00033  * are met:
00034  * 1. Redistributions of source code must retain the above copyright
00035  *    notice, this list of conditions and the following disclaimer.
00036  * 2. Redistributions in binary form must reproduce the above copyright
00037  *    notice, this list of conditions and the following disclaimer in the
00038  *    documentation and/or other materials provided with the distribution.
00039  * 3. The name of the author may not be used to endorse or promote products
00040  *    derived from this software without specific prior written permission.
00041  *
00042  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00043  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00044  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00045  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00046  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00047  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00048  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00049  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00050  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00051  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  */
00054 
00055 #include "WKFUtils.h"
00056 
00057 #include <string.h>
00058 #include <ctype.h>
00059 #include <math.h>
00060 #include <stdio.h>
00061 #include <stdlib.h>
00062 
00063 #if defined(_MSC_VER)
00064 #include <windows.h>
00065 #include <conio.h>
00066 #else
00067 #include <unistd.h>
00068 #include <sys/time.h>
00069 #include <errno.h>
00070 
00071 #if defined(ARCH_AIX4)
00072 #include <strings.h>
00073 #endif
00074 
00075 #if defined(__irix)
00076 #include <bstring.h>
00077 #endif
00078 
00079 #if defined(__hpux)
00080 #include <time.h>
00081 #endif // HPUX
00082 #endif // _MSC_VER
00083 
00084 
00085 #ifdef __cplusplus
00086 extern "C" {
00087 #endif
00088 
00089 
00090 #if defined(_MSC_VER)
00091 typedef struct {
00092   DWORD starttime;
00093   DWORD endtime;
00094 } wkf_timer;
00095 
00096 void wkf_timer_start(wkf_timerhandle v) {
00097   wkf_timer * t = (wkf_timer *) v;
00098   t->starttime = GetTickCount();
00099 }
00100 
00101 void wkf_timer_stop(wkf_timerhandle v) {
00102   wkf_timer * t = (wkf_timer *) v;
00103   t->endtime = GetTickCount();
00104 }
00105 
00106 double wkf_timer_time(wkf_timerhandle v) {
00107   wkf_timer * t = (wkf_timer *) v;
00108   double ttime;
00109 
00110   ttime = ((double) (t->endtime - t->starttime)) / 1000.0;
00111 
00112   return ttime;
00113 }
00114 
00115 double wkf_timer_start_time(wkf_timerhandle v) {
00116   wkf_timer * t = (wkf_timer *) v;
00117   double ttime;
00118   ttime = ((double) (t->starttime)) / 1000.0;
00119   return ttime;
00120 }
00121 
00122 double wkf_timer_stop_time(wkf_timerhandle v) {
00123   wkf_timer * t = (wkf_timer *) v;
00124   double ttime;
00125   ttime = ((double) (t->endtime)) / 1000.0;
00126   return ttime;
00127 }
00128 
00129 #else
00130 
00131 // Unix with gettimeofday()
00132 typedef struct {
00133   struct timeval starttime, endtime;
00134   struct timezone tz;
00135 } wkf_timer;
00136 
00137 void wkf_timer_start(wkf_timerhandle v) {
00138   wkf_timer * t = (wkf_timer *) v;
00139   gettimeofday(&t->starttime, &t->tz);
00140 }
00141 
00142 void wkf_timer_stop(wkf_timerhandle v) {
00143   wkf_timer * t = (wkf_timer *) v;
00144   gettimeofday(&t->endtime, &t->tz);
00145 }
00146 
00147 double wkf_timer_time(wkf_timerhandle v) {
00148   wkf_timer * t = (wkf_timer *) v;
00149   double ttime;
00150   ttime = ((double) (t->endtime.tv_sec - t->starttime.tv_sec)) +
00151           ((double) (t->endtime.tv_usec - t->starttime.tv_usec)) / 1000000.0;
00152   return ttime;
00153 }
00154 
00155 double wkf_timer_start_time(wkf_timerhandle v) {
00156   wkf_timer * t = (wkf_timer *) v;
00157   double ttime;
00158   ttime = ((double) t->starttime.tv_sec) +
00159           ((double) t->starttime.tv_usec) / 1000000.0;
00160   return ttime;
00161 }
00162 
00163 double wkf_timer_stop_time(wkf_timerhandle v) {
00164   wkf_timer * t = (wkf_timer *) v;
00165   double ttime;
00166   ttime = ((double) t->endtime.tv_sec) +
00167           ((double) t->endtime.tv_usec) / 1000000.0;
00168   return ttime;
00169 }
00170 
00171 #endif
00172 
00173 // system independent routines to create and destroy timers
00174 wkf_timerhandle wkf_timer_create(void) {
00175   wkf_timer * t;
00176   t = (wkf_timer *) malloc(sizeof(wkf_timer));
00177   memset(t, 0, sizeof(wkf_timer));
00178   return t;
00179 }
00180 
00181 void wkf_timer_destroy(wkf_timerhandle v) {
00182   free(v);
00183 }
00184 
00185 double wkf_timer_timenow(wkf_timerhandle v) {
00186   wkf_timer_stop(v);
00187   return wkf_timer_time(v);
00188 }
00189 
00191 wkfmsgtimer * wkf_msg_timer_create(double updatetime) {
00192   wkfmsgtimer *mt;
00193   mt = (wkfmsgtimer *) malloc(sizeof(wkfmsgtimer));
00194   if (mt != NULL) {
00195     mt->timer = wkf_timer_create();
00196     mt->updatetime = updatetime;
00197     wkf_timer_start(mt->timer);
00198   }
00199   return mt;
00200 }
00201 
00203 int wkf_msg_timer_timeout(wkfmsgtimer *mt) {
00204   double elapsed = wkf_timer_timenow(mt->timer);
00205   if (elapsed > mt->updatetime) {
00206     // reset the clock and return true that our timer expired
00207     wkf_timer_start(mt->timer);
00208     return 1;
00209   } else if (elapsed < 0) {
00210     // time went backwards, best reset our clock!
00211     wkf_timer_start(mt->timer);
00212   }
00213   return 0;
00214 }
00215 
00217 void wkf_msg_timer_destroy(wkfmsgtimer * mt) {
00218   wkf_timer_destroy(mt->timer);
00219   free(mt);
00220 }
00221 
00222 #ifdef __cplusplus
00223 }
00224 #endif
00225 

Generated on Fri Nov 8 02:45:52 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002