libcex  1.0.0
Modern C++11 embedded webserver library
util.hpp
Go to the documentation of this file.
1 //*************************************************************************
2 // File util.hpp
3 // Date 24.04.2018 - #1
4 // Copyright (c) 2018-2018 by Patrick Fial
5 //-------------------------------------------------------------------------
6 // Utility functions
7 //*************************************************************************
8 
9 #ifndef __UTIL_HPP__
10 #define __UTIL_HPP__
11 
15 //***************************************************************************
16 // includes
17 //***************************************************************************
18 
19 #include <string>
20 #include <vector>
21 
22 struct evbuffer;
23 
24 namespace cex
25 {
26 
27 //***************************************************************************
28 // Definitions
29 //***************************************************************************
30 
31 enum CompressionMode
32 {
33  cmUnknown= -1,
34 
35  cmDeflate,
36  cmGZip
37 };
38 
39 //***************************************************************************
40 // Utility
41 //***************************************************************************
42 
43 std::vector<std::string> splitString(const char* str, char delim = ',', int trim = 1);
44 std::string randomStringHex(int len);
45 
46 #ifdef CEX_WITH_ZLIB
47 int compress(const char* src, size_t srcLen, struct evbuffer* dest, CompressionMode compMode= cmGZip);
48 int compress(std::istream* stream, std::function<void(char*,size_t)> onChunk, CompressionMode compMode);
49 #endif
50 
51 static inline void lTrim(std::string &s)
52 {
53  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
54  {
55  return !std::isspace(ch);
56  }));
57 }
58 
59 static inline void rTrim(std::string &s)
60 {
61  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
62  {
63  return !std::isspace(ch);
64  }).base(), s.end());
65 }
66 
67 static inline void trim(std::string &s)
68 {
69  lTrim(s);
70  rTrim(s);
71 }
72 
73 
74 static inline const char* notNull(const char* s) { return s ? s : ""; };
75 static inline int isEmpty(const char* s) { return !s || !strlen(s); };
76 
77 //***************************************************************************
78 } // namespace cex
79 
80 #endif // __UTIL_HPP_
81