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

Go to the SVN repository for this file.

1 /* $Id: ftacpp.hpp 102351 2024-04-25 17:49:42Z 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 inline char* StringSave(unique_ptr<string> s)
79 {
80  if (! s)
81  return nullptr;
82  return StringSave(*s);
83 }
84 
85 
86 inline const char* StringStr(const char* s1, const char* s2) { return std::strstr(s1, s2); }
87 inline char* StringStr(char* s1, const char* s2) { return std::strstr(s1, s2); }
88 inline void StringCat(char* d, const char* s) { std::strcat(d, s); }
89 inline void StringCpy(char* d, const char* s) { std::strcpy(d, s); }
90 inline void StringNCpy(char* d, const char* s, size_t n) { std::strncpy(d, s, n); }
91 inline const char* StringChr(const char* s, const char c) { return std::strchr(s, c); }
92 inline char* StringChr(char* s, const char c) { return std::strchr(s, c); }
93 inline char* StringRChr(char* s, const char c) { return std::strrchr(s, c); }
94 
95 inline int StringCmp(const char* s1, const char* s2)
96 {
97  if (s1) {
98  if (s2) {
99  return std::strcmp(s1, s2);
100  } else {
101  return 1;
102  }
103  } else {
104  if (s2) {
105  return -1;
106  } else {
107  return 0;
108  }
109  }
110 }
111 inline bool StringEqu(const char* s1, const char* s2)
112 {
113  if (s1 && s2)
114  return (std::strcmp(s1, s2) == 0);
115 
116  if (! s1 && ! s2)
117  return true;
118 
119  return false;
120 }
121 inline bool StringEquN(const char* s1, const char* s2, size_t n)
122 {
123  if (s1 && s2)
124  return (std::strncmp(s1, s2, n) == 0);
125 
126  if (! s1 && ! s2)
127  return true;
128 
129  return false;
130 }
131 inline bool StringEquNI(const char* s1, const char* s2, size_t n)
132 {
133  const string S1(s1), S2(s2);
134  return (NStr::CompareNocase(S1.substr(0, n), S2.substr(0, n)) == 0);
135 }
136 
137 inline bool StringHasNoText(const char* s)
138 {
139  if (s)
140  while (*s)
141  if ((unsigned char)(*s++) > ' ')
142  return false;
143  return true;
144 }
145 
146 inline bool StringDoesHaveText(const char* s) { return ! StringHasNoText(s); }
147 
149 
150 #endif // FTACPP_HPP
The NCBI C++ standard methods for dealing with std::string.
int StringCmp(const char *s1, const char *s2)
Definition: ftacpp.hpp:95
bool StringDoesHaveText(const char *s)
Definition: ftacpp.hpp:146
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:131
bool StringEquN(const char *s1, const char *s2, size_t n)
Definition: ftacpp.hpp:121
bool StringEqu(const char *s1, const char *s2)
Definition: ftacpp.hpp:111
void StringCpy(char *d, const char *s)
Definition: ftacpp.hpp:89
void StringNCpy(char *d, const char *s, size_t n)
Definition: ftacpp.hpp:90
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:137
void StringCat(char *d, const char *s)
Definition: ftacpp.hpp:88
void MemCpy(void *p, const void *q, size_t sz)
Definition: ftacpp.hpp:50
char * StringRChr(char *s, const char c)
Definition: ftacpp.hpp:93
const char * StringChr(const char *s, const char c)
Definition: ftacpp.hpp:91
char * StringNew(size_t sz)
Definition: ftacpp.hpp:43
const char * StringStr(const char *s1, const char *s2)
Definition: ftacpp.hpp:86
#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
Modified on Fri Sep 20 14:57:19 2024 by modify_doxy.py rev. 669887