home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / remind / src / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  5.6 KB  |  182 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /*  UTILS.C                                                    */
  4. /*                                                             */
  5. /*  Useful utility functions.                                  */
  6. /*                                                             */
  7. /*  This file is part of REMIND.                               */
  8. /*  Copyright (C) 1992, 1993 by David F. Skoll.                */
  9. /*                                                             */
  10. /***************************************************************/
  11.  
  12. #include "config.h"
  13. #include <string.h>
  14. #include <stdio.h>
  15. #ifdef HAVE_STDLIB_H
  16. #include <stdlib.h>
  17. #endif
  18. #ifdef HAVE_MALLOC_H
  19. #include <malloc.h>
  20. #endif
  21. #include <ctype.h>
  22. #include "types.h"
  23. #include "globals.h"
  24. #include "protos.h"
  25.  
  26. #define UPPER(c) (islower(c) ? toupper(c) : c)
  27.  
  28. /***************************************************************/
  29. /*                                                             */
  30. /*  StrnCpy                                                    */
  31. /*                                                             */
  32. /*  Just like strncpy EXCEPT we ALWAYS copy the trailing 0.    */
  33. /*                                                             */
  34. /***************************************************************/
  35. #ifdef HAVE_PROTOS
  36. PUBLIC char *StrnCpy(char *dest, const char *source, int n)
  37. #else
  38. char *StrnCpy(dest, source, n)
  39. char *dest, *source;
  40. int n;
  41. #endif
  42. {
  43.    register char *odest = dest;
  44.  
  45.    while (n-- && (*dest++ = *source++)) ;
  46.    if (*(dest-1)) *dest = 0;
  47.    return odest;
  48. }
  49.  
  50. /***************************************************************/
  51. /*                                                             */
  52. /*  StrMatch                                                   */
  53. /*                                                             */
  54. /*  Checks that two strings match (case-insensitive) to at     */
  55. /*  least the specified number of characters, or the length    */
  56. /*  of the first string, whichever is greater.                 */
  57. /*                                                             */
  58. /***************************************************************/
  59. #ifdef HAVE_PROTOS
  60. PUBLIC int StrMatch(const char *s1, const char *s2, int n)
  61. #else
  62. int StrMatch(s1, s2, n)
  63. char *s1, *s2;
  64. int n;
  65. #endif
  66. {
  67.    int l;
  68.    if ((l = strlen(s1)) < n) return 0;
  69.    return !StrinCmp(s1, s2, l);
  70. }
  71.  
  72. /***************************************************************/
  73. /*                                                             */
  74. /*  StrinCmp - compare strings, case-insensitive               */
  75. /*                                                             */
  76. /***************************************************************/
  77. #ifdef HAVE_PROTOS
  78. PUBLIC int StrinCmp(const char *s1, const char *s2, int n)
  79. #else
  80. int StrinCmp(s1, s2, n)
  81. char *s1, *s2;
  82. int n;
  83. #endif
  84. {
  85.    register int r;
  86.    while (n && *s1 && *s2) {
  87.       n--;
  88.       r = UPPER(*s1) - UPPER(*s2);
  89.       if (r) return r;
  90.       s1++;
  91.       s2++;
  92.    }
  93.    if (n) return (UPPER(*s1) - UPPER(*s2)); else return 0;
  94. }
  95.  
  96. /***************************************************************/
  97. /*                                                             */
  98. /*  StrDup                                                     */
  99. /*                                                             */
  100. /*  Like ANSI strdup                                           */
  101. /*                                                             */
  102. /***************************************************************/
  103. #ifdef HAVE_PROTOS
  104. PUBLIC char *StrDup(const char *s)
  105. #else
  106. char *StrDup(s)
  107. char *s;
  108. #endif
  109. {
  110.    char *ret = (char *) malloc(strlen(s)+1);
  111.    if (!ret) return (char *) NULL;
  112.    strcpy(ret, s);
  113.    return ret;
  114. }
  115.  
  116. /***************************************************************/
  117. /*                                                             */
  118. /*  StrCmpi                                                    */
  119. /*                                                             */
  120. /*  Compare strings, case insensitive.                         */
  121. /*                                                             */
  122. /***************************************************************/
  123. #ifdef HAVE_PROTOS
  124. PUBLIC int StrCmpi(const char *s1, const char *s2)
  125. #else
  126. int StrCmpi(s1, s2)
  127. char *s1, *s2;
  128. #endif
  129. {
  130.    int r;
  131.    while (*s1 && *s2) {
  132.       r = UPPER(*s1) - UPPER(*s2);
  133.       if (r) return r;
  134.       s1++;
  135.       s2++;
  136.    }
  137.    return UPPER(*s1) - UPPER(*s2);
  138. }
  139.  
  140. #ifdef NO_STRSTR
  141. #ifdef HAVE_PROTOS
  142. PUBLIC char *strstr(char *s1, char *s2)
  143. #else
  144. char *strstr(s1, s2)
  145. char *s1, *s2;
  146. #endif
  147. {
  148.    char *s = s1;
  149.    int len2 = strlen(s2);
  150.    int len1 = strlen(s1);
  151.  
  152.    while (s-s1 <= len1-len2) {
  153.       if (!strncmp(s, s2, len2)) return s;
  154.       s++;
  155.    }
  156.    return NULL;
  157. }
  158. #endif
  159.  
  160. /***************************************************************/
  161. /*                                                             */
  162. /*  DateOK                                                     */
  163. /*                                                             */
  164. /*  Return 1 if the date is OK, 0 otherwise.                   */
  165. /*                                                             */
  166. /***************************************************************/
  167. #ifdef HAVE_PROTOS
  168. PUBLIC int DateOK(int y, int m, int d)
  169. #else
  170. int DateOK(y, m, d)
  171. int y, m, d;
  172. #endif
  173. {
  174.    if (d < 1                 ||
  175.        m < 0                 ||
  176.        y < BASE              ||
  177.        m > 11                ||
  178.        y > BASE + YR_RANGE   ||
  179.        d > DaysInMonth(m, y) ) return 0;
  180.    else return 1;
  181. }
  182.