?? cm_lib.c
字號:
/********************************************************************20**
Name: common library functions
Type: C source file
Desc: Common functions that are implemented in
both a portable and a performance-efficient manner. These
functions are selected based on the operating system.
File: cm_lib.c
Sid: cm_lib.c@@/main/10 - Fri Feb 9 16:23:51 2001
Prg: ak
*********************************************************************21*/
/***********************************************************************
* This file provides memory and string library functions implemented in
* one of two ways:
*
* 1. portable: does not assume library support for any of the functions
* needed.
*
* 2. using environment specific: this uses existing library
* functions and is therefore, for an environment where such
* functionality is available and , a more desirable choice!
*
* The following functions are available in this file:
*
* 1. cmMemcpy: copies specified number of octets from one memory
* location to another.
* 2. cmMemcmp: compares specified number of octets at one memory
* locaton with the same number from another.
* 3. cmMemset: sets specified number of octets starting at given
* memory location with the given value.
* 4. cmStrCmp: compares two strings, until the '\0' character is
* encountered.
* 5. cmStrncmp: compares two strings, until \0 is encountered or
* until the specified length is exceeded.
* 6. cmStrlen: finds length of a string until the \0 character.
*
* The following flags are used in this file:
*
* 1. DONT_USE_SYS_LIB: If defined, this will compile in the portable
* versions of each function in this file. This overrides all other
* flags defined in this file.
*
* For the SOLARIS environment:
*
* The functionality defined in this file is generally available as library
* functions in the native operating systems' libraries. We have provided
* the functionality using the SOLARIS libraries, in this file.
*
* If you want to take advantage of these library functions, all you need
* to do is the following:
*
* 1. Do not turn on the DONT_USE_SYS_LIB flag.
* 2. Turn on the SUNOS51 flag.
*
* This, in turn, turns on the following flags
*
* 1. MEMCPY_AVAIL : maps cmMemcpy to C library function memcpy
* 2. MEMCMP_AVAIL : maps cmMemcmp to C library function memcmp
* 3. MEMSET_AVAIL : maps cmMemset to C library function memset
* 4. STRCMP_AVAIL : maps cmStrcmp to Strings library function strcmp
* 5. STRNCMP_AVAIL : maps cmStrncmp to Strings library function strncmp
* 5. STRLEN_AVAIL : maps cmStrlen to Strings library function strlen
*
* For an environment different from SOLARIS:
*
* You will need to modify this file to do more or less the same stuff
* as has been done for Solaris. i.e.
*
* 1. create a section inside the #ifndef DONT_USE_SYS_LIB section
* similar to the #ifdef SUNOS51 section that:
* 1. includes system header files.
* 2. defines MEMCPY_AVAIL etc. as needed.
* 2. modify code inside functions to make use of the system library
* primitive.
* 3. communicate your changes to Trillium so they can be incorporated
* in the next release of this file
*
* To add a new library primitive to this file:
* 1. it should be implemented in both a portable and environment specific
* manner.
* 2. the portable implementation will not be the default
* 3. the portable version and the environment specif versions must be
* enclosed in #ifdef XXXXXX_AVAIL
* <environment specific implementation>
* #else
* <portable implementation>
* #endif
* 4. It must be communicated to Trillium so it will be included in the
* next release of the file.
* 5. Trillium engineering must have all changes approved by engineering
* management.
* 6. Trillium reserves the right to not incorporate any changes submitted
* by customers, if not approved by Trillium engineering management.
* 7. Trillium reserves the right to modify code submitted by customers to
* enhance this file.
************************************************************************/
#ifndef DONT_USE_SYS_LIB
#define MEMCPY_AVAIL 1
#define MEMCMP_AVAIL 1
#define MEMSET_AVAIL 1
#define STRCMP_AVAIL 1
#define STRNCMP_AVAIL 1
#define STRLEN_AVAIL 1
#else /* DONT_USE_SYS_LIB */
#define MEMCPY_AVAIL 0
#define MEMCMP_AVAIL 0
#define MEMSET_AVAIL 0
#define STRCMP_AVAIL 0
#define STRNCMP_AVAIL 0
#define STRLEN_AVAIL 0
#endif /* not DONT_USE_SYS_LIB */
#include "envopt.h" /* environment options */
#include "envind.h" /* environment options */
#include "envdep.h" /* environment options */
#include "gen.h" /* general layer */
#include "ssi.h" /* system services */
/* header/extern include files (.x) */
#include "gen.x" /* general layer */
#include "ssi.x" /* system services */
#include "cm_lib.x" /* prototypes of primitives in this file */
#include "stdio.h"
#include "ctype.h"
#include "stdlib.h"
#include "string.h"
#if (ERRCLASS & ERRCLS_DEBUG)
#define ECMLIBBASE 0
#define ECMLIB001 ECMLIBBASE + 1
#define ECMLIB002 ECMLIBBASE + 2
#define ECMLIB003 ECMLIBBASE + 3
#define ECMLIB004 ECMLIBBASE + 4
#define ECMLIB005 ECMLIBBASE + 5
#define ECMLIB006 ECMLIBBASE + 6
#define ECMLIB007 ECMLIBBASE + 7
#define CMLIBERR(_eCode, _eVal, _eDesc) \
SLogError ((Ent) 0, (Inst)0, (ProcId)0, __FILE__, __LINE__, \
(ErrCls)ERRCLS_DEBUG, (ErrCode)_eCode, (ErrVal) _eVal, \
(Txt *) _eDesc)
#else
#define CMLIBERR(_eCode, _eVal, _eDesc)
#endif
/*
*
* Fun: cmMemcpy
*
* Desc: common primitive to copy a contiguous string of bytes
* optimized for when memcpy is available. It uses memcpy
* when available. Otherwise, copies in a 'for' loop.
*
* sets "len" memory locations starting from "tgt" to the values
* of corresponding memory locations starting from "src".
*
* Ret: pointer to target string
*
* Notes: None
*
* File: cm_lib.c
*
*/
#ifdef ANSI
PUBLIC U8 *cmMemcpy
(
U8 *tgt,
CONSTANT U8 *src,
PTR len
)
#else
PUBLIC U8 *cmMemcpy(tgt, src, len)
U8 *tgt;
CONSTANT U8 *src;
PTR len;
#endif
{
#if (MEMCPY_AVAIL) /* memcpy is available */
RETVALUE((U8 *) memcpy((Void *)tgt, (CONSTANT Void *)src, (size_t)len));
#else
while (len--)
*tgt++ = *src++;
RETVALUE(tgt);
#endif /* MEMCPY_AVAIL */
} /* end of cmMemcpy */
/*
*
* Fun: cmMemcmp
*
* Desc: common primitive to compare a contiguous string of bytes
* optimized for when memcmp is available. It uses memcmp
* when available. Otherwise, compares in a 'for' loop.
*
* Ret: < 0 => s1 < s2
* > 0 => s1 > s2
* = 0 => s1 = s2
*
* Notes: None
*
* File: cm_lib.c
*
*/
#ifdef ANSI
PUBLIC S16 cmMemcmp
(
CONSTANT U8 *s1,
CONSTANT U8 *s2,
PTR len
)
#else
PUBLIC S16 cmMemcmp (s1, s2, len)
CONSTANT U8 *s1;
CONSTANT U8 *s2;
PTR len;
#endif
{
#if MEMCMP_AVAIL /* memcmp is available */
RETVALUE((S16) memcmp((CONSTANT Void *)s1, (CONSTANT Void *)s2, (size_t)len));
#else /* MEMCMP_AVAIL: memcmp is not available */
while (len--)
{
if (*s1 ^ *s2)
RETVALUE((S16) (*s1 - *s2));
s1++;
s2++;
}
RETVALUE(0);
#endif /* MEMCMP_AVAIL */
} /* end of cmMemcmp */
/*
*
* Fun: cmMemset
*
* Desc: common primitive to set a contiguous string of bytes
* with a specified value optimized for when memset is available.
* It uses memset when available. Otherwise, uses a 'for' loop.
*
* sets "len" memory locations starting from "str" to the value
* "val".
*
* Ret: pointer to string
*
* Notes: None
*
* File: cm_lib.c
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -