funcs.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 libraries 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. #ifndef _funcs_h
  50. #define _funcs_h 1
  51. #include <unistd.h>
  52. #include <time.h>
  53. #include <errno.h>
  54. #include <string.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #ifdef C_API
  58. #include "utility/c_stream.h"
  59. #include "utility/c_fstream.h"
  60. #include "utility/c_stringstream.h"
  61. #else
  62. #include <assert.h>
  63. #include <iostream>
  64. #include <fstream>
  65. #include <sstream>
  66. using namespace std;
  67. #endif
  68. #include <math.h>
  69. #ifdef __CENTERLINE__
  70. // centerline does not define these
  71. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  72. #define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
  73. #endif
  74. #include "utility/debug.h"
  75. #include "utility/types.h"
  76. #include "utility/const.h"
  77. #include "utility/macro.h"
  78. #include "utility/mmdb_exception.h"
  79. #ifdef _IBMR2
  80. /* no C++ version of <strings.h>, C version causes conflicts */
  81. extern "C" {
  82. extern int strcasecmp(const char *, const char *);
  83. extern int strncasecmp(const char *, const char *, size_t);
  84. }
  85. #endif
  86. #ifdef mips
  87. int gethostname(char* name, int namelen);
  88. #endif
  89. int compare_stream(ostringstream& x, ostringstream& y);
  90. char * cuserid(char *s);
  91. inline float flog2(unsigned int x) {
  92. return (float)(log((double)x) / log((double)2));
  93. }; // return log_2(x)
  94. inline float flog2(const float x) {
  95. return (float)log((double)x) / (float)log((float)2);
  96. };
  97. inline int pow2(const int x) {
  98. return (int)pow((double)2, (double)x);
  99. }; // x's power of 2
  100. inline int pow2(const float x) {
  101. return (int)pow((double)2, (double)x);
  102. }; // x's power of 2
  103. int pos_of_LSB(const unsigned int x); // position of the MSB
  104. int ceiling(const float); // ceiling of x
  105. unsigned getbits(unsigned, unsigned, unsigned);
  106. inline void char_swap(char& c1, char& c2) {
  107. char tmp = c1; c1 = c2; c2 = tmp;
  108. }; // switch two chars
  109. inline void short_swap(short& s1, short& s2) {
  110. short tmp = s1; s1 = s2; s2 = tmp;
  111. };// switch two shorts
  112. inline void int_swap(int& i1, int& i2) {
  113. int tmp = i1; i1 = i2; i2 = tmp;
  114. }; // switch two ints
  115. // file functions
  116. int del_file(const char* file_nm, const char* path_nm = 0);
  117. Boolean copy_file(const char* source, const char* sink);
  118. Boolean copy_file(const char* path, const char* filenm,
  119. const char* source_ext, const char* target_ext);
  120. Boolean exist_file(const char* name, const char* path = 0);
  121. Boolean cat_file(const char* source1, const char* source2,
  122. const char* target);
  123. int check_file(istream&, const char* msg = "");
  124. Boolean exist_dir(const char* path);
  125. Boolean check_and_create_dir(const char* path);
  126. int open_file_prot();
  127. int open_dir_prot();
  128. Boolean cc_is_digit(istream&); // "cc" stands for current char
  129. unsigned long disk_space(const char* path);
  130. char* access_info( char* request );
  131. Boolean int_eq(void*, void*);
  132. Boolean int_ls(void*, void*);
  133. // return an lease largest int of x, the returned value is
  134. // also a multiple of sizeof(void*)
  135. int ll4(int x);
  136. /*
  137. enum lock_t { SHARED, EXCLUSIVE };
  138. Boolean fcntl_lock( int fd, lock_t lt );
  139. Boolean fcntl_unlock( int fd );
  140. Boolean timed_lock( int fd, lock_t lt, int seconds = 5);
  141. Boolean timed_unlock( int fd, int seconds = 5);
  142. void onalarm(int);
  143. */
  144. #ifdef C_API
  145. int bytes(int fd);
  146. int bytes(fstream&);
  147. #else
  148. int bytes(fstream*);
  149. #endif
  150. int bytes(char* file_name);
  151. char* form(const char* ...);
  152. // lsb is considered as the 0th bit
  153. void lsb_putbits(unsigned& target, unsigned position_from_lsb,
  154. unsigned bits, unsigned source);
  155. unsigned lsb_getbits(unsigned source, unsigned position_from_lsb, unsigned bits);
  156. Boolean writeToTmpFile(char* unique_nm, char* str, int size);
  157. #endif