home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / 3d / irit / misc_lib / xgeneral.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-13  |  19.2 KB  |  488 lines

  1. /*****************************************************************************
  2. * replacer for the MSDOS functions.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Aug. 1991   *
  5. *****************************************************************************/
  6.  
  7. #include <math.h>
  8. #include <time.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <signal.h>
  13. #ifdef OS2GCC
  14. #define INCL_DOSPROCESS
  15. #include <os2.h>
  16. #endif /* OS2GCC */
  17. #ifdef SGINAP
  18. #include <limits.h>
  19. #endif /* SGINAP */
  20. #ifdef __WINNT__
  21. #include <windows.h>
  22. #else
  23. #include <sys/types.h>
  24. #if !(defined(AMIGA) && defined(__SASC))
  25. #include <sys/times.h>
  26. #include <sys/param.h>
  27. #endif /* !(AMIGA && __SASC) */
  28. #ifndef HZ
  29. #define HZ 60
  30. #endif /* HZ */
  31. #endif /* __WINNT__ */
  32.  
  33. #include "irit_sm.h"
  34. #include "imalloc.h"
  35.  
  36. /*****************************************************************************
  37. * DESCRIPTION:                                                               M
  38. * Routine to duplicate a string. Exists in some computer environments.       M
  39. *                                                                            *
  40. * PARAMETERS:                                                                M
  41. *   s:        String to duplicate.                                           M
  42. *                                                                            *
  43. * RETURN VALUE:                                                              M
  44. *   char *:   Duplicated string.                                             M
  45. *                                                                            *
  46. * KEYWORDS:                                                                  M
  47. *   IritStrdup, strdup                                                       M
  48. *****************************************************************************/
  49. char *IritStrdup(char *s)
  50. {
  51.     char
  52.     *p = IritMalloc(strlen(s) + 1);
  53.  
  54.     if (p == NULL)
  55.     return NULL;
  56.  
  57.     strcpy(p, s);
  58.  
  59.     return p;
  60. }
  61.  
  62. #ifdef ITIMERVAL
  63. /*****************************************************************************
  64. * DESCRIPTION:                                                               *
  65. * Dummy routine to catch signals for IritSleep below.                 *
  66. *                                                                            *
  67. * PARAMETERS:                                                                *
  68. *   None                                                                     *
  69. *                                                                            *
  70. * RETURN VALUE:                                                              *
  71. *   void                                                                     *
  72. *****************************************************************************/
  73. static void TrapSignal(void)
  74. {
  75. }
  76. #endif /* ITIMERVAL */
  77.  
  78. /*****************************************************************************
  79. * DESCRIPTION:                                                               M
  80. * Routine to force a process to sleep.                                         M
  81. *                                                                            *
  82. * PARAMETERS:                                                                M
  83. *   MilliSeconds:   Sleeping time required, in miliseconds.                  M
  84. *                                                                            *
  85. * RETURN VALUE:                                                              M
  86. *   void                                                                     M
  87. *                                                                            *
  88. * KEYWORDS:                                                                  M
  89. *   IritSleep, sleep                                                         M
  90. *****************************************************************************/
  91. void IritSleep(int MilliSeconds)
  92. {
  93. #ifdef NANOSLEEP
  94.     struct timespec rqtp;
  95.  
  96.     rqtp.tv_sec = MilliSeconds / 1000;
  97.     rqtp.tv_nsec = (MilliSeconds % 1000) * 1000000;
  98.     nanosleep(&rqtp, NULL);
  99. #else
  100. #ifdef USLEEP
  101.     usleep(MilliSeconds * 1000);
  102. #else
  103. #ifdef SGINAP
  104.     sginap((long) (MAX(CLK_TCK * MilliSeconds / 1000, 1)));
  105. #else
  106. #ifdef OS2GCC
  107.     DosSleep(MilliSeconds);
  108. #else
  109. #ifdef __WINNT__
  110.     Sleep(MilliSeconds);
  111. #else
  112. #ifdef ITIMERVAL
  113.     /* Sometimes fail on hpux (sleeps forever in sigpause). Use with care. */
  114.     struct itimerval Tmr;
  115.     void (*OldTrapSignal)(void);
  116.  
  117.     Tmr.it_interval.tv_sec = 0;
  118.     Tmr.it_interval.tv_usec = 0;
  119.     Tmr.it_value.tv_sec = MilliSeconds / 1000;
  120.     Tmr.it_value.tv_usec = (MilliSeconds % 1000) * 1000;
  121.     OldTrapSignal = (void (*)(void)) signal(SIGALRM,
  122.                         (void (*)(void)) TrapSignal);
  123.     setitimer(ITIMER_REAL, &Tmr, NULL);
  124.     sigpause(~sigmask(SIGALRM));
  125.     signal(SIGALRM, OldTrapSignal);
  126. #else /* No way to milli-sleep. */
  127.     int i;
  128.  
  129.     if (MilliSeconds >= 1000) {
  130.     sleep(MilliSeconds / 1000);
  131.     MilliSeconds = MilliSeconds % 1000;
  132.     }
  133.     for (i = MilliSeconds * 10000; i > 0; i--);
  134. #endif /* ITIMERVAL */
  135. #endif /* __WINNT__ */
  136. #endif /* OS2GCC */
  137. #endif /* SGINAP */
  138. #endif /* USLEEP */
  139. #endif /* NANOSLEEP */
  140. }
  141.  
  142. /*****************************************************************************
  143. * DESCRIPTION:                                                               M
  144. * Routine to initialize the random number generator.                 M
  145. *                                                                            *
  146. * PARAMETERS:                                                                M
  147. *   Seed:       To initialize the random number generator with.              M
  148. *                                                                            *
  149. * RETURN VALUE:                                                              M
  150. *   void                                                                     M
  151. *                                                                            *
  152. * KEYWORDS:                                                                  M
  153. *   IritRandomInit, random numbers                                           M
  154. *****************************************************************************/
  155. void IritRandomInit(long Seed)
  156. {
  157. #ifdef RAND
  158.     srand(Seed);
  159. #else
  160. #ifdef RAND48
  161.     srand48(Seed);
  162. #else
  163.     srandom(Seed);
  164. #endif /* RAND48 */
  165. #endif /* RAND */
  166. }
  167.  
  168. /*****************************************************************************
  169. * DESCRIPTION:                                                               M
  170. * Routine to compute a random number in a specified range.             M
  171. *   See also IritRandomInit.                             M
  172. *                                                                            *
  173. * PARAMETERS:                                                                M
  174. *   Min:      Minimum range of random number requested.                      M
  175. *   Max:      Maximum range of random number requested.                      M
  176. *                                                                            *
  177. * RETURN VALUE:                                                              M
  178. *   RealType:   A random number between Min andMax.                          M
  179. *                                                                            *
  180. * KEYWORDS:                                                                  M
  181. *   IritRandom, random numbers                                               M
  182. *****************************************************************************/
  183. RealType IritRandom(RealType Min, RealType Max)
  184. {
  185.     long R;
  186.  
  187. #ifdef RAND
  188.     R = rand();
  189. #else
  190. #ifdef RAND48
  191.     R = lrand48();
  192. #else
  193.     R = random();
  194. #endif /* RAND48 */
  195. #endif /* RAND */
  196.  
  197. #if defined(OS2GCC) || defined(sgi)
  198.     return Min + (Max - Min) * (R & RAND_MAX) / ((double) RAND_MAX);
  199. #else
  200.     return Min + (Max - Min) * (R & 0x7fffffff) / ((double) 0x7fffffff);
  201. #endif /* OS2GCC || sgi */
  202. }
  203.  
  204. /*****************************************************************************
  205. * DESCRIPTION:                                                               M
  206. * Routine to compute the cpu time of the running process.             M
  207. *                                                                            *
  208. * PARAMETERS:                                                                M
  209. *   Reset:     If TRUE, clock is reset back to zero.                         M
  210. *                                                                            *
  211. * RETURN VALUE:                                                              M
  212. *   RealType:   CPU time since last reset or beginning of execution.         M
  213. *                                                                            *
  214. * KEYWORDS:                                                                  M
  215. *   IritCPUTime, time                                                        M
  216. *****************************************************************************/
  217. RealType IritCPUTime(int Reset)
  218. {
  219.     static RealType
  220.     LastTime = 0.0;
  221.     RealType Time;
  222.  
  223.     if (Reset) {
  224. #ifdef TIMES
  225.     struct tms tim;
  226.  
  227.     times(&tim);
  228.  
  229.     LastTime = (tim.tms_utime + tim.tms_stime) / ((RealType) HZ);
  230. #else
  231.     LastTime = time(NULL);                /* No real timing routines!? */
  232. #endif /* __WINNT__ */
  233.  
  234.     return 0.0;
  235.     }
  236.  
  237. #ifdef TIMES
  238.     {
  239.     struct tms tim;
  240.  
  241.     times(&tim);
  242.  
  243.     Time = (tim.tms_utime + tim.tms_stime) / ((RealType) HZ) - LastTime;
  244.     }
  245. #else
  246.     Time = time(NULL) - LastTime;
  247. #endif /* TIMES */
  248.  
  249.     return Time;
  250. }
  251.  
  252. /*****************************************************************************
  253. * DESCRIPTION:                                                               M
  254. * Routine to create and return a string describing current date and time.    M
  255. *                                                                            *
  256. * PARAMETERS:                                                                M
  257. *   None                                                                     *
  258. *                                                                            *
  259. * RETURN VALUE:                                                              M
  260. *   char *:    A string describing current date and time.                    M
  261. *                                                                            *
  262. * KEYWORDS:                                                                  M
  263. *   IritRealTimeDate, date, time                                             M
  264. *****************************************************************************/
  265. char *IritRealTimeDate(void)
  266. {
  267.     static char StrTime[LINE_LEN];
  268.     int i;
  269.     long
  270.     t = (long) time(NULL);
  271.  
  272.     strncpy(StrTime, ctime(&t), LINE_LEN - 1);
  273.  
  274.     /* Remove trailing EOL. */
  275.     for (i = strlen(StrTime) - 1; StrTime[i] < ' ' && i >= 0; i--);
  276.     StrTime[i + 1] = 0;
  277.  
  278.     return StrTime;
  279. }
  280.  
  281. #ifndef AMIGA
  282. /*****************************************************************************
  283. * DESCRIPTION:                                                               M
  284. * Routine to move a block in memory. Unlike memcpy/bcopy, this routine       M
  285. * should support overlaying blocks. This stupid implemetation will copy it   M
  286. * twice - to a temporary block and back again. The temporary block size will M
  287. * be allocated by demand.                             M
  288. *                                                                            *
  289. * PARAMETERS:                                                                M
  290. *   Src:     Of block to copy.                                               M
  291. *   Dest:    Of block to copy.                                               M
  292. *   Len:     Of block to copy.                                               M
  293. *                                                                            *
  294. * RETURN VALUE:                                                              M
  295. *   void                                                                     M
  296. *                                                                            *
  297. * KEYWORDS:                                                                  M
  298. *   movmem, copy                                                             M
  299. *****************************************************************************/
  300. void movmem(VoidPtr Src, VoidPtr Dest, int Len)
  301. {
  302.     VoidPtr
  303.     p = IritMalloc(Len);
  304.  
  305.     memcpy(p, Src, Len);
  306.     memcpy(Dest, p, Len);
  307.  
  308.     IritFree(p);
  309. }
  310. #endif /* AMIGA */
  311.  
  312. /*****************************************************************************
  313. * DESCRIPTION:                                                               M
  314. * RRoutine to search for a given file name.                     M
  315. *                                                                            *
  316. * PARAMETERS:                                                                M
  317. *   Name:     Of file to search for.                                         M
  318. *                                                                            *
  319. * RETURN VALUE:                                                              M
  320. *   char *:   Complete file name of Name.                                    M
  321. *                                                                            *
  322. * KEYWORDS:                                                                  M
  323. *   searchpath                                                               M
  324. *****************************************************************************/
  325. char *searchpath(char *Name)
  326. {
  327.     static char FullPath[LINE_LEN_LONG];
  328.     char *p;
  329.  
  330.     if ((p = getenv("IRIT_PATH")) != NULL) {
  331.     strcpy(FullPath, p);
  332.     strcat(FullPath, Name);
  333.     }
  334.     else {
  335.     static int Printed = FALSE;
  336.     
  337.     strcpy(FullPath, Name);
  338.  
  339.     if (!Printed) {
  340.         fprintf(stderr,
  341.             "IRIT_PATH env. not set. Only current directory is being searched.\n");
  342.         Printed = TRUE;
  343.     }
  344.     }
  345.     return FullPath;
  346. }
  347.  
  348. #ifdef STRICMP
  349. /*****************************************************************************
  350. * DESCRIPTION:                                                               M
  351. * Routine to compare two strings, ignoring case, up to given length.         M
  352. *                                                                            *
  353. * PARAMETERS:                                                                M
  354. *   s1, s2:  The two strings to compare.                                     M
  355. *   n:       maximum number of characters to compare.                        M
  356. *                                                                            *
  357. * RETURN VALUE:                                                              M
  358. *   int:     <0, 0, >0 according to the relation between s1 and s2.          M
  359. *                                                                            *
  360. * KEYWORDS:                                                                  M
  361. *   strnicmp                                                                 M
  362. *****************************************************************************/
  363. int strnicmp(char *s1, char *s2, int n)
  364. {
  365.     /* Use to be simply 'return strncasecmp(s1, s2, n);' but seems that some */
  366.     /* unix systems (sun4?) does have it so here it is explicitly.         */
  367.     /* Written by Reiner Wilhelms <reiner@shs.ohio-state.edu>.             */
  368.     int i;
  369.     char c1, c2;
  370.  
  371.     for (i = 0; i < n; i++) {
  372.         if (islower(s1[i]))
  373.         c1 = toupper(s1[i]);
  374.         else
  375.         c1 = s1[i];
  376.         if (islower(s2[i]))
  377.         c2 = toupper(s2[i]);
  378.         else
  379.         c2 = s2[i];
  380.  
  381.         if (c1 != c2) {
  382.         if (c1 > c2)
  383.         return 1;
  384.             if (c1 < c2)
  385.         return -1;
  386.     }
  387.     }
  388.     return 0;
  389. }
  390.  
  391. /*****************************************************************************
  392. * DESCRIPTION:                                                               M
  393. * Routine to compare two strings, ignoring case.                             M
  394. *                                                                            *
  395. * PARAMETERS:                                                                M
  396. *   s1, s2:  The two strings to compare.                                     M
  397. *                                                                            *
  398. * RETURN VALUE:                                                              M
  399. *   int:     <0, 0, >0 according to the relation between s1 and s2.          M
  400. *                                                                            *
  401. * KEYWORDS:                                                                  M
  402. *   stricmp                                                                  M
  403. *****************************************************************************/
  404. int stricmp(char *s1, char *s2)
  405. {
  406.     int i;
  407.     char *u1, *u2;
  408.  
  409.     if (s1 == NULL)
  410.     return s2 != NULL;
  411.     else if (s2 == NULL)
  412.     return 1;
  413.  
  414.     u1 = IritStrdup(s1);
  415.     u2 = IritStrdup(s2);
  416.  
  417.     for (i = 0; i < (int) strlen(u1); i++)
  418.     if (islower(u1[i]))
  419.         u1[i] = toupper(u1[i]);
  420.     for (i = 0; i < (int) strlen(u2); i++)
  421.     if (islower(u2[i]))
  422.         u2[i] = toupper(u2[i]);
  423.  
  424.     i = strcmp(u1, u2);
  425.  
  426.     IritFree(u1);
  427.     IritFree(u2);
  428.  
  429.     return i;
  430. }
  431. #endif /* STRICMP */
  432.  
  433. #ifdef STRSTR
  434. /*****************************************************************************
  435. * DESCRIPTION:                                                               M
  436. * Routine to search for a Pattern (no regular expression) in s. Returns      M
  437. * address in s of first occurance of Pattern, NULL if non found.         M
  438. *                                                                            M
  439. *                                                                            *
  440. * PARAMETERS:                                                                M
  441. *   s:          To search for Pattern in.                                    M
  442. *   Pattern:    To search in s.                                              M
  443. *                                                                            *
  444. * RETURN VALUE:                                                              M
  445. *   char *:     Address in s where Pattern was first found, NULL otherwise.  M
  446. *                                                                            *
  447. * KEYWORDS:                                                                  M
  448. *   strstr                                                                   M
  449. *****************************************************************************/
  450. char *strstr(char *s, char *Pattern)
  451. {
  452.     int Len = strlen(Pattern);
  453.     char
  454.     *p = (char *) s;
  455.  
  456.     while (p = strchr(p, Pattern[0]))
  457.     if (strncmp(p, Pattern, Len) == 0)
  458.         return p;
  459.     else
  460.         p++;
  461.  
  462.     return NULL;
  463. }
  464. #endif /* STRSTR */
  465.  
  466. #ifdef GETCWD
  467. /*****************************************************************************
  468. * DESCRIPTION:                                                               M
  469. *   Get current working directory - BSD4.3 style.                 *
  470. *                                                                            *
  471. * PARAMETERS:                                                                M
  472. *   s:       Where to save current working direction.                        M
  473. *   Len:     Length of s.                                                    M
  474. *                                                                            *
  475. * RETURN VALUE:                                                              M
  476. *   char *:  Same as s.                                                      M
  477. *                                                                            *
  478. * KEYWORDS:                                                                  M
  479. *   getcwd                                                                   M
  480. *****************************************************************************/
  481. char *getcwd(char *s, int Len)
  482. {
  483.     getwd(s);
  484.  
  485.     return s;
  486. }
  487. #endif /* GETCWD */
  488.