NCBI C++ ToolKit
ftacpp.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ftacpp.hpp 102100 2024-03-31 13:04:58Z stakhovv $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * File Name: ftacpp.hpp
27  *
28  * Author: Serge Bazhin
29  *
30  * File Description:
31  * Contains common C++ Toolkit header files
32  *
33  */
34 
35 #ifndef FTACPP_HPP
36 #define FTACPP_HPP
37 
38 #include <cstring>
39 #include <corelib/ncbistr.hpp>
40 
42 
43 inline char* StringNew(size_t sz)
44 {
45  char* p = new char[sz + 1];
46  std::memset(p, 0, sz + 1);
47  return p;
48 }
49 inline void MemSet(void* p, int n, size_t sz) { std::memset(p, n, sz); }
50 inline void MemCpy(void* p, const void* q, size_t sz)
51 {
52  if (q)
53  std::memcpy(p, q, sz);
54 }
55 inline void MemFree(char* p)
56 {
57  delete[] p;
58 }
59 
60 inline size_t StringLen(const char* s) { return s ? std::strlen(s) : 0; }
61 inline char* StringSave(const char* s)
62 {
63  if (! s)
64  return nullptr;
65  const size_t n = std::strlen(s) + 1;
66  char* p = new char[n];
67  std::memcpy(p, s, n);
68  return p;
69 }
70 inline char* StringSave(string_view s)
71 {
72  const size_t n = s.length();
73  char* p = new char[n + 1];
74  std::memcpy(p, s.data(), n);
75  p[n] = '\0';
76  return p;
77 }
78 
79 
80 inline const char* StringStr(const char* s1, const char* s2) { return std::strstr(s1, s2); }
81 inline char* StringStr(char* s1, const char* s2) { return std::strstr(s1, s2); }
82 inline void StringCat(char* d, const char* s) { std::strcat(d, s); }
83 inline void StringCpy(char* d, const char* s) { std::strcpy(d, s); }
84 inline void StringNCpy(char* d, const char* s, size_t n) { std::strncpy(d, s, n); }
85 inline const char* StringChr(const char* s, const char c) { return std::strchr(s, c); }
86 inline char* StringChr(char* s, const char c) { return std::strchr(s, c); }
87 inline char* StringRChr(char* s, const char c) { return std::strrchr(s, c); }
88 
89 inline int StringCmp(const char* s1, const char* s2)
90 {
91  if (s1) {
92  if (s2) {
93  return std::strcmp(s1, s2);
94  } else {
95  return 1;
96  }
97  } else {
98  if (s2) {
99  return -1;
100  } else {
101  return 0;
102  }
103  }
104 }
105 inline bool StringEqu(const char* s1, const char* s2)
106 {
107  if (s1 && s2)
108  return (std::strcmp(s1, s2) == 0);
109 
110  if (! s1 && ! s2)
111  return true;
112 
113  return false;
114 }
115 inline bool StringEquN(const char* s1, const char* s2, size_t n)
116 {
117  if (s1 && s2)
118  return (std::strncmp(s1, s2, n) == 0);
119 
120  if (! s1 && ! s2)
121  return true;
122 
123  return false;
124 }
125 inline bool StringEquNI(const char* s1, const char* s2, size_t n)
126 {
127  const string S1(s1), S2(s2);
128  return (NStr::CompareNocase(S1.substr(0, n), S2.substr(0, n)) == 0);
129 }
130 
131 inline bool StringHasNoText(const char* s)
132 {
133  if (s)
134  while (*s)
135  if ((unsigned char)(*s++) > ' ')
136  return false;
137  return true;
138 }
139 
140 inline bool StringDoesHaveText(const char* s) { return ! StringHasNoText(s); }
141 
143 
144 #endif // FTACPP_HPP
The NCBI C++ standard methods for dealing with std::string.
int StringCmp(const char *s1, const char *s2)
Definition: ftacpp.hpp:89
bool StringDoesHaveText(const char *s)
Definition: ftacpp.hpp:140
void MemSet(void *p, int n, size_t sz)
Definition: ftacpp.hpp:49
char * StringSave(const char *s)
Definition: ftacpp.hpp:61
bool StringEquNI(const char *s1, const char *s2, size_t n)
Definition: ftacpp.hpp:125
bool StringEquN(const char *s1, const char *s2, size_t n)
Definition: ftacpp.hpp:115
bool StringEqu(const char *s1, const char *s2)
Definition: ftacpp.hpp:105
void StringCpy(char *d, const char *s)
Definition: ftacpp.hpp:83
void StringNCpy(char *d, const char *s, size_t n)
Definition: ftacpp.hpp:84
void MemFree(char *p)
Definition: ftacpp.hpp:55
size_t StringLen(const char *s)
Definition: ftacpp.hpp:60
bool StringHasNoText(const char *s)
Definition: ftacpp.hpp:131
void StringCat(char *d, const char *s)
Definition: ftacpp.hpp:82
void MemCpy(void *p, const void *q, size_t sz)
Definition: ftacpp.hpp:50
char * StringRChr(char *s, const char c)
Definition: ftacpp.hpp:87
const char * StringChr(const char *s, const char c)
Definition: ftacpp.hpp:85
char * StringNew(size_t sz)
Definition: ftacpp.hpp:43
const char * StringStr(const char *s1, const char *s2)
Definition: ftacpp.hpp:80
#define strcat(s, k)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
yy_size_t n
int strncmp(const char *str1, const char *str2, size_t count)
Definition: odbc_utils.hpp:133
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define S2(x)
Definition: sha256.c:129
#define S1(x)
Definition: sha256.c:127
Modified on Wed Apr 17 13:10:29 2024 by modify_doxy.py rev. 669887