Critical Link MityCam SoC Firmware  1.0
Critical Link MityCam SoC Firmware
str_funcs.h
Go to the documentation of this file.
1 // Name.......: str_funcs.h
3 // Purpose....: Commonly used string functions missing from standard C++ string library
4 // By/Date....: Gary James, 2008
5 // Copyright 2008 Critical Link LLC, All Rights Reserved
7 // CORE FILE
8 
9 #ifndef STR_FUNCS_H_
10 #define STR_FUNCS_H_
11 
12 #include <stdint.h>
13 #include <string>
14 #include <list>
15 #include <vector>
16 #include <stdarg.h>
17 #ifndef PVAR
18 #define PVAR(v) " "#v" = " << (v)
19 #endif
20 #ifndef PVARX
21 #define PVARX(v) " "#v" = 0x" << std::hex << (v) << std::dec
22 #endif
23 #ifndef PLOC
24 #define PLOC __FILE__ << ":" << __LINE__ << " "
25 #endif
26 
27 extern void str_replace (std::string &s1, const std::string &s2, const std::string &s3);
28 extern void str_remove (std::string &s1, const std::string &s2);
29 extern void str_length_trim (std::string &s1, int maxlen);
30 extern void str_pad (std::string &s1, int len, char cc);
31 extern uint32_t str_to_uint32 (const std::string &s1, int base = 10);
32 extern void str_toupper (std::string &s1);
33 extern void str_tolower (std::string &s1);
34 extern void str_vsprintf (std::string &s1, const char *fmt, va_list args);
35 extern void str_sprintf (std::string &s1, const char *fmt, ...);
36 extern std::string str_sprintf (const char *fmt, ...);
37 extern void str_split (std::vector<std::string>& list, const std::string &s1, const std::string &s2, bool keep_empty = true);
38 extern void str_strcpy (char* destination, const std::string& source, size_t dest_buf_size);
39 
40 
41 //Templated wrapper of the above that lets us just pass in a fixed-length
42 //character buffer, and template magic will infer the buffer size. If a non-fixed
43 //length string is used as the destination, this function won't compile.
44 template <size_t dest_buf_size>
45 void str_strcpy (char (&destination)[dest_buf_size], const std::string& source)
46 {
47  str_strcpy(destination, source, dest_buf_size);
48 }
49 
50 //------------------------------------------------------------------------------
51 // In these IP address functions the MS byte of the uint32 values is first
52 // octet of the address (i.e. 192.168.0.1 = 0xC0A80001).
53 //------------------------------------------------------------------------------
54 extern void str_from_ip (std::string &s1, uint32_t ip);
55 extern std::string format_ipaddr (uint32_t ip);
56 extern uint32_t ip_from_str (const char *s1);
57 extern uint32_t ip_from_str (const std::string &s1);
58 
59 extern void parse_macaddr (const std::string &s1, uint8_t* macaddr);
60 extern std::string format_macaddr (const uint8_t *macaddr);
61 extern std::string xml_escape (const char *str);
62 extern std::string xml_escape (std::string str);
63 extern void split_ip (uint32_t ip, uint8_t *octets);
64 extern void form_ip (uint32_t &ip, uint8_t *octets);
65 
66 extern std::string fileToString(const char *filename);
67 extern std::string getcwd_string();
68 
69 void hexdump(void *ptr, int buflen);
70 
71 #endif //STR_FUNCS_H_
72 
ip_from_str
uint32_t ip_from_str(const char *s1)
Definition: str_funcs.cpp:281
str_length_trim
void str_length_trim(std::string &s1, int maxlen)
Definition: str_funcs.cpp:78
fileToString
std::string fileToString(const char *filename)
Definition: str_funcs.cpp:387
str_replace
void str_replace(std::string &s1, const std::string &s2, const std::string &s3)
Definition: str_funcs.cpp:39
form_ip
void form_ip(uint32_t &ip, uint8_t *octets)
Converts four octets into a uint32 IP address.
Definition: str_funcs.cpp:376
str_to_uint32
uint32_t str_to_uint32(const std::string &s1, int base=10)
Definition: str_funcs.cpp:98
xml_escape
std::string xml_escape(const char *str)
Definition: str_funcs.cpp:313
parse_macaddr
void parse_macaddr(const std::string &s1, uint8_t *macaddr)
Definition: str_funcs.cpp:291
str_sprintf
void str_sprintf(std::string &s1, const char *fmt,...)
Definition: str_funcs.cpp:174
getcwd_string
std::string getcwd_string()
Get the cwd as a string object.
Definition: str_funcs.cpp:409
str_tolower
void str_tolower(std::string &s1)
Definition: str_funcs.cpp:116
str_vsprintf
void str_vsprintf(std::string &s1, const char *fmt, va_list args)
Definition: str_funcs.cpp:129
str_toupper
void str_toupper(std::string &s1)
Definition: str_funcs.cpp:103
str_from_ip
void str_from_ip(std::string &s1, uint32_t ip)
Definition: str_funcs.cpp:268
split_ip
void split_ip(uint32_t ip, uint8_t *octets)
Converts a uint32 IP address into four octets.
Definition: str_funcs.cpp:365
format_macaddr
std::string format_macaddr(const uint8_t *macaddr)
Definition: str_funcs.cpp:304
str_remove
void str_remove(std::string &s1, const std::string &s2)
Definition: str_funcs.cpp:73
format_ipaddr
std::string format_ipaddr(uint32_t ip)
Definition: str_funcs.cpp:274
str_split
void str_split(std::vector< std::string > &list, const std::string &s1, const std::string &s2, bool keep_empty=true)
Definition: str_funcs.cpp:213
str_pad
void str_pad(std::string &s1, int len, char cc)
Definition: str_funcs.cpp:90
str_strcpy
void str_strcpy(char *destination, const std::string &source, size_t dest_buf_size)
Definition: str_funcs.cpp:255
hexdump
void hexdump(void *ptr, int buflen)
simple hex dump routine
Definition: str_funcs.cpp:422