Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_str.h
Go to the documentation of this file.
1
8 #ifndef N_STRFUNC
9 #define N_STRFUNC
10
11 #ifdef __cplusplus
12 extern "C"
13 {
14 #endif
15
21 #ifdef LINUX
22 #ifndef _XOPEN_SOURCE
23 #define _XOPEN_SOURCE 500
24 #endif
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <math.h>
32
33 #include "n_common.h"
34 #include "n_log.h"
35 #include "n_list.h"
36
38 #define local_strdup( __src_ ) \
39 ( \
40 { \
41 char *str = NULL ; \
42 if( (__src_) ) \
43 { \
44 int len = strlen( (__src_) ); \
45 Malloc( str , char , len + 5 ); \
46 if( !str ) \
47 { \
48 n_log( LOG_ERR , "Couldn't allocate %d byte for duplicating \"%s\"" , (__src_) ); \
49 } \
50 else \
51 { \
52 for( int it = 0 ; it <= len ; it ++ ) \
53 { \
54 str[ it ] = (__src_)[ it ] ; \
55 } \
56 } \
57 } \
58 str ; \
59 } \
60 )
61
62
64 #define WILDMAT_ABORT -2
65
66 #define WILDMAT_NEGATE_CLASS '^'
67
68 #undef WILDMAT_MATCH_TAR_PATTERN
69
70
71
73 #define n_printf( __n_var , ... ) \
74 { \
75 NSTRBYTE needed = 0 ; \
76 needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
77 Malloc( buffer , char , needed + 1 );\
78 if( buffer )\
79 { \
80 snprintf( buffer , needed + 1 , __VA_ARGS__ ); \
81 __n_var = buffer; \
82 } \
83 else \
84 { \
85 __n_var = NULL ; \
86 }\
87 }
88
89 #ifdef LINUX
90 #include <inttypes.h>
91 #endif
92
93 typedef int32_t NSTRBYTE;
94
96 typedef struct N_STR
97 {
99 char *data;
101 NSTRBYTE length ;
103 NSTRBYTE written ;
104 } N_STR;
105
106 /* create a new string */
107 N_STR *new_nstr( NSTRBYTE size );
108 /* reinitialize a nstr */
109 int empty_nstr( N_STR *nstr );
110 /* Make a copy of a N_STR */
111 N_STR *nstrdup( N_STR *msg );
112 /* Convert a char into a N_STR */
113 int char_to_nstr_ex( char *from , NSTRBYTE nboct , N_STR **to );
114 /* Convert a char into a N_STR, shorter version */
115 N_STR *char_to_nstr( char *src );
116 /* cat data inside a N8STR */
117 int nstrcat_ex( N_STR *dest , void *src , NSTRBYTE size , NSTRBYTE blk_size ,int resize_flag );
118 /* Wrapper to nstrcat_ex to concatenate N_STR *datas */
119 int nstrcat( N_STR *dst , N_STR *src );
120 /* Wrapper to nstrcat_ex to concatenate void *data */
121 int nstrcat_bytes_ex( N_STR *dest , void *src , NSTRBYTE size );
122 /* Wrapper to nstrcat_bytes_ex to cat null termined data streams */
123 int nstrcat_bytes( N_STR *dest , void *src );
124 /* Load a whole file into a N_STR. Be aware of the (4GB ||System Memory) limit */
125 N_STR *file_to_nstr( char *filename );
126 /* Write a whole N_STR into a file */
127 int nstr_to_file( N_STR *n_str , char *filename );
128 /* Free a N_STR */
129 void free_nstr_ptr( void *ptr );
130 /* Free a N_STR structure and set it to NULL */
131 int free_nstr( N_STR **ptr );
132
133 /* String to integer, with error checking */
134 int str_to_int_ex( const char *s , NSTRBYTE start , NSTRBYTE end , int *i, const int base );
135 /* String to integer, shorter version */
136 int str_to_int( const char *s , int *i, const int base);
137
138 /* Skip character from string while string[iterator] == toskip step inc */
139 int skipw( char *string , char toskip , NSTRBYTE *iterator , int inc );
140 /* Skip character from string until string[iterator] == toskip step inc */
141 int skipu( char *string , char toskip , NSTRBYTE *iterator , int inc );
142 /* Upper case a string */
143 int strup( char *string , char *dest );
144 /* Lower case a string */
145 int strlo( char *string , char *dest );
146 /* Copy from string to dest until from[ iterator ] == split */
147 int strcpy_u( char *from , char *to , NSTRBYTE to_size , char split , NSTRBYTE *it );
148 /* Return an array of char pointer to the splitted section */
149 char **split( char* chaine , const char* delim , int vide );
150 /* Count split elements */
151 int split_count( char **split_result );
152 /* Free a char **tab and set it to NULL */
153 int free_split_result( char ***tab );
154 /* Write and fit into the char array */
155 int write_and_fit( char **dest , NSTRBYTE *size , NSTRBYTE *written , char *src );
156 /* get a list of the file in a directory */
157 int scan_dir( const char *dir, LIST *result , const int recurse );
158
159 /* pattern matching */
160 int wildmat( register const char *text, register const char *p );
161 /* pattern matching case insensitive */
162 int wildmatcase( register const char *text, register const char *p );
163
164 /* return a replaced string */
165 char *str_replace ( const char *string, const char *substr, const char *replacement );
166
168 #ifdef __cplusplus
169 }
170 #endif
171 /* #ifndef N_STR*/
172 #endif