1 | /****************************************************************************** |
---|
2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
3 | * |
---|
4 | * Module: misc.h |
---|
5 | * Author : Byungil Jeong |
---|
6 | * |
---|
7 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
8 | * University of Illinois at Chicago |
---|
9 | * |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * Redistribution and use in source and binary forms, with or without |
---|
13 | * modification, are permitted provided that the following conditions are met: |
---|
14 | * |
---|
15 | * * Redistributions of source code must retain the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer. |
---|
17 | * * Redistributions in binary form must reproduce the above |
---|
18 | * copyright notice, this list of conditions and the following disclaimer |
---|
19 | * in the documentation and/or other materials provided with the distribution. |
---|
20 | * * Neither the name of the University of Illinois at Chicago nor |
---|
21 | * the names of its contributors may be used to endorse or promote |
---|
22 | * products derived from this software without specific prior written permission. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
25 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
26 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
27 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
35 | * |
---|
36 | * Direct questions, comments etc about SAGE to sage_users@listserv.uic.edu or |
---|
37 | * http://www.evl.uic.edu/cavern/forum/ |
---|
38 | * |
---|
39 | *****************************************************************************/ |
---|
40 | |
---|
41 | #ifndef _MISC_H |
---|
42 | #define _MISC_H |
---|
43 | |
---|
44 | #define TOKEN_LEN 1024 |
---|
45 | #define STRBUF_SIZE 1024 |
---|
46 | #define MAX_TOKENS 10000 |
---|
47 | |
---|
48 | #ifndef MIN |
---|
49 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
---|
50 | #endif |
---|
51 | #ifndef MAX |
---|
52 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
---|
53 | #endif |
---|
54 | |
---|
55 | #include "sageBase.h" |
---|
56 | |
---|
57 | int getToken(char *, char *); |
---|
58 | int getToken(char *, char *, char **); |
---|
59 | int getToken(FILE *, char *); |
---|
60 | int execRemBin(char *ip, char *com, char *xid = NULL); |
---|
61 | |
---|
62 | int getMax2n(int val); |
---|
63 | int getPixelSize(sagePixFmt type); |
---|
64 | |
---|
65 | long getnumber( char *str ); |
---|
66 | |
---|
67 | #if defined(WIN32) |
---|
68 | int gettimeofday(struct timeval *, void *); |
---|
69 | |
---|
70 | #pragma warning( disable : 4244) // time_t and long mismatch |
---|
71 | #pragma warning( disable : 4267) // size_t and int mismatch |
---|
72 | #pragma warning( disable : 4018) // '<' : signed/unsigned mismatch |
---|
73 | #endif |
---|
74 | |
---|
75 | namespace sage { |
---|
76 | void usleep(unsigned long usec); |
---|
77 | unsigned int sleep(unsigned int seconds); |
---|
78 | void switchThread(); |
---|
79 | int condition_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, time_t miliSec); |
---|
80 | int toupper(char *str); |
---|
81 | int tolower(char *str); |
---|
82 | bool isDataReady(int sockFd, int sec = 0, int usec = 0); |
---|
83 | int send(int fd, void *buf, int len); |
---|
84 | int recv(int fd, void *buf, int len, int flags = 0); |
---|
85 | void transformFrameNum(int blockFrame, int &curFrame); |
---|
86 | char *tokenSeek(char *buf, int tnum); // return the string pointer from which (tnum+1)th token starts |
---|
87 | void printLog(const char *format,...); |
---|
88 | void initUtil(); |
---|
89 | double getTime(); |
---|
90 | #if defined(WIN32) |
---|
91 | void win32Init(); |
---|
92 | #endif |
---|
93 | } |
---|
94 | |
---|
95 | class sageToken { |
---|
96 | private: |
---|
97 | char *tokenBuf; |
---|
98 | int idx; |
---|
99 | int strLen; |
---|
100 | |
---|
101 | public: |
---|
102 | sageToken(char *str); |
---|
103 | sageToken(int len); |
---|
104 | ~sageToken(); |
---|
105 | |
---|
106 | int getToken(char *token); |
---|
107 | int putToken(char *token); |
---|
108 | char* getBuffer() { return tokenBuf; } |
---|
109 | }; |
---|
110 | |
---|
111 | class sageTimer { |
---|
112 | private: |
---|
113 | //struct timespec startTime; |
---|
114 | double startTime; |
---|
115 | double accumulatedTime; |
---|
116 | bool paused; |
---|
117 | public: |
---|
118 | sageTimer() { reset(); } |
---|
119 | void resume(); |
---|
120 | void reset(); |
---|
121 | void pausedReset(); |
---|
122 | void pause(); |
---|
123 | double getTimeUS(bool resetFlag = false); // return elapsed time in micro second |
---|
124 | double getTimeSec(); // return elapsed time in second |
---|
125 | }; |
---|
126 | |
---|
127 | class sageCounter { |
---|
128 | private: |
---|
129 | unsigned int count; |
---|
130 | |
---|
131 | public: |
---|
132 | sageCounter() : count(0) {} |
---|
133 | void reset() { count = 0; } |
---|
134 | unsigned int getValue() { return count; } |
---|
135 | void operator++(int) { count++; } |
---|
136 | }; |
---|
137 | |
---|
138 | class sageIndexTable { |
---|
139 | private: |
---|
140 | int *table; |
---|
141 | int minID, maxID; |
---|
142 | std::vector<int> entries; |
---|
143 | |
---|
144 | public: |
---|
145 | sageIndexTable() : table(NULL), minID(0), maxID(0) { entries.clear(); } |
---|
146 | void addEntry(int id) { entries.push_back(id); } |
---|
147 | int generateTable(); |
---|
148 | int setTable(int id, int index); |
---|
149 | int getIndex(int id); |
---|
150 | int getID(int idx); |
---|
151 | int size() { return entries.size(); } |
---|
152 | ~sageIndexTable(); |
---|
153 | }; |
---|
154 | |
---|
155 | // Inspired by NVIDIA Toolkit |
---|
156 | |
---|
157 | class data_path |
---|
158 | { |
---|
159 | public: |
---|
160 | std::string file_path; |
---|
161 | std::string path_name; |
---|
162 | std::vector<std::string> path; |
---|
163 | |
---|
164 | data_path(std::string subdir=""); |
---|
165 | |
---|
166 | // Finds and returns the directory (or NULL) to a given filename |
---|
167 | std::string get_path(std::string filename); |
---|
168 | // Finds and returns the complete path (or NULL) to a given filename |
---|
169 | std::string get_file(std::string filename); |
---|
170 | |
---|
171 | // Finds and opens a given filename |
---|
172 | FILE *fopen(std::string filename, const char * mode = "rb"); |
---|
173 | |
---|
174 | // Prints the list of directories in the search list |
---|
175 | void print(); |
---|
176 | |
---|
177 | #ifdef WIN32 |
---|
178 | int fstat(std::string filename, struct _stat * stat); |
---|
179 | #else |
---|
180 | int fstat(std::string filename, struct stat * stat); |
---|
181 | #endif |
---|
182 | }; |
---|
183 | |
---|
184 | |
---|
185 | #endif |
---|