home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * *
- * util.h *
- * Copyright 1992 by Nik A Gervae *
- * *
- * For use with the three Objective-C classes (SktSocketManager, SktSocket, *
- * and SktSocketUser) which implement a convenient interface to Berkeley *
- * stream sockets under NeXTSTEP(r). See the accompanying class *
- * specifications (files with a .rtf or .spec suffix) for further *
- * information. *
- * *
- * NeXTSTEP is a registered trademark of NeXT Computer, Inc. *
- * *
- ****************************************************************************
- * *
- * LICENSE *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation. *
- * *
- * The program and this makefile are distributed in the hope that it will *
- * be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without *
- * any express or implied warranty of MERCHANTABILITY or FITNESS FOR A *
- * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
- * Any use or distribution of the program and documentation must include *
- * appropriate copyrights to acknowledge Nik A. Gervae and the Free *
- * Software Foundation, Inc. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the Free Software *
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
- * *
- ****************************************************************************
- * *
- * VERSION HISTORY *
- * *
- * Version numbers are simply dates in the form YYYYMMDD. These represent *
- * the date that version was finished. Only significantly changed versions *
- * are reported here, or those versions requiring explanation of changes. *
- * There may be many interim stages between dated versions. *
- * *
- * DateVersion Primary Author Notes *
- * ----------- --------------- -------------------------------------------- *
- * 19920327 Nik A Gervae First released version *
- * *
- ***************************************************************************/
-
- #import <string.h>
- #import "util.h"
-
- /***************************************************************************
- * *
- * rstrncmp() *
- * *
- * Compares strings backwards! Returns >0 if str1 is "greater than" str2, *
- * 0 if they're equal in len, and <0 if str2 is "greater than" str1. *
- * *
- ***************************************************************************/
- int rstrncmp(const char *str1, const char *str2, int len)
- {
- const char *sp1, *sp2;
-
- /*
- * Find the end of each string.
- */
- for (sp1 = str1; *sp1; sp1++)
- ;
- sp1--;
- for (sp2 = str2; *sp2; sp2++)
- ;
- sp2--;
-
- /*
- * While we have to compare and they're equal, step back. Having the
- * length check first here is imporant for the return checks below.
- */
- for (; len && *sp1 == *sp2 && sp1>=str1 && sp2>=str2; len--, sp1--, sp2--)
- ;
-
- /*
- * If we've exausted the length, they must be equal. Else check various
- * conditions.
- */
- if (!len) return 0; // len exhausted and still equal
- if (sp1 >= str1 && sp2 >= str2) return *sp1 - *sp2; // inequality found
- else if (sp1 >= str1 && sp2 < str2) return 1; // str2 shorter than str1
- else if (sp1 < str1 && sp2 >= str2) return -1; // str1 shorter than str2
- else return 0; // Equal but shorter than len
- }
-
- /***************************************************************************
- * *
- * strdup() *
- * *
- * Returns a pointer to a copy of the supplied string, or NULL. *
- * *
- ***************************************************************************/
- char *strdup(const char *string)
- {
- return zoneStrdup(NXDefaultMallocZone(), string);
- }
-
- /***************************************************************************
- * *
- * strrecat() *
- * *
- * Returns a pointer to a new string made from string1 and string2, or *
- * NULL. *
- * *
- ***************************************************************************/
- char *strrecat(const char *string1, const char *string2)
- {
- return zoneStrrecat(NXDefaultMallocZone(), string1, string2);
- }
-
- /***************************************************************************
- * *
- * zoneStrdup() *
- * *
- * Returns a pointer to a copy of the supplied string, allocated from *
- * zonep, or NULL. *
- * *
- ***************************************************************************/
- char *zoneStrdup(NXZone *zonep, const char *string)
- {
- char *newString;
- int len;
-
- /*
- * Do the obvious.
- */
- if (NULL == string) return NULL;
-
- /*
- *Get a new buffer, return NULL on failure.
- */
- len = strlen(string);
- newString = (char *)NXZoneMalloc(zonep, 1 + len);
- if (NULL == newString) return NULL;
-
- /*
- * Copy that puppy in there.
- */
- memcpy(newString, string, 1+len); // "1+" gets the NULL at the end
-
- return newString;
-
- } /*zoneStrdup*/
-
- /***************************************************************************
- * *
- * zoneStrrecat() *
- * *
- * Returns a pointer to a new string made from string1 and string2, *
- * allocated from zonep, or NULL. *
- * *
- ***************************************************************************/
- char *zoneStrrecat(NXZone *zonep, const char *string1,const char *string2)
- {
- char *newString;
- int len1, len2, newLen;
-
- /*
- * Take care of the easy cases.
- */
- if (NULL == string1 && NULL == string2) return NULL;
- else if (string1 && NULL == string2) return zoneStrdup(zonep, string1);
- else if (NULL == string1 && string2) return zoneStrdup(zonep, string2);
-
- /*
- * How much do we need?
- */
- len1 = strlen(string1);
- len2 = strlen(string2);
- newLen = len1 + len2;
-
- /*
- * Get a new buffer, return NULL on failure.
- */
- newString = (char *)NXZoneMalloc(zonep, 1 + newLen);
- if (NULL == newString) return NULL;
-
- /*
- * Copy that puppy in there.
- */
- memcpy(newString, string1, len1);
- memcpy(newString+len1, string2, 1+len2); // "1+" gets the NULL at the end
-
- return newString;
-
- } /*zoneStrrecat*/
-
-