util.hpp 2.68 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1
2
3
4
5
6
7
/*
 * util.hpp
 *
 *  Created on: May 7, 2015
 *      Author: Pietro Incardona
 */

8
9
#include "config.h"

Pietro Incardona's avatar
Pietro Incardona committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef UTIL_HPP_
#define UTIL_HPP_

#include <boost/iostreams/device/mapped_file.hpp>


/*! \brief Compare two files, return true if they match
 *
 * \param file1 path1
 * \param file2 path2
 *
 * \return true if they match
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
24
static inline bool compare(std::string file1, std::string file2)
Pietro Incardona's avatar
Pietro Incardona committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
    boost::iostreams::mapped_file_source f1(file1);
    boost::iostreams::mapped_file_source f2(file2);

    if( f1.size() == f2.size() && std::equal(f1.data(), f1.data() + f1.size(), f2.data()) )
        return true;
    else
        return false;
}

struct RGB
{
	float R;
	float G;
	float B;

	std::string toString()
	{
		return std::to_string(R) + " " + std::to_string(G) + " " + std::to_string(B);
	}
};

/*! \brief Return the color sampled from one group
 *
 * groups:
 *
 * 0: Red
 * 1: Green
 * 2: Blue
 * 3: Yellow
 * 4: Cyan
 * 5: Magenta
 * 6: Orange
 * 7: Chartreuse-Green
 * 8: Spring Green
 * 9: Azure
 * 10: Violet
 * 11: Rose
 *
 * \param group
 *
 */

Pietro Incardona's avatar
Pietro Incardona committed
68
static inline struct RGB getColor(int group, std::uniform_real_distribution<float> & d, std::default_random_engine & g)
Pietro Incardona's avatar
Pietro Incardona committed
69
70
71
{
	struct RGB col;

72
73
	float s = d(g);

74
75
	group = group % 12;

76
77
78
79
#ifdef ON_IO_UNIT_TESTS
	s = 0.5;
#endif

Pietro Incardona's avatar
Pietro Incardona committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	if (group == 0)
	{
		col.R = s/2 + 0.5;
		col.G = 0.0;
		col.B = 0.0;
	}
	else if (group == 1)
	{
		col.R = 0.0;
		col.G = s/2 + 0.5;
		col.B = 0.0;
	}
	else if (group == 2)
	{
		col.R = 0.0;
		col.G = 0.0;
		col.B = s;
	}
	else if (group == 3)
	{
		col.R = s/2 + 0.5;
		col.G = s/2 + 0.5;
		col.B = 0.0;
	}
	else if (group == 4)
	{
		col.R = s/2 + 0.5;
		col.G = 0.0;
		col.B = s/2 + 0.5;
	}
	else if (group == 5)
	{
		col.R = 0.0;
		col.G = s/2 + 0.5;
		col.B = s/2 + 0.5;
	}
	else if (group == 6)
	{
		col.R = s/2 + 0.5;
		col.G = s/4 + 0.5;
		col.B = 0.0;
	}
	else if (group == 7)
	{
		col.R = s/4 + 0.5;
		col.G = s/2 + 0.5;
		col.B = 0.0;
	}
	else if (group == 8)
	{
		col.R = 0.0;
		col.G = s/2 + 0.5;
		col.B = s/4 + 0.5;
	}
	else if (group == 9)
	{
		col.R = 0.0;
		col.G = s/4 + 0.5;
		col.B = s/2 + 0.5;
	}
	else if (group == 10)
	{
		col.R = s/4 + 0.5;
		col.G = 0.0;
		col.B = s/2 + 0.5;
	}
	else if (group == 11)
	{
		col.R = s/2 + 0.5;
		col.G = 0.0;
		col.B = s/4 + 0.5;
	}

	return col;
}

/*! \brief Check if one string end with a particular string
 *
 * \param fullString string to check
 * \param ending ending string to check
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
162
static inline bool hasEnding (std::string const &fullString, std::string const &ending)
Pietro Incardona's avatar
Pietro Incardona committed
163
164
165
166
167
168
169
170
{
    if (fullString.length() >= ending.length())
    {return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));}
    else
    {return false;}
}

#endif /* UTIL_HPP_ */