libbtc
bitcoinclibrary
cstr.h
Go to the documentation of this file.
1 /*
2 
3  The MIT License (MIT)
4 
5  Copyright (c) 2015 BitPay, Inc.
6  Copyright (c) 2015 Jonas Schnelli
7 
8  Permission is hereby granted, free of charge, to any person obtaining
9  a copy of this software and associated documentation files (the "Software"),
10  to deal in the Software without restriction, including without limitation
11  the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  and/or sell copies of the Software, and to permit persons to whom the
13  Software is furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included
16  in all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  OTHER DEALINGS IN THE SOFTWARE.
25 
26 */
27 
28 
29 #ifndef __LIBBTC_CSTR_H__
30 #define __LIBBTC_CSTR_H__
31 
32 #include "btc.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include <stdlib.h>
39 #include <sys/types.h>
40 
41 typedef struct cstring
42 {
43  char* str; /* string data, incl. NUL */
44  size_t len; /* length of string, not including NUL */
45  size_t alloc; /* total allocated buffer length */
46 } cstring;
47 
48 LIBBTC_API cstring* cstr_new(const char* init_str);
49 LIBBTC_API cstring* cstr_new_sz(size_t sz);
50 LIBBTC_API cstring* cstr_new_buf(const void* buf, size_t sz);
51 LIBBTC_API void cstr_free(cstring* s, btc_bool free_buf);
52 
53 LIBBTC_API btc_bool cstr_equal(const cstring* a, const cstring* b);
54 LIBBTC_API btc_bool cstr_resize(cstring* s, size_t sz);
55 LIBBTC_API btc_bool cstr_erase(cstring* s, size_t pos, ssize_t len);
56 
57 LIBBTC_API btc_bool cstr_append_buf(cstring* s, const void* buf, size_t sz);
58 
59 LIBBTC_API static inline btc_bool cstr_append_c(cstring* s, char ch)
60 {
61  return cstr_append_buf(s, &ch, 1);
62 }
63 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 #endif //__LIBBTC_CSTR_H__
LIBBTC_API btc_bool cstr_erase(cstring *s, size_t pos, ssize_t len)
Definition: cstr.c:129
uint8_t btc_bool
Definition: btc.h:34
char * str
Definition: cstr.h:43
#define LIBBTC_API
Definition: btc.h:59
LIBBTC_API btc_bool cstr_equal(const cstring *a, const cstring *b)
Definition: cstr.c:118
static LIBBTC_API btc_bool cstr_append_c(cstring *s, char ch)
Definition: cstr.h:59
LIBBTC_API void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
size_t alloc
Definition: cstr.h:45
LIBBTC_API btc_bool cstr_resize(cstring *s, size_t sz)
Definition: cstr.c:81
LIBBTC_API btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
size_t len
Definition: cstr.h:44
struct cstring cstring
LIBBTC_API cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
Definition: cstr.h:41
LIBBTC_API cstring * cstr_new(const char *init_str)
Definition: cstr.c:60
LIBBTC_API cstring * cstr_new_buf(const void *buf, size_t sz)
Definition: cstr.c:47