File: strings_defs.h

package info (click to toggle)
libvpl-tools 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,652 kB
  • sloc: cpp: 107,469; python: 4,303; ansic: 3,202; sh: 159; lisp: 52; makefile: 13
file content (85 lines) | stat: -rw-r--r-- 2,787 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*############################################################################
  # Copyright (C) 2005 Intel Corporation
  #
  # SPDX-License-Identifier: MIT
  ############################################################################*/

#ifndef __STRING_DEFS_H__
#define __STRING_DEFS_H__

#ifndef __STDC_WANT_LIB_EXT1__
    #define __STDC_WANT_LIB_EXT1__ 1
#endif

#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#include <cstdarg>
#ifdef __cplusplus
    #include <string>
#endif

#ifndef __STDC_LIB_EXT1__
inline int strncpy_s(char* dest, size_t destsz, const char* src, size_t count) {
    if (!src || !dest || destsz == 0 || (count >= destsz && destsz <= strnlen(src, count))) {
        return 1;
    }
    strncpy(dest, src, count);
    return 0;
}
#endif

#if defined(_WIN32) || defined(_WIN64)
    #define msdk_strncopy_s strncpy_s

    #define MSDK_MEMCPY_BITSTREAM(bitstream, offset, src, count) \
        memcpy_s((bitstream).Data + (offset), (bitstream).MaxLength - (offset), (src), (count))

    #define MSDK_MEMCPY_BUF(bufptr, offset, maxsize, src, count) \
        memcpy_s((bufptr) + (offset), (maxsize) - (offset), (src), (count))

    #define MSDK_MEMCPY_VAR(dstVarName, src, count) \
        memcpy_s(&(dstVarName), sizeof(dstVarName), (src), (count))

    #define MSDK_MEMCPY(dst, dest_sz, src, src_sz) memcpy_s((dst), (dest_sz), (src), (src_sz))

#else // #if defined(_WIN32) || defined(_WIN64)
    #define msdk_strncopy_s strncpy_s

    #define MSDK_MEMCPY_BITSTREAM(bitstream, offset, src, count) \
        memcpy((bitstream).Data + (offset), (src), (count))

    #define MSDK_MEMCPY_BUF(bufptr, offset, maxsize, src, count) \
        memcpy((bufptr) + (offset), (src), (count))

    #define MSDK_MEMCPY_VAR(dstVarName, src, count) memcpy(&(dstVarName), (src), (count))

    #define MSDK_MEMCPY(dst, dest_sz, src, src_sz) memcpy((dst), (src), (src_sz))

#endif // #if defined(_WIN32) || defined(_WIN64)

#ifdef __cplusplus
inline bool msdk_match(const std::string& left, const std::string& right) {
    return left == std::string(right.begin(), right.end());
}

inline bool msdk_match_ch_i(char a, char b) {
    return std::tolower(a) == std::tolower(b);
}
inline bool msdk_match_i(const std::string& left, const std::string& right) {
    if (left.length() != right.length()) {
        return false;
    }
    return std::equal(left.begin(), left.end(), right.begin(), msdk_match_ch_i);
}

inline bool msdk_starts_with(const std::string& left, const std::string& right) {
    return left.find(std::string(right.begin(), right.end())) == 0;
}
inline bool msdk_contains(const std::string& left, const std::string& right) {
    return left.find(std::string(right.begin(), right.end())) != left.npos;
}
#endif

#endif //__STRING_DEFS_H__