]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/win/banal.c
Initial revision
[dttsp.git] / jDttSP / win / banal.c
1 /* banal.c\r
2 \r
3 This file is part of a program that implements a Software-Defined Radio.\r
4 \r
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY\r
6 \r
7 This program is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 This program is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with this program; if not, write to the Free Software\r
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20 \r
21 The authors can be reached by email at\r
22 \r
23 ab2kt@arrl.net\r
24 or\r
25 rwmcgwier@comcast.net\r
26 \r
27 or by paper mail at\r
28 \r
29 The DTTS Microwave Society\r
30 6 Kathleen Place\r
31 Bridgewater, NJ 08807\r
32 */\r
33 \r
34 #include <fromsys.h>\r
35 #include <banal.h>\r
36 \r
37 int\r
38 in_blocks(int count, int block_size) {\r
39   if (block_size < 1) {\r
40     fprintf(stderr, "block_size zero in in_blocks\n");\r
41     exit(1);\r
42   }\r
43   return (1 + ((count - 1) / block_size));\r
44 }\r
45 \r
46 FILE *\r
47 efopen(char *path, char *mode) {\r
48   FILE *iop = fopen(path, mode);\r
49   if (!iop) {\r
50     fprintf(stderr, "can't open \"%s\" in mode \"%s\"\n", path, mode);\r
51     exit(1);\r
52   }\r
53   return iop;\r
54 }\r
55 \r
56 FILE *\r
57 efreopen(char *path, char *mode, FILE *strm) {\r
58   FILE *iop = freopen(path, mode, strm);\r
59   if (!iop) {\r
60     fprintf(stderr, "can't reopen \"%s\" in mode \"%s\"\n", path, mode);\r
61     exit(1);\r
62   }\r
63   return iop;\r
64 }\r
65 \r
66 size_t\r
67 filesize(char *path) {\r
68   struct stat sbuf;\r
69   if (stat(path, &sbuf) == -1) return -1;\r
70   return sbuf.st_size;\r
71 }\r
72 \r
73 size_t\r
74 fdsize(int fd) {\r
75   struct stat sbuf;\r
76   if (fstat(fd, &sbuf) == -1) return -1;\r
77   return sbuf.st_size;\r
78 }\r
79 \r
80 #define MILLION (1000000)\r
81 #ifndef _WINDOWS\r
82 // return current tv\r
83 struct timeval\r
84 now_tv(void) {\r
85   struct timeval tv;\r
86   gettimeofday(&tv, 0);\r
87   return tv;\r
88 }\r
89 \r
90 // return ta - tb\r
91 struct timeval\r
92 diff_tv(struct timeval *ta, struct timeval *tb) {\r
93   struct timeval tv;\r
94   if (tb->tv_usec > ta->tv_usec) {\r
95     ta->tv_sec--;\r
96     ta->tv_usec += MILLION;\r
97   }\r
98   tv.tv_sec = ta->tv_sec - tb->tv_sec;\r
99   if ((tv.tv_usec = ta->tv_usec - tb->tv_usec) >= MILLION) {\r
100     tv.tv_usec -= MILLION;\r
101     tv.tv_sec++;\r
102   }\r
103   return tv;\r
104 }\r
105 \r
106 // return ta + tb\r
107 struct timeval\r
108 sum_tv(struct timeval *ta, struct timeval *tb) {\r
109   struct timeval tv; \r
110   tv.tv_sec = ta->tv_sec + tb->tv_sec;\r
111   if ((tv.tv_usec = ta->tv_usec + tb->tv_usec) >= MILLION) {\r
112     tv.tv_usec -= MILLION;\r
113     tv.tv_sec++;\r
114   }\r
115   return tv;\r
116 }\r
117 \r
118 char *\r
119 fmt_tv(struct timeval *tv) {\r
120 #ifndef _WINDOWS\r
121   static char buff[256];\r
122   snprintf(buff, sizeof(buff), "%ds%du", tv->tv_sec, tv->tv_usec);\r
123 #else\r
124   static char buff[512];\r
125   sprintf(buff, "%ds%du", tv->tv_sec, tv->tv_usec);\r
126 #endif\r
127   return buff;\r
128 }\r
129 \r
130 char *\r
131 since(struct timeval *tv) {\r
132   struct timeval nt = now_tv(),\r
133                  dt = diff_tv(&nt, tv);\r
134   return fmt_tv(&dt);\r
135 }\r
136 #endif\r
137 \r
138 int\r
139 hinterp_vec(REAL *u, int m, REAL *v, int n) {\r
140   if (!u || !v || (n < 2) || (m < n) || (m % n)) return 0;\r
141   else {\r
142     int div = m / n, i, j = 0;\r
143     for (i = 1; i < n; i++) {\r
144       int k;\r
145       REAL vl = v[i - 1], del = (v[i] - vl) / div;\r
146       u[j++] = vl;\r
147       for (k = 1; k < div; k++) u[j++] = vl + k * del;\r
148     }\r
149     u[j++] = v[n - 1];\r
150     return j;\r
151   }\r
152 }\r