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

Last change on this file since 13552 was 13221, checked in by tbretz, 13 years ago
Added TrimQuotes
File size: 3.4 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 and
79//! if available leading and trailing quotes, can be either ' or "
80//!
81//! @param buf
82//! a pointer to the char array to be trimmed
83//!
84//! @returns
85//! a std::string with the content trimmed
86//
87string Tools::TrimQuotes(const string &str)
88{
89 string rc = Trim(str);
90 if (rc.length()<2)
91 return rc;
92
93 const char b = rc[0];
94 const char e = rc[rc.length()-1];
95
96 if ((b=='\"' && e=='\"') || (b=='\'' && e=='\''))
97 return rc.substr(1, rc.length()-2);
98
99 return rc;
100}
101
102// --------------------------------------------------------------------------
103//
104//! This is a static helper to remove leading and trailing whitespaces.
105//!
106//! Usage example:
107//!
108//! \code
109//! string str = " Dies ist ein test fuer einen ganz langen Satz "
110//! "und ob er korrekt umgebrochen und formatiert wird. Alles "
111//! "nur ein simpler test aber trotzdem ganz wichtig.";
112//!
113//! cout << setfill('-') << setw(40) << "+" << endl;
114//! while (1)
115//! {
116//! const string rc = Tools::Wrap(str, 40);
117//! if (rc.empty())
118//! break;
119//! cout << rc << endl;
120//! }
121//! \endcode
122//!
123string Tools::Wrap(string &str, size_t width)
124{
125 const size_t pos = str.length()<width ? string::npos : str.find_last_of(' ', width);
126 if (pos==string::npos)
127 {
128 const string rc = str;
129 str = "";
130 return rc;
131 }
132
133 const size_t indent = str.find_first_not_of(' ');
134
135 const string rc = str.substr(0, pos);
136 const size_t p2 = str.find_first_not_of(' ', pos+1);
137
138 str = str.substr(0, indent) + str.substr(p2==string::npos ? pos+1 : p2);
139
140 return rc;
141}
Note: See TracBrowser for help on using the repository browser.