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

Last change on this file since 19438 was 19211, checked in by tbretz, 6 years ago
Fixed a typo in the pre-processor directive
File size: 1.8 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// Fix for gcc 4.7.7 at ISDC
60#if !defined(__clang_major__) && defined(__GNUC__) && (__GNUC__ <= 4)
61
62namespace std
63{
64 string to_string(const size_t &val);
65 string to_string(const int &val);
66 string to_string(const unsigned int &val);
67}
68#endif
69
70#endif
Note: See TracBrowser for help on using the repository browser.