Seg3D  2.4
Seg3D is a free volume segmentation and processing tool developed by the NIH Center for Integrative Biomedical Computing at the University of Utah Scientific Computing and Imaging (SCI) Institute.
the_text.hxx
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2016 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27  */
28 
29 // rename text formatter
30 
31 // File : the_text.hxx
32 // Author : Pavel A. Koshevoy
33 // Created : Sun Aug 29 14:53:00 MDT 2004
34 // Copyright : (C) 2004-2008 University of Utah
35 // Description : text convenience class.
36 
37 #ifndef THE_TEXT_HXX_
38 #define THE_TEXT_HXX_
39 
40 // system includes:
41 #include <string.h>
42 #include <stdlib.h>
43 #include <assert.h>
44 #include <stdio.h>
45 #include <sstream>
46 #include <string>
47 #include <iostream>
48 #include <vector>
49 #include <list>
50 
51 #ifdef _WIN32
52 #ifndef snprintf
53 #define snprintf _snprintf_s
54 #endif
55 #endif
56 
57 
58 //----------------------------------------------------------------
59 // the_text_t
60 //
62 {
63 public:
64  the_text_t(const char * text = "");
65  the_text_t(const char * text, const size_t & size);
66  the_text_t(const the_text_t & text);
67  the_text_t(const std::list<char> & text);
68  ~the_text_t();
69 
70  // assignment operator:
71  inline the_text_t & operator = (const the_text_t & text)
72  {
73  if (this != &text)
74  {
75  assign(text.text_, text.size_);
76  }
77 
78  return *this;
79  }
80 
81  // clear the string:
82  inline void clear()
83  {
84  delete [] text_;
85  text_ = NULL;
86  size_ = 0;
87  }
88 
89  // shorthand:
90  inline bool is_empty() const
91  { return size_ == 0; }
92 
93  // assign a new string to this text:
94  inline void assign(const char * text)
95  { assign(text, strlen(text)); }
96 
97  void assign(const char * text, const size_t & text_size);
98 
99  // append a new string to this text:
100  inline void append(const char * text)
101  { append(text, strlen(text)); }
102 
103  void append(const char * text, const size_t & text_size);
104 
105  inline bool operator < (const the_text_t & text) const
106  { return (strcmp(text_, text.text_) < 0); }
107 
108  // arithmetic:
109  inline the_text_t & operator += (const the_text_t & text)
110  {
111  append(text.text_, text.size_);
112  return *this;
113  }
114 
115  inline the_text_t operator + (const the_text_t & text) const
116  {
117  the_text_t text_sum(*this);
118  text_sum += text;
119  return text_sum;
120  }
121 
122  // accessors:
123  inline const char * text() const
124  { return text_; }
125 
126  inline const size_t & size() const
127  { return size_; }
128 
129  // conversion operator:
130  inline operator const char * () const
131  { return text_; }
132 
133 
134  inline static const char* pad(const char * str,
135  const size_t width = 0,
136  const char pad_char = ' ',
137  const bool pad_left = true)
138  {
139  the_text_t txt(str);
140 
141  if (width > txt.size())
142  {
143  the_text_t padding;
144  padding.fill(pad_char, width - txt.size());
145  txt = pad_left ? padding + txt : txt + padding;
146  }
147 
148  return txt;
149  }
150 
151 
152  // helpers:
153  template <class number_t>
154  static const char* number(const number_t & number,
155  const size_t width = 0,
156  const char pad_char = ' ',
157  const bool pad_left = true)
158  {
159  std::ostringstream os;
160  os << number;
161 
162  std::string str = os.str();
163  return pad(str.c_str(), width, pad_char, pad_left);
164  }
165 
166  // needed because C++ stream operators don't convert
167  // unsigned char
168  inline static const char* number(const size_t & number,
169  const size_t width = 0,
170  const char pad_char = ' ',
171  const bool pad_left = true)
172  {
173 //#ifdef _WIN32
174 //#ifndef snprintf
175 //#define snprintf _snprintf_s
176 //#endif
177 //#endif
178 
179  static char buffer[256];
180  snprintf(buffer, sizeof(buffer), "%llu", (long long unsigned int)(number));
181  return pad(buffer, width, pad_char, pad_left);
182  }
183 
184  void fill(const char & c, const size_t size);
185 
186  void fill(const char & c)
187  { fill(c, size_); }
188 
189 private:
190  // the text itself:
191  char * text_;
192 
193  // the length of the text:
194  size_t size_;
195 };
196 
197 #endif // THE_TEXT_HXX_
Definition: the_text.hxx:61