Home | History | Annotate | Download | only in engine
      1 /*
      2  * Copyright (C) 2008,2009  OMRON SOFTWARE Co., Ltd.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "nj_lib.h"
     18 #include "nj_ext.h"
     19 
     20 
     21 NJ_CHAR *nj_strcpy(NJ_CHAR *dst, NJ_CHAR *src) {
     22 
     23     NJ_CHAR *ret = dst;
     24 
     25 
     26     while (*src != NJ_CHAR_NUL) {
     27         *dst++ = *src++;
     28     }
     29     *dst = *src;
     30     return ret;
     31 }
     32 
     33 NJ_CHAR *nj_strncpy(NJ_CHAR *dst, NJ_CHAR *src, NJ_UINT16 n) {
     34 
     35     NJ_CHAR *d = dst;
     36 
     37 
     38     while (n != 0) {
     39         if (*src == NJ_CHAR_NUL) {
     40             while (n != 0) {
     41                 *d++ = NJ_CHAR_NUL;
     42                 n--;
     43             }
     44             break;
     45         } else {
     46             *d++ = *src++;
     47         }
     48         n--;
     49     }
     50     return dst;
     51 }
     52 
     53 NJ_UINT16 nj_strlen(NJ_CHAR *c) {
     54 
     55     NJ_UINT16 count = 0;
     56 
     57 
     58     while (*c++ != NJ_CHAR_NUL) {
     59         count++;
     60     }
     61     return count;
     62 }
     63 
     64 NJ_INT16 nj_strcmp(NJ_CHAR *s1, NJ_CHAR *s2) {
     65 
     66     while (*s1 == *s2) {
     67         if (*s1 == NJ_CHAR_NUL) {
     68             return (0);
     69         }
     70         s1++;
     71         s2++;
     72     }
     73     return NJ_CHAR_DIFF(s1, s2);
     74 }
     75 
     76 NJ_INT16 nj_strncmp(NJ_CHAR *s1, NJ_CHAR *s2, NJ_UINT16 n) {
     77 
     78     while (n != 0) {
     79         if (*s1 != *s2++) {
     80             return NJ_CHAR_DIFF(s1, (s2 - 1));
     81         }
     82         if (*s1++ == NJ_CHAR_NUL) {
     83             break;
     84         }
     85         n--;
     86     }
     87     return (0);
     88 }
     89 
     90 NJ_UINT16 nj_charlen(NJ_CHAR *c) {
     91 
     92     NJ_UINT16 count = 0;
     93 
     94 
     95     while (*c != NJ_CHAR_NUL) {
     96         count++;
     97         c += NJ_CHAR_LEN(c);
     98     }
     99     return count;
    100 }
    101 
    102 NJ_INT16 nj_charncmp(NJ_CHAR *s1, NJ_CHAR *s2, NJ_UINT16 n) {
    103     NJ_UINT16 i;
    104 
    105 
    106     while (n != 0) {
    107         for (i = NJ_CHAR_LEN(s1); i != 0; i--) {
    108             if (*s1 != *s2) {
    109                 return NJ_CHAR_DIFF(s1, s2);
    110             }
    111             if (*s1 == NJ_CHAR_NUL) {
    112                 break;
    113             }
    114             s1++;
    115             s2++;
    116         }
    117         n--;
    118     }
    119     return (0);
    120 }
    121 
    122 NJ_CHAR *nj_charncpy(NJ_CHAR *dst, NJ_CHAR *src, NJ_UINT16 n) {
    123 
    124     NJ_CHAR *d = dst;
    125     NJ_UINT16 i;
    126 
    127 
    128     while (n != 0) {
    129         for (i = NJ_CHAR_LEN(src); i != 0; i--) {
    130             *d = *src;
    131             if (*src == NJ_CHAR_NUL) {
    132                 return dst;
    133             }
    134             d++;
    135             src++;
    136         }
    137         n--;
    138     }
    139     *d = NJ_CHAR_NUL;
    140     return dst;
    141 }
    142 
    143 NJ_UINT8 *nj_memcpy(NJ_UINT8 *dst, NJ_UINT8 *src, NJ_UINT16 n) {
    144 
    145     NJ_UINT8 *d = dst;
    146 
    147 
    148     while (n != 0) {
    149         *d++ = *src++;
    150         n--;
    151     }
    152     return dst;
    153 }
    154