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

Generated on Mon Nov 20 01:17:15 2017 for NAMD by  doxygen 1.4.7