os/ossrv/genericopenlibs/cstdlib/LCHAR/STRNCMP.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LCHAR/STRNCMP.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,80 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* FUNCTION
    1.19 +* <<strncmp>>---character string compare
    1.20 +* INDEX
    1.21 +* strncmp
    1.22 +* ANSI_SYNOPSIS
    1.23 +* #include <string.h>
    1.24 +* int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
    1.25 +* TRAD_SYNOPSIS
    1.26 +* #include <string.h>
    1.27 +* int strncmp(<[a]>, <[b]>, <[length]>)
    1.28 +* char *<[a]>;
    1.29 +* char *<[b]>;
    1.30 +* size_t <[length]>
    1.31 +* <<strncmp>> compares up to <[length]> characters
    1.32 +* from the string at <[a]> to the string at <[b]>.
    1.33 +* RETURNS
    1.34 +* If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
    1.35 +* <<strncmp>> returns a number greater than zero.  If the two
    1.36 +* strings are equivalent, <<strncmp>> returns zero.  If <<*<[a]>>>
    1.37 +* sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a
    1.38 +* number less than zero.
    1.39 +* PORTABILITY
    1.40 +* <<strncmp>> is ANSI C.
    1.41 +* <<strncmp>> requires no supporting OS subroutines.
    1.42 +* QUICKREF
    1.43 +* strncmp ansi pure
    1.44 +* 
    1.45 +*
    1.46 +*/
    1.47 +
    1.48 +
    1.49 +
    1.50 +#include <string.h>
    1.51 +
    1.52 +/**
    1.53 +Compare some characters of two strings.
    1.54 +Compares the first num characters of string1 to the first num characters of string2.
    1.55 +The comparison is performed character by character. 
    1.56 +If a character that is not equal in both strings is found the function ends 
    1.57 +and returns a value that determines which of them was greater.
    1.58 +@return a value indicating the lexicographical relation between the strings.
    1.59 +@param s1 Null-terminated string to compare 
    1.60 +@param s2 Null-terminated string to compare. 
    1.61 +@param n Maximum number of characters to compare.
    1.62 +*/
    1.63 +EXPORT_C int 
    1.64 +strncmp (const char *s1, const char *s2, size_t n)
    1.65 +{
    1.66 +	const unsigned char* p1 = (const unsigned char*)s1;
    1.67 +	const unsigned char* p2 = (const unsigned char*)s2;
    1.68 +
    1.69 +	if (n == 0)
    1.70 +		return 0;
    1.71 +
    1.72 +	for (;;)
    1.73 +	{
    1.74 +		int c1 = *p1++;
    1.75 +		int d = c1 - *p2++;
    1.76 +		if (d != 0)
    1.77 +			return d;
    1.78 +		if (c1 == 0)
    1.79 +			return d;
    1.80 +		if (--n == 0)
    1.81 +			return d;
    1.82 +	}
    1.83 +}