1 // -*- mode: c++; c-set-style: "stroustrup"; tab-width: 4; -*-
2 //
3 // common.h
4 //
5 // Copyright (C) 2004 Koji Nakamaru
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21
22 #ifndef _common_h_
23 #define _common_h_
24
25 #if defined(__APPLE__)
26 #include <GLUT/glut.h>
27 #else
28 #include <GL/glut.h>
29 #endif
30 #include <algorithm>
31 #include <vector>
32 #include <cassert>
33 #include <cctype>
34 #include <cerrno>
35 #include <cfloat>
36 #include <climits>
37 #include <cmath>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <cstring>
41 #include <dirent.h>
42 #include <fcntl.h>
43 #include <map>
44 #include <pthread.h>
45 #include <stdint.h>
46 #include <string>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <unistd.h>
50 #include <vector>
51 #include "error.h"
52
53 using namespace std;
54
55 template <class T>
forget(T ** o)56 inline void forget(T **o)
57 {
58 if (*o != NULL) {
59 delete *o;
60 }
61 *o = NULL;
62 }
63
64 template <class T>
forgetArray(T ** a)65 inline void forgetArray(T **a)
66 {
67 if (*a != NULL) {
68 delete[] *a;
69 }
70 *a = NULL;
71 }
72
forgetFD(int * fd)73 inline void forgetFD(int *fd)
74 {
75 if (*fd != -1) {
76 close(*fd);
77 }
78 *fd = -1;
79 }
80
forgetFILE(FILE ** fp)81 inline void forgetFILE(FILE **fp)
82 {
83 if (*fp != NULL) {
84 fclose(*fp);
85 }
86 *fp = NULL;
87 }
88
forgetPIPE(FILE ** fp)89 inline void forgetPIPE(FILE **fp)
90 {
91 if (*fp != NULL) {
92 pclose(*fp);
93 }
94 *fp = NULL;
95 }
96
forgetDIR(DIR ** dir)97 inline void forgetDIR(DIR **dir)
98 {
99 if (*dir != NULL) {
100 closedir(*dir);
101 }
102 *dir = NULL;
103 }
104
105 template <class T>
deg2rad(T angle)106 inline T deg2rad(T angle)
107 {
108 return angle * M_PI / 180.0;
109 }
110
111 template <class T>
clamp(T x,T a,T b)112 inline T clamp(T x, T a, T b)
113 {
114 if (x < a) {
115 return a;
116 } else if (x > b) {
117 return b;
118 }
119 return x;
120 }
121
122 extern bool isLittleEndian();
123
124 #if defined (_WIN32) && !defined (__CYGWIN__)
125
126 #include <windows.h>
127
128 // extern int usleep(unsigned long usec);
129
130 #endif // defined (_WIN32) && !defined (__CYGWIN__)
131
132 extern void run(const char *command);
133
134 #endif
135