SoDaFormat-1.0.0-main:f160581  1.0.0
Format.hxx
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <exception>
4 #include <iostream>
5 #include <list>
6 
7 /*
8 BSD 2-Clause License
9 
10 Copyright (c) 2020, Matt Reilly - kb1vc
11 All rights reserved.
12 
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 
16 1. Redistributions of source code must retain the above copyright notice, this
17  list of conditions and the following disclaimer.
18 
19 2. Redistributions in binary form must reproduce the above copyright notice,
20  this list of conditions and the following disclaimer in the documentation
21  and/or other materials provided with the distribution.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 
194 namespace SoDa {
195 
269  class Format {
270  public:
276  Format(const std::string & fmt_string);
277 
291  Format & addI(int v, unsigned int width = 0);
292 
306  Format & addU(unsigned int v, unsigned int width = 0);
307 
308 
379  Format & addF(double v, char fmt = 'f', unsigned int width = 0, unsigned int frac_precision = 3);
380 
399  Format & addS(const std::string & v, unsigned int width = 0);
400 
410  Format & addC(char v);
411 
420  Format & reset();
421 
429  class BadFormat : public std::runtime_error {
430  public:
438  BadFormat(const std::string & problem, const Format & fmt) :
439  std::runtime_error(problem + " format string was \n" + fmt.getOrig()) { }
440  };
441 
453  const std::string & str(bool check_for_filled_out = false) const;
454 
464  static char separator;
465 
466  protected:
467 
468  const std::string & getOrig() const { return orig_fmt_string; }
469 
470  std::string fmt_string;
471  std::string orig_fmt_string;
472  unsigned int cur_arg_number;
473  int max_field_num;
474 
475  std::list<size_t> escape_positions;
476 
477  void initialScan();
478 
479  void insertField(const std::string & s);
480  };
481 }
482 
483 std::ostream& operator<<(std::ostream & os, const SoDa::Format & f);
484 
Exception to announce that there was something wrong with the format string, or that too many values ...
Definition: Format.hxx:429
Format & reset()
reset the format string to its original value, with all the placeholders restored.
Definition: Format.cxx:207
Format & addI(int v, unsigned int width=0)
insert a signed integer into the format string
Definition: Format.cxx:50
Format(const std::string &fmt_string)
create a format object with placeholders and all that stuff.
Definition: Format.cxx:41
Format & addU(unsigned int v, unsigned int width=0)
insert an unsigned integer into the format string
Definition: Format.cxx:60
BadFormat(const std::string &problem, const Format &fmt)
build a BadFormat exception object.
Definition: Format.hxx:438
Format & addF(double v, char fmt='f', unsigned int width=0, unsigned int frac_precision=3)
insert a float or double into the format string
Definition: Format.cxx:70
Not much else is spelled that way, so we're probably not going to have too many collisions with code ...
Definition: Format.cxx:38
Format & addC(char v)
insert a character into the format string
Definition: Format.cxx:142
std::ostream & operator<<(std::ostream &os, const SoDa::Format &f)
Definition: Format.cxx:218
Format & addS(const std::string &v, unsigned int width=0)
insert a string into the format string
Definition: Format.cxx:132
const std::string & str(bool check_for_filled_out=false) const
provide a string representing the current state of the format string with all placeholders "filled in...
Definition: Format.cxx:213
A format object that may be "filled in" with integer, float, double, string, or character values.
Definition: Format.hxx:269
static char separator
the radix separator character.
Definition: Format.hxx:464