NUMf2c.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* NUMf2c.c */
  2. #include <ctype.h>
  3. #include "NUMf2c.h"
  4. //#include "melder.h"
  5. double d_sign (double *a, double *b) {
  6. double x;
  7. x = (*a >= 0.0 ? *a : - *a);
  8. return (*b >= 0.0 ? x : -x);
  9. }
  10. integer lsame_ (const char *ca, const char *cb) {
  11. int a = * (unsigned char *) ca;
  12. int b = * (unsigned char *) cb;
  13. return tolower (a) == tolower (b);
  14. }
  15. double pow_di (double *ap, integer *bp) {
  16. double pow, x;
  17. integer n;
  18. uinteger u;
  19. pow = 1;
  20. x = *ap;
  21. n = *bp;
  22. if (n != 0) {
  23. if (n < 0) {
  24. n = -n;
  25. x = 1.0 / x;
  26. }
  27. for (u = n; ;) {
  28. if (u & 01) {
  29. pow *= x;
  30. }
  31. if (u >>= 1) {
  32. x *= x;
  33. } else {
  34. break;
  35. }
  36. }
  37. }
  38. return (pow);
  39. }
  40. void s_cat (char *lp, const char *rpp[], integer rnp[], integer *np, integer ll) {
  41. integer i, nc;
  42. char *rp;
  43. integer n = *np;
  44. #ifndef NO_OVERWRITE
  45. integer L, m;
  46. char *lp0, *lp1;
  47. lp0 = 0;
  48. lp1 = lp;
  49. L = ll;
  50. i = 0;
  51. while (i < n) {
  52. rp = (char *) rpp[i];
  53. m = rnp[i++];
  54. if (rp >= lp1 || rp + m <= lp) {
  55. if ( (L -= m) <= 0) {
  56. n = i;
  57. break;
  58. }
  59. lp1 += m;
  60. continue;
  61. }
  62. lp0 = lp;
  63. lp = lp1 = Melder_malloc_f (char, L = ll);
  64. break;
  65. }
  66. lp1 = lp;
  67. #endif /* NO_OVERWRITE */
  68. for (i = 0 ; i < n ; ++i) {
  69. nc = ll;
  70. if (rnp[i] < nc) {
  71. nc = rnp[i];
  72. }
  73. ll -= nc;
  74. rp = (char *) rpp[i];
  75. while (--nc >= 0) {
  76. *lp++ = *rp++;
  77. }
  78. }
  79. while (--ll >= 0) {
  80. *lp++ = ' ';
  81. }
  82. #ifndef NO_OVERWRITE
  83. if (lp0) {
  84. memcpy (lp0, lp1, L);
  85. free (lp1);
  86. }
  87. #endif
  88. }
  89. /* compare two strings */
  90. integer s_cmp (const char *a0, const char *b0, integer la, integer lb) {
  91. unsigned char *a, *aend, *b, *bend;
  92. a = (unsigned char *) a0;
  93. b = (unsigned char *) b0;
  94. aend = a + la;
  95. bend = b + lb;
  96. if (la <= lb) {
  97. while (a < aend)
  98. if (*a != *b) {
  99. return (*a - *b);
  100. } else {
  101. ++a;
  102. ++b;
  103. }
  104. while (b < bend)
  105. if (*b != ' ') {
  106. return (' ' - *b);
  107. } else {
  108. ++b;
  109. }
  110. }
  111. else {
  112. while (b < bend)
  113. if (*a == *b) {
  114. ++a;
  115. ++b;
  116. } else {
  117. return (*a - *b);
  118. }
  119. while (a < aend)
  120. if (*a != ' ') {
  121. return (*a - ' ');
  122. } else {
  123. ++a;
  124. }
  125. }
  126. return (0);
  127. }
  128. void s_copy (char *a, char *b, integer la, integer lb) {
  129. char *aend, *bend;
  130. aend = a + la;
  131. if (la <= lb)
  132. #ifndef NO_OVERWRITE
  133. if (a <= b || a >= b + la)
  134. #endif
  135. while (a < aend) {
  136. *a++ = *b++;
  137. }
  138. #ifndef NO_OVERWRITE
  139. else
  140. for (b += la; a < aend;) {
  141. *--aend = *--b;
  142. }
  143. #endif
  144. else {
  145. bend = b + lb;
  146. #ifndef NO_OVERWRITE
  147. if (a <= b || a >= bend)
  148. #endif
  149. while (b < bend) {
  150. *a++ = *b++;
  151. }
  152. #ifndef NO_OVERWRITE
  153. else {
  154. a += lb;
  155. while (b < bend) {
  156. *--a = *--bend;
  157. }
  158. a += lb;
  159. }
  160. #endif
  161. while (a < aend) {
  162. *a++ = ' ';
  163. }
  164. }
  165. }
  166. /* End of file NUMf2c.c */