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

Last change on this file since 18956 was 18876, checked in by tbretz, 8 years ago
Added new function 'WordWrap'
File size: 1.4 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 std::string Format(const char *fmt, va_list &ap);
11 std::string Form(const char *fmt, ...);
12 std::string Trim(const std::string &str);
13 std::string TrimQuotes(const std::string &str);
14 std::string Wrap(std::string &str, size_t width=78);
15 std::string Scientific(uint64_t val);
16
17 std::map<std::string,std::string> Split(std::string &, bool = false);
18 std::vector<std::string> Split(const std::string &, const std::string &);
19 std::string Uncomment(const std::string &opt);
20
21 std::vector<std::string> WordWrap(std::string, const uint16_t & = 80);
22
23 template<typename T>
24 uint16_t Fletcher16(const T *t, size_t cnt)
25 {
26 const uint8_t *data = reinterpret_cast<const uint8_t*>(t);
27
28 size_t bytes = cnt*sizeof(T);
29
30 uint16_t sum1 = 0xff;
31 uint16_t sum2 = 0xff;
32
33 while (bytes)
34 {
35 size_t tlen = bytes > 20 ? 20 : bytes;
36 bytes -= tlen;
37
38 do {
39 sum2 += sum1 += *data++;
40 } while (--tlen);
41
42 sum1 = (sum1 & 0xff) + (sum1 >> 8);
43 sum2 = (sum2 & 0xff) + (sum2 >> 8);
44 }
45
46 // Second reduction step to reduce sums to 8 bits
47 sum1 = (sum1 & 0xff) + (sum1 >> 8);
48 sum2 = (sum2 & 0xff) + (sum2 >> 8);
49
50 return sum2 << 8 | sum1;
51 }
52
53}
54
55#endif
Note: See TracBrowser for help on using the repository browser.