source: trunk/FACT++/src/tools.cc@ 10487

Last change on this file since 10487 was 10429, checked in by tbretz, 14 years ago
Moved the tools function into their own namespace to get rid of problems whenlinking with root.
File size: 1.6 KB
Line 
1// **************************************************************************
2/** @file tools.cc
3
4@todo
5 - Resolve the dependancies with dim
6 - Move code to a more appropriate place
7 - put stuff in namespaces
8*/
9// **************************************************************************
10#include "tools.h"
11
12#include <stdarg.h>
13
14using namespace std;
15
16string Tools::Format(const char *fmt, va_list &ap)
17{
18 int n=256;
19
20 char *ret=0;
21 while (1)
22 {
23 ret = new char[n+1];
24
25 const int sz = vsnprintf(ret, n, fmt, ap);
26 if (sz<=n)
27 break;
28
29 n *= 2;
30 delete [] ret;
31 };
32
33 string str(ret);
34
35 delete [] ret;
36
37 return str;
38}
39
40string Tools::Form(const char *fmt, ...)
41{
42 va_list ap;
43 va_start(ap, fmt);
44
45 string str = Format(fmt, ap);
46
47 va_end(ap);
48
49 return str;
50}
51
52// --------------------------------------------------------------------------
53//
54//! This is a static helper to remove leading and trailing whitespaces.
55//!
56//! @param buf
57//! a pointer to the char array from which the whitespaces should be
58//! removed
59//!
60//! @returns
61//! a std::string with the whitespaces removed from buf
62//
63string Tools::Trim(const string &str)
64{
65 // Trim Both leading and trailing spaces
66 const size_t start = str.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
67 const size_t end = str.find_last_not_of(' '); // Find the first character position from reverse af
68
69 // if all spaces or empty return an empty string
70 if (string::npos==start || string::npos==end)
71 return string();
72
73 return str.substr(start, end-start+1);
74}
Note: See TracBrowser for help on using the repository browser.