mbgtools-lx  4.2.8
str_util.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: str_util.h 1.6 2018/08/23 13:10:26 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Definitions and prototypes for str_util.c
10  *
11  * -----------------------------------------------------------------------
12  * $Log: str_util.h $
13  * Revision 1.6 2018/08/23 13:10:26 martin
14  * New inline functions mbg_buffer_specs_valid() and
15  * mbg_chk_snprint_results() that can also be called
16  * from code used in kernel mode.
17  * Updated function prototypes.
18  * Revision 1.5 2018/06/25 13:24:15 martin
19  * Updated function prototypes.
20  * Revision 1.4 2017/05/10 15:26:10 martin
21  * Tiny cleanup.
22  * Revision 1.3 2016/12/14 16:22:24 martin
23  * Added macro _sn_cpy_str_safe() to simplify calls.
24  * Revision 1.2 2016/08/05 12:33:17 martin
25  * Moved string trim functions from cfg_util module here.
26  * Added variable str_not_avail.
27  * Fixed some spelling.
28  * Updated function prototypes.
29  * Revision 1.1 2015/08/25 15:57:43 martin
30  * Initial revision.
31  *
32  **************************************************************************/
33 
34 #ifndef _STR_UTIL_H
35 #define _STR_UTIL_H
36 
37 /* Other headers to be included */
38 
39 #include <words.h> // implicitly includes mbg_tgt.h for non-firmware projects
40 
41 #if defined( MBG_TGT_KERNEL )
42  #include <mbgddmsg.h>
43 #else
44  #include <stdlib.h>
45  #include <stdarg.h>
46 #endif
47 
48 
49 #ifdef _STR_UTIL
50  #define _ext
51  #define _DO_INIT
52 #else
53  #define _ext extern
54 #endif
55 
56 
57 /* Start of header body */
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 _ext const char *str_not_avail
65 #ifdef _DO_INIT
66  = "N/A"
67 #endif
68 ;
69 
70 
71 
72 static __mbg_inline /*HDR*/
88 bool mbg_buffer_specs_valid( char *s, size_t max_len )
89 {
90  return s != NULL && max_len > 0;
91 
92 } // mbg_buffer_specs_valid
93 
94 
95 
96 static __mbg_inline /*HDR*/
123 int mbg_chk_snprint_results( size_t n, char *s, size_t max_len )
124 {
125  if ( !mbg_buffer_specs_valid( s, max_len ) )
126  return 0; // Buffer parameters are not valid.
127 
128 
129  // Force proper worst-case termination of the output string.
130  s[max_len - 1] = 0;
131 
132  // If n is > 0, but less than the specified buffer size we
133  // assume the value is correct.
134  if ( n > 0 && n < max_len )
135  goto out;
136 
137  // Determine the real string length, but don't just call strlen()
138  // since that function may not be available in kernel mode.
139  for ( n = 0; s[n]; n++ );
140 
141 out:
142  // Most snprintf()-like functions take a "size_t" to specify the buffer size,
143  // but just return an "int", which may be a smaller data type than "size_t",
144  // so we do a conversion here, if required, and try to do the conversion
145  // in a safe way.
146  return _int_from_size_t( n );
147 
148 } // mbg_chk_snprint_results
149 
150 
151 
152 #define _sn_cpy_str_safe( _dst, _src ) sn_cpy_str_safe( _dst, sizeof( _dst ), _src )
153 
154 
155 /* ----- function prototypes begin ----- */
156 
157 /* This section was generated automatically */
158 /* by MAKEHDR, do not remove the comments. */
159 
210  __attribute__( ( format( printf, 3, 0 ) ) ) int vsnprintf_safe( char *s, size_t max_len, const char *fmt, va_list args ) ;
211 
229  __attribute__( ( format( printf, 3, 4 ) ) ) int snprintf_safe( char *s, size_t max_len, const char * fmt, ... ) ;
230 
253  char *strncpy_safe( char *dst, const char *src, size_t max_len ) ;
254 
273  int sn_cpy_str_safe( char *dst, size_t max_len, const char *src ) ;
274 
294  int sn_cpy_char_safe( char *dst, size_t max_len, char c ) ;
295 
301  void trim_trailing_whitespace( char *s ) ;
302 
308  void trim_leading_whitespace( char *s ) ;
309 
315  void trim_whitespace( char *s ) ;
316 
332  void mbg_memcpy( void *dst, const void *src, size_t n_bytes ) ;
333 
347  void mbg_memcpy_reversed( void *dst, const void *src, size_t n_bytes ) ;
348 
349 
350 /* ----- function prototypes end ----- */
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 
356 
357 /* End of header body */
358 
359 #undef _ext
360 #undef _DO_INIT
361 
362 #endif /* _STR_UTIL_H */
#define _ext
Definition: str_util.h:53
void trim_leading_whitespace(char *s)
Trim whitespace at the beginning of a string.
Definition: str_util.c:353
int vsnprintf_safe(char *s, size_t max_len, const char *fmt, va_list args)
A portable, safe implementation of vsnprintf()
Definition: str_util.c:121
int sn_cpy_char_safe(char *dst, size_t max_len, char c)
A function to copy a character safely to a string buffer.
Definition: str_util.c:306
#define _int_from_size_t(_n)
Definition: words.h:662
void mbg_memcpy_reversed(void *dst, const void *src, size_t n_bytes)
Copy an array of bytes in reversed order, starting at end of buffer.
Definition: str_util.c:438
void trim_trailing_whitespace(char *s)
Trim whitespace at the end of a string.
Definition: str_util.c:328
void mbg_memcpy(void *dst, const void *src, size_t n_bytes)
Copy array of bytes starting at beginning of buffer.
Definition: str_util.c:412
int sn_cpy_str_safe(char *dst, size_t max_len, const char *src)
A function to copy a string safely, returning the number of characters copied.
Definition: str_util.c:276
char * strncpy_safe(char *dst, const char *src, size_t max_len)
A portable, safe implementation of strncpy()
Definition: str_util.c:247
const char * str_not_avail
int snprintf_safe(char *s, size_t max_len, const char *fmt,...)
A portable, safe implementation of snprintf()
Definition: str_util.c:156
static __mbg_inline bool mbg_buffer_specs_valid(char *s, size_t max_len)
Check if the buffer plus size parameters passed to a function are valid.
Definition: str_util.h:88
static __mbg_inline int mbg_chk_snprint_results(size_t n, char *s, size_t max_len)
Check the results of an snprintf()-like function.
Definition: str_util.h:123
void trim_whitespace(char *s)
Trim both leading and trailing whitespace from a string.
Definition: str_util.c:387