chiark / gitweb /
Moved most of the code into exported macros. The main functions now
authormdw <mdw>
Fri, 21 May 1999 22:13:12 +0000 (22:13 +0000)
committermdw <mdw>
Fri, 21 May 1999 22:13:12 +0000 (22:13 +0000)
implemented using the macros.

tv.c
tv.h

diff --git a/tv.c b/tv.c
index eeeb8b0d660dbcfb04ef77d02e20bee787b57269..8f41a2f604bf8389d89ddcda24642b9984cc7d49 100644 (file)
--- a/tv.c
+++ b/tv.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: tv.c,v 1.4 1999/05/17 20:37:52 mdw Exp $
+ * $Id: tv.c,v 1.5 1999/05/21 22:13:12 mdw Exp $
  *
  * Manipulation of timeval structures
  *
  *
  * Manipulation of timeval structures
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tv.c,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tv.c,v $
+ * Revision 1.5  1999/05/21 22:13:12  mdw
+ * Moved most of the code into exported macros.  The main functions now
+ * implemented using the macros.
+ *
  * Revision 1.4  1999/05/17 20:37:52  mdw
  * New function `tv_addl' to add a literal to a time value.  Use magical
  * `MILLION' constant in place of 1000000 for ease of reading.
  * Revision 1.4  1999/05/17 20:37:52  mdw
  * New function `tv_addl' to add a literal to a time value.  Use magical
  * `MILLION' constant in place of 1000000 for ease of reading.
 #include <sys/time.h>
 #include "tv.h"
 
 #include <sys/time.h>
 #include "tv.h"
 
-/*----- A macro to make reading easier ------------------------------------*/
-
-#define MILLION 1000000
-
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @tv_add@ --- *
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @tv_add@ --- *
 void tv_add(struct timeval *dst,
            const struct timeval *a, const struct timeval *b)
 {
 void tv_add(struct timeval *dst,
            const struct timeval *a, const struct timeval *b)
 {
-  dst->tv_sec = a->tv_sec + b->tv_sec;
-  dst->tv_usec = a->tv_usec + b->tv_usec;
-  if (dst->tv_usec >= MILLION) {
-    dst->tv_usec -= MILLION;
-    dst->tv_sec++;
-  }
+  TV_ADD(dst, a, b);
 }
 
 /* --- @tv_addl@ --- *
 }
 
 /* --- @tv_addl@ --- *
@@ -91,12 +86,7 @@ void tv_add(struct timeval *dst,
 void tv_addl(struct timeval *dst, const struct timeval *a,
             time_t sec, unsigned long usec)
 {
 void tv_addl(struct timeval *dst, const struct timeval *a,
             time_t sec, unsigned long usec)
 {
-  dst->tv_sec = a->tv_sec + sec;
-  dst->tv_usec = a->tv_usec + usec;
-  if (dst->tv_usec >= MILLION) {
-    dst->tv_usec -= MILLION;
-    dst->tv_sec++;
-  }
+  TV_ADDL(dst, a, sec, usec);
 }
 
 /* --- @tv_sub@ --- *
 }
 
 /* --- @tv_sub@ --- *
@@ -112,13 +102,24 @@ void tv_addl(struct timeval *dst, const struct timeval *a,
 void tv_sub(struct timeval *dst,
            const struct timeval *a, const struct timeval *b)
 {
 void tv_sub(struct timeval *dst,
            const struct timeval *a, const struct timeval *b)
 {
-  dst->tv_sec = a->tv_sec - b->tv_sec;
-  if (a->tv_usec >= b->tv_usec)
-    dst->tv_usec = a->tv_usec - b->tv_usec;
-  else {
-    dst->tv_usec = a->tv_usec + MILLION - b->tv_usec;
-    dst->tv_sec--;
-  }
+  TV_SUB(dst, a, b);
+}
+
+/* --- @tv_subl@ --- *
+ *
+ * Arguments:  @struct timeval *dst@ = destination block
+ *             @const struct timeval *a@ = source blocks
+ *             @time_t sec@, @unsigned long usec@ = time to subtract
+ *
+ * Returns:    ---
+ *
+ * Use:                Subtracts a literal time in seconds and microseconds.
+ */
+
+void tv_subl(struct timeval *dst, const struct timeval *a,
+                   time_t sec, unsigned long usec)
+{
+  TV_SUBL(dst, a, sec, usec);
 }
 
 /* --- @tv_cmp@ --- *
 }
 
 /* --- @tv_cmp@ --- *
@@ -132,6 +133,8 @@ void tv_sub(struct timeval *dst,
 
 int tv_cmp(const struct timeval *a, const struct timeval *b)
 {
 
 int tv_cmp(const struct timeval *a, const struct timeval *b)
 {
+  /* --- This is more awkward than the case the macro deals with --- */
+
   if (a->tv_sec > b->tv_sec)
     return (1);
   else if (a->tv_sec < b->tv_sec)
   if (a->tv_sec > b->tv_sec)
     return (1);
   else if (a->tv_sec < b->tv_sec)
diff --git a/tv.h b/tv.h
index 60bcfdf3811897f331b31964642329d8390c1df6..a963e173dc5744225511207878677b6659f69ae6 100644 (file)
--- a/tv.h
+++ b/tv.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: tv.h,v 1.4 1999/05/17 20:37:25 mdw Exp $
+ * $Id: tv.h,v 1.5 1999/05/21 22:13:12 mdw Exp $
  *
  * Manipulation of timeval structures
  *
  *
  * Manipulation of timeval structures
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tv.h,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tv.h,v $
+ * Revision 1.5  1999/05/21 22:13:12  mdw
+ * Moved most of the code into exported macros.  The main functions now
+ * implemented using the macros.
+ *
  * Revision 1.4  1999/05/17 20:37:25  mdw
  * New function `tv_addl' to add a literal to a time value.
  *
  * Revision 1.4  1999/05/17 20:37:25  mdw
  * New function `tv_addl' to add a literal to a time value.
  *
 
 #include <sys/time.h>
 
 
 #include <sys/time.h>
 
+/*----- A macro to make reading easier ------------------------------------*/
+
+#define MILLION 1000000
+
 /*----- Functions provided ------------------------------------------------*/
 
 /* --- @tv_add@ --- *
 /*----- Functions provided ------------------------------------------------*/
 
 /* --- @tv_add@ --- *
@@ -71,6 +79,8 @@ extern void tv_add(struct timeval */*dst*/,
                   const struct timeval */*a*/,
                   const struct timeval */*b*/);
 
                   const struct timeval */*a*/,
                   const struct timeval */*b*/);
 
+#define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
+
 /* --- @tv_addl@ --- *
  *
  * Arguments:  @struct timeval *dst@ = destination block
 /* --- @tv_addl@ --- *
  *
  * Arguments:  @struct timeval *dst@ = destination block
@@ -86,6 +96,15 @@ extern void tv_addl(struct timeval */*dst*/,
                    const struct timeval */*a*/,
                    time_t /*sec*/, unsigned long /*usec*/);
 
                    const struct timeval */*a*/,
                    time_t /*sec*/, unsigned long /*usec*/);
 
+#define TV_ADDL(dst, a, sec, usec) do {                                        \
+  (dst)->tv_sec = (a)->tv_sec + (sec);                                 \
+  (dst)->tv_usec = (a)->tv_usec + (usec);                              \
+  if ((dst)->tv_usec >= MILLION) {                                     \
+    (dst)->tv_usec -= MILLION;                                         \
+    (dst)->tv_sec++;                                                   \
+  }                                                                    \
+} while (0)
+
 /* --- @tv_sub@ --- *
  *
  * Arguments:  @struct timeval *dst@ = destination block
 /* --- @tv_sub@ --- *
  *
  * Arguments:  @struct timeval *dst@ = destination block
@@ -100,6 +119,33 @@ extern void tv_sub(struct timeval */*dst*/,
                   const struct timeval */*a*/,
                   const struct timeval */*b*/);
 
                   const struct timeval */*a*/,
                   const struct timeval */*b*/);
 
+#define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
+
+/* --- @tv_subl@ --- *
+ *
+ * Arguments:  @struct timeval *dst@ = destination block
+ *             @const struct timeval *a@ = source blocks
+ *             @time_t sec@, @unsigned long usec@ = time to subtract
+ *
+ * Returns:    ---
+ *
+ * Use:                Subtracts a literal time in seconds and microseconds.
+ */
+
+extern void tv_subl(struct timeval */*dst*/,
+                   const struct timeval */*a*/,
+                   time_t /*sec*/, unsigned long /*usec*/);
+
+#define TV_SUBL(dst, a, sec, usec) do {                                        \
+  (dst)->tv_sec = (a)->tv_sec - (sec);                                 \
+  if ((a)->tv_usec >= (usec))                                          \
+    (dst)->tv_usec = (a)->tv_usec - (usec);                            \
+  else {                                                               \
+    (dst)->tv_usec = (a)->tv_usec + MILLION - (usec);                  \
+    (dst)->tv_sec--;                                                   \
+  }                                                                    \
+} while (0)
+
 /* --- @tv_cmp@ --- *
  *
  * Arguments:  @const struct timeval *a, *b@ = source blocks
 /* --- @tv_cmp@ --- *
  *
  * Arguments:  @const struct timeval *a, *b@ = source blocks
@@ -112,6 +158,10 @@ extern void tv_sub(struct timeval */*dst*/,
 extern int tv_cmp(const struct timeval */*a*/,
                  const struct timeval */*b*/);
 
 extern int tv_cmp(const struct timeval */*a*/,
                  const struct timeval */*b*/);
 
+#define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ?                 \
+                           (a)->tv_usec op (b)->tv_usec :              \
+                           (a)->tv_sec op (b)->tv_sec)
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus