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

Last change on this file since 19027 was 18963, checked in by tbretz, 7 years ago
Tell the compiler that fmt is a format string
File size: 1.5 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
21 std::map<std::string,std::string> Split(std::string &, bool = false);
22 std::vector<std::string> Split(const std::string &, const std::string &);
23 std::string Uncomment(const std::string &opt);
24
25 std::vector<std::string> WordWrap(std::string, const uint16_t & = 80);
26
27 template<typename T>
28 uint16_t Fletcher16(const T *t, size_t cnt)
29 {
30 const uint8_t *data = reinterpret_cast<const uint8_t*>(t);
31
32 size_t bytes = cnt*sizeof(T);
33
34 uint16_t sum1 = 0xff;
35 uint16_t sum2 = 0xff;
36
37 while (bytes)
38 {
39 size_t tlen = bytes > 20 ? 20 : bytes;
40 bytes -= tlen;
41
42 do {
43 sum2 += sum1 += *data++;
44 } while (--tlen);
45
46 sum1 = (sum1 & 0xff) + (sum1 >> 8);
47 sum2 = (sum2 & 0xff) + (sum2 >> 8);
48 }
49
50 // Second reduction step to reduce sums to 8 bits
51 sum1 = (sum1 & 0xff) + (sum1 >> 8);
52 sum2 = (sum2 & 0xff) + (sum2 >> 8);
53
54 return sum2 << 8 | sum1;
55 }
56
57}
58
59#endif
Note: See TracBrowser for help on using the repository browser.