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

Last change on this file since 12160 was 12160, checked in by tbretz, 13 years ago
Added PixelMap.cc to fact_SOURCES
File size: 2.8 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}
75
76// --------------------------------------------------------------------------
77//
78//! This is a static helper to remove leading and trailing whitespaces.
79//!
80//! Usage example:
81//!
82//! \code
83//! string str = " Dies ist ein test fuer einen ganz langen Satz "
84//! "und ob er korrekt umgebrochen und formatiert wird. Alles "
85//! "nur ein simpler test aber trotzdem ganz wichtig.";
86//!
87//! cout << setfill('-') << setw(40) << "+" << endl;
88//! while (1)
89//! {
90//! const string rc = Tools::Wrap(str, 40);
91//! if (rc.empty())
92//! break;
93//! cout << rc << endl;
94//! }
95//! \endcode
96//!
97string Tools::Wrap(string &str, size_t width)
98{
99 const size_t pos = str.length()<width ? string::npos : str.find_last_of(' ', width);
100 if (pos==string::npos)
101 {
102 const string rc = str;
103 str = "";
104 return rc;
105 }
106
107 const size_t indent = str.find_first_not_of(' ');
108
109 const string rc = str.substr(0, pos);
110 const size_t p2 = str.find_first_not_of(' ', pos+1);
111
112 str = str.substr(0, indent) + str.substr(p2==string::npos ? pos+1 : p2);
113
114 return rc;
115}
Note: See TracBrowser for help on using the repository browser.