cstr.h 3.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*********************************************************************
######################################################################
##
##  Created by: Denis Filatov
##
##  Copyleft (c) 2003 - 2015
##  This code is provided under the CeCill-C license agreement.
######################################################################
*********************************************************************/
10
11
#ifndef CSTR_H
#define CSTR_H
12
13
#include <string.h>
#include <stdlib.h>
14
15
16
17
#ifdef __cplusplus
extern "C" {
#endif

18
19
#define cisspace(X) isspace((int)(X))
#define cisdigit(X) isdigit((int)(X))
filatov's avatar
filatov committed
20
#define cisalnum(X) isalnum((int)(X))
21

22
int    cstrlen(const char * str);
23
int    cstrnlen(const char * str, int maxsize);
filatov's avatar
filatov committed
24
/* return pointer to the end of line*/
25
26
char*  cstrend(const char * str);
/* copy src to dst and return pointer to the next byte after the end */
27
28
29
30
31
char * cstrcpy(char * dst, const char * src);

/* copy up to maxsize characters from src to dst and return pointer to the next byte after the end */ 
char * cstrncpy(char * dst, int maxsize, const char * src);

filatov's avatar
filatov committed
32
33
34
/* copy length characters from src to dst or fill dst by zero if src is null and return pointer to the next byte after the end*/
char * cmemcpy(char * dst, const char * src, int length);

35
36
37
/* copy up to maxsize characters from parameters to dst and return pointer to the next byte after the end */ 
char * cvstrncpy(char * dst, int maxsize, const char * ptr, ...);

filatov's avatar
filatov committed
38
/* return nonzero if strings are equals*/
39
int cstrequal(const char * s1, const char * s2);
filatov's avatar
filatov committed
40
char* cstrisprefix(const char * str, const char * prefix);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/* allocate copy of the str */ 
char * cstralloc(int size);
char * cstrdup(const char * str);
char * cstrndup(const char * str, int max_size);

/* allocate new str and collect all paramaters */ 
char * cvstrdup(const char * ptr, ...);

int cstr_write(const char * const p, char ** const ptr, const char * const end, int * const error);
int cstr_read (char * const p, const char ** const ptr, const char * const end, int * const error);

int cstrn_write(const char * const p, int length, char ** const ptr, const char * const end, int * const error);
int cstrn_read (char * const p, int length, const char ** const ptr, const char * const end, int * const error);

56
57
char * cstr_hex2bin(char * bin, int blen, const char * hex, int hlen);
char * cstr_bin2hex(char * hex, int hlen, const char * bin, int blen);
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
86
87
88
89
90
91
92
93
94
95
96
97
98

/* file access functions */
#ifndef PCHAR_T_DEFINED
#if defined(UNICODE) && ( defined(_WIN32) || defined(_WIN64) )
#define PCHAR16BITS
#include <wchar.h>
typedef wchar_t pchar_t;
#define _PCH(X) L##X
#else
typedef char pchar_t;
#define _PCH(X) X
#endif
#endif

/* load file content to the buffer and return pointer to the next byte after the buffer */
char * cstrnload(char * dst, int max_size, const pchar_t * path);
/* load file content to the new allocated buffer, assign it to p and return pointer to the next byte after the buffer */
char * cstraload(char ** p, const pchar_t * path);

/* return the last element of the path */
const pchar_t * cstrlastpathelement(const pchar_t * str);
pchar_t * cstrpathextension(const pchar_t * str);


#ifdef PCHAR16BITS
#define pchar_len(S)      wcslen(S)
#define pchar_ncpy(D,S,N) wcsncpy(D,S,N)
#define pchar_cpy(D,S)    wcscpy(D,S)
#define pchar_rchr(S,C)   wcsrchr(S,C)
#define pchar_fopen(P,M) _wfopen(P, M)
#define pchar_main(A,V)   wmain(A,V)
#else
#define pchar_len(S)      strlen(S)
#define pchar_ncpy(D,S,N) strncpy(D,S,N)
#define pchar_cpy(D,S)    strcpy(D,S)
#define pchar_rchr(S,C)   strrchr(S,C)
#define pchar_fopen(P,M)  fopen(P, M)
#define pchar_main(A,V)   main(A,V)
#endif
#define pchar_alloc(S) ((pchar_t*)malloc(sizeof(pchar_t)*((S)+1)))

99
100
101
102
#ifdef __cplusplus
}
#endif
#endif