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

Last change on this file since 18486 was 18188, checked in by tbretz, 9 years ago
Added Fletcher16 checksum calculation.
File size: 1.3 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 template<typename T>
22 uint16_t Fletcher16(const T *t, size_t cnt)
23 {
24 const uint8_t *data = reinterpret_cast<const uint8_t*>(t);
25
26 size_t bytes = cnt*sizeof(T);
27
28 uint16_t sum1 = 0xff;
29 uint16_t sum2 = 0xff;
30
31 while (bytes)
32 {
33 size_t tlen = bytes > 20 ? 20 : bytes;
34 bytes -= tlen;
35
36 do {
37 sum2 += sum1 += *data++;
38 } while (--tlen);
39
40 sum1 = (sum1 & 0xff) + (sum1 >> 8);
41 sum2 = (sum2 & 0xff) + (sum2 >> 8);
42 }
43
44 // Second reduction step to reduce sums to 8 bits
45 sum1 = (sum1 & 0xff) + (sum1 >> 8);
46 sum2 = (sum2 & 0xff) + (sum2 >> 8);
47
48 return sum2 << 8 | sum1;
49 }
50
51}
52
53#endif
Note: See TracBrowser for help on using the repository browser.