Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / funcs.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: funcs.h /main/12 1996/09/13 20:48:55 cde-hal $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51
52 #ifndef _funcs_h
53 #define _funcs_h 1
54
55 #if !defined(USL) && !defined(__osf__)
56 #include <libc.h>
57 #endif
58 #if defined(hpux) || defined(sgi) || defined(USL) ||defined(__osf__)
59 #include <unistd.h>
60 #else
61 #include <sysent.h>
62 #endif
63 #include <time.h>
64 #include <errno.h>
65 #include <string.h>
66 #include <sys/types.h>
67 #include <sys/stat.h>
68
69 #ifdef C_API
70 #include "utility/c_stream.h"
71 #include "utility/c_fstream.h"
72 #include "utility/c_strstream.h"
73 #else
74 #include <assert.h>
75 #include <stream.h>
76 #include <fstream.h>
77 #include <strstream.h>
78 #endif
79
80 #include <math.h>
81
82 #ifdef __CENTERLINE__
83 // centerline does not define these
84 #define S_ISDIR(m)      (((m)&S_IFMT) == S_IFDIR)
85 #define S_ISREG(m)      (((m)&S_IFMT) == S_IFREG)
86
87 #endif
88
89 #include "utility/debug.h"
90 #include "utility/types.h"
91 #include "utility/const.h"
92 #include "utility/macro.h"
93 #include "utility/mmdb_exception.h"
94
95 #ifdef USL
96 int _DtMmdbStrcasecmp(const char *s1, const char *s2);
97 int _DtMmdbStrncasecmp(const char *s1, const char *s2, int n);
98 #define strcasecmp(s1,s2) _DtMmdbStrcasecmp(s1,s2)
99 #define strncasecmp(s1,s2,n) _DtMmdbStrncasecmp(s1,s2,n)
100 #endif
101 #ifdef _IBMR2
102 /* no C++ version of <strings.h>, C version causes conflicts */
103 extern "C" {
104 extern int  strcasecmp(const char *, const char *);
105 extern int  strncasecmp(const char *, const char *, size_t);
106 }
107 #endif
108 #ifdef mips
109 int gethostname(char* name, int namelen);
110 #endif
111
112 int compare_stream(ostrstream& x, ostrstream& y);
113
114
115 inline float flog2(unsigned int x) {
116    return (float)(log((double)x) / log((double)2));
117 #if defined(__osf__)
118 }     // return log_2(x) 
119 #else
120 };     // return log_2(x) 
121 #endif /* (__osf__) */
122
123 inline float flog2(const float x) {
124    return (float)log((double)x) / (float)log((float)2);
125 #if defined(__osf__)
126 }
127 #else
128 };
129 #endif /* (__osf__) */
130
131 inline int pow2(const int x) {
132    return (int)pow((double)2, (double)x);
133 #if defined(__osf__)
134 }     // x's power of 2
135 #else
136 };     // x's power of 2
137 #endif /* (__osf__) */
138
139 inline int pow2(const float x) {
140    return (int)pow((double)2, (double)x);
141 #if defined(__osf__)
142 }    // x's power of 2
143 #else
144 };    // x's power of 2
145 #endif /* (__osf__) */
146
147 int pos_of_LSB(const unsigned int x); // position of the MSB
148
149 int ceiling(const float);    // ceiling of x
150 unsigned getbits(unsigned, unsigned, unsigned);
151
152 inline void char_swap(char& c1, char& c2) {
153    char tmp = c1; c1 = c2; c2 = tmp;
154 #if defined(__osf__)
155 }  // switch two chars
156 #else
157 };  // switch two chars
158 #endif /* (__osf__) */
159
160 inline void short_swap(short& s1, short& s2) {
161    short tmp = s1; s1 = s2; s2 = tmp;
162 #if defined(__osf__)
163 }// switch two shorts
164 #else
165 };// switch two shorts
166 #endif /* (__osf__) */
167
168 inline void int_swap(int& i1, int& i2) {
169    int tmp = i1; i1 = i2; i2 = tmp;
170 #if defined(__osf__)
171 }     // switch two ints
172 #else
173 };     // switch two ints
174 #endif /* (__osf__) */
175
176 // file functions
177
178 int del_file(const char* file_nm, const char* path_nm = 0);
179
180 Boolean copy_file(const char* source, const char* sink);
181 Boolean copy_file(const char* path, const char* filenm,
182                   const char* source_ext, const char* target_ext);
183
184 Boolean exist_file(const char* name, const char* path = 0);
185 Boolean cat_file(const char* source1, const char* source2, 
186                     const char* target);
187 int check_file(istream&, const char* msg = "");
188
189 Boolean exist_dir(const char* path);
190 Boolean check_and_create_dir(const char* path);
191
192 int open_file_prot();
193 int open_dir_prot();
194
195
196 Boolean cc_is_digit(istream&); // "cc" stands for current char
197
198 int bytes(fstream&);
199 unsigned long disk_space(const char* path);
200
201 Boolean int_eq(void*, void*);
202 Boolean int_ls(void*, void*);
203
204 // return an lease largest int of x, the returned value is 
205 // also a multiple of sizeof(void*)
206 int ll4(int x);
207
208 /*
209 enum lock_t { SHARED, EXCLUSIVE };
210
211 Boolean fcntl_lock( int fd, lock_t lt );
212 Boolean fcntl_unlock( int fd );
213
214 Boolean timed_lock( int fd, lock_t lt, int seconds = 5);
215 Boolean timed_unlock( int fd, int seconds = 5);
216
217 void onalarm(int);
218 */
219
220 int bytes(int fd);
221 char* access_info( char* request );
222
223 // lsb is considered as the 0th bit
224 void lsb_putbits(unsigned& target, unsigned position_from_lsb, 
225              unsigned bits, unsigned source);
226 unsigned lsb_getbits(unsigned source, unsigned position_from_lsb, unsigned bits);
227
228 Boolean writeToTmpFile(char* unique_nm, char* str, int size);
229
230 #endif
231