StringUtil.cpp
Go to the documentation of this file.
00001 /*
00002  * StringUtil.cpp
00003  *
00004  * Copyright 2002, Log4cpp Project. All rights reserved.
00005  *
00006  * See the COPYING file for the terms of usage and distribution.
00007  */
00008 #include "StringUtil.hh"
00009 #include <iterator>
00010 #include <stdio.h>
00011 
00012 #if defined(_MSC_VER)
00013     #define VSNPRINTF _vsnprintf
00014 #else
00015 #ifdef LOG4CPP_HAVE_SNPRINTF
00016     #define VSNPRINTF vsnprintf
00017 #else
00018 /* use alternative snprintf() from http://www.ijs.si/software/snprintf/ */
00019 
00020 #define HAVE_SNPRINTF
00021 #define PREFER_PORTABLE_SNPRINTF
00022 
00023 #include <stdlib.h>
00024 #include <stdarg.h>
00025 
00026 extern "C" {
00027 #include "snprintf.c"
00028 }
00029 
00030 #define VSNPRINTF portable_vsnprintf
00031 
00032 #endif // LOG4CPP_HAVE_SNPRINTF
00033 #endif // _MSC_VER
00034 
00035 namespace log4cpp {
00036 
00037     std::string StringUtil::vform(const char* format, va_list args) {
00038         size_t size = 1024;
00039         char* buffer = new char[size];
00040             
00041         while (1) {
00042             int n = VSNPRINTF(buffer, size, format, args);
00043                 
00044             // If that worked, return a string.
00045             if ((n > -1) && (static_cast<size_t>(n) < size)) {
00046                 std::string s(buffer);
00047                 delete [] buffer;
00048                 return s;
00049             }
00050                 
00051             // Else try again with more space.
00052             size = (n > -1) ?
00053                 n + 1 :   // ISO/IEC 9899:1999
00054                 size * 2; // twice the old size
00055                 
00056             delete [] buffer;
00057             buffer = new char[size];
00058         }
00059     }
00060 
00061     std::string StringUtil::trim(const std::string& s) {
00062         static const char* whiteSpace = " \t\r\n";
00063 
00064         // test for null string
00065         if(s.empty())
00066             return s;
00067 
00068         // find first non-space character
00069         std::string::size_type b = s.find_first_not_of(whiteSpace);
00070         if(b == std::string::npos) // No non-spaces
00071             return "";
00072 
00073         // find last non-space character
00074         std::string::size_type e = s.find_last_not_of(whiteSpace);
00075 
00076         // return the remaining characters
00077         return std::string(s, b, e - b + 1);
00078     }
00079 
00080     unsigned int StringUtil::split(std::vector<std::string>& v,
00081                                    const std::string& s,
00082                                    char delimiter, unsigned int maxSegments) {
00083         v.clear();
00084         std::back_insert_iterator<std::vector<std::string> > it(v);
00085         return split(it, s, delimiter, maxSegments);
00086     }
00087 
00088 }


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Wed Sep 16 2015 10:27:14