source: trunk/FACT++/src/tools.h@ 19136

Last change on this file since 19136 was 19119, checked in by tbretz, 6 years ago
Added Fractional as a complement to scientific.
File size: 1.6 KB
Line 
1#ifndef FACT_Tools
2#define FACT_Tools
3
4#include <map>
5#include <string>
6#include <vector>
7
8namespace Tools
9{
10 __attribute__((__format__ (__printf__, 1, 0)))
11 std::string Format(const char *fmt, va_list &ap);
12
13 __attribute__((__format__ (__printf__, 1, 0)))
14 std::string Form(const char *fmt, ...);
15
16 std::string Trim(const std::string &str);
17 std::string TrimQuotes(const std::string &str);
18 std::string Wrap(std::string &str, size_t width=78);
19 std::string Scientific(uint64_t val);
20 std::string Fractional(const double &val);
21
22 std::map<std::string,std::string> Split(std::string &, bool = false);
23 std::vector<std::string> Split(const std::string &, const std::string &);
24 std::string Uncomment(const std::string &opt);
25
26 std::vector<std::string> WordWrap(std::string, const uint16_t & = 80);
27
28 template<typename T>
29 uint16_t Fletcher16(const T *t, size_t cnt)
30 {
31 const uint8_t *data = reinterpret_cast<const uint8_t*>(t);
32
33 size_t bytes = cnt*sizeof(T);
34
35 uint16_t sum1 = 0xff;
36 uint16_t sum2 = 0xff;
37
38 while (bytes)
39 {
40 size_t tlen = bytes > 20 ? 20 : bytes;
41 bytes -= tlen;
42
43 do {
44 sum2 += sum1 += *data++;
45 } while (--tlen);
46
47 sum1 = (sum1 & 0xff) + (sum1 >> 8);
48 sum2 = (sum2 & 0xff) + (sum2 >> 8);
49 }
50
51 // Second reduction step to reduce sums to 8 bits
52 sum1 = (sum1 & 0xff) + (sum1 >> 8);
53 sum2 = (sum2 & 0xff) + (sum2 >> 8);
54
55 return sum2 << 8 | sum1;
56 }
57
58}
59
60#endif
Note: See TracBrowser for help on using the repository browser.