home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / SocketClasses / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-23  |  8.9 KB  |  194 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. * util.h                                                                   *
  4. * Copyright 1992 by Nik A Gervae                                           *
  5. *                                                                          *
  6. * For use with the three Objective-C classes (SktSocketManager, SktSocket, *
  7. * and SktSocketUser) which implement a convenient interface to Berkeley    *
  8. * stream sockets under NeXTSTEP(r).  See the accompanying class            *
  9. * specifications (files with a .rtf or .spec suffix) for further           *
  10. * information.                                                             *
  11. *                                                                          *
  12. * NeXTSTEP is a registered trademark of NeXT Computer, Inc.                *
  13. *                                                                          *
  14. ****************************************************************************
  15. *                                                                          *
  16. * LICENSE                                                                  *
  17. *                                                                          *
  18. * This program is free software; you can redistribute it and/or modify     *
  19. * it under the terms of the GNU General Public License as published by     *
  20. * the Free Software Foundation.                                            *
  21. *                                                                          *
  22. * The program and this makefile are distributed in the hope that it will   *
  23. * be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without    *
  24. * any express or implied warranty of MERCHANTABILITY or FITNESS FOR A      *
  25. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  26. * Any use or distribution of the program and documentation must include    *
  27. * appropriate copyrights to acknowledge Nik A. Gervae and the Free         *
  28. * Software Foundation, Inc.                                                *
  29. *                                                                          *
  30. * You should have received a copy of the GNU General Public License        *
  31. * along with this program; if not, write to the Free Software              *
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
  33. *                                                                          *
  34. ****************************************************************************
  35. *                                                                          *
  36. * VERSION HISTORY                                                          *
  37. *                                                                          *
  38. * Version numbers are simply dates in the form YYYYMMDD.  These represent  *
  39. * the date that version was finished.  Only significantly changed versions *
  40. * are reported here, or those versions requiring explanation of changes.   *
  41. * There may be many interim stages between dated versions.                 *
  42. *                                                                          *
  43. * DateVersion Primary Author  Notes                                        *
  44. * ----------- --------------- -------------------------------------------- *
  45. * 19920327    Nik A Gervae    First released version                       *
  46. *                                                                          *
  47. ***************************************************************************/
  48.  
  49. #import <string.h>
  50. #import "util.h"
  51.  
  52. /***************************************************************************
  53. *                                                                          *
  54. * rstrncmp()                                                               *
  55. *                                                                          *
  56. * Compares strings backwards!  Returns >0 if str1 is "greater than" str2,  *
  57. * 0 if they're equal in len, and <0 if str2 is "greater than" str1.        *
  58. *                                                                          *
  59. ***************************************************************************/
  60. int rstrncmp(const char *str1, const char *str2, int len)
  61. {
  62.   const char *sp1, *sp2;
  63.  
  64.  /*
  65.   * Find the end of each string.
  66.   */
  67.   for (sp1 = str1; *sp1; sp1++)
  68.     ;
  69.   sp1--;
  70.   for (sp2 = str2; *sp2; sp2++)
  71.     ;
  72.   sp2--;
  73.  
  74.  /*
  75.   * While we have to compare and they're equal, step back.  Having the
  76.   * length check first here is imporant for the return checks below.
  77.   */
  78.   for (; len && *sp1 == *sp2 && sp1>=str1 && sp2>=str2; len--, sp1--, sp2--)
  79.     ;
  80.  
  81.  /*
  82.   * If we've exausted the length, they must be equal. Else check various
  83.   * conditions.
  84.   */
  85.   if (!len) return 0;                      // len exhausted and still equal
  86.   if (sp1 >= str1 && sp2 >= str2) return *sp1 - *sp2;   // inequality found
  87.   else if (sp1 >= str1 && sp2 < str2) return 1;   // str2 shorter than str1
  88.   else if (sp1 < str1 && sp2 >= str2) return -1;  // str1 shorter than str2
  89.   else return 0;                              // Equal but shorter than len
  90. }
  91.  
  92. /***************************************************************************
  93. *                                                                          *
  94. * strdup()                                                                 *
  95. *                                                                          *
  96. * Returns a pointer to a copy of the supplied string, or NULL.             *
  97. *                                                                          *
  98. ***************************************************************************/
  99. char *strdup(const char *string)
  100. {
  101.   return zoneStrdup(NXDefaultMallocZone(), string);
  102. }
  103.  
  104. /***************************************************************************
  105. *                                                                          *
  106. * strrecat()                                                               *
  107. *                                                                          *
  108. * Returns a pointer to a new string made from string1 and string2, or      *
  109. * NULL.                                                                    *
  110. *                                                                          *
  111. ***************************************************************************/
  112. char *strrecat(const char *string1, const char *string2)
  113. {
  114.   return zoneStrrecat(NXDefaultMallocZone(), string1, string2);
  115. }
  116.  
  117. /***************************************************************************
  118. *                                                                          *
  119. * zoneStrdup()                                                             *
  120. *                                                                          *
  121. * Returns a pointer to a copy of the supplied string, allocated from       *
  122. * zonep, or NULL.                                                          *
  123. *                                                                          *
  124. ***************************************************************************/
  125. char *zoneStrdup(NXZone *zonep, const char *string)
  126. {
  127.   char *newString;
  128.   int   len;
  129.  
  130.  /*
  131.   * Do the obvious.
  132.   */
  133.   if (NULL == string) return NULL;
  134.  
  135.  /*
  136.   *Get a new buffer, return NULL on failure.
  137.   */
  138.   len = strlen(string);
  139.   newString = (char *)NXZoneMalloc(zonep, 1 + len);
  140.   if (NULL == newString) return NULL;
  141.  
  142.  /*
  143.   * Copy that puppy in there.
  144.   */
  145.   memcpy(newString, string, 1+len);  // "1+" gets the NULL at the end
  146.  
  147.   return newString;
  148.  
  149. } /*zoneStrdup*/
  150.  
  151. /***************************************************************************
  152. *                                                                          *
  153. * zoneStrrecat()                                                           *
  154. *                                                                          *
  155. * Returns a pointer to a new string made from string1 and string2,         *
  156. * allocated from zonep, or NULL.                                           *
  157. *                                                                          *
  158. ***************************************************************************/
  159. char *zoneStrrecat(NXZone *zonep, const char *string1,const char *string2)
  160. {
  161.   char *newString;
  162.   int   len1, len2, newLen;
  163.  
  164.  /*
  165.   * Take care of the easy cases.
  166.   */
  167.   if (NULL == string1 && NULL == string2) return NULL;
  168.   else if (string1 && NULL == string2) return zoneStrdup(zonep, string1);
  169.   else if (NULL == string1 && string2) return zoneStrdup(zonep, string2);
  170.  
  171.  /*
  172.   * How much do we need?
  173.   */
  174.   len1 = strlen(string1);
  175.   len2 = strlen(string2);
  176.   newLen = len1 + len2;
  177.  
  178.  /*
  179.   * Get a new buffer, return NULL on failure.
  180.   */
  181.   newString = (char *)NXZoneMalloc(zonep, 1 + newLen);
  182.   if (NULL == newString) return NULL;
  183.  
  184.  /*
  185.   * Copy that puppy in there.
  186.   */
  187.   memcpy(newString, string1, len1);
  188.   memcpy(newString+len1, string2, 1+len2);  // "1+" gets the NULL at the end
  189.  
  190.   return newString;  
  191.  
  192. } /*zoneStrrecat*/
  193.  
  194.