StringUtil.cpp
Go to the documentation of this file.
1 /*
2  * StringUtil.cpp
3  *
4  * Copyright 2002, Log4cpp Project. All rights reserved.
5  *
6  * See the COPYING file for the terms of usage and distribution.
7  */
8 #include "StringUtil.hh"
9 #include <iterator>
10 #include <stdio.h>
11 
12 #if defined(_MSC_VER)
13  #define VSNPRINTF _vsnprintf
14 #else
15 #ifdef LOG4CPP_HAVE_SNPRINTF
16  #define VSNPRINTF vsnprintf
17 #else
18 /* use alternative snprintf() from http://www.ijs.si/software/snprintf/ */
19 
20 #define HAVE_SNPRINTF
21 #define PREFER_PORTABLE_SNPRINTF
22 
23 #include <stdlib.h>
24 #include <stdarg.h>
25 
26 extern "C" {
27 #include "snprintf.c"
28 }
29 
30 #define VSNPRINTF portable_vsnprintf
31 
32 #endif // LOG4CPP_HAVE_SNPRINTF
33 #endif // _MSC_VER
34 
35 namespace log4cpp {
36 
37  std::string StringUtil::vform(const char* format, va_list args) {
38  size_t size = 1024;
39  char* buffer = new char[size];
40 
41  while (1) {
42  va_list args_copy;
43 
44 #if defined(_MSC_VER) || defined(__BORLANDC__)
45  args_copy = args;
46 #else
47  va_copy(args_copy, args);
48 #endif
49 
50  int n = VSNPRINTF(buffer, size, format, args_copy);
51 
52  va_end(args_copy);
53 
54  // If that worked, return a string.
55  if ((n > -1) && (static_cast<size_t>(n) < size)) {
56  std::string s(buffer);
57  delete [] buffer;
58  return s;
59  }
60 
61  // Else try again with more space.
62  size = (n > -1) ?
63  n + 1 : // ISO/IEC 9899:1999
64  size * 2; // twice the old size
65 
66  delete [] buffer;
67  buffer = new char[size];
68  }
69  }
70 
71  std::string StringUtil::trim(const std::string& s) {
72  static const char* whiteSpace = " \t\r\n";
73 
74  // test for null string
75  if(s.empty())
76  return s;
77 
78  // find first non-space character
79  std::string::size_type b = s.find_first_not_of(whiteSpace);
80  if(b == std::string::npos) // No non-spaces
81  return "";
82 
83  // find last non-space character
84  std::string::size_type e = s.find_last_not_of(whiteSpace);
85 
86  // return the remaining characters
87  return std::string(s, b, e - b + 1);
88  }
89 
90  unsigned int StringUtil::split(std::vector<std::string>& v,
91  const std::string& s,
92  char delimiter, unsigned int maxSegments) {
93  v.clear();
94  std::back_insert_iterator<std::vector<std::string> > it(v);
95  return split(it, s, delimiter, maxSegments);
96  }
97 
98 }
static std::string vform(const char *format, va_list args)
Definition: StringUtil.cpp:37
static std::string trim(const std::string &s)
Definition: StringUtil.cpp:71
#define VSNPRINTF
Definition: StringUtil.cpp:30
static unsigned int split(std::vector< std::string > &v, const std::string &s, char delimiter, unsigned int maxSegments=INT_MAX)
Definition: StringUtil.cpp:90


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00